Skip to content

Commit bee8ce3

Browse files
Improve LSP configuration change notification (#878)
1 parent 1d9e88a commit bee8ce3

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

src/sourcekit-lsp/LanguageClientManager.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ export class LanguageClientManager {
112112
// used by single server support to keep a record of the project folders
113113
// that are not at the root of their workspace
114114
public subFolderWorkspaces: vscode.Uri[];
115+
/** Get the current state of the underlying LanguageClient */
116+
public get state(): langclient.State {
117+
if (!this.languageClient) {
118+
return langclient.State.Stopped;
119+
}
120+
return this.languageClient.state;
121+
}
115122

116123
constructor(public workspaceContext: WorkspaceContext) {
117124
this.singleServerSupport = workspaceContext.swiftVersion.isGreaterThanOrEqual(
@@ -161,18 +168,40 @@ export class LanguageClientManager {
161168
}
162169
// on change config restart server
163170
const onChangeConfig = vscode.workspace.onDidChangeConfiguration(event => {
164-
if (event.affectsConfiguration("swift.sourcekit-lsp")) {
165-
vscode.window
166-
.showInformationMessage(
167-
"Changing LSP settings requires the language server be restarted.",
168-
"Ok"
169-
)
170-
.then(selected => {
171-
if (selected === "Ok") {
172-
this.restart();
173-
}
174-
});
171+
if (!event.affectsConfiguration("swift.sourcekit-lsp")) {
172+
return;
173+
}
174+
let message =
175+
"Changing SourceKit-LSP settings requires the language server be restarted. Would you like to restart it now?";
176+
let restartLSPButton = "Restart Language Server";
177+
// Enabling/Disabling sourcekit-lsp shows a special notification
178+
if (event.affectsConfiguration("swift.sourcekit-lsp.disable")) {
179+
if (configuration.lsp.disable) {
180+
if (this.state === langclient.State.Stopped) {
181+
// Language client is already stopped
182+
return;
183+
}
184+
message =
185+
"You have disabled the Swift language server, but it is still running. Would you like to stop it now?";
186+
restartLSPButton = "Stop Language Server";
187+
} else {
188+
if (this.state !== langclient.State.Stopped) {
189+
// Langauge client is already running
190+
return;
191+
}
192+
message =
193+
"You have enabled the Swift language server. Would you like to start it now?";
194+
restartLSPButton = "Start Language Server";
195+
}
196+
} else if (configuration.lsp.disable && this.state === langclient.State.Stopped) {
197+
// Ignore configuration changes if SourceKit-LSP is disabled
198+
return;
175199
}
200+
vscode.window.showInformationMessage(message, restartLSPButton).then(selected => {
201+
if (selected === restartLSPButton) {
202+
this.restart();
203+
}
204+
});
176205
});
177206

178207
this.subscriptions.push(onChangeConfig);

0 commit comments

Comments
 (0)