]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Support custom locations request (#587)
authorBenYip <yebenmy@gmail.com>
Tue, 21 Jan 2025 15:27:32 +0000 (23:27 +0800)
committerGitHub <noreply@github.com>
Tue, 21 Jan 2025 15:27:32 +0000 (07:27 -0800)
* Support custom locations request

* specify server name in FindLocations

* Rename LspFindLocations

autoload/lsp/buffer.vim
autoload/lsp/lsp.vim
autoload/lsp/lspserver.vim

index 5a07654879473c59468301b3e58915f7579fd5ec..f48c3b1c5d83cf4f6759f28bffe0c3a1c2f26013 100644 (file)
@@ -191,4 +191,15 @@ export def CurbufGetServerChecked(feature: string = null_string): dict<any>
   return lspserver
 enddef
 
+export def CurbufGetServerByName(name: string): dict<any>
+  var lspservers: list<dict<any>> = CurbufGetServers()
+
+  for lspserver in lspservers
+    if lspserver.name == name
+      return lspserver
+    endif
+  endfor
+  return {}
+enddef
+
 # vim: tabstop=8 shiftwidth=2 softtabstop=2
index 1ed7b8d2541b0f3ca6efbc7381b47fc9bc399448..69029f2949de2cce69bd48438b0d92d669816903 100644 (file)
@@ -832,6 +832,24 @@ export def ShowReferences(peek: bool)
   lspserver.showReferences(peek)
 enddef
 
+# send custom locations request
+def g:LspFindLocations(server_name: string, peek: bool, method: string, args: dict<any> = {})
+  var lspserver: dict<any> = buf.CurbufGetServerByName(server_name)
+  if lspserver->empty()
+    return
+  endif
+  if !lspserver.running
+    util.ErrMsg($'Language server "{server_name}" is not running')
+    return
+  endif
+  if !lspserver.ready
+    util.ErrMsg($'Language server "{server_name}" is not ready')
+    return
+  endif
+
+  lspserver.findLocations(peek, method, args)
+enddef
+
 # highlight all the places where a symbol is referenced
 def g:LspDocHighlight(bnr: number = bufnr(), cmdmods: string = '')
   var lspserver: dict<any> = buf.CurbufGetServerChecked('documentHighlight')
index 1d4b0ed729e48a4a53cb6a73fc839aade79f3858..11e8d67ce6dfef3d1a003c0375cb6f70087e32a4 100644 (file)
@@ -1006,6 +1006,29 @@ def ShowReferences(lspserver: dict<any>, peek: bool): void
   symbol.ShowLocations(lspserver, reply.result, peek, 'Symbol References')
 enddef
 
+# send custom locations request
+def FindLocations(lspserver: dict<any>, peek: bool, method: string, args: dict<any>): void
+  var param: dict<any>
+  param = lspserver.getTextDocPosition(true)->extend(args)
+  var reply = lspserver.rpc(method, param)
+
+  # Result: Location[] | null
+  if reply->empty() || reply.result->empty()
+    util.WarnMsg('No references found')
+    return
+  endif
+
+  if lspserver.needOffsetEncoding
+    # Decode the position encoding in all the reference locations
+    reply.result->map((_, loc) => {
+      lspserver.decodeLocation(loc)
+      return loc
+    })
+  endif
+
+  symbol.ShowLocations(lspserver, reply.result, peek, 'Symbol References')
+enddef
+
 # process the 'textDocument/documentHighlight' reply from the LSP server
 # Result: DocumentHighlight[] | null
 def DocHighlightReply(lspserver: dict<any>, docHighlightReply: any,
@@ -1948,6 +1971,7 @@ export def NewLspServer(serverParams: dict<any>): dict<any>
     didSaveFile: function(DidSaveFile, [lspserver]),
     hover: function(ShowHoverInfo, [lspserver]),
     showReferences: function(ShowReferences, [lspserver]),
+    findLocations: function(FindLocations, [lspserver]),
     docHighlight: function(DocHighlight, [lspserver]),
     getDocSymbols: function(GetDocSymbols, [lspserver]),
     textDocFormat: function(TextDocFormat, [lspserver]),