Skip to content

Commit

Permalink
Add Re-Index Project Command (#964)
Browse files Browse the repository at this point in the history
* Add Re-Index Project Command

Adds a command to trigger a re-indexing of the open project. Should only
be needed when background indexing is enabled and it gets out of sync.
While that is a bug, this can act as a temporary workaround.
  • Loading branch information
plemarquand authored Jul 15, 2024
1 parent e52a9ce commit efb2f9f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
"title": "Restart LSP Server",
"category": "Swift"
},
{
"command": "swift.reindexProject",
"title": "Re-Index Project",
"category": "Swift"
},
{
"command": "swift.switchPlatform",
"title": "Select Target Platform...",
Expand Down Expand Up @@ -677,6 +682,10 @@
{
"command": "swift.attachDebugger",
"when": "swift.lldbVSCodeAvailable"
},
{
"command": "swift.reindexProject",
"when": "swift.supportsReindexing"
}
],
"editor/context": [
Expand Down
2 changes: 0 additions & 2 deletions src/TestExplorer/LSPTestDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ interface ILanguageClientManager {
* these results.
*/
export class LSPTestDiscovery {
private capCache = new Map<string, boolean>();

constructor(private languageClient: ILanguageClientManager) {}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/WorkspaceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ export class WorkspaceContext implements vscode.Disposable {
} else {
contextKeys.currentTargetType = undefined;
}

// LSP can be configured per workspace to support reindexing
this.languageClientManager.useLanguageClient(async client => {
const experimentalCaps = client.initializeResult?.capabilities.experimental;
contextKeys.supportsReindexing =
experimentalCaps && experimentalCaps["workspace/triggerReindex"] !== undefined;
});

setSnippetContextKey(this);
}

Expand Down
21 changes: 21 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { SwiftExecOperation, TaskOperation } from "./tasks/TaskQueue";
import { SwiftProjectTemplate } from "./toolchain/toolchain";
import { showToolchainSelectionQuickPick, showToolchainError } from "./ui/ToolchainSelection";
import { captureDiagnostics } from "./commands/captureDiagnostics";
import { reindexProjectRequest } from "./sourcekit-lsp/lspExtensions";

/**
* References:
Expand Down Expand Up @@ -656,6 +657,25 @@ function restartLSPServer(workspaceContext: WorkspaceContext): Promise<void> {
return workspaceContext.languageClientManager.restart();
}

/** Request that the SourceKit-LSP server reindexes the workspace */
function reindexProject(workspaceContext: WorkspaceContext): Promise<unknown> {
return workspaceContext.languageClientManager.useLanguageClient(async (client, token) => {
try {
return await client.sendRequest(reindexProjectRequest, {}, token);
} catch (err) {
const error = err as { code: number; message: string };
// methodNotFound, version of sourcekit-lsp is likely too old.
if (error.code === -32601) {
vscode.window.showWarningMessage(
"The installed version of SourceKit-LSP does not support background indexing."
);
} else {
vscode.window.showWarningMessage(error.message);
}
}
});
}

/** Execute task and show UI while running */
async function executeTaskWithUI(
task: vscode.Task,
Expand Down Expand Up @@ -817,6 +837,7 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
vscode.commands.registerCommand("swift.debugSnippet", () => debugSnippet(ctx)),
vscode.commands.registerCommand("swift.runPluginTask", () => runPluginTask()),
vscode.commands.registerCommand("swift.restartLSPServer", () => restartLSPServer(ctx)),
vscode.commands.registerCommand("swift.reindexProject", () => reindexProject(ctx)),
vscode.commands.registerCommand("swift.insertFunctionComment", () =>
insertFunctionComment(ctx)
),
Expand Down
7 changes: 7 additions & 0 deletions src/contextKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ const contextKeys = {
set createNewProjectAvailable(value: boolean) {
vscode.commands.executeCommand("setContext", "swift.createNewProjectAvailable", value);
},

/**
* Whether the SourceKit-LSP server supports reindexing the workspace.
*/
set supportsReindexing(value: boolean) {
vscode.commands.executeCommand("setContext", "swift.supportsReindexing", value);
},
};

export default contextKeys;
4 changes: 4 additions & 0 deletions src/sourcekit-lsp/lspExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,7 @@ export const textDocumentTestsRequest = new langclient.RequestType<
LSPTestItem[],
unknown
>("textDocument/tests");

export const reindexProjectRequest = new langclient.RequestType<null, unknown, unknown>(
"workspace/triggerReindex"
);

0 comments on commit efb2f9f

Please sign in to comment.