From aeede042a93441f038f6b36448aaf383969bc86b Mon Sep 17 00:00:00 2001 From: Yegappan Lakshmanan Date: Tue, 1 Feb 2022 18:12:31 -0800 Subject: [PATCH] Vim9 no longer supports # in function names. Use global functions --- README.md | 4 +- autoload/handlers.vim | 6 +- autoload/lsp.vim | 109 ++++++------- autoload/lspoptions.vim | 2 +- autoload/outline.vim | 3 +- doc/lsp.txt | 22 +-- plugin/lsp.vim | 338 ++++++++++++++++++++++++++++++++-------- test/unit_tests.vim | 40 +++-- 8 files changed, 371 insertions(+), 153 deletions(-) diff --git a/README.md b/README.md index c1b073b..39edd9b 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ To register a LSP server, add the following lines to your .vimrc file: \ 'args': ['--nthreads=1', '--use_signature_help', '--hover_signature'] \ } \ ] - call lsp#addServer(lspServers) + call LspAddServer(lspServers) ``` The above lines add the LSP servers for C, C++, Javascript, Typescript, Shell script, Vim script, Go and Python file types. @@ -77,7 +77,7 @@ filetype|One or more file types supported by the LSP server. This can be a Stri path|complete path to the LSP server executable (without any arguments). args|a list of command-line arguments passed to the LSP server. Each argument is a separate List item. -The LSP servers are added using the lsp#addServer() function. This function accepts a list of LSP servers with the above information. +The LSP servers are added using the LspAddServer() function. This function accepts a list of LSP servers with the above information. ## Supported Commands Command|Description diff --git a/autoload/handlers.vim b/autoload/handlers.vim index 3e8dacd..aa82659 100644 --- a/autoload/handlers.vim +++ b/autoload/handlers.vim @@ -79,7 +79,7 @@ def s:processInitializeReply(lspserver: dict, req: dict, reply: dicthas_key('signatureHelpProvider') var triggers = caps.signatureHelpProvider.triggerCharacters for ch in triggers - exe 'inoremap ' .. ch .. ' ' .. ch .. "=lsp#showSignature()" + exe 'inoremap ' .. ch .. ' ' .. ch .. "=LspShowSignature()" endfor endif @@ -92,8 +92,8 @@ def s:processInitializeReply(lspserver: dict, req: dict, reply: dict call lsp#docHighlightClear() - | call lsp#docHighlight() + autocmd CursorMoved call g:LspDocHighlightClear() + | call g:LspDocHighlight() augroup END endif diff --git a/autoload/lsp.vim b/autoload/lsp.vim index 3e8ccce..f51996e 100644 --- a/autoload/lsp.vim +++ b/autoload/lsp.vim @@ -22,7 +22,6 @@ if has('patch-8.2.4019') import './symbol.vim' as symbol_import import './outline.vim' as outline_import - opt.LspOptionsSet = opt_import.LspOptionsSet opt.lspOptions = opt_import.lspOptions lserver.NewLspServer = server_import.NewLspServer util.WarnMsg = util_import.WarnMsg @@ -45,7 +44,7 @@ if has('patch-8.2.4019') outline.OpenOutlineWindow = outline_import.OpenOutlineWindow outline.SkipOutlineRefresh = outline_import.SkipOutlineRefresh else - import {lspOptions, LspOptionsSet} from './lspoptions.vim' + import {lspOptions} from './lspoptions.vim' import NewLspServer from './lspserver.vim' import {WarnMsg, ErrMsg, @@ -66,7 +65,6 @@ else import ShowSymbolMenu from './symbol.vim' import {OpenOutlineWindow, SkipOutlineRefresh} from './outline.vim' - opt.LspOptionsSet = LspOptionsSet opt.lspOptions = lspOptions lserver.NewLspServer = NewLspServer util.WarnMsg = WarnMsg @@ -104,11 +102,6 @@ var bufnrToServer: dict> = {} var lspInitializedOnce = false -# Set user configurable LSP options -def lsp#setOptions(lspOpts: dict) - opt.LspOptionsSet(lspOpts) -enddef - def s:lspInitOnce() # Signs used for LSP diagnostics sign_define([{name: 'LspDiagError', text: 'E ', texthl: 'ErrorMsg', @@ -186,13 +179,13 @@ def s:lspOmniComplSet(ftype: string, enabled: bool) ftypeOmniCtrlMap->extend({[ftype]: enabled}) enddef -def lsp#enableServerTrace() +export def EnableServerTrace() util.ClearTraceLogs() util.ServerTrace(true) enddef # Show information about all the LSP servers -def lsp#showServers() +export def ShowServers() for [ftype, lspserver] in ftypeServerMap->items() var msg = ftype .. " " if lspserver.running @@ -206,7 +199,7 @@ def lsp#showServers() enddef # Go to a definition using "textDocument/definition" LSP request -def lsp#gotoDefinition(peek: bool) +export def GotoDefinition(peek: bool) var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -216,7 +209,7 @@ def lsp#gotoDefinition(peek: bool) enddef # Go to a declaration using "textDocument/declaration" LSP request -def lsp#gotoDeclaration(peek: bool) +export def GotoDeclaration(peek: bool) var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -226,7 +219,7 @@ def lsp#gotoDeclaration(peek: bool) enddef # Go to a type definition using "textDocument/typeDefinition" LSP request -def lsp#gotoTypedef(peek: bool) +export def GotoTypedef(peek: bool) var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -236,7 +229,7 @@ def lsp#gotoTypedef(peek: bool) enddef # Go to a implementation using "textDocument/implementation" LSP request -def lsp#gotoImplementation(peek: bool) +export def GotoImplementation(peek: bool) var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -247,7 +240,7 @@ enddef # Show the signature using "textDocument/signatureHelp" LSP method # Invoked from an insert-mode mapping, so return an empty string. -def lsp#showSignature(): string +def g:LspShowSignature(): string var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return '' @@ -260,7 +253,7 @@ def lsp#showSignature(): string enddef # buffer change notification listener -def lsp#bufchange_listener(bnr: number, start: number, end: number, added: number, changes: list>) +def s:bufchange_listener(bnr: number, start: number, end: number, added: number, changes: list>) var lspserver: dict = s:curbufGetServer() if lspserver->empty() || !lspserver.running return @@ -309,7 +302,7 @@ def g:LspDiagExpr(): string enddef # Called after leaving insert mode. Used to process diag messages (if any) -def lsp#leftInsertMode() +def g:LspLeftInsertMode() if !exists('b:LspDiagsUpdatePending') return endif @@ -324,7 +317,7 @@ def lsp#leftInsertMode() enddef # A new buffer is opened. If LSP is supported for this buffer, then add it -def lsp#addFile(bnr: number): void +export def AddFile(bnr: number): void if bufnrToServer->has_key(bnr) # LSP server for this buffer is already initialized and running return @@ -352,7 +345,7 @@ def lsp#addFile(bnr: number): void lspserver.textdocDidOpen(bnr, ftype) # add a listener to track changes to this buffer - listener_add(function('lsp#bufchange_listener'), bnr) + listener_add(function('s:bufchange_listener'), bnr) # set options for insert mode completion if opt.lspOptions.autoComplete @@ -364,11 +357,11 @@ def lsp#addFile(bnr: number): void endif else if s:lspOmniComplEnabled(ftype) - setbufvar(bnr, '&omnifunc', 'lsp#omniFunc') + setbufvar(bnr, '&omnifunc', 'LspOmniFunc') endif endif - setbufvar(bnr, '&balloonexpr', 'LspDiagExpr()') + setbufvar(bnr, '&balloonexpr', 'g:LspDiagExpr()') # map characters that trigger signature help if opt.lspOptions.showSignature && @@ -376,7 +369,7 @@ def lsp#addFile(bnr: number): void var triggers = lspserver.caps.signatureHelpProvider.triggerCharacters for ch in triggers exe 'inoremap ' .. ch .. ' ' .. ch - .. "=lsp#showSignature()" + .. "=LspShowSignature()" endfor endif @@ -387,18 +380,18 @@ def lsp#addFile(bnr: number): void if opt.lspOptions.autoComplete # Trigger 24x7 insert mode completion when text is changed - exe 'autocmd TextChangedI call lsp#complete()' + exe 'autocmd TextChangedI call LspComplete()' endif # Update the diagnostics when insert mode is stopped - exe 'autocmd InsertLeave call lsp#leftInsertMode()' + exe 'autocmd InsertLeave call LspLeftInsertMode()' if opt.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()' + .. 'call LspDocHighlightClear() | call LspDocHighlight()' endif augroup END @@ -406,7 +399,7 @@ def lsp#addFile(bnr: number): void enddef # Notify LSP server to remove a file -def lsp#removeFile(bnr: number): void +export def RemoveFile(bnr: number): void var lspserver: dict = s:bufGetServer(bnr) if lspserver->empty() || !lspserver.running return @@ -417,7 +410,7 @@ def lsp#removeFile(bnr: number): void enddef # Stop all the LSP servers -def lsp#stopAllServers() +export def StopAllServers() for lspserver in lspServers if lspserver.running lspserver.stopServer() @@ -426,7 +419,7 @@ def lsp#stopAllServers() enddef # Register a LSP server for one or more file types -def lsp#addServer(serverList: list>) +export def AddServer(serverList: list>) for server in serverList if !server->has_key('filetype') || !server->has_key('path') util.ErrMsg('Error: LSP server information is missing filetype or path') @@ -478,7 +471,7 @@ enddef # The LSP server is considered ready when the server capabilities are # received ('initialize' LSP reply message) -def lsp#serverReady(): bool +export def ServerReady(): bool var fname: string = @% if fname == '' return false @@ -493,7 +486,7 @@ enddef # set the LSP server trace level for the current buffer # Params: SetTraceParams -def lsp#setTraceServer(traceVal: string) +export def SetTraceServer(traceVal: string) if ['off', 'message', 'verbose']->index(traceVal) == -1 util.ErrMsg("Error: Unsupported LSP server trace value " .. traceVal) return @@ -509,7 +502,7 @@ enddef # Display the diagnostic messages from the LSP server for the current buffer # in a quickfix list -def lsp#showDiagnostics(): void +export def ShowDiagnostics(): void var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -519,7 +512,7 @@ def lsp#showDiagnostics(): void enddef # Show the diagnostic message for the current line -def lsp#showCurrentDiag() +export def LspShowCurrentDiag() var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -529,7 +522,7 @@ def lsp#showCurrentDiag() enddef # Display the diagnostics for the current line in the status line. -def lsp#showCurrentDiagInStatusLine() +export def LspShowCurrentDiagInStatusLine() var fname: string = @% if fname == '' return @@ -544,7 +537,7 @@ def lsp#showCurrentDiagInStatusLine() enddef # get the count of diagnostics in the current buffer -def lsp#errorCount(): dict +export def ErrorCount(): dict var res = {'Error': 0, 'Warn': 0, 'Info': 0, 'Hint': 0} var fname: string = @% if fname == '' @@ -560,7 +553,7 @@ def lsp#errorCount(): dict enddef # jump to the next/previous/first diagnostic message in the current buffer -def lsp#jumpToDiag(which: string): void +export def JumpToDiag(which: string): void var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -571,7 +564,7 @@ enddef # Insert mode completion handler. Used when 24x7 completion is enabled # (default). -def lsp#complete() +def g:LspComplete() var lspserver: dict = s:curbufGetServer() if lspserver->empty() || !lspserver.running || !lspserver.ready return @@ -607,7 +600,7 @@ def lsp#complete() enddef # omni complete handler -def lsp#omniFunc(findstart: number, base: string): any +def g:LspOmniFunc(findstart: number, base: string): any var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return -2 @@ -650,7 +643,7 @@ enddef # Display the hover message from the LSP server for the current cursor # location -def lsp#hover() +export def Hover() var lspserver: dict = s:curbufGetServer() if lspserver->empty() || !lspserver.running || !lspserver.ready return @@ -660,7 +653,7 @@ def lsp#hover() enddef # show symbol references -def lsp#showReferences(peek: bool) +export def ShowReferences(peek: bool) var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -670,7 +663,7 @@ def lsp#showReferences(peek: bool) enddef # highlight all the places where a symbol is referenced -def lsp#docHighlight() +def g:LspDocHighlight() var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -680,13 +673,13 @@ def lsp#docHighlight() enddef # clear the symbol reference highlight -def lsp#docHighlightClear() +def g:LspDocHighlightClear() prop_remove({'type': 'LspTextRef', 'all': true}, 1, line('$')) prop_remove({'type': 'LspReadRef', 'all': true}, 1, line('$')) prop_remove({'type': 'LspWriteRef', 'all': true}, 1, line('$')) enddef -def lsp#requestDocSymbols() +def g:LspRequestDocSymbols() if outline.SkipOutlineRefresh() return endif @@ -705,13 +698,13 @@ def lsp#requestDocSymbols() enddef # open a window and display all the symbols in a file (outline) -def lsp#outline() +export def Outline() outline.OpenOutlineWindow() - lsp#requestDocSymbols() + g:LspRequestDocSymbols() enddef # Format the entire file -def lsp#textDocFormat(range_args: number, line1: number, line2: number) +export def TextDocFormat(range_args: number, line1: number, line2: number) if !&modifiable util.ErrMsg('Error: Current file is not a modifiable file') return @@ -735,7 +728,7 @@ enddef # Display all the locations where the current symbol is called from. # Uses LSP "callHierarchy/incomingCalls" request -def lsp#incomingCalls() +export def IncomingCalls() var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -747,13 +740,13 @@ enddef # Display all the symbols used by the current symbol. # Uses LSP "callHierarchy/outgoingCalls" request -def lsp#outgoingCalls() +export def OutgoingCalls() :echomsg 'Error: Not implemented yet' enddef # Rename a symbol # Uses LSP "textDocument/rename" request -def lsp#rename() +export def Rename() var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -769,7 +762,7 @@ enddef # Perform a code action # Uses LSP "textDocument/codeAction" request -def lsp#codeAction() +export def CodeAction() var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -781,7 +774,7 @@ enddef # Perform a workspace wide symbol lookup # Uses LSP "workspace/symbol" request -def lsp#symbolSearch(queryArg: string) +export def SymbolSearch(queryArg: string) var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -804,7 +797,7 @@ def lsp#symbolSearch(queryArg: string) enddef # Display the list of workspace folders -def lsp#listWorkspaceFolders() +export def ListWorkspaceFolders() var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -814,7 +807,7 @@ def lsp#listWorkspaceFolders() enddef # Add a workspace folder. Default is to use the current folder. -def lsp#addWorkspaceFolder(dirArg: string) +export def AddWorkspaceFolder(dirArg: string) var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -837,7 +830,7 @@ def lsp#addWorkspaceFolder(dirArg: string) enddef # Remove a workspace folder. Default is to use the current folder. -def lsp#removeWorkspaceFolder(dirArg: string) +export def RemoveWorkspaceFolder(dirArg: string) var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -860,7 +853,7 @@ def lsp#removeWorkspaceFolder(dirArg: string) enddef # visually select a range of positions around the current cursor. -def lsp#selectionRange() +export def SelectionRange() var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -872,7 +865,7 @@ def lsp#selectionRange() enddef # fold the entire document -def lsp#foldDocument() +export def FoldDocument() var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return @@ -888,17 +881,17 @@ def lsp#foldDocument() enddef # Enable diagnostic highlighting for all the buffers -def lsp#diagHighlightEnable() +export def DiagHighlightEnable() diag.DiagsHighlightEnable() enddef # Disable diagnostic highlighting for all the buffers -def lsp#diagHighlightDisable() +export def DiagHighlightDisable() diag.DiagsHighlightDisable() enddef # Display the LSP server capabilities -def lsp#showServerCapabilities() +export def ShowServerCapabilities() var lspserver: dict = s:curbufGetServerChecked() if lspserver->empty() return diff --git a/autoload/lspoptions.vim b/autoload/lspoptions.vim index f779017..4b99d37 100644 --- a/autoload/lspoptions.vim +++ b/autoload/lspoptions.vim @@ -1,7 +1,7 @@ vim9script # LSP plugin options -# User can override these by calling the lsp#setOptions() function. +# User can override these by calling the LspOptionsSet() function. export var lspOptions: dict = { # In insert mode, complete the current symbol automatically # Otherwise, use omni-completion diff --git a/autoload/outline.vim b/autoload/outline.vim index 30a591d..71840f8 100644 --- a/autoload/outline.vim +++ b/autoload/outline.vim @@ -10,6 +10,7 @@ if has('patch-8.2.4019') else import GetLineByteFromPos from './util.vim' import lspOptions from './lspoptions.vim' + util.GetLineByteFromPos = GetLineByteFromPos opt.lspOptions = lspOptions endif @@ -276,7 +277,7 @@ export def OpenOutlineWindow() augroup LSPOutline au! - autocmd BufEnter * call lsp#requestDocSymbols() + autocmd BufEnter * call g:LspRequestDocSymbols() # when the outline window is closed, do the cleanup autocmd BufUnload LSP-Outline call s:outlineCleanup() autocmd CursorHold * call s:outlineHighlightCurrentSymbol() diff --git a/doc/lsp.txt b/doc/lsp.txt index a16c085..e4e2843 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -129,7 +129,7 @@ The following commands are provided: ============================================================================== 4. Configuration *lsp-configuration* -To register one or more LSP servers, use the lsp#addServer() function with a +To register one or more LSP servers, use the LspAddServer() function with a list of LSP server details in the .vimrc file. For example, to add the LSP servers for the Javascript, Typescript and Python @@ -147,7 +147,7 @@ file types, add the following commands to the .vimrc file: > \ 'args': ['--check-parent-process', '-v'] \ } \ ] - call lsp#addServer(lspServers) + call LspAddServer(lspServers) < Depending on the location of the typescript and python pyls language servers installed in your system, update the 'path' in the above snippet @@ -173,7 +173,7 @@ Vim file types: > \ args: ['--stdio'] \ } \ ] - call lsp#addServer(lspServers) + call LspAddServer(lspServers) < To add a LSP server, the following information is needed: @@ -187,12 +187,12 @@ To add a LSP server, the following information is needed: omnicompl a boolean value that enables (true) or disables (false) omni-completion for this file type. -The LSP servers are added using the lsp#addServer() function. This function +The LSP servers are added using the LspAddServer() function. This function accepts a list of LSP servers with the above information. *lsp-options* Some of the LSP plugin features can be enabled or disabled by using the -lsp#setOptions() function. This function accepts a dictionary argument with the +LspOptionsSet() function. This function accepts a dictionary argument with the following optional items: autoComplete In insert mode, automatically complete the current @@ -228,7 +228,7 @@ ignoreMissingServer Do not print a missing language server executable. For example, to disable the automatic placement of signs for the LSP diagnostic messages, you can add the following line to your .vimrc file: - call lsp#setOptions({'autoHighlightDiags': v:false}) + call LspOptionsSet({'autoHighlightDiags': v:false}) ============================================================================== 5. Commands *lsp-commands* @@ -236,7 +236,7 @@ diagnostic messages, you can add the following line to your .vimrc file: *:LspShowServers* :LspShowServers Displays the list of registered LSP servers and their status. The LSP servers are registered using the - lsp#addServer() function. The output shows the Vim + LspAddServer() function. The output shows the Vim file type, the corresponding LSP server status and the path to the LSP server executable. @@ -301,14 +301,14 @@ diagnostic messages, you can add the following line to your .vimrc file: this, you can set the showSignature option to false in your .vimrc file: > - call lsp#setOptions({'showSignature': v:false}) + call LspOptionsSet({'showSignature': v:false}) < Default is true. You can get the function signature echoed in cmdline rather than displayed in popup if you use > - call lsp#setOptions({'echoSignature': v:true}) + call LspOptionsSet({'echoSignature': v:true}) < Default is false. @@ -449,7 +449,7 @@ diagnostic messages, you can add the following line to your .vimrc file: documentation in the preview window instead of in a popup set > - call lsp#setOptions({'hoverInPreview': v:true}) + call LspOptionsSet({'hoverInPreview': v:true}) < Default is false @@ -488,7 +488,7 @@ use the keys described in |popupmenu-keys| with this menu. To disable the auto-compeltion, you can set the autoComplete option to v:false in your .vimrc file: > - call lsp#setOptions({'autoComplete': v:false}) + call LspOptionsSet({'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 6029d96..a118632 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -7,108 +7,310 @@ if !has('patch-8.2.2342') endif var opt = {} -if has('patch-8.2.4019') +var lspf = {} +if has('patch-8.2.4257') + import '../autoload/lspoptions.vim' as lspoptions + import autoload 'lsp.vim' + + opt.LspOptionsSet = lspoptions.LspOptionsSet + opt.lspOptions = lspoptions.lspOptions + lspf.enableServerTrace = lsp.EnableServerTrace + lspf.addServer = lsp.AddServer + lspf.LspServerReady = lsp.ServerReady + lspf.addFile = lsp.AddFile + lspf.removeFile = lsp.RemoveFile + lspf.showCurrentDiagInStatusLine = lsp.LspShowCurrentDiagInStatusLine + lspf.showServers = lsp.ShowServers + lspf.showServerCapabilities = lsp.ShowServerCapabilities + lspf.setTraceServer = lsp.SetTraceServer + lspf.gotoDefinition = lsp.GotoDefinition + lspf.gotoDeclaration = lsp.GotoDeclaration + lspf.gotoTypedef = lsp.GotoTypedef + lspf.gotoImplementation = lsp.GotoImplementation + lspf.gotoDefinition = lsp.GotoDefinition + lspf.gotoDeclaration = lsp.GotoDeclaration + lspf.gotoTypedef = lsp.GotoTypedef + lspf.gotoImplementation = lsp.GotoImplementation + lspf.showDiagnostics = lsp.ShowDiagnostics + lspf.showCurrentDiag = lsp.LspShowCurrentDiag + lspf.jumpToDiag = lsp.JumpToDiag + lspf.jumpToDiag = lsp.JumpToDiag + lspf.jumpToDiag = lsp.JumpToDiag + lspf.diagHighlightEnable = lsp.DiagHighlightEnable + lspf.diagHighlightDisable = lsp.DiagHighlightDisable + lspf.showReferences = lsp.ShowReferences + lspf.showReferences = lsp.ShowReferences + lspf.outline = lsp.Outline + lspf.textDocFormat = lsp.TextDocFormat + lspf.incomingCalls = lsp.IncomingCalls + lspf.outgoingCalls = lsp.OutgoingCalls + lspf.rename = lsp.Rename + lspf.codeAction = lsp.CodeAction + lspf.symbolSearch = lsp.SymbolSearch + lspf.hover = lsp.Hover + lspf.selectionRange = lsp.SelectionRange + lspf.foldDocument = lsp.FoldDocument + lspf.listWorkspaceFolders = lsp.ListWorkspaceFolders + lspf.addWorkspaceFolder = lsp.AddWorkspaceFolder + lspf.removeWorkspaceFolder = lsp.RemoveWorkspaceFolder +elseif has('patch-8.2.4019') import '../autoload/lspoptions.vim' as opt_import + import '../autoload/lsp.vim' as lsp_import + + opt.LspOptionsSet = opt_import.LspOptionsSet opt.lspOptions = opt_import.lspOptions + lspf.enableServerTrace = lsp_import.EnableServerTrace + lspf.addServer = lsp_import.AddServer + lspf.LspServerReady = lsp_import.ServerReady + lspf.addFile = lsp_import.AddFile + lspf.removeFile = lsp_import.RemoveFile + lspf.showCurrentDiagInStatusLine = lsp_import.LspShowCurrentDiagInStatusLine + lspf.showServers = lsp_import.ShowServers + lspf.showServerCapabilities = lsp_import.ShowServerCapabilities + lspf.setTraceServer = lsp_import.SetTraceServer + lspf.gotoDefinition = lsp_import.GotoDefinition + lspf.gotoDeclaration = lsp_import.GotoDeclaration + lspf.gotoTypedef = lsp_import.GotoTypedef + lspf.gotoImplementation = lsp_import.GotoImplementation + lspf.gotoDefinition = lsp_import.GotoDefinition + lspf.gotoDeclaration = lsp_import.GotoDeclaration + lspf.gotoTypedef = lsp_import.GotoTypedef + lspf.gotoImplementation = lsp_import.GotoImplementation + lspf.showDiagnostics = lsp_import.ShowDiagnostics + lspf.showCurrentDiag = lsp_import.LspShowCurrentDiag + lspf.jumpToDiag = lsp_import.JumpToDiag + lspf.jumpToDiag = lsp_import.JumpToDiag + lspf.jumpToDiag = lsp_import.JumpToDiag + lspf.diagHighlightEnable = lsp_import.DiagHighlightEnable + lspf.diagHighlightDisable = lsp_import.DiagHighlightDisable + lspf.showReferences = lsp_import.ShowReferences + lspf.showReferences = lsp_import.ShowReferences + lspf.outline = lsp_import.Outline + lspf.textDocFormat = lsp_import.TextDocFormat + lspf.incomingCalls = lsp_import.IncomingCalls + lspf.outgoingCalls = lsp_import.OutgoingCalls + lspf.rename = lsp_import.Rename + lspf.codeAction = lsp_import.CodeAction + lspf.symbolSearch = lsp_import.SymbolSearch + lspf.hover = lsp_import.Hover + lspf.selectionRange = lsp_import.SelectionRange + lspf.foldDocument = lsp_import.FoldDocument + lspf.listWorkspaceFolders = lsp_import.ListWorkspaceFolders + lspf.addWorkspaceFolder = lsp_import.AddWorkspaceFolder + lspf.removeWorkspaceFolder = lsp_import.RemoveWorkspaceFolder else - import lspOptions from '../autoload/lspoptions.vim' + import {lspOptions, LspOptionsSet} from '../autoload/lspoptions.vim' + import {EnableServerTrace, + AddServer, + ServerReady, + AddFile, + RemoveFile, + LspShowCurrentDiagInStatusLine, + ShowServers, + ShowServerCapabilities, + SetTraceServer, + GotoDefinition, + GotoDeclaration, + GotoTypedef, + GotoImplementation, + ShowDiagnostics, + LspShowCurrentDiag, + JumpToDiag, + DiagHighlightEnable, + DiagHighlightDisable, + ShowReferences, + Outline, + TextDocFormat, + IncomingCalls, + OutgoingCalls, + Rename, + CodeAction, + SymbolSearch, + Hover, + SelectionRange, + FoldDocument, + ListWorkspaceFolders, + AddWorkspaceFolder, + RemoveWorkspaceFolder} from '../autoload/lsp.vim' + + opt.LspOptionsSet = LspOptionsSet opt.lspOptions = lspOptions + lspf.enableServerTrace = EnableServerTrace + lspf.addServer = AddServer + lspf.LspServerReady = ServerReady + lspf.addFile = AddFile + lspf.removeFile = RemoveFile + lspf.showCurrentDiagInStatusLine = LspShowCurrentDiagInStatusLine + lspf.showServers = ShowServers + lspf.showServerCapabilities = ShowServerCapabilities + lspf.setTraceServer = SetTraceServer + lspf.gotoDefinition = GotoDefinition + lspf.gotoDeclaration = GotoDeclaration + lspf.gotoTypedef = GotoTypedef + lspf.gotoImplementation = GotoImplementation + lspf.gotoDefinition = GotoDefinition + lspf.gotoDeclaration = GotoDeclaration + lspf.gotoTypedef = GotoTypedef + lspf.gotoImplementation = GotoImplementation + lspf.showDiagnostics = ShowDiagnostics + lspf.showCurrentDiag = LspShowCurrentDiag + lspf.jumpToDiag = JumpToDiag + lspf.jumpToDiag = JumpToDiag + lspf.jumpToDiag = JumpToDiag + lspf.diagHighlightEnable = DiagHighlightEnable + lspf.diagHighlightDisable = DiagHighlightDisable + lspf.showReferences = ShowReferences + lspf.showReferences = ShowReferences + lspf.outline = Outline + lspf.textDocFormat = TextDocFormat + lspf.incomingCalls = IncomingCalls + lspf.outgoingCalls = OutgoingCalls + lspf.rename = Rename + lspf.codeAction = CodeAction + lspf.symbolSearch = SymbolSearch + lspf.hover = Hover + lspf.selectionRange = SelectionRange + lspf.foldDocument = FoldDocument + lspf.listWorkspaceFolders = ListWorkspaceFolders + lspf.addWorkspaceFolder = AddWorkspaceFolder + lspf.removeWorkspaceFolder = RemoveWorkspaceFolder endif +g:LspOptionsSet = s:opt.LspOptionsSet +g:LspServerTraceEnable = s:lspf.enableServerTrace + +def g:LspAddServer(serverList: list>) + lspf.addServer(serverList) +enddef + +def g:LspServerReady(): bool + return lspf.LspServerReady() +enddef + +var TshowServers = s:lspf.showServers +var TshowServerCapabilities = s:lspf.showServerCapabilities +var TsetTraceServer = s:lspf.setTraceServer +var TaddFile = s:lspf.addFile +var TremoveFile = s:lspf.removeFile +var TshowCurrentDiagInStatusLine = s:lspf.showCurrentDiagInStatusLine +var TgotoDefinition = s:lspf.gotoDefinition +var TgotoDeclaration = s:lspf.gotoDeclaration +var TgotoTypedef = s:lspf.gotoTypedef +var TgotoImplementation = s:lspf.gotoImplementation +var TshowDiagnostics = s:lspf.showDiagnostics +var TshowCurrentDiag = s:lspf.showCurrentDiag +var TjumpToDiag = s:lspf.jumpToDiag +var TdiagHighlightEnable = s:lspf.diagHighlightEnable +var TdiagHighlightDisable = s:lspf.diagHighlightDisable +var TshowReferences = s:lspf.showReferences +var Toutline = s:lspf.outline +var TtextDocFormat = s:lspf.textDocFormat +var TincomingCalls = s:lspf.incomingCalls +var ToutgoingCalls = s:lspf.outgoingCalls +var Trename = s:lspf.rename +var TcodeAction = s:lspf.codeAction +var TsymbolSearch = s:lspf.symbolSearch +var Thover = s:lspf.hover +var TselectionRange = s:lspf.selectionRange +var TfoldDocument = s:lspf.foldDocument +var TlistWorkspaceFolders = s:lspf.listWorkspaceFolders +var TaddWorkspaceFolder = s:lspf.addWorkspaceFolder +var TremoveWorkspaceFolder = s:lspf.removeWorkspaceFolder + augroup LSPAutoCmds au! - autocmd BufNewFile,BufReadPost * lsp#addFile(expand('')->str2nr()) + autocmd BufNewFile,BufReadPost * TaddFile(expand('')->str2nr()) # Note that when BufWipeOut is invoked, the current buffer may be different # from the buffer getting wiped out. - autocmd BufWipeOut * lsp#removeFile(expand('')->str2nr()) + autocmd BufWipeOut * TremoveFile(expand('')->str2nr()) if opt.lspOptions.showDiagOnStatusLine - autocmd CursorMoved * lsp#showCurrentDiagInStatusLine() + autocmd CursorMoved * TshowCurrentDiagInStatusLine() endif augroup END # TODO: Is it needed to shutdown all the LSP servers when exiting Vim? # This takes some time. -# autocmd VimLeavePre * call lsp#stopAllServers() +# autocmd VimLeavePre * call TstopAllServers() # LSP commands -command! -nargs=0 -bar LspShowServers call lsp#showServers() -command! -nargs=0 -bar LspShowServerCapabilities call lsp#showServerCapabilities() -command! -nargs=1 -bar LspSetTrace call lsp#setTraceServer() -command! -nargs=0 -bar LspGotoDefinition call lsp#gotoDefinition(v:false) -command! -nargs=0 -bar LspGotoDeclaration call lsp#gotoDeclaration(v:false) -command! -nargs=0 -bar LspGotoTypeDef call lsp#gotoTypedef(v:false) -command! -nargs=0 -bar LspGotoImpl call lsp#gotoImplementation(v:false) -command! -nargs=0 -bar LspPeekDefinition call lsp#gotoDefinition(v:true) -command! -nargs=0 -bar LspPeekDeclaration call lsp#gotoDeclaration(v:true) -command! -nargs=0 -bar LspPeekTypeDef call lsp#gotoTypedef(v:true) -command! -nargs=0 -bar LspPeekImpl call lsp#gotoImplementation(v:true) -command! -nargs=0 -bar LspShowSignature call lsp#showSignature() -command! -nargs=0 -bar LspDiagShow call lsp#showDiagnostics() -command! -nargs=0 -bar LspDiagCurrent call lsp#showCurrentDiag() -command! -nargs=0 -bar LspDiagFirst call lsp#jumpToDiag('first') -command! -nargs=0 -bar LspDiagNext call lsp#jumpToDiag('next') -command! -nargs=0 -bar LspDiagPrev call lsp#jumpToDiag('prev') -command! -nargs=0 -bar LspDiagHighlightEnable call lsp#diagHighlightEnable() -command! -nargs=0 -bar LspDiagHighlightDisable call lsp#diagHighlightDisable() -command! -nargs=0 -bar LspShowReferences call lsp#showReferences(v:false) -command! -nargs=0 -bar LspPeekReferences call lsp#showReferences(v:true) -command! -nargs=0 -bar LspHighlight call lsp#docHighlight() -command! -nargs=0 -bar LspHighlightClear call lsp#docHighlightClear() -command! -nargs=0 -bar LspOutline call lsp#outline() -command! -nargs=0 -bar -range=% LspFormat call lsp#textDocFormat(, , ) -command! -nargs=0 -bar LspCalledBy call lsp#incomingCalls() -command! -nargs=0 -bar LspCalling call lsp#outgoingCalls() -command! -nargs=0 -bar LspRename call lsp#rename() -command! -nargs=0 -bar LspCodeAction call lsp#codeAction() -command! -nargs=? -bar LspSymbolSearch call lsp#symbolSearch() -command! -nargs=0 -bar LspHover call lsp#hover() -command! -nargs=0 -bar LspSelectionRange call lsp#selectionRange() -command! -nargs=0 -bar LspFold call lsp#foldDocument() -command! -nargs=0 -bar LspWorkspaceListFolders call lsp#listWorkspaceFolders() -command! -nargs=1 -bar -complete=dir LspWorkspaceAddFolder call lsp#addWorkspaceFolder() -command! -nargs=1 -bar -complete=dir LspWorkspaceRemoveFolder call lsp#removeWorkspaceFolder() +command! -nargs=0 -bar LspShowServers call s:TshowServers() +command! -nargs=0 -bar LspShowServerCapabilities call s:TshowServerCapabilities() +command! -nargs=1 -bar LspSetTrace call TsetTraceServer() +command! -nargs=0 -bar LspGotoDefinition call TgotoDefinition(v:false) +command! -nargs=0 -bar LspGotoDeclaration call TgotoDeclaration(v:false) +command! -nargs=0 -bar LspGotoTypeDef call TgotoTypedef(v:false) +command! -nargs=0 -bar LspGotoImpl call TgotoImplementation(v:false) +command! -nargs=0 -bar LspPeekDefinition call TgotoDefinition(v:true) +command! -nargs=0 -bar LspPeekDeclaration call TgotoDeclaration(v:true) +command! -nargs=0 -bar LspPeekTypeDef call TgotoTypedef(v:true) +command! -nargs=0 -bar LspPeekImpl call TgotoImplementation(v:true) +command! -nargs=0 -bar LspShowSignature call LspShowSignature() +command! -nargs=0 -bar LspDiagShow call TshowDiagnostics() +command! -nargs=0 -bar LspDiagCurrent call TshowCurrentDiag() +command! -nargs=0 -bar LspDiagFirst call TjumpToDiag('first') +command! -nargs=0 -bar LspDiagNext call TjumpToDiag('next') +command! -nargs=0 -bar LspDiagPrev call TjumpToDiag('prev') +command! -nargs=0 -bar LspDiagHighlightEnable call TdiagHighlightEnable() +command! -nargs=0 -bar LspDiagHighlightDisable call TdiagHighlightDisable() +command! -nargs=0 -bar LspShowReferences call s:TshowReferences(v:false) +command! -nargs=0 -bar LspPeekReferences call s:TshowReferences(v:true) +command! -nargs=0 -bar LspHighlight call LspDocHighlight() +command! -nargs=0 -bar LspHighlightClear call LspDocHighlightClear() +command! -nargs=0 -bar LspOutline call Toutline() +command! -nargs=0 -bar -range=% LspFormat call TtextDocFormat(, , ) +command! -nargs=0 -bar LspCalledBy call TincomingCalls() +command! -nargs=0 -bar LspCalling call ToutgoingCalls() +command! -nargs=0 -bar LspRename call Trename() +command! -nargs=0 -bar LspCodeAction call TcodeAction() +command! -nargs=? -bar LspSymbolSearch call TsymbolSearch() +command! -nargs=0 -bar LspHover call Thover() +command! -nargs=0 -bar LspSelectionRange call TselectionRange() +command! -nargs=0 -bar LspFold call TfoldDocument() +command! -nargs=0 -bar LspWorkspaceListFolders call TlistWorkspaceFolders() +command! -nargs=1 -bar -complete=dir LspWorkspaceAddFolder call TaddWorkspaceFolder() +command! -nargs=1 -bar -complete=dir LspWorkspaceRemoveFolder call TremoveWorkspaceFolder() # Add the GUI menu entries if has('gui_running') - anoremenu L&sp.Goto.Definition :call lsp#gotoDefinition(v:false) - anoremenu L&sp.Goto.Declaration :call lsp#gotoDeclaration(v:false) - anoremenu L&sp.Goto.Implementation :call lsp#gotoImplementation(v:false) - anoremenu L&sp.Goto.TypeDef :call lsp#gotoTypedef(v:false) + anoremenu L&sp.Goto.Definition :LspGotoDefinition + anoremenu L&sp.Goto.Declaration :LspGotoDeclaration + anoremenu L&sp.Goto.Implementation :LspGotoImpl + anoremenu L&sp.Goto.TypeDef :LspGotoTypeDef - anoremenu L&sp.Show\ Signature :call lsp#showSignature() - anoremenu L&sp.Show\ References :call lsp#showReferences(v:false) - anoremenu L&sp.Show\ Detail :call lsp#hover() - anoremenu L&sp.Outline :call lsp#outline() + anoremenu L&sp.Show\ Signature :LspShowSignature + anoremenu L&sp.Show\ References :LspShowReferences + anoremenu L&sp.Show\ Detail :LspHover + anoremenu L&sp.Outline :LspOutline - anoremenu L&sp.Symbol\ Search :call lsp#symbolSearch('') - anoremenu L&sp.CalledBy :call lsp#incomingCalls() - anoremenu L&sp.Calling :call lsp#outgoingCalls() - anoremenu L&sp.Rename :call lsp#rename() - anoremenu L&sp.Code\ Action :call lsp#codeAction() + anoremenu L&sp.Symbol\ Search :LspSymbolSearch + anoremenu L&sp.CalledBy :LspCalledBy + anoremenu L&sp.Calling :LspCalling + anoremenu L&sp.Rename :LspRename + anoremenu L&sp.Code\ Action :LspCodeAction - anoremenu L&sp.Highlight\ Symbol :call lsp#docHighlight() - anoremenu L&sp.Highlight\ Clear :call lsp#docHighlightClear() + anoremenu L&sp.Highlight\ Symbol :LspHighlight + anoremenu L&sp.Highlight\ Clear :LspHighlightClear # Diagnostics - anoremenu L&sp.Diagnostics.Current :call lsp#showCurrentDiag - anoremenu L&sp.Diagnostics.Show\ All :call lsp#showDiagnostics() - anoremenu L&sp.Diagnostics.First :call lsp#jumpToDiag('first') - anoremenu L&sp.Diagnostics.Next :call lsp#jumpToDiag('next') - anoremenu L&sp.Diagnostics.Prev :call lsp#jumpToDiag('prev') + anoremenu L&sp.Diagnostics.Current :LspDiagCurrent + anoremenu L&sp.Diagnostics.Show\ All :LspDiagShow + anoremenu L&sp.Diagnostics.First :LspDiagFirst + anoremenu L&sp.Diagnostics.Next :LspDiagNext + anoremenu L&sp.Diagnostics.Prev :LspDiagPrev if &mousemodel =~ 'popup' anoremenu PopUp.L&sp.Go\ to\ Definition - \ :call lsp#gotoDefinition(v:false) + \ :LspGotoDefinition anoremenu PopUp.L&sp.Go\ to\ Declaration - \ :call lsp#gotoDeclaration(v:false) + \ :LspGotoDeclaration anoremenu Popup.L&sp.Find\ All\ References - \ :call lsp#showReferences(v:false) + \ :LspShowReferences anoremenu PopUp.L&sp.Show\ Detail - \ :call lsp#hover() + \ :LspHover anoremenu PopUp.L&sp.Highlight\ Symbol - \ :call lsp#docHighlight() + \ :LspHighlight anoremenu PopUp.L&sp.Highlight\ Clear - \ :call lsp#docHighlightClear() + \ :LspHighlightClear endif endif diff --git a/test/unit_tests.vim b/test/unit_tests.vim index 0b3057d..8de33bb 100644 --- a/test/unit_tests.vim +++ b/test/unit_tests.vim @@ -18,14 +18,14 @@ if do_profile profile! file */lsp/* endif -set rtp+=../ -source ../plugin/lsp.vim +set packpath+=../../../../../ +packadd lsp var lspServers = [{ filetype: ['c', 'cpp'], path: '/usr/bin/clangd-12', args: ['--background-index', '--clang-tidy'] }] -lsp#addServer(lspServers) +call LspAddServer(lspServers) g:LSPTest = true @@ -550,9 +550,22 @@ def Test_LspHighlight() cursor(1, 13) :LspHighlight :sleep 1 - assert_equal([{'id': 0, 'col': 13, 'type_bufnr': 0, 'end': 1, 'type': 'LspTextRef', 'length': 3, 'start': 1}], prop_list(1)) - assert_equal([{'id': 0, 'col': 11, 'type_bufnr': 0, 'end': 1, 'type': 'LspReadRef', 'length': 3, 'start': 1}], prop_list(3)) - assert_equal([{'id': 0, 'col': 3, 'type_bufnr': 0, 'end': 1, 'type': 'LspWriteRef', 'length': 3, 'start': 1}], prop_list(4)) + var expected: dict + expected = {id: 0, col: 13, end: 1, type: 'LspTextRef', length: 3, start: 1} + if has('patch-8.2.3233') + expected.type_bufnr = 0 + endif + assert_equal([expected], prop_list(1)) + expected = {id: 0, col: 11, end: 1, type: 'LspReadRef', length: 3, start: 1} + if has('patch-8.2.3233') + expected.type_bufnr = 0 + endif + assert_equal([expected], prop_list(3)) + expected = {id: 0, col: 3, end: 1, type: 'LspWriteRef', length: 3, start: 1} + if has('patch-8.2.3233') + expected.type_bufnr = 0 + endif + assert_equal([expected], prop_list(4)) :LspHighlightClear :sleep 1 assert_equal([], prop_list(1)) @@ -614,7 +627,12 @@ def Test_LspShowSignature() var bnr: number = winbufnr(p[0]) assert_equal(1, p->len()) assert_equal(['MyFunc(int a, int b) -> int'], getbufline(bnr, 1, '$')) - assert_equal([{'id': 0, 'col': 8, 'type_bufnr': 11, 'end': 1, 'type': 'signature', 'length': 5, 'start': 1}], prop_list(1, {bufnr: bnr})) + var expected: dict + expected = {id: 0, col: 8, end: 1, type: 'signature', length: 5, start: 1} + if has('patch-8.2.3233') + expected.type_bufnr = 11 + endif + assert_equal([expected], prop_list(1, {bufnr: bnr})) popup_close(p[0]) setline(line('.'), ' MyFunc(10, ') @@ -625,7 +643,11 @@ def Test_LspShowSignature() bnr = winbufnr(p[0]) assert_equal(1, p->len()) assert_equal(['MyFunc(int a, int b) -> int'], getbufline(bnr, 1, '$')) - assert_equal([{'id': 0, 'col': 15, 'type_bufnr': 11, 'end': 1, 'type': 'signature', 'length': 5, 'start': 1}], prop_list(1, {bufnr: bnr})) + expected = {id: 0, col: 15, end: 1, type: 'signature', length: 5, start: 1} + if has('patch-8.2.3233') + expected.type_bufnr = 11 + endif + assert_equal([expected], prop_list(1, {bufnr: bnr})) popup_close(p[0]) :%bw! enddef @@ -639,7 +661,7 @@ def LspRunTests() :edit Xtest.c # Wait for the LSP server to become ready (max 10 seconds) var maxcount = 100 - while maxcount > 0 && !lsp#serverReady() + while maxcount > 0 && !g:LspServerReady() :sleep 100m maxcount -= 1 endwhile -- 2.48.1