diff --git a/package.json b/package.json index a2b17554..f2b4d7ab 100644 --- a/package.json +++ b/package.json @@ -143,6 +143,21 @@ "Automatically restart the server", "Do nothing" ] + }, + "clangd.onSettingsChanged": { + "type": "string", + "default": "prompt", + "description": "What to do if any setting is changed that requires a server restart to take effect.", + "enum": [ + "prompt", + "restart", + "ignore" + ], + "enumDescriptions": [ + "Prompt the user for restarting the server", + "Automatically restart the server", + "Do nothing" + ] } } }, diff --git a/src/clangd-context.ts b/src/clangd-context.ts index 65d5d253..83ec2a0d 100644 --- a/src/clangd-context.ts +++ b/src/clangd-context.ts @@ -2,7 +2,7 @@ import * as vscode from 'vscode'; import * as vscodelc from 'vscode-languageclient/node'; import * as config from './config'; -import * as configFileWatcher from './config-file-watcher'; +import * as configWatchers from './config-watchers'; import * as fileStatus from './file-status'; import * as install from './install'; import * as memoryUsage from './memory-usage'; @@ -139,7 +139,7 @@ export class ClangdContext implements vscode.Disposable { console.log('Clang Language Server is now active!'); fileStatus.activate(this); switchSourceHeader.activate(this); - configFileWatcher.activate(this); + configWatchers.activate(this); } dispose() { diff --git a/src/config-file-watcher.ts b/src/config-watchers.ts similarity index 68% rename from src/config-file-watcher.ts rename to src/config-watchers.ts index 50807114..00fd0fff 100644 --- a/src/config-file-watcher.ts +++ b/src/config-watchers.ts @@ -4,10 +4,54 @@ import * as vscodelc from 'vscode-languageclient/node'; import {ClangdContext} from './clangd-context'; import * as config from './config'; +async function promtRestart(settingName: string, promptMessage: string) { + switch (config.get(settingName)) { + case 'restart': + vscode.commands.executeCommand('clangd.restart'); + break; + case 'ignore': + break; + case 'prompt': + default: + switch (await vscode.window.showInformationMessage( + promptMessage, 'Yes', 'Yes, always', 'No, never')) { + case 'Yes': + vscode.commands.executeCommand('clangd.restart'); + break; + case 'Yes, always': + vscode.commands.executeCommand('clangd.restart'); + config.update(settingName, 'restart', + vscode.ConfigurationTarget.Global); + break; + case 'No, never': + config.update(settingName, 'ignore', + vscode.ConfigurationTarget.Global); + break; + default: + break; + } + break; + } +} + export function activate(context: ClangdContext) { if (config.get('onConfigChanged') != 'ignore') { context.client.registerFeature(new ConfigFileWatcherFeature(context)); } + vscode.workspace.onDidChangeConfiguration(event => { + let Settings: string[] = + ['path', 'arguments', 'trace', 'semanticHighlighting', 'fallbackFlags']; + for (let Setting of Settings) { + let ExpandedSetting = `clangd.${Setting}` + if (event.affectsConfiguration(ExpandedSetting)) { + promtRestart( + 'onSettingsChanged', + `setting '${ + ExpandedSetting}' has changed. Do you want to reload the server?`); + break; + } + } + }); } // Clangd extension capabilities. @@ -64,49 +108,20 @@ class ConfigFileWatcher implements vscode.Disposable { if (this.debounceTimer) { clearTimeout(this.debounceTimer); } - - this.debounceTimer = setTimeout(async () => { - await this.handleConfigFilesChanged(uri); - this.debounceTimer = undefined; - }, 2000); - } - - async handleConfigFilesChanged(uri: vscode.Uri) { // Sometimes the tools that generate the compilation database, before // writing to it, they create a new empty file or they clear the existing // one, and after the compilation they write the new content. In this cases // the server is not supposed to restart - if ((await vscode.workspace.fs.stat(uri)).size <= 0) + if ((await vscode.workspace.fs.stat(uri)).size <= 0) { + clearTimeout(this.debounceTimer); return; - - switch (config.get('onConfigChanged')) { - case 'restart': - vscode.commands.executeCommand('clangd.restart'); - break; - case 'ignore': - break; - case 'prompt': - default: - switch (await vscode.window.showInformationMessage( - `Clangd configuration file at '${ - uri.fsPath}' has been changed. Do you want to restart it?`, - 'Yes', 'Yes, always', 'No, never')) { - case 'Yes': - vscode.commands.executeCommand('clangd.restart'); - break; - case 'Yes, always': - vscode.commands.executeCommand('clangd.restart'); - config.update('onConfigChanged', 'restart', - vscode.ConfigurationTarget.Global); - break; - case 'No, never': - config.update('onConfigChanged', 'ignore', - vscode.ConfigurationTarget.Global); - break; - default: - break; - } - break; } + this.debounceTimer = setTimeout(async () => { + await promtRestart( + 'onConfigChanged', + `Clangd configuration file at '${ + uri.fsPath}' has been changed. Do you want to restart it?`); + this.debounceTimer = undefined; + }, 2000); } }