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

Disable config file watcher if a hot-reload server capability is set #161

Merged
merged 1 commit into from
Jan 8, 2021
Merged
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
3 changes: 2 additions & 1 deletion src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Ctx {
this.config = new Config();
}

async startServer(bin: string) {
async startServer(bin: string, ...features: StaticFeature[]) {
const old = this.client;
if (old) {
await old.stop();
Expand Down Expand Up @@ -103,6 +103,7 @@ export class Ctx {
client.registerFeature(new SemanticHighlightingFeature(client, this.context));
}
}
for (const feature of features) client.registerFeature(feature);
this.context.subscriptions.push(services.registLanguageClient(client));
await client.onReady();

Expand Down
28 changes: 2 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { commands, ExtensionContext, services, State, workspace } from 'coc.nvim';
import { basename } from 'path';
import * as cmds from './cmds';
import { Ctx } from './ctx';
import { FileStatus } from './file_status';
import { ReloadFeature } from './reload';
import * as install from './install';

export async function activate(context: ExtensionContext): Promise<void> {
Expand All @@ -24,18 +24,14 @@ export async function activate(context: ExtensionContext): Promise<void> {
}

try {
await ctx.startServer(clangdPath);
await ctx.startServer(clangdPath, new ReloadFeature(ctx, () => activate(context)));
} catch (e) {
return;
}

const fileStatus = new FileStatus();
const fileWatcher = workspace.createFileSystemWatcher('**/{compile_commands.json,compile_flags.txt}');
fileWatcher.onDidChange((e) => reload(e.fsPath, ctx, context));
fileWatcher.onDidCreate((e) => reload(e.fsPath, ctx, context));

context.subscriptions.push(
fileWatcher,
fileStatus,

commands.registerCommand('clangd.switchSourceHeader', cmds.switchSourceHeader(ctx)),
Expand All @@ -60,23 +56,3 @@ export async function activate(context: ExtensionContext): Promise<void> {
fileStatus.onFileUpdated(status);
});
}

async function reload(url: string, ctx: Ctx, context: ExtensionContext) {
const notification = ctx.config.showDBChangedNotification;
if (notification) {
const msg = `${basename(url)} has changed, clangd is reloading...`;
workspace.showMessage(msg);
}

for (const sub of ctx.subscriptions) {
try {
sub.dispose();
} catch (e) {
console.error(e);
}
}

await activate(context);

if (notification) workspace.showMessage(`clangd has reloaded`);
}
48 changes: 48 additions & 0 deletions src/reload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { StaticFeature, workspace } from 'coc.nvim';
import { ServerCapabilities } from 'vscode-languageserver-protocol';
import { basename } from 'path';
import { Ctx } from './ctx';

interface ClangdClientCapabilities {
compilationDatabase?: { automaticReload?: boolean };
}

// FIXME: remove this feature once clangd 12 is old enough to assume that
// server-side reload is always supported.
export class ReloadFeature implements StaticFeature {
constructor(private ctx: Ctx, private activate: () => void) {}
initialize(caps: ServerCapabilities) {
// Don't restart the server if it's able to reload config files itself.
if ((caps as ClangdClientCapabilities).compilationDatabase?.automaticReload) {
return;
}

const fileWatcher = workspace.createFileSystemWatcher('**/{compile_commands.json,compile_flags.txt}');
this.ctx.subscriptions.push(
fileWatcher,
fileWatcher.onDidChange((e) => this.reload(e.fsPath)),
fileWatcher.onDidCreate((e) => this.reload(e.fsPath))
);
}

fillClientCapabilities() {}

async reload(url: string) {
const notification = this.ctx.config.showDBChangedNotification;
if (notification) {
const msg = `${basename(url)} has changed, clangd is reloading...`;
workspace.showMessage(msg);
}

for (const sub of this.ctx.subscriptions) {
try {
sub.dispose();
} catch (e) {
console.error(e);
}
}

this.activate();
if (notification) workspace.showMessage(`clangd has reloaded`);
}
}