]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Add support for highlighting keyword under cursor
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 20 Dec 2020 00:19:49 +0000 (16:19 -0800)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 20 Dec 2020 00:19:49 +0000 (16:19 -0800)
autoload/lsp.vim
doc/lsp.txt
plugin/lsp.vim

index 50aab9a580fa1d93b22588c58208869b2b1aef0b..0648410d81a69bdaf5b92583fdb08fa68897f28f 100644 (file)
@@ -11,6 +11,10 @@ var lsp_servers: dict<dict<any>> = {}
 
 var lsp_log_dir: string = '/tmp/'
 
+prop_type_add('LSPTextRef', {'highlight': 'Search'})
+prop_type_add('LSPReadRef', {'highlight': 'DiffChange'})
+prop_type_add('LSPWriteRef', {'highlight': 'DiffDelete'})
+
 # process the 'initialize' method reply from the LSP server
 def LSPprocessInitializeReply(ftype: string, reply: dict<any>): void
   if reply.result->len() <= 0
@@ -172,6 +176,36 @@ def LSPprocessReferencesReply(ftype: string, reply: dict<any>): void
   win_gotoid(save_winid)
 enddef
 
+# process the 'textDocument/documentHighlight' reply from the LSP server
+def LSPprocessDocHighlightReply(ftype: string, req: dict<any>, reply: dict<any>): void
+  if reply.result->len() == 0
+    return
+  endif
+
+  var fname: string = req.params.textDocument.uri[7:]
+  var bnum = bufnr(fname)
+
+  for docHL in reply.result
+    var kind: number = docHL->get('kind', 1)
+    var propName: string
+    if kind == 2
+      # Read-access
+      propName = 'LSPReadRef'
+    elseif kind == 3
+      # Write-access
+      propName = 'LSPWriteRef'
+    else
+      # textual reference
+      propName = 'LSPTextRef'
+    endif
+    prop_add(docHL.range.start.line + 1, docHL.range.start.character + 1,
+               {'end_lnum': docHL.range.end.line + 1,
+                'end_col': docHL.range.end.character + 1,
+                'bufnr': bnum,
+                'type': propName})
+  endfor
+enddef
+
 # Process varous reply messages from the LSP server
 def lsp#process_reply(ftype: string, req: dict<any>, reply: dict<any>): void
   if req.method == 'initialize'
@@ -189,6 +223,8 @@ def lsp#process_reply(ftype: string, req: dict<any>, reply: dict<any>): void
     LSPprocessHoverReply(ftype, reply)
   elseif req.method == 'textDocument/references'
     LSPprocessReferencesReply(ftype, reply)
+  elseif req.method == 'textDocument/documentHighlight'
+    LSPprocessDocHighlightReply(ftype, req, reply)
   else
     echomsg "Error: Unsupported reply received from LSP server: " .. string(reply)
   endif
@@ -861,6 +897,7 @@ def lsp#showReferences()
   # Check whether LSP server supports getting reference information
   if !lsp_servers[ftype].caps->has_key('referencesProvider')
               || !lsp_servers[ftype].caps.referencesProvider
+    echomsg "Error: LSP server does not support showing references"
     return
   endif
 
@@ -883,4 +920,50 @@ def lsp#showReferences()
   LSPsendto_server(ftype, req)
 enddef
 
+def lsp#docHighlight()
+  var ftype = &filetype
+  if ftype == ''
+    return
+  endif
+
+  if !lsp_servers->has_key(ftype)
+    echomsg 'Error: LSP server for "' .. ftype .. '" filetype is not found'
+    return
+  endif
+  if !lsp_servers[ftype].running
+    echomsg 'Error: LSP server for "' .. ftype .. '" filetype is not running'
+    return
+  endif
+
+  # Check whether LSP server supports getting reference information
+  if !lsp_servers[ftype].caps->has_key('documentHighlightProvider')
+              || !lsp_servers[ftype].caps.documentHighlightProvider
+    echomsg "Error: LSP server does not support document highlight"
+    return
+  endif
+
+  var fname = expand('%:p')
+  if fname == ''
+    return
+  endif
+  var lnum = line('.') - 1
+  var col = col('.') - 1
+
+  var req = lsp#create_reqmsg(ftype, 'textDocument/documentHighlight')
+  # interface DocumentHighlightParams
+  # interface TextDocumentPositionParams
+  # interface TextDocumentIdentifier
+  req.params->extend({'textDocument': {'uri': 'file://' .. fname}})
+  # interface Position
+  req.params->extend({'position': {'line': lnum, 'character': col}})
+
+  LSPsendto_server(ftype, req)
+enddef
+
+def lsp#docHighlightClear()
+  prop_remove({'type': 'LSPTextRef', 'all': v:true}, 1, line('$'))
+  prop_remove({'type': 'LSPReadRef', 'all': v:true}, 1, line('$'))
+  prop_remove({'type': 'LSPWriteRef', 'all': v:true}, 1, line('$'))
+enddef
+
 # vim: shiftwidth=2 sts=2 expandtab
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0a4bef4ea0e59fe6f637a95cbebcb48db5d3dcd1 100644 (file)
@@ -0,0 +1,88 @@
+*lsp.txt*      Language Server Protocol (LSP) Plugin for Vim9
+
+Author: Yegappan Lakshmanan  (yegappan AT yahoo DOT com)
+For Vim version 8.2.2082 and above
+Last change: Dec 19, 2020
+
+==============================================================================
+                                               *lsp-license*
+License: MIT License
+Copyright (c) 2020 Yegappan Lakshmanan
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+==============================================================================
+1. Overview                                    *lsp-overview*
+
+The Language Server Protocol (LSP) plugin implements a LSP client for Vim9.
+Refer to the following pages for more information about LSP:
+
+    https://microsoft.github.io/language-server-protocol/
+    https://langserver.org/
+
+This plugin needs Vim version 8.2.2082 and after. You will need a language
+specific server in your system to use this plugin. Refer to the above pages
+for a list of available language servers for the various programming
+languages.
+
+The Github repository for this plugin is available at:
+
+      http://github.com/yegappan/lsp
+
+==============================================================================
+2. Installation                                        *lsp-installation*
+
+You can install this plugin directly from github using the following steps:
+
+    $ mkdir -p $HOME/.vim/pack/downloads/lsp
+    $ cd $HOME/.vim/packa/downloads/lsp
+    $ git clone https://github.com/yegappan/lsp
+
+or you can use any one of the Vim plugin managers (dein.vim, pathogen, vam,
+vim-plug, volt, Vundle, etc.) to install and manage this plugin.
+
+To uninstall the LSP plugin, either use the uninstall command provided by the
+plugin manager or manually remove the $HOME/.vim/pack/downloads/lsp directory.
+
+To use this plugin, add the following line to your .vimrc file:
+
+    packadd lsp
+
+==============================================================================
+3. Usage                                       *lsp-usage*
+
+The following commands are provided:
+
+:LspShowServers                Display the list of registered LSP servers
+:LspGotoDefinition     Go to the definition of the keyword under cursor
+:LspGotoDeclaration    Go to the declaration of the keyword under cursor
+:LspGotoTypeDef                Go to the type definition of the keyword under cursor
+:LspGotoImpl           Go to the implementation of the keyword under cursor
+:LspShowSignature      Display the signature of the keyword under cursor
+:LspShowDiagnostics    Display the diagnostics messages from the LSP server for
+                       the current buffer
+:LspShowReferences     Display the list of references to the keyword under
+                       cursor in a new quickfix list.
+:LspHighlight          Highlight all the matches for the keyword under cursor
+:LspHighlightClear     Clear all the matches highlighted by :LspHighlight
+
+==============================================================================
+4. Configuration                               *lsp-configuration*
+
+
+vim:tw=78:ts=8:noet:ft=help:
index cd1a823326bb6475c31bd4cde25b9f48bef07d36..1de62e1e4aaa7b18401d66aeccc2b0bad7bc7c2b 100644 (file)
@@ -20,4 +20,6 @@ command! -nargs=0 LspGotoImpl call lsp#gotoImplementation(expand('%:p'), &filety
 command! -nargs=0 LspShowSignature call lsp#showSignature()
 command! -nargs=0 LspShowDiagnostics call lsp#showDiagnostics()
 command! -nargs=0 LspShowReferences call lsp#showReferences()
+command! -nargs=0 LspHighlight call lsp#docHighlight()
+command! -nargs=0 LspHighlightClear call lsp#docHighlightClear()