From 8c2afedbc46e78b502aa0eedd5c05e6b227d586b Mon Sep 17 00:00:00 2001 From: Yegappan Lakshmanan Date: Sun, 27 Nov 2022 13:36:40 -0800 Subject: [PATCH] When starting to edit a file, update the inlay hints --- autoload/lsp/inlayhints.vim | 28 ++++++++++++++++++++++------ autoload/lsp/lspserver.vim | 6 ++++++ doc/lsp.txt | 3 ++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/autoload/lsp/inlayhints.vim b/autoload/lsp/inlayhints.vim index 86350bb..864ecb0 100644 --- a/autoload/lsp/inlayhints.vim +++ b/autoload/lsp/inlayhints.vim @@ -66,7 +66,7 @@ def InlayHintsCallback(lspserver: dict, timerid: number) enddef # Update all the inlay hints. A timer is used to throttle the updates. -def InlayHintsUpdate() +def LspInlayHintsUpdate() if !get(b:, 'LspInlayHintsNeedsUpdate', true) return endif @@ -87,12 +87,18 @@ def InlayHintsUpdate() enddef # Text is modified. Need to update the inlay hints. -def InlayHintsChanged() +def LspInlayHintsChanged() b:LspInlayHintsNeedsUpdate = true enddef +# Trigger an update of the inlay hints in the current buffer. +export def LspInlayHintsUpdateNow() + b:LspInlayHintsNeedsUpdate = true + LspInlayHintsUpdate() +enddef + # Stop updating the inlay hints. -def InlayHintsUpdateStop() +def LspInlayHintsUpdateStop() var timerid = get(b:, 'LspInlayHintsTimer', -1) if timerid != -1 timerid->timer_stop() @@ -104,18 +110,28 @@ enddef export def BufferInit(bnr: number) var acmds: list> = [] + # Update the inlay hints (if needed) when the cursor is not moved for some + # time. acmds->add({bufnr: bnr, event: ['CursorHold'], group: 'LSPBufferAutocmds', - cmd: 'InlayHintsUpdate()'}) + cmd: 'LspInlayHintsUpdate()'}) + # After the text in the current buffer is modified, the inlay hints need to + # be updated. acmds->add({bufnr: bnr, event: ['TextChanged'], group: 'LSPBufferAutocmds', - cmd: 'InlayHintsChanged()'}) + cmd: 'LspInlayHintsChanged()'}) + # Editing a file should trigger an inlay hint update. + acmds->add({bufnr: bnr, + event: ['BufReadPost'], + group: 'LSPBufferAutocmds', + cmd: 'LspInlayHintsUpdateNow()'}) + # Inlay hints need not be updated if a buffer is no longer active. acmds->add({bufnr: bnr, event: ['BufLeave'], group: 'LSPBufferAutocmds', - cmd: 'InlayHintsUpdateStop()'}) + cmd: 'LspInlayHintsUpdateStop()'}) autocmd_add(acmds) enddef diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index 4a9148c..15d29ec 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -367,6 +367,12 @@ def ServerInitReply(lspserver: dict, initResult: dict): void if bufwinid('LSP-Outline') != -1 lspserver.getDocSymbols(@%) endif + + # Update the inlay hints (if enabled) + if opt.lspOptions.showInlayHints && (lspserver.isInlayHintProvider + || lspserver.isClangdInlayHintsProvider) + inlayhints.LspInlayHintsUpdateNow() + endif enddef # Request: 'initialize' diff --git a/doc/lsp.txt b/doc/lsp.txt index a17f785..bc3d47b 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -292,7 +292,8 @@ showDiagOnStatusLine Show a diagnostic message on a status line. By default this is set to false. showInlayHints Show inlay hints from the language server. By default this is set to false. The inlay hint text is - displayed as a virtual text. + displayed as a virtual text. Needs Vim version + 9.0.0178 or later. showSignature In insert mode, automatically show the current symbol signature in a popup. By default this is set to true. snippetSupport Enable snippet completion support. Need a snippet -- 2.48.1