]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
When starting to edit a file, update the inlay hints
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 27 Nov 2022 21:36:40 +0000 (13:36 -0800)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 27 Nov 2022 21:36:40 +0000 (13:36 -0800)
autoload/lsp/inlayhints.vim
autoload/lsp/lspserver.vim
doc/lsp.txt

index 86350bbd4184ccac999777b127a35bcdf60f6b0a..864ecb0d77abcd29739fdcd3edc2853655fcf82e 100644 (file)
@@ -66,7 +66,7 @@ def InlayHintsCallback(lspserver: dict<any>, 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<dict<any>> = []
 
+  # 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
index 4a9148c9e531faac4738218c0b4933ac59ba4411..15d29eccf80a6d0c2eead081f5f3b8405b0438c3 100644 (file)
@@ -367,6 +367,12 @@ def ServerInitReply(lspserver: dict<any>, initResult: dict<any>): 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'
index a17f7859b590d693b24027db6881b84cfedcdc5f..bc3d47bbdd9d9a12e6ec1ec2b7754c61a5abc4ac 100644 (file)
@@ -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