parent
4a146e3d8d
commit
c42ed843b0
2 changed files with 35 additions and 22 deletions
|
@ -3,7 +3,7 @@
|
||||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
* 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 type * as Proto from '../protocol'
|
||||||
import { ITypeScriptServiceClient } from '../typescriptService'
|
import { ITypeScriptServiceClient } from '../typescriptService'
|
||||||
import API from '../utils/api'
|
import API from '../utils/api'
|
||||||
|
@ -43,28 +43,48 @@ export default class TypeScriptInlayHintsProvider implements Disposable {
|
||||||
this._inlayHints.clear()
|
this._inlayHints.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(private readonly client: ITypeScriptServiceClient, private readonly fileConfigurationManager: FileConfigurationManager) {
|
constructor(
|
||||||
events.on('InsertLeave', async bufnr => {
|
private readonly client: ITypeScriptServiceClient,
|
||||||
const doc = workspace.getDocument(bufnr)
|
private readonly fileConfigurationManager: FileConfigurationManager,
|
||||||
await this.syncAndRenderHints(doc)
|
private readonly languageIds: string[]
|
||||||
}, this, this._disposables)
|
) {
|
||||||
|
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 => {
|
workspace.onDidOpenTextDocument(async e => {
|
||||||
const doc = workspace.getDocument(e.bufnr)
|
const doc = workspace.getDocument(e.bufnr)
|
||||||
await this.syncAndRenderHints(doc)
|
await this.syncAndRenderHints(doc)
|
||||||
}, this, this._disposables)
|
}, null, this._disposables)
|
||||||
|
|
||||||
workspace.onDidChangeTextDocument(async e => {
|
workspace.onDidChangeTextDocument(async e => {
|
||||||
const doc = workspace.getDocument(e.bufnr)
|
const doc = workspace.getDocument(e.bufnr)
|
||||||
await this.syncAndRenderHints(doc)
|
if (this.languageIds.includes(doc.textDocument.languageId)) {
|
||||||
}, this, this._disposables)
|
this.renderHintsForAllDocuments()
|
||||||
|
}
|
||||||
|
}, null, this._disposables)
|
||||||
|
|
||||||
this.syncAndRenderHints()
|
this.renderHintsForAllDocuments()
|
||||||
}
|
}
|
||||||
|
|
||||||
private async syncAndRenderHints(doc?: Document) {
|
private async renderHintsForAllDocuments(): Promise<void> {
|
||||||
if (!doc) doc = await workspace.document
|
for (let doc of workspace.documents) {
|
||||||
if (!isESDocument(doc)) return
|
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) {
|
if (this._tokenSource) {
|
||||||
this._tokenSource.cancel()
|
this._tokenSource.cancel()
|
||||||
|
@ -118,8 +138,6 @@ export default class TypeScriptInlayHintsProvider implements Disposable {
|
||||||
}
|
}
|
||||||
|
|
||||||
async provideInlayHints(document: TextDocument, range: Range, token: CancellationToken): Promise<InlayHint[]> {
|
async provideInlayHints(document: TextDocument, range: Range, token: CancellationToken): Promise<InlayHint[]> {
|
||||||
if (!this.inlayHintsEnabled(document.languageId)) return []
|
|
||||||
|
|
||||||
const filepath = this.client.toOpenedFilePath(document.uri)
|
const filepath = this.client.toOpenedFilePath(document.uri)
|
||||||
if (!filepath) return []
|
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 {
|
function fromProtocolInlayHintKind(kind: Proto.InlayHintKind): InlayHintKind {
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case 'Parameter': return InlayHintKind.Parameter
|
case 'Parameter': return InlayHintKind.Parameter
|
||||||
|
|
|
@ -161,7 +161,7 @@ export default class LanguageProvider {
|
||||||
this._register(new TagClosing(this.client, this.description.id))
|
this._register(new TagClosing(this.client, this.description.id))
|
||||||
}
|
}
|
||||||
if (this.client.apiVersion.gte(API.v440) && workspace.isNvim) {
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue