diff --git a/docs/settings.md b/docs/settings.md index 177995a22..6cc4c475e 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -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' diff --git a/package.json b/package.json index cf51c6de3..ac4cf4fc1 100644 --- a/package.json +++ b/package.json @@ -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 }, @@ -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": { @@ -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", @@ -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", @@ -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." } } }, diff --git a/src/configuration.ts b/src/configuration.ts index e80e7a78f..bad3a282e 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -225,6 +225,12 @@ const configuration = { .getConfiguration("swift") .get("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 diff --git a/src/sourcekit-lsp/LanguageClientManager.ts b/src/sourcekit-lsp/LanguageClientManager.ts index dae647541..ee131b1c8 100644 --- a/src/sourcekit-lsp/LanguageClientManager.ts +++ b/src/sourcekit-lsp/LanguageClientManager.ts @@ -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 { @@ -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