From: Yegappan Lakshmanan Date: Sun, 20 Dec 2020 00:19:49 +0000 (-0800) Subject: Add support for highlighting keyword under cursor X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=f7a270165867e447a1f8e53f3c4dc9ab9c04dd36;p=vim-lsp.git Add support for highlighting keyword under cursor --- diff --git a/autoload/lsp.vim b/autoload/lsp.vim index 50aab9a..0648410 100644 --- a/autoload/lsp.vim +++ b/autoload/lsp.vim @@ -11,6 +11,10 @@ var lsp_servers: dict> = {} 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): void if reply.result->len() <= 0 @@ -172,6 +176,36 @@ def LSPprocessReferencesReply(ftype: string, reply: dict): void win_gotoid(save_winid) enddef +# process the 'textDocument/documentHighlight' reply from the LSP server +def LSPprocessDocHighlightReply(ftype: string, req: dict, reply: dict): 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, reply: dict): void if req.method == 'initialize' @@ -189,6 +223,8 @@ def lsp#process_reply(ftype: string, req: dict, reply: dict): 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 diff --git a/doc/lsp.txt b/doc/lsp.txt index e69de29..0a4bef4 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -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: diff --git a/plugin/lsp.vim b/plugin/lsp.vim index cd1a823..1de62e1 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -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()