From f6632f9b9165f0a58980d736a16dc5693afd180e Mon Sep 17 00:00:00 2001 From: Nathan James Date: Sat, 12 Dec 2020 11:57:32 +0000 Subject: [PATCH 1/4] Add a watcher for clangd.path and clangd.arguments --- package.json | 15 ++++ src/clangd-context.ts | 4 +- ...fig-file-watcher.ts => config-watchers.ts} | 86 +++++++++++-------- 3 files changed, 65 insertions(+), 40 deletions(-) rename src/{config-file-watcher.ts => config-watchers.ts} (62%) diff --git a/package.json b/package.json index fab06ed3..6c1248a3 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 clangd.path or clangd.arguments is changed.", + "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 dc8a304a..23a7bb09 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'; @@ -138,7 +138,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 62% rename from src/config-file-watcher.ts rename to src/config-watchers.ts index 6924b25e..c6f1f5b2 100644 --- a/src/config-file-watcher.ts +++ b/src/config-watchers.ts @@ -3,10 +3,50 @@ import * as vscode from 'vscode'; 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') { const watcher = new ConfigFileWatcher(context); } + vscode.workspace.onDidChangeConfiguration(event => { + let Settings: string[] = ['clangd.path', 'clangd.arguments']; + Settings.forEach(element => { + if (event.affectsConfiguration(element)) { + promtRestart('onSettingsChanged', `setting '${ + element}' has changed. Do you want to reload the server?`); + } + }); + }); } class ConfigFileWatcher { @@ -39,49 +79,19 @@ class ConfigFileWatcher { 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); } } From 1b31472b59c7e1c08014e18213597e06cf57da4c Mon Sep 17 00:00:00 2001 From: Nathan James Date: Sat, 12 Dec 2020 12:11:04 +0000 Subject: [PATCH 2/4] clang-format --- src/config-watchers.ts | 54 ++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/config-watchers.ts b/src/config-watchers.ts index c6f1f5b2..606760d3 100644 --- a/src/config-watchers.ts +++ b/src/config-watchers.ts @@ -5,32 +5,31 @@ import * as config from './config'; async function promtRestart(settingName: string, promptMessage: string) { switch (config.get(settingName)) { - case 'restart': + 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 'ignore': + 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; - 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; + } + break; } } @@ -42,8 +41,10 @@ export function activate(context: ClangdContext) { let Settings: string[] = ['clangd.path', 'clangd.arguments']; Settings.forEach(element => { if (event.affectsConfiguration(element)) { - promtRestart('onSettingsChanged', `setting '${ - element}' has changed. Do you want to reload the server?`); + promtRestart( + 'onSettingsChanged', + `setting '${ + element}' has changed. Do you want to reload the server?`); } }); }); @@ -88,9 +89,10 @@ class ConfigFileWatcher { return; } this.debounceTimer = setTimeout(async () => { - await promtRestart('onConfigChanged', - `Clangd configuration file at '${ - uri.fsPath}' has been changed. Do you want to restart it?`); + await promtRestart( + 'onConfigChanged', + `Clangd configuration file at '${ + uri.fsPath}' has been changed. Do you want to restart it?`); this.debounceTimer = undefined; }, 2000); } From a71ccf7d47a2fa6d9e9cbf4cf9a31f5a617cb313 Mon Sep 17 00:00:00 2001 From: Nathan James Date: Mon, 14 Dec 2020 21:34:18 +0000 Subject: [PATCH 3/4] Break on the first affected config setting --- src/config-watchers.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/config-watchers.ts b/src/config-watchers.ts index 606760d3..786ea7bb 100644 --- a/src/config-watchers.ts +++ b/src/config-watchers.ts @@ -38,15 +38,17 @@ export function activate(context: ClangdContext) { const watcher = new ConfigFileWatcher(context); } vscode.workspace.onDidChangeConfiguration(event => { - let Settings: string[] = ['clangd.path', 'clangd.arguments']; - Settings.forEach(element => { - if (event.affectsConfiguration(element)) { + let Settings: string[] = ['path', 'arguments']; + for (let Setting of Settings) { + let ExpandedSetting = `clangd.${Setting}` + if (event.affectsConfiguration(ExpandedSetting)) { promtRestart( 'onSettingsChanged', `setting '${ - element}' has changed. Do you want to reload the server?`); + ExpandedSetting}' has changed. Do you want to reload the server?`); + break; } - }); + } }); } From d5c287617a51b7981bd1c3c0a90b44377f063d62 Mon Sep 17 00:00:00 2001 From: Nathan James Date: Thu, 7 Jan 2021 21:16:36 +0000 Subject: [PATCH 4/4] Add trace, semanticHighlighing and fallbackFlags Also update the config description to be more generic. --- package.json | 2 +- src/config-watchers.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e2020ca8..f2b4d7ab 100644 --- a/package.json +++ b/package.json @@ -147,7 +147,7 @@ "clangd.onSettingsChanged": { "type": "string", "default": "prompt", - "description": "What to do if clangd.path or clangd.arguments is changed.", + "description": "What to do if any setting is changed that requires a server restart to take effect.", "enum": [ "prompt", "restart", diff --git a/src/config-watchers.ts b/src/config-watchers.ts index 1e842a20..00fd0fff 100644 --- a/src/config-watchers.ts +++ b/src/config-watchers.ts @@ -39,7 +39,8 @@ export function activate(context: ClangdContext) { context.client.registerFeature(new ConfigFileWatcherFeature(context)); } vscode.workspace.onDidChangeConfiguration(event => { - let Settings: string[] = ['path', 'arguments']; + let Settings: string[] = + ['path', 'arguments', 'trace', 'semanticHighlighting', 'fallbackFlags']; for (let Setting of Settings) { let ExpandedSetting = `clangd.${Setting}` if (event.affectsConfiguration(ExpandedSetting)) {