From: Yegappan Lakshmanan Date: Sat, 13 Nov 2021 17:44:00 +0000 (-0800) Subject: Move all the user configurable options to a dictionary and add a function to set... X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=ec095b55d5dff163f0dc607dd1185c87ef33e689;p=vim-lsp.git Move all the user configurable options to a dictionary and add a function to set the options. Add an option to auto highlight the current keyword. Move the LSP autocmds to a group --- diff --git a/autoload/handlers.vim b/autoload/handlers.vim index e895a79..5cf9d12 100644 --- a/autoload/handlers.vim +++ b/autoload/handlers.vim @@ -4,6 +4,7 @@ vim9script # 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 @@ def s:processInitializeReply(lspserver: dict, req: dict, reply: dicthas_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 @@ def s:processSignaturehelpReply(lspserver: dict, req: dict, reply: dic 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 @@ def s:processCompletionReply(lspserver: dict, req: dict, reply: dictadd(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 27981f0..66e43e8 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, @@ -30,6 +31,13 @@ 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 sign_define([{name: 'LspDiagError', text: 'E ', texthl: 'ErrorMsg', @@ -238,6 +246,12 @@ def g:LspDiagExpr(): string 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 @@ def lsp#addFile(bnr: number): void 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,10 +313,10 @@ def lsp#addFile(bnr: number): void 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 @@ -315,6 +324,28 @@ def lsp#addFile(bnr: number): void 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 @@ def lsp#rename() 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 3f8e4ae..5246d99 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -2,12 +2,12 @@ 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 @@ accepts a list of LSP servers with the above information. 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 512b57b..8683e63 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 *