Skip to content

Commit

Permalink
feat: Add a reindex command (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blond11516 authored May 5, 2024
1 parent 9b0737b commit bfb2071
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 70 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## [Unreleased]

### Added

- Added the "Rebuild code search index" which instructs Lexical to rebuild the
code search index for the current project.

## [0.0.15]

### Added
Expand Down
12 changes: 12 additions & 0 deletions __mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,15 @@ export const l10n = {
return message;
},
};

export class CallHierarchyItem {}
export class CodeAction {}
export class CodeLens {}
export class CompletionItem {}
export class Converter {}
export class Diagnostic {}
export class DocumentLink {}
export class InlayHint {}
export class TypeHierarchyItem {}
export class SymbolInformation {}
export class CancellationError {}
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
"command": "lexical.server.restart",
"category": "Lexical",
"title": "Restart Lexical's language server"
},
{
"command": "lexical.server.reindexProject",
"category": "Lexical",
"title": "Rebuild code search index"
}
],
"configurationDefaults": {
Expand Down Expand Up @@ -180,7 +185,7 @@
"ovsx": "0.8.3",
"prettier": "3.2.5",
"ts-jest": "29.1.2",
"typescript": "5.3.3",
"typescript": "5.4.5",
"vscode-uri": "3.0.8"
},
"dependencies": {
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions src/clientCommands/reindex-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Commands from ".";
import Logger from "../logger";
import { LanguageClient } from "vscode-languageclient/node";
import ServerCommands from "../serverCommands/server-commands";

interface Context {
client: LanguageClient;
}

const reindexProject: Commands.T<Context> = {
id: "lexical.server.reindexProject",
createHandler: ({ client }) => {
function handle() {
if (!client.isRunning()) {
Logger.error("Client is not running, cannot send command to server.");
return;
}

ServerCommands.reindex(client);
}

return handle;
},
};

export default reindexProject;
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { LanguageClient } from "vscode-languageclient/node";
import Commands from ".";
import ClientCommands from ".";
import Logger from "../logger";

interface Context {
client: LanguageClient;
}

const restartServer: Commands.T<Context> = {
const restartServer: ClientCommands.T<Context> = {
id: "lexical.server.restart",
createHandler: ({ client }) => {
function handle() {
Expand Down
10 changes: 5 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
} from "vscode-languageclient/node";
import { join } from "path";
import * as fs from "fs";
import Commands from "./commands";
import restartServer from "./commands/restart-server";
import { URI } from "vscode-uri";
import Logger from "./logger";
import Commands from "./clientCommands";
import restartServer from "./clientCommands/restart-server";
import reindexProject from "./clientCommands/reindex-project";

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
Expand All @@ -28,9 +29,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
context.subscriptions.push(commands.registerCommand(id, handler));
});

registerCommand(restartServer, {
client,
});
registerCommand(restartServer, { client });
registerCommand(reindexProject, { client });
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/serverCommands/server-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
ExecuteCommandRequest,
LanguageClient,
} from "vscode-languageclient/node";

const REINDEX_COMMAND_NAME = "Reindex";

namespace ServerCommands {
export async function reindex(client: LanguageClient): Promise<void> {
await client.sendRequest(ExecuteCommandRequest.type, {
command: REINDEX_COMMAND_NAME,
arguments: [],
});
}
}

export default ServerCommands;
19 changes: 5 additions & 14 deletions src/test/auto-installer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, test, expect, jest } from "@jest/globals";
import { describe, test, expect, jest, beforeEach } from "@jest/globals";
import ReleaseVersion from "../release/version";
import ReleaseFixture from "./fixtures/release-fixture";
import AutoInstaller from "../auto-installer";
Expand All @@ -11,17 +11,6 @@ import * as os from "os";
import Zip from "../zip";
import Release from "../release";

jest.mock("../installation-manifest", () => {
const original = jest.requireActual<typeof InstallationManifest>(
"../installation-manifest",
);

return {
...original,
fetch: jest.fn(),
};
});
jest.mock("../github");
jest.mock("fs", () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { mockKeys } = require("./utils/strict-mocks");
Expand All @@ -33,10 +22,12 @@ jest.mock("fs", () => {
realpathSync: original.realpathSync,
};
});
jest.mock("../zip");
jest.mock("os");

describe("AutoInstaller", () => {
beforeEach(() => {
mockResolvedValue(Zip, "extract");
});

describe("isInstalledReleaseLatest", () => {
test("should return false given manifest version is lower than remote version", () => {
givenAnInstallationManifestWithVersion("0.3.0");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { describe, expect, jest, test } from "@jest/globals";
import Commands from "../../commands";
import ClientCommands from "../../clientCommands";

describe("Commands", () => {
describe("getRegisterFunction", () => {
test("returns a function that registers the command with the client when called", () => {
const clientRegister = jest.fn();
const register = Commands.getRegisterFunction(clientRegister);
const register = ClientCommands.getRegisterFunction(clientRegister);

register(commandStub, undefined);

Expand All @@ -16,7 +16,7 @@ describe("Commands", () => {

const handler = jest.fn();

const commandStub: Commands.T<undefined> = {
const commandStub: ClientCommands.T<undefined> = {
id: "stub",
createHandler: () => handler,
};
29 changes: 29 additions & 0 deletions src/test/clientCommands/reindex-project.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, test } from "@jest/globals";
import reindexProject from "../../clientCommands/reindex-project";
import { mockResolvedValue } from "../utils/strict-mocks";
import ServerCommands from "../../serverCommands/server-commands";
import clientStub from "../fixtures/client-stub";

describe("reindexProject", () => {
test("should call the 'Reindex' server command", () => {
const handler = reindexProject.createHandler({
client: clientStub({ isRunning: true }),
});
mockResolvedValue(ServerCommands, "reindex");

handler();

expect(ServerCommands.reindex).toHaveBeenCalled();
});

test("given client is not running, it should do nothing", () => {
const handler = reindexProject.createHandler({
client: clientStub({ isRunning: false }),
});
mockResolvedValue(ServerCommands, "reindex");

handler();

expect(ServerCommands.reindex).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { describe, expect, jest, test } from "@jest/globals";
import restartServer from "../../commands/restart-server";
import { LanguageClient } from "vscode-languageclient/node";
import restartServer from "../../clientCommands/restart-server";
import clientStub from "../fixtures/client-stub";

describe("restartServer", () => {
test("given it is running, restarts it", () => {
const restart = jest.fn<LanguageClient["restart"]>();
const handler = restartServer.createHandler({
client: getClientStub({ isRunning: true, restart }),
client: clientStub({ isRunning: true, restart }),
});

handler();
Expand All @@ -17,29 +18,11 @@ describe("restartServer", () => {
test("given the client is not running, starts it", () => {
const start = jest.fn<LanguageClient["restart"]>();
const handler = restartServer.createHandler({
client: getClientStub({ isRunning: false, start }),
client: clientStub({ isRunning: false, start }),
});

handler();

expect(start).toHaveBeenCalled();
});
});

function getClientStub({
isRunning,
restart = jest.fn<LanguageClient["restart"]>(),
start = jest.fn<LanguageClient["start"]>(),
}: {
isRunning: boolean;
restart?: LanguageClient["restart"];
start?: LanguageClient["start"];
}): LanguageClient {
return {
isRunning() {
return isRunning;
},
restart,
start,
} as unknown as LanguageClient;
}
25 changes: 25 additions & 0 deletions src/test/fixtures/client-stub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { jest } from "@jest/globals";
import { LanguageClient } from "vscode-languageclient/node";

interface Opts {
sendRequest?: jest.Mock;
isRunning?: boolean;
restart?: jest.Mock;
start?: jest.Mock;
}

export default function clientStub({
sendRequest = jest.fn(),
isRunning = true,
restart = jest.fn(),
start = jest.fn(),
}: Opts): LanguageClient {
return {
isRunning() {
return isRunning;
},
sendRequest,
start,
restart,
} as unknown as LanguageClient;
}
18 changes: 18 additions & 0 deletions src/test/serverCommands/server-commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, jest, test } from "@jest/globals";
import { ExecuteCommandRequest } from "vscode-languageclient/node";
import ServerCommands from "../../serverCommands/server-commands";
import clientStub from "../fixtures/client-stub";

describe("reindex", () => {
test("it should send a request to the server with the 'Reindex' command", () => {
const sendRequest = jest.fn();
const client = clientStub({ sendRequest });

ServerCommands.reindex(client);

expect(sendRequest).toHaveBeenCalledWith(ExecuteCommandRequest.type, {
command: "Reindex",
arguments: [],
});
});
});
Loading

0 comments on commit bfb2071

Please sign in to comment.