autoload/handlers.vim | 18 ++++++++++++++---- autoload/lsp.vim | 49 ++++++++++++++++++++++++++++++++++++++++--------- doc/lsp.txt | 22 +++++++++++++--------- plugin/lsp.vim | 14 -------------- diff --git a/autoload/handlers.vim b/autoload/handlers.vim index e895a79b9c2224bf90f4b66ad8dafa22e3f3eb0c..5cf9d12a9688023847c75b4687c8794bfc27a510 100644 --- a/autoload/handlers.vim +++ b/autoload/handlers.vim @@ -4,6 +4,7 @@ # Handlers for messages from the LSP server # Refer to https://microsoft.github.io/language-server-protocol/specification # for the Language Server Protocol (LSP) specificaiton. +import lspOptions from './lspoptions.vim' import {WarnMsg, ErrMsg, TraceLog, @@ -25,18 +26,27 @@ # TODO: Check all the buffers with filetype corresponding to this LSP server # and then setup the below mapping for those buffers. # map characters that trigger signature help - if g:LSP_Show_Signature && caps->has_key('signatureHelpProvider') + if lspOptions.showSignature && caps->has_key('signatureHelpProvider') var triggers = caps.signatureHelpProvider.triggerCharacters for ch in triggers exe 'inoremap ' .. ch .. ' ' .. ch .. "=lsp#showSignature()" endfor endif - if g:LSP_24x7_Complete && caps->has_key('completionProvider') + if lspOptions.autoComplete && caps->has_key('completionProvider') var triggers = caps.completionProvider.triggerCharacters lspserver.completionTriggerChars = triggers endif + if lspOptions.autoHighlight && caps->has_key('documentHighlightProvider') + && caps.documentHighlightProvider + # Highlight all the occurrences of the current keyword + augroup LSPBufferAutocmds + autocmd CursorMoved call lsp#docHighlightClear() + | call lsp#docHighlight() + augroup END + endif + # send a "initialized" notification to server lspserver.sendInitializedNotif() @@ -130,7 +140,7 @@ hllen = label->len() startcol = text->stridx(label) endif endif - if g:LSP_Echo_Signature + if lspOptions.showSignature echon "\r\r" echon '' echon strpart(text, 0, startcol) @@ -227,7 +237,7 @@ d.user_data = item completeItems->add(d) endfor - if g:LSP_24x7_Complete + if lspOptions.autoComplete if completeItems->empty() # no matches return diff --git a/autoload/lsp.vim b/autoload/lsp.vim index 27981f058275e2784a6ffd46bb7199a55a2cdb04..66e43e807091e28097c5bd67c00a0a0c80ffd869 100644 --- a/autoload/lsp.vim +++ b/autoload/lsp.vim @@ -2,6 +2,7 @@ vim9script # Vim9 LSP client +import lspOptions from './lspoptions.vim' import NewLspServer from './lspserver.vim' import {WarnMsg, ErrMsg, @@ -29,6 +30,13 @@ # Buffer number to LSP server map var bufnrToServer: dict> = {} var lspInitializedOnce = false + +# Set user configurable LSP options +def lsp#setOptions(lspOpts: dict) + for key in lspOpts->keys() + lspOptions[key] = lspOpts[key] + endfor +enddef def s:lspInitOnce() # Signs used for LSP diagnostics @@ -238,6 +246,12 @@ # No diagnostic for the current cursor location return '' endif + # Display the diagnostic message only if the mouse is over the first two + # columns + if v:beval_col >= 3 + return '' + endif + return diagInfo.message enddef @@ -283,18 +297,13 @@ lspserver.startServer() endif lspserver.textdocDidOpen(bnr, ftype) - # file saved notification handler - autocmd BufWritePost call s:lspSavedFile() - # add a listener to track changes to this buffer listener_add(function('lsp#bufchange_listener'), bnr) # set options for insert mode completion - if g:LSP_24x7_Complete + if lspOptions.autoComplete setbufvar(bnr, '&completeopt', 'menuone,popup,noinsert,noselect') setbufvar(bnr, '&completepopup', 'border:off') - # autocmd for 24x7 insert mode completion - autocmd TextChangedI call lsp#complete() # in insert mode stops completion and inserts a inoremap pumvisible() ? "\\" : "\" else @@ -304,16 +313,38 @@ endif endif setbufvar(bnr, '&balloonexpr', 'LspDiagExpr()') - exe 'autocmd InsertLeave call lsp#leftInsertMode()' # map characters that trigger signature help - if g:LSP_Show_Signature && lspserver.caps->has_key('signatureHelpProvider') + if lspOptions.showSignature && + lspserver.caps->has_key('signatureHelpProvider') var triggers = lspserver.caps.signatureHelpProvider.triggerCharacters for ch in triggers exe 'inoremap ' .. ch .. ' ' .. ch .. "=lsp#showSignature()" endfor endif + + # Set buffer local autocmds + augroup LSPBufferAutocmds + # file saved notification handler + exe 'autocmd BufWritePost call s:lspSavedFile()' + + if lspOptions.autoComplete + # Trigger 24x7 insert mode completion when text is changed + exe 'autocmd TextChangedI call lsp#complete()' + endif + + # Update the diagnostics when insert mode is stopped + exe 'autocmd InsertLeave call lsp#leftInsertMode()' + + if lspOptions.autoHighlight && + lspserver.caps->has_key('documentHighlightProvider') + && lspserver.caps.documentHighlightProvider + # Highlight all the occurrences of the current keyword + exe 'autocmd CursorMoved ' + .. 'call lsp#docHighlightClear() | call lsp#docHighlight()' + endif + augroup END bufnrToServer[bnr] = lspserver enddef @@ -1139,7 +1170,7 @@ if fname == '' return endif - var newName: string = input("Enter new name: ", expand('')) + var newName: string = input("Rename symbol: ", expand('')) if newName == '' return endif diff --git a/doc/lsp.txt b/doc/lsp.txt index 3f8e4ae0d03405768a64d43fbab689cf569f6bf1..5246d998bfad27897e699d13eab7ad718b058920 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -2,12 +2,12 @@ *lsp.txt* Language Server Protocol (LSP) Plugin for Vim9 Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com) For Vim version 8.2.2342 and above -Last change: Feb 24, 2021 +Last change: Nov 13, 2021 ============================================================================== *lsp-license* License: MIT License -Copyright (c) 2020 Yegappan Lakshmanan +Copyright (c) 2020-2021 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 @@ -214,16 +214,20 @@ or method) before the cursor in a popup. The popup is also automatically displayed in insert mode after entering a symbol name followed by a - separator (e.g. a opening parenthesis). Unless if: > - let g:LSP_Show_Signature = v:false. + separator (e.g. a opening parenthesis). To disable + this, you can set the showSignature option to false in + your .vimrc file: > + + call lsp#setOptions({showSignature: v:false}) < Default is true. You can get the function signature echoed in cmdline rather than displayed in popup if you use > - let g:LSP_Echo_Signature = v:true + + call lsp#setOptions({echoSignature: v:true}) < - Default is false + Default is false. *:LspDiagShow* :LspDiagShow Creates a new location list with the diagnostics @@ -366,10 +370,10 @@ By default, in insert mode, the LSP plugin automatically displays the matches for the symbol under the cursor in an insert-completion popup menu. You can use the keys described in |popupmenu-keys| with this menu. -To disable the auto-compeltion, you can set the LSP_24x7_Complete variable -to v:false in your .vimrc file: > +To disable the auto-compeltion, you can set the autoComplete option to v:false +in your .vimrc file: > - let g:LSP_24x7_Complete = v:false + call lsp#setOptions({'autoComplete': v:false}) < If this variable is set, then the LSP plugin doesn't automatically start completion in insert mode and instead supports omni-completion (|compl-omni|). diff --git a/plugin/lsp.vim b/plugin/lsp.vim index 512b57be9a9b19dd2977632bdd581ce40c6c13d4..8683e639089068d7879f39e45e49bb0d3b81f521 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -5,20 +5,6 @@ if v:version < 802 || !has('patch-8.2.2342') finish endif -" Perform completion in insert mode automatically. Otherwise use -" omni-complete. -if !exists('g:LSP_24x7_Complete') - let g:LSP_24x7_Complete = v:true -endif - -if !exists('g:LSP_Show_Signature') - let g:LSP_Show_Signature = v:true -endif - -if !exists('g:LSP_Echo_Signature') - let g:LSP_Echo_Signature = v:false -endif - augroup LSPAutoCmds au! autocmd BufNewFile,BufReadPost *