diff --git a/src/handler/inlayHint/buffer.ts b/src/handler/inlayHint/buffer.ts index 4eec0debbd8..f0f92e0d964 100644 --- a/src/handler/inlayHint/buffer.ts +++ b/src/handler/inlayHint/buffer.ts @@ -123,16 +123,26 @@ export default class InlayHintBuffer implements SyncItem { return enable === true } - public toggle(): void { + public enable() { + if (!languages.hasProvider(ProviderName.InlayHint, this.doc.textDocument)) throw new Error('Inlay hint provider not found for current document') + if (!this.configEnabled) throw new Error(`Filetype "${this.doc.filetype}" not enabled by inlayHint configuration`) + this.config.display = true + void this.renderRange() + } + + public disable() { if (!languages.hasProvider(ProviderName.InlayHint, this.doc.textDocument)) throw new Error('Inlay hint provider not found for current document') if (!this.configEnabled) throw new Error(`Filetype "${this.doc.filetype}" not enabled by inlayHint configuration`) + this.config.display = false + this.clearCache() + this.clearVirtualText() + } + + public toggle(): void { if (this.config.display) { - this.config.display = false - this.clearCache() - this.clearVirtualText() + this.disable() } else { - this.config.display = true - void this.renderRange() + this.enable() } } diff --git a/src/handler/inlayHint/index.ts b/src/handler/inlayHint/index.ts index a68c1d4c857..ca4f0dc94f0 100644 --- a/src/handler/inlayHint/index.ts +++ b/src/handler/inlayHint/index.ts @@ -59,11 +59,43 @@ export default class InlayHintHandler { return this.toggle(bufnr ?? workspace.bufnr) }, }, false, 'toggle inlayHint display of current buffer') + commands.register({ + id: 'document.enableInlayHint', + execute: (bufnr?: number) => { + return this.enable(bufnr ?? workspace.bufnr) + }, + }, false, 'enable codeLens display of current buffer') + commands.register({ + id: 'document.disableInlayHint', + execute: (bufnr?: number) => { + return this.disable(bufnr ?? workspace.bufnr) + }, + }, false, 'disable codeLens display of current buffer') handler.addDisposable(Disposable.create(() => { disposeAll(this.disposables) })) } + public enable(bufnr: number): void { + let item = this.getItem(bufnr) + try { + workspace.getAttachedDocument(bufnr) + item.enable() + } catch (e) { + void window.showErrorMessage((e as Error).message) + } + } + + public disable(bufnr: number): void { + let item = this.getItem(bufnr) + try { + workspace.getAttachedDocument(bufnr) + item.disable() + } catch (e) { + void window.showErrorMessage((e as Error).message) + } + } + public toggle(bufnr: number): void { let item = this.getItem(bufnr) try {