Skip to content
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

Add a watcher for clangd.path and clangd.arguments #117

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/clangd-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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() {
Expand Down
91 changes: 53 additions & 38 deletions src/config-file-watcher.ts → src/config-watchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(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<string>(settingName, 'restart',
vscode.ConfigurationTarget.Global);
break;
case 'No, never':
config.update<string>(settingName, 'ignore',
vscode.ConfigurationTarget.Global);
break;
default:
break;
}
break;
}
}

export function activate(context: ClangdContext) {
if (config.get<string>('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.
Expand Down Expand Up @@ -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<string>('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<string>('onConfigChanged', 'restart',
vscode.ConfigurationTarget.Global);
break;
case 'No, never':
config.update<string>('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);
}
}