Skip to content

Add setting for background indexing #992

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 3 commits into from
Aug 9, 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: 1 addition & 8 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ This document outlines useful configuration options not covered by the settings

If you're using a nightly (`main`) or recent `6.0` toolchain you can enable support for background indexing in Sourcekit-LSP. This removes the need to do a build before getting code completion and diagnostics.

To enable support, set the following Sourcekit-LSP server arguments in your settings.json, or add two new entries to the `Sourcekit-lsp: Server Arguments` entry in the extension settings page.

```
"swift.sourcekit-lsp.serverArguments": [
"--experimental-feature",
"background-indexing"
]
```
To enable support, set the `swift.sourcekit-lsp.backgroundIndexing` setting to `true`.

### Support for 'Expand Macro'

Expand Down
50 changes: 25 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@
"markdownEnumDescriptions": [
"Use whichever diagnostics style `swiftc` produces by default.",
"Use the `llvm` diagnostic style. This allows the parsing of \"notes\".",
"Use the `swift` diagnostic style. This means that \"notes\" will not be parsed. This option will not work for Swift versions prior to 5.10."
"Use the `swift` diagnostic style. This means that \"notes\" will not be parsed. This option has no effect in Swift versions prior to 5.10."
],
"order": 9
},
Expand Down Expand Up @@ -457,6 +457,12 @@
"cpp"
]
},
"order": 3
},
"swift.sourcekit-lsp.backgroundIndexing": {
"type": "boolean",
"default": false,
"markdownDescription": "**Experimental**: Enable or disable background indexing. This option has no effect in Swift versions prior to 6.0.",
"order": 4
},
"swift.sourcekit-lsp.trace.server": {
Expand All @@ -476,28 +482,11 @@
"markdownDescription": "Disable SourceKit-LSP",
"order": 6
},
"sourcekit-lsp.serverPath": {
"type": "string",
"markdownDescription": "The path of the `sourcekit-lsp` executable. The default is to look in the path where `swift` is found.",
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.serverPath#` instead.",
"order": 1
},
"sourcekit-lsp.serverArguments": {
"type": "array",
"default": [],
"items": {
"type": "string"
},
"markdownDescription": "Arguments to pass to SourceKit-LSP. Keys and values should be provided as individual entries in the list. e.g. `['--log-level', 'debug']`",
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.serverArguments#` instead.",
"order": 2
},
"sourcekit-lsp.inlayHints.enabled": {
"type": "boolean",
"default": true,
"markdownDescription": "Display Inlay Hints. Inlay Hints are variable annotations indicating their inferred type. They are only available if you are using Swift 5.6 or later.",
"markdownDeprecationMessage": "**Deprecated**: Please use `#editor.inlayHints.enabled#` instead.",
"order": 3
"markdownDeprecationMessage": "**Deprecated**: Please use `#editor.inlayHints.enabled#` instead."
},
"sourcekit-lsp.support-c-cpp": {
"type": "string",
Expand All @@ -513,8 +502,21 @@
"Disable when C/C++ extension is active"
],
"markdownDescription": "Add LSP functionality for C/C++ files. By default this is set to disable when the C/C++ extension is active.",
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.supported-languages#` instead.",
"order": 5
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.supported-languages#` instead."
},
"sourcekit-lsp.serverPath": {
"type": "string",
"markdownDescription": "The path of the `sourcekit-lsp` executable. The default is to look in the path where `swift` is found.",
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.serverPath#` instead."
},
"sourcekit-lsp.serverArguments": {
"type": "array",
"default": [],
"items": {
"type": "string"
},
"markdownDescription": "Arguments to pass to SourceKit-LSP. Keys and values should be provided as individual entries in the list. e.g. `['--log-level', 'debug']`",
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.serverArguments#` instead."
},
"sourcekit-lsp.trace.server": {
"type": "string",
Expand All @@ -525,15 +527,13 @@
"verbose"
],
"markdownDescription": "Traces the communication between VS Code and the SourceKit-LSP language server.",
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.trace.server#` instead.",
"order": 6
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.trace.server#` instead."
},
"sourcekit-lsp.disable": {
"type": "boolean",
"default": false,
"markdownDescription": "Disable the running of SourceKit-LSP.",
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.disable#` instead.",
"order": 7
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.disable#` instead."
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ const configuration = {
.getConfiguration("swift")
.get<boolean>("backgroundCompilation", false);
},
/** background indexing */
get backgroundIndexing(): boolean {
return vscode.workspace
.getConfiguration("swift.sourcekit-lsp")
.get("backgroundIndexing", false);
},
/** focus on problems view whenever there is a build error */
get actionAfterBuildError(): ActionAfterBuildError {
return vscode.workspace
Expand Down
35 changes: 25 additions & 10 deletions src/sourcekit-lsp/LanguageClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,16 +576,7 @@ export class LanguageClientManager {
// Avoid attempting to reinitialize multiple times. If we fail to initialize
// we aren't doing anything different the second time and so will fail again.
initializationFailedHandler: () => false,
initializationOptions: {
"workspace/peekDocuments": true, // workaround for client capability to handle `PeekDocumentsRequest`
"workspace/getReferenceDocument": true, // the client can handle URIs with scheme `sourcekit-lsp:`
"textDocument/codeLens": {
supportedCommands: {
"swift.run": "swift.run",
"swift.debug": "swift.debug",
},
},
},
initializationOptions: this.initializationOptions(),
};

return {
Expand All @@ -599,6 +590,30 @@ export class LanguageClientManager {
};
}

/* eslint-disable @typescript-eslint/no-explicit-any */
private initializationOptions(): any {
let options: any = {
"workspace/peekDocuments": true, // workaround for client capability to handle `PeekDocumentsRequest`
"workspace/getReferenceDocument": true, // the client can handle URIs with scheme `sourcekit-lsp:`
"textDocument/codeLens": {
supportedCommands: {
"swift.run": "swift.run",
"swift.debug": "swift.debug",
},
},
};

if (configuration.backgroundIndexing) {
options = {
...options,
backgroundIndexing: configuration.backgroundIndexing,
backgroundPreparationMode: "enabled",
};
}
return options;
}
/* eslint-enable @typescript-eslint/no-explicit-any */

private async startClient(
client: langclient.LanguageClient,
errorHandler: SourceKitLSPErrorHandler
Expand Down