Skip to content

Add Re-Index Project Command #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -664,6 +669,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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to check this in advance so that we can only show the command in the command palette if background indexing is enabled?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SourceKit-LSP should report this capability

"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"
);