Skip to content

Commit

Permalink
Disable config file watcher if a hot-reload capability is set (#128)
Browse files Browse the repository at this point in the history
Corresponds to https://reviews.llvm.org/D94222

This is the minimal change. I have a feeling the dispose/re-watch logic could be made cleaner.
But this client-side feature can just be removed in future, so I'm not sure it's worth the risk of trying to polish it.

I've tested this locally with clangd with/without the capability.
  • Loading branch information
sam-mccall authored Jan 7, 2021
1 parent b07e166 commit cf65a5a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"clangd.onConfigChanged": {
"type": "string",
"default": "prompt",
"description": "What to do when clangd configuration files are changed.",
"description": "What to do when clangd configuration files are changed. Ignored for clangd 12+, which can reload such files itself.",
"enum": [
"prompt",
"restart",
Expand Down
29 changes: 27 additions & 2 deletions src/config-file-watcher.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
import * as vscode from 'vscode';
import * as vscodelc from 'vscode-languageclient/node';

import {ClangdContext} from './clangd-context';
import * as config from './config';

export function activate(context: ClangdContext) {
if (config.get<string>('onConfigChanged') != 'ignore') {
const watcher = new ConfigFileWatcher(context);
context.client.registerFeature(new ConfigFileWatcherFeature(context));
}
}

class ConfigFileWatcher {
// Clangd extension capabilities.
interface ClangdClientCapabilities {
compilationDatabase?: {automaticReload?: boolean;},
}

class ConfigFileWatcherFeature implements vscodelc.StaticFeature {
constructor(private context: ClangdContext) {}
fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {}

initialize(capabilities: vscodelc.ServerCapabilities,
_documentSelector: vscodelc.DocumentSelector|undefined) {
if ((capabilities as ClangdClientCapabilities)
.compilationDatabase?.automaticReload)
return;
this.context.subscriptions.push(new ConfigFileWatcher(this.context));
}
dispose() {}
}

class ConfigFileWatcher implements vscode.Disposable {
private databaseWatcher: vscode.FileSystemWatcher = undefined;
private debounceTimer: NodeJS.Timer = undefined;

dispose() {
if (this.databaseWatcher)
this.databaseWatcher.dispose();
}

constructor(private context: ClangdContext) {
this.createFileSystemWatcher();
context.subscriptions.push(vscode.workspace.onDidChangeWorkspaceFolders(
Expand Down

0 comments on commit cf65a5a

Please sign in to comment.