Skip to content

Add "auto" mode to sourcekit-lsp backgroundIndexing setting #1232

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
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
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,14 @@
"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.",
"type": "string",
"enum": [
"on",
"off",
"auto"
],
"default": "auto",
"markdownDescription": "Turns background indexing `on` or `off`. `auto` will enable background indexing if the Swift version is >= 6.1. This option has no effect in Swift versions prior to 6.0.",
"order": 4
},
"swift.sourcekit-lsp.trace.server": {
Expand Down
13 changes: 10 additions & 3 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,17 @@ const configuration = {
.get<boolean>("backgroundCompilation", false);
},
/** background indexing */
get backgroundIndexing(): boolean {
return vscode.workspace
get backgroundIndexing(): "on" | "off" | "auto" {
const value = vscode.workspace
.getConfiguration("swift.sourcekit-lsp")
.get("backgroundIndexing", false);
.get("backgroundIndexing", "auto");

// Legacy versions of this setting were a boolean, convert to the new string version.
if (typeof value === "boolean") {
return value ? "on" : "off";
} else {
return value;
}
},
/** focus on problems view whenever there is a build error */
get actionAfterBuildError(): ActionAfterBuildError {
Expand Down
19 changes: 14 additions & 5 deletions src/sourcekit-lsp/LanguageClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ export class LanguageClientManager {
// that are not at the root of their workspace
public subFolderWorkspaces: vscode.Uri[];
private namedOutputChannels: Map<string, LSPOutputChannel> = new Map();
private swiftVersion: Version;

/** Get the current state of the underlying LanguageClient */
public get state(): langclient.State {
if (!this.languageClient) {
Expand All @@ -142,9 +144,8 @@ export class LanguageClientManager {
LanguageClientManager.indexingLogName,
new LSPOutputChannel(LanguageClientManager.indexingLogName, false, true)
);
this.singleServerSupport = workspaceContext.swiftVersion.isGreaterThanOrEqual(
new Version(5, 7, 0)
);
this.swiftVersion = workspaceContext.swiftVersion;
this.singleServerSupport = this.swiftVersion.isGreaterThanOrEqual(new Version(5, 7, 0));
this.subscriptions = [];
this.subFolderWorkspaces = [];
if (this.singleServerSupport) {
Expand Down Expand Up @@ -613,10 +614,18 @@ export class LanguageClientManager {
},
};

if (configuration.backgroundIndexing) {
// Swift 6.0.0 and later supports background indexing.
// In 6.0.0 it is experimental so only "true" enables it.
// In 6.1.0 it is no longer experimental, and so "auto" or "true" enables it.
if (
this.swiftVersion.isGreaterThanOrEqual(new Version(6, 0, 0)) &&
(configuration.backgroundIndexing === "on" ||
(configuration.backgroundIndexing === "auto" &&
this.swiftVersion.isGreaterThanOrEqual(new Version(6, 1, 0))))
) {
options = {
...options,
backgroundIndexing: configuration.backgroundIndexing,
backgroundIndexing: true,
backgroundPreparationMode: "enabled",
};
}
Expand Down
54 changes: 53 additions & 1 deletion test/unit-tests/sourcekit-lsp/LanguageClientManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ suite("LanguageClientManager Suite", () => {
let createFilesEmitter: AsyncEventEmitter<vscode.FileCreateEvent>;
let deleteFilesEmitter: AsyncEventEmitter<vscode.FileDeleteEvent>;

const doesNotHave = (prop: any) =>
match(function (actual) {
if (typeof actual === "object") {
return !(prop in actual);
}
return actual[prop] === undefined;
}, "doesNotHave");

setup(async () => {
// Mock pieces of the VSCode API
mockedVSCodeWindow.activeTextEditor = undefined;
Expand Down Expand Up @@ -153,7 +161,7 @@ suite("LanguageClientManager Suite", () => {
// LSP configuration defaults
mockedConfig.path = "";
mockedConfig.buildArguments = [];
mockedConfig.backgroundIndexing = false;
mockedConfig.backgroundIndexing = "off";
mockedConfig.swiftEnvironmentVariables = {};
mockedLspConfig.supportCFamily = "cpptools-inactive";
mockedLspConfig.disable = false;
Expand All @@ -177,6 +185,50 @@ suite("LanguageClientManager Suite", () => {
expect(languageClientMock.start).to.have.been.calledOnce;
});

test("chooses the correct backgroundIndexing value is auto, swift version if 6.0.0", async () => {
mockedWorkspace.swiftVersion = new Version(6, 0, 0);
mockedConfig.backgroundIndexing = "auto";
new LanguageClientManager(instance(mockedWorkspace));
await waitForReturnedPromises(languageClientMock.start);

expect(mockedLangClientModule.LanguageClient).to.have.been.calledOnceWith(
match.string,
match.string,
match.object,
match.hasNested("initializationOptions", doesNotHave("backgroundIndexing"))
);
});

test("chooses the correct backgroundIndexing value is auto, swift version if 6.1.0", async () => {
mockedWorkspace.swiftVersion = new Version(6, 1, 0);
mockedConfig.backgroundIndexing = "auto";

new LanguageClientManager(instance(mockedWorkspace));
await waitForReturnedPromises(languageClientMock.start);

expect(mockedLangClientModule.LanguageClient).to.have.been.calledOnceWith(
match.string,
match.string,
match.object,
match.hasNested("initializationOptions.backgroundIndexing", match.truthy)
);
});

test("chooses the correct backgroundIndexing value is true, swift version if 6.0.0", async () => {
mockedWorkspace.swiftVersion = new Version(6, 0, 0);
mockedConfig.backgroundIndexing = "on";

new LanguageClientManager(instance(mockedWorkspace));
await waitForReturnedPromises(languageClientMock.start);

expect(mockedLangClientModule.LanguageClient).to.have.been.calledOnceWith(
match.string,
match.string,
match.object,
match.hasNested("initializationOptions.backgroundIndexing", match.truthy)
);
});

test("notifies SourceKit-LSP of WorkspaceFolder changes", async () => {
const folder1 = mockObject<FolderContext>({
isRootFolder: false,
Expand Down