From beaa9dfdc196e0d1af7c2921584b51826789c192 Mon Sep 17 00:00:00 2001 From: Heyward Fann Date: Tue, 12 Dec 2023 11:07:10 +0800 Subject: [PATCH] refactor: cleanup --- src/cmds.ts | 10 ++++----- src/index.ts | 8 +++----- src/inlay-hints.ts | 8 +++----- src/install.ts | 16 +++++++-------- src/memory-usage.ts | 5 ++--- src/reload.ts | 49 --------------------------------------------- 6 files changed, 21 insertions(+), 75 deletions(-) delete mode 100644 src/reload.ts diff --git a/src/cmds.ts b/src/cmds.ts index 629d3f4..451655d 100644 --- a/src/cmds.ts +++ b/src/cmds.ts @@ -33,7 +33,7 @@ export function switchSourceHeader(ctx: Ctx) { }; const dest = await ctx.client.sendRequest(SwitchSourceHeaderRequest.type.method, params); if (!dest) { - window.showMessage(`Didn't find a corresponding file.`); + window.showInformationMessage(`Didn't find a corresponding file.`); return; } @@ -64,7 +64,7 @@ export function symbolInfo(ctx: Ctx) { // TODO const detail = details[0]; - window.showMessage(`name: ${detail.name}, containerName: ${detail.containerName}, usr: ${detail.usr}`); + window.showInformationMessage(`name: ${detail.name}, containerName: ${detail.containerName}, usr: ${detail.usr}`); }; } @@ -98,7 +98,7 @@ export function userConfig() { if (file) { openConfigFile(file); } else { - window.showMessage("Couldn't get global configuration directory", 'warning'); + window.showWarningMessage("Couldn't get global configuration directory"); } } @@ -107,6 +107,6 @@ export function projectConfig() { const folder = workspace.workspaceFolders[0]; openConfigFile(path.join(Uri.parse(folder.uri).fsPath, '.clangd')); } else { - window.showMessage('No project is open', 'warning'); + window.showWarningMessage('No project is open'); } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 007d94d..2343735 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ import { Ctx, ClangdExtensionFeature } from './ctx'; import { FileStatus, Status } from './file-status'; import { InlayHintsFeature } from './inlay-hints'; import * as install from './install'; -import { ReloadFeature } from './reload'; import { MemoryUsageFeature } from './memory-usage'; import { ASTFeature } from './ast'; @@ -16,7 +15,7 @@ export async function activate(context: ExtensionContext): Promise { const service = services.getService('clangd'); if (service) { - window.showMessage(`Looks like you've configured clangd in coc-settings.json, you should remove it to use coc-clangd`, 'warning'); + window.showWarningMessage("Looks like you've configured clangd in coc-settings.json, you should remove it to use coc-clangd"); return; } @@ -28,10 +27,9 @@ export async function activate(context: ExtensionContext): Promise { try { const astFeature = new ASTFeature(ctx); const extFeature = new ClangdExtensionFeature(); - const reloadFeature = new ReloadFeature(ctx, () => activate(context)); const memoryUsageFeature = new MemoryUsageFeature(ctx); const inlayFeature = new InlayHintsFeature(ctx); - await ctx.startServer(clangdPath, ...[astFeature, extFeature, reloadFeature, memoryUsageFeature, inlayFeature]); + await ctx.startServer(clangdPath, ...[astFeature, extFeature, memoryUsageFeature, inlayFeature]); } catch (e) { return; } @@ -63,4 +61,4 @@ export async function activate(context: ExtensionContext): Promise { ctx.client?.onNotification('textDocument/clangd.fileStatus', (status: Status) => { fileStatus.onFileUpdated(status); }); -} +} \ No newline at end of file diff --git a/src/inlay-hints.ts b/src/inlay-hints.ts index aa685cc..f5e518c 100644 --- a/src/inlay-hints.ts +++ b/src/inlay-hints.ts @@ -12,7 +12,6 @@ import { StaticFeature, TextDocumentIdentifier, } from 'coc.nvim'; -import { ServerCapabilities } from 'vscode-languageserver-protocol'; import { Ctx, documentSelector } from './ctx'; @@ -40,11 +39,10 @@ export class InlayHintsFeature implements StaticFeature { fillClientCapabilities() {} fillInitializeParams() {} - initialize(capabilities: ServerCapabilities) { - const serverCapabilities: ServerCapabilities & { clangdInlayHintsProvider?: boolean; inlayHintProvider?: any } = capabilities; + initialize(capabilities: any) { // If the clangd server supports LSP 3.17 inlay hints, these are handled by // the vscode-languageclient library - don't send custom requests too! - if (!serverCapabilities.clangdInlayHintsProvider || languages.registerInlayHintsProvider === undefined || serverCapabilities.inlayHintProvider) { + if (!capabilities.clangdInlayHintsProvider || languages.registerInlayHintsProvider === undefined || capabilities.inlayHintProvider) { return; } this.context.subscriptions.push(languages.registerInlayHintsProvider(documentSelector, new Provider(this.context))); @@ -83,4 +81,4 @@ class Provider implements InlayHintsProvider { const result = await this.context.client!.sendRequest(protocol.InlayHintsRequest.type, request, token); return result.map(this.decode, this); } -} +} \ No newline at end of file diff --git a/src/install.ts b/src/install.ts index b084b25..6d1161e 100644 --- a/src/install.ts +++ b/src/install.ts @@ -10,14 +10,14 @@ class UI { return this.context.storagePath; } slow(title: string, result: Promise) { - coc.window.showMessage(title + '...'); + coc.window.showInformationMessage(`${title}...}`); return result; } error(s: string) { - coc.window.showMessage(s, 'error'); + coc.window.showErrorMessage(s); } info(s: string) { - coc.window.showMessage(s); + coc.window.showInformationMessage(s); } progress(title: string, _cancel: any, body: (progress: (fraction: number) => void) => Promise) { return this.slow( @@ -27,7 +27,7 @@ class UI { } async shouldReuse(release: string) { - coc.window.showMessage(`Reusing existing ${release} installation in ${this.storagePath}`); + coc.window.showInformationMessage(`Reusing existing ${release} installation in ${this.storagePath}`); return true; } async promptReload() { @@ -35,15 +35,15 @@ class UI { } showHelp(message: string, url: string) { message += ` See ${url}.`; - coc.window.showMessage(message); + coc.window.showInformationMessage(message); } async promptUpdate(oldVersion: string, newVersion: string) { const message = `clangd ${newVersion} is available (you have ${oldVersion}). :CocCommand clangd.install, or :CocSettings to disable clangd.checkUpdates.`; - coc.window.showMessage(message); + coc.window.showInformationMessage(message); } async promptInstall(version: string) { const message = `clangd was not found on your PATH. :CocCommand clangd.install will install ${version}.`; - coc.window.showMessage(message); + coc.window.showInformationMessage(message); } get clangdPath(): string { @@ -75,4 +75,4 @@ export async function activate(context: coc.ExtensionContext): Promise common.checkUpdates(true, ui))); const status = await common.prepare(ui, cfg.get('checkUpdates', false)); return status.clangdPath; -} +} \ No newline at end of file diff --git a/src/memory-usage.ts b/src/memory-usage.ts index b0607bc..9defefc 100644 --- a/src/memory-usage.ts +++ b/src/memory-usage.ts @@ -1,7 +1,6 @@ // Implements the "memory usage" feature. import { commands, RequestType0, StaticFeature, window } from 'coc.nvim'; -import { ServerCapabilities } from 'vscode-languageserver-protocol'; import { Ctx } from './ctx'; // LSP wire format for this clangd feature. @@ -57,7 +56,7 @@ export class MemoryUsageFeature implements StaticFeature { fillClientCapabilities() {} fillInitializeParams() {} - initialize(capabilities: ServerCapabilities) { + initialize(capabilities: any) { if ('memoryUsageProvider' in capabilities) { this.ctx.subscriptions.push( commands.registerCommand('clangd.memoryUsage', async () => { @@ -70,4 +69,4 @@ export class MemoryUsageFeature implements StaticFeature { } } dispose() {} -} +} \ No newline at end of file diff --git a/src/reload.ts b/src/reload.ts deleted file mode 100644 index f6729e9..0000000 --- a/src/reload.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { StaticFeature, window, workspace } from 'coc.nvim'; -import { basename } from 'path'; -import { ServerCapabilities } from 'vscode-languageserver-protocol'; -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) {} - dispose(): 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...`; - window.showMessage(msg); - } - - for (const sub of this.ctx.subscriptions) { - try { - sub.dispose(); - } catch (e) { - console.error(e); - } - } - - this.activate(); - if (notification) window.showMessage(`clangd has reloaded`); - } -}