Skip to content

Commit 0e39568

Browse files
authored
Add setting for background indexing (#992)
Add setting for background indexing Adds a setting that will send `backgroundIndexing: true` along with a `backgroundPreparationMode` in the SourceKit-LSP initialization request. The server must be restarted when either of these settings are changed. Issue: #987
1 parent 74a4447 commit 0e39568

File tree

4 files changed

+57
-43
lines changed

4 files changed

+57
-43
lines changed

docs/settings.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@ This document outlines useful configuration options not covered by the settings
1212

1313
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.
1414

15-
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.
16-
17-
```
18-
"swift.sourcekit-lsp.serverArguments": [
19-
"--experimental-feature",
20-
"background-indexing"
21-
]
22-
```
15+
To enable support, set the `swift.sourcekit-lsp.backgroundIndexing` setting to `true`.
2316

2417
### Support for 'Expand Macro'
2518

package.json

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@
299299
"markdownEnumDescriptions": [
300300
"Use whichever diagnostics style `swiftc` produces by default.",
301301
"Use the `llvm` diagnostic style. This allows the parsing of \"notes\".",
302-
"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."
302+
"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."
303303
],
304304
"order": 9
305305
},
@@ -457,6 +457,12 @@
457457
"cpp"
458458
]
459459
},
460+
"order": 3
461+
},
462+
"swift.sourcekit-lsp.backgroundIndexing": {
463+
"type": "boolean",
464+
"default": false,
465+
"markdownDescription": "**Experimental**: Enable or disable background indexing. This option has no effect in Swift versions prior to 6.0.",
460466
"order": 4
461467
},
462468
"swift.sourcekit-lsp.trace.server": {
@@ -476,28 +482,11 @@
476482
"markdownDescription": "Disable SourceKit-LSP",
477483
"order": 6
478484
},
479-
"sourcekit-lsp.serverPath": {
480-
"type": "string",
481-
"markdownDescription": "The path of the `sourcekit-lsp` executable. The default is to look in the path where `swift` is found.",
482-
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.serverPath#` instead.",
483-
"order": 1
484-
},
485-
"sourcekit-lsp.serverArguments": {
486-
"type": "array",
487-
"default": [],
488-
"items": {
489-
"type": "string"
490-
},
491-
"markdownDescription": "Arguments to pass to SourceKit-LSP. Keys and values should be provided as individual entries in the list. e.g. `['--log-level', 'debug']`",
492-
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.serverArguments#` instead.",
493-
"order": 2
494-
},
495485
"sourcekit-lsp.inlayHints.enabled": {
496486
"type": "boolean",
497487
"default": true,
498488
"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.",
499-
"markdownDeprecationMessage": "**Deprecated**: Please use `#editor.inlayHints.enabled#` instead.",
500-
"order": 3
489+
"markdownDeprecationMessage": "**Deprecated**: Please use `#editor.inlayHints.enabled#` instead."
501490
},
502491
"sourcekit-lsp.support-c-cpp": {
503492
"type": "string",
@@ -513,8 +502,21 @@
513502
"Disable when C/C++ extension is active"
514503
],
515504
"markdownDescription": "Add LSP functionality for C/C++ files. By default this is set to disable when the C/C++ extension is active.",
516-
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.supported-languages#` instead.",
517-
"order": 5
505+
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.supported-languages#` instead."
506+
},
507+
"sourcekit-lsp.serverPath": {
508+
"type": "string",
509+
"markdownDescription": "The path of the `sourcekit-lsp` executable. The default is to look in the path where `swift` is found.",
510+
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.serverPath#` instead."
511+
},
512+
"sourcekit-lsp.serverArguments": {
513+
"type": "array",
514+
"default": [],
515+
"items": {
516+
"type": "string"
517+
},
518+
"markdownDescription": "Arguments to pass to SourceKit-LSP. Keys and values should be provided as individual entries in the list. e.g. `['--log-level', 'debug']`",
519+
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.serverArguments#` instead."
518520
},
519521
"sourcekit-lsp.trace.server": {
520522
"type": "string",
@@ -525,15 +527,13 @@
525527
"verbose"
526528
],
527529
"markdownDescription": "Traces the communication between VS Code and the SourceKit-LSP language server.",
528-
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.trace.server#` instead.",
529-
"order": 6
530+
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.trace.server#` instead."
530531
},
531532
"sourcekit-lsp.disable": {
532533
"type": "boolean",
533534
"default": false,
534535
"markdownDescription": "Disable the running of SourceKit-LSP.",
535-
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.disable#` instead.",
536-
"order": 7
536+
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.sourcekit-lsp.disable#` instead."
537537
}
538538
}
539539
},

src/configuration.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ const configuration = {
225225
.getConfiguration("swift")
226226
.get<boolean>("backgroundCompilation", false);
227227
},
228+
/** background indexing */
229+
get backgroundIndexing(): boolean {
230+
return vscode.workspace
231+
.getConfiguration("swift.sourcekit-lsp")
232+
.get("backgroundIndexing", false);
233+
},
228234
/** focus on problems view whenever there is a build error */
229235
get actionAfterBuildError(): ActionAfterBuildError {
230236
return vscode.workspace

src/sourcekit-lsp/LanguageClientManager.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -576,16 +576,7 @@ export class LanguageClientManager {
576576
// Avoid attempting to reinitialize multiple times. If we fail to initialize
577577
// we aren't doing anything different the second time and so will fail again.
578578
initializationFailedHandler: () => false,
579-
initializationOptions: {
580-
"workspace/peekDocuments": true, // workaround for client capability to handle `PeekDocumentsRequest`
581-
"workspace/getReferenceDocument": true, // the client can handle URIs with scheme `sourcekit-lsp:`
582-
"textDocument/codeLens": {
583-
supportedCommands: {
584-
"swift.run": "swift.run",
585-
"swift.debug": "swift.debug",
586-
},
587-
},
588-
},
579+
initializationOptions: this.initializationOptions(),
589580
};
590581

591582
return {
@@ -599,6 +590,30 @@ export class LanguageClientManager {
599590
};
600591
}
601592

593+
/* eslint-disable @typescript-eslint/no-explicit-any */
594+
private initializationOptions(): any {
595+
let options: any = {
596+
"workspace/peekDocuments": true, // workaround for client capability to handle `PeekDocumentsRequest`
597+
"workspace/getReferenceDocument": true, // the client can handle URIs with scheme `sourcekit-lsp:`
598+
"textDocument/codeLens": {
599+
supportedCommands: {
600+
"swift.run": "swift.run",
601+
"swift.debug": "swift.debug",
602+
},
603+
},
604+
};
605+
606+
if (configuration.backgroundIndexing) {
607+
options = {
608+
...options,
609+
backgroundIndexing: configuration.backgroundIndexing,
610+
backgroundPreparationMode: "enabled",
611+
};
612+
}
613+
return options;
614+
}
615+
/* eslint-enable @typescript-eslint/no-explicit-any */
616+
602617
private async startClient(
603618
client: langclient.LanguageClient,
604619
errorHandler: SourceKitLSPErrorHandler

0 commit comments

Comments
 (0)