From c42ed843b0b172b6082d4ec75366c9d969ba9f64 Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Sat, 25 Dec 2021 00:29:52 +0800 Subject: [PATCH] fixx issues with inlayHints Closes #336 --- src/server/features/inlayHints.ts | 55 +++++++++++++++++++------------ src/server/languageProvider.ts | 2 +- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/server/features/inlayHints.ts b/src/server/features/inlayHints.ts index c018a89..30ad69a 100644 --- a/src/server/features/inlayHints.ts +++ b/src/server/features/inlayHints.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { CancellationToken, CancellationTokenSource, Disposable, disposeAll, Document, events, Position, Range, TextDocument, workspace } from 'coc.nvim' +import { CancellationToken, CancellationTokenSource, Disposable, disposeAll, Document, Position, Range, TextDocument, workspace } from 'coc.nvim' import type * as Proto from '../protocol' import { ITypeScriptServiceClient } from '../typescriptService' import API from '../utils/api' @@ -43,28 +43,48 @@ export default class TypeScriptInlayHintsProvider implements Disposable { this._inlayHints.clear() } - constructor(private readonly client: ITypeScriptServiceClient, private readonly fileConfigurationManager: FileConfigurationManager) { - events.on('InsertLeave', async bufnr => { - const doc = workspace.getDocument(bufnr) - await this.syncAndRenderHints(doc) - }, this, this._disposables) - + constructor( + private readonly client: ITypeScriptServiceClient, + private readonly fileConfigurationManager: FileConfigurationManager, + private readonly languageIds: string[] + ) { + let languageId = this.languageIds[0] + let section = `${languageId}.inlayHints` + workspace.onDidChangeConfiguration(async e => { + if (e.affectsConfiguration(section)) { + for (let doc of workspace.documents) { + if (!this.inlayHintsEnabled(languageId)) { + doc.buffer.clearNamespace(this.inlayHintsNS) + } else { + await this.syncAndRenderHints(doc) + } + } + } + }, null, this._disposables) workspace.onDidOpenTextDocument(async e => { const doc = workspace.getDocument(e.bufnr) await this.syncAndRenderHints(doc) - }, this, this._disposables) + }, null, this._disposables) workspace.onDidChangeTextDocument(async e => { const doc = workspace.getDocument(e.bufnr) - await this.syncAndRenderHints(doc) - }, this, this._disposables) + if (this.languageIds.includes(doc.textDocument.languageId)) { + this.renderHintsForAllDocuments() + } + }, null, this._disposables) - this.syncAndRenderHints() + this.renderHintsForAllDocuments() } - private async syncAndRenderHints(doc?: Document) { - if (!doc) doc = await workspace.document - if (!isESDocument(doc)) return + private async renderHintsForAllDocuments(): Promise { + for (let doc of workspace.documents) { + await this.syncAndRenderHints(doc) + } + } + + private async syncAndRenderHints(doc: Document) { + if (!this.languageIds.includes(doc.textDocument.languageId)) return + if (!this.inlayHintsEnabled(this.languageIds[0])) return if (this._tokenSource) { this._tokenSource.cancel() @@ -118,8 +138,6 @@ export default class TypeScriptInlayHintsProvider implements Disposable { } async provideInlayHints(document: TextDocument, range: Range, token: CancellationToken): Promise { - if (!this.inlayHintsEnabled(document.languageId)) return [] - const filepath = this.client.toOpenedFilePath(document.uri) if (!filepath) return [] @@ -145,11 +163,6 @@ export default class TypeScriptInlayHintsProvider implements Disposable { } } -function isESDocument(doc: Document) { - if (!doc || !doc.attached) return false - return doc.filetype === 'typescript' || doc.filetype === 'javascript' -} - function fromProtocolInlayHintKind(kind: Proto.InlayHintKind): InlayHintKind { switch (kind) { case 'Parameter': return InlayHintKind.Parameter diff --git a/src/server/languageProvider.ts b/src/server/languageProvider.ts index 9330fe0..6667608 100644 --- a/src/server/languageProvider.ts +++ b/src/server/languageProvider.ts @@ -161,7 +161,7 @@ export default class LanguageProvider { this._register(new TagClosing(this.client, this.description.id)) } if (this.client.apiVersion.gte(API.v440) && workspace.isNvim) { - this._register(new TypeScriptInlayHintsProvider(this.client, this.fileConfigurationManager)) + this._register(new TypeScriptInlayHintsProvider(this.client, this.fileConfigurationManager, languageIds)) } }