fixx issues with inlayHints

Closes #336
This commit is contained in:
Qiming Zhao 2021-12-25 00:29:52 +08:00
parent 4a146e3d8d
commit c42ed843b0
No known key found for this signature in database
GPG key ID: 9722CD0E8D4DCB8C
2 changed files with 35 additions and 22 deletions

View file

@ -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<void> {
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<InlayHint[]> {
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

View file

@ -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))
}
}