From: Yegappan Lakshmanan Date: Sat, 12 Dec 2020 21:36:30 +0000 (-0800) Subject: Add additional initialization parameters X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=3ba30aa6f4cb159c4a08b67d10d2c883a2a7a65b;p=vim-lsp.git Add additional initialization parameters --- diff --git a/autoload/lsp.vim b/autoload/lsp.vim index 4ef0b16..278c07e 100644 --- a/autoload/lsp.vim +++ b/autoload/lsp.vim @@ -6,24 +6,25 @@ var lsp_servers: dict> = {} var lsp_log_dir: string = '/tmp/' -# process the initialize reply from the LSP server +# process the 'initialize' method reply from the LSP server def lsp#processInitializeReply(ftype: string, reply: dict): void if reply.result->len() <= 0 return endif + # interface 'InitializeResult' var caps: dict = reply.result.capabilities lsp_servers[ftype].caps = caps if caps->has_key('signatureHelpProvider') var triggers = caps.signatureHelpProvider.triggerCharacters for ch in triggers - exe 'inoremap ' .. ch .. ' ' .. ch .. "=lsp#show_signature()" + exe 'inoremap ' .. ch .. ' ' .. ch .. "=lsp#showSignature()" endfor endif enddef -# process the 'textDocument/definition'/'textDocument/declaration' replies -# from the LSP server +# process the 'textDocument/definition' / 'textDocument/declaration' method +# replies from the LSP server def lsp#processDefDeclReply(reply: dict): void if reply.result->len() == 0 echomsg "Error: definition is not found" @@ -42,10 +43,11 @@ def lsp#processDefDeclReply(reply: dict): void redraw! enddef -# process the signatureHelp reply from the LSP server -def lsp#process_signatureHelp_reply(reply: dict): void +# process the 'textDocument/signatureHelp' reply from the LSP server +def lsp#processSignaturehelpReply(reply: dict): void var result: dict = reply.result if result.signatures->len() <= 0 + echomsg 'No signature help available' return endif @@ -67,6 +69,11 @@ def lsp#process_signatureHelp_reply(reply: dict): void endif enddef +# process the 'textDocument/completion' reply from the LSP server +def lsp#processCompletionReply(reply: dict): void + echomsg "Completion reply " .. string(reply) +enddef + # Process varous reply messages from the LSP server def lsp#process_reply(ftype: string, req: dict, reply: dict): void if req.method == 'initialize' @@ -74,7 +81,9 @@ def lsp#process_reply(ftype: string, req: dict, reply: dict): void elseif req.method == 'textDocument/definition' || req.method == 'textDocument/declaration' lsp#processDefDeclReply(reply) elseif req.method == 'textDocument/signatureHelp' - lsp#process_signatureHelp_reply(reply) + lsp#processSignaturehelpReply(reply) + elseif req.method == 'textDocument/completion' + lsp#processCompletionReply(reply) else echomsg "Error: Unsupported reply received from LSP server: " .. string(reply) endif @@ -116,7 +125,11 @@ def lsp#process_server_msg(ftype: string): void lsp_servers[ftype].requests->remove(string(reply.id)) if reply->has_key('error') - echomsg "Error: request " .. req.method .. " failed (" .. reply.error.message .. ")" + var msg: string = reply.error.message + if reply.error->has_key('data') + msg = msg .. ', data = ' .. reply.error.message + endif + echomsg "Error: request " .. req.method .. " failed (" .. msg .. ")" else lsp#process_reply(ftype, req, reply) endif @@ -160,9 +173,7 @@ def lsp#sendto_server(ftype: string, content: dict): void var msg = "Content-Length: " .. req_js->len() .. "\r\n\r\n" var ch = lsp_servers[ftype].job->job_getchannel() ch->ch_sendraw(msg) - call writefile(["req_js length = " .. req_js->len()], "lsp_trace.txt", 'a') ch->ch_sendraw(req_js) - call writefile(["After sending data"], "lsp_trace.txt", 'a') enddef def lsp#create_reqmsg(ftype: string, method: string): dict @@ -190,6 +201,14 @@ enddef # Send a "initialize" LSP request def lsp#init_server(ftype: string): number var req = lsp#create_reqmsg(ftype, 'initialize') + + # interface 'InitializeParams' + var initparams: dict = {} + initparams.processId = getpid() + initparams.clientInfo = {'name': 'Vim', 'version': string(v:versionlong)} + initparams.capabilities = {} + req.params->extend(initparams) + lsp#sendto_server(ftype, req) return 1 enddef @@ -350,10 +369,11 @@ def lsp#goto_declaration(fname: string, ftype: string, lnum: number, col: number lsp#sendto_server(ftype, req) enddef -# Get the signature using "textDocument/signatureHelp" LSP request -def lsp#show_signature(): string +# Show the signature using "textDocument/signatureHelp" LSP method +# Invoked from an insert-mode mapping, so return an empty string. +def lsp#showSignature(): string - # first send all the changes to the current buffer to the LSP server + # first send all the changes in the current buffer to the LSP server listener_flush() var fname: string = expand('%:p') @@ -455,18 +475,58 @@ def lsp#add_server(ftype: string, serverpath: string, args: list) lsp_servers->extend({[ftype]: sinfo}) enddef -def lsp#show_servers() - echomsg lsp_servers +def lsp#showServers() + for [ftype, server] in items(lsp_servers) + var msg = ftype .. " " + if server.running + msg ..= 'running' + else + msg ..= 'not running' + endif + msg ..= ' ' .. server.path + echomsg msg + endfor enddef -def lsp#showDiagnostics(ftype: string): void +def lsp#showDiagnostics(): void + var ftype: string = &filetype var msgs: list = [] - echomsg lsp_servers[ftype].diags + for diag in lsp_servers[ftype].diags diag.message = diag.message->substitute("\n\\+", "\n", 'g') msgs->extend(split(diag.message, "\n")) endfor setqflist([], ' ', {'lines': msgs}) + cwindow +enddef + +def lsp#getCompletion(): void + var fname = expand('%:p') + var ftype = &filetype + var lnum = line('.') - 1 + var col = col('.') - 1 + if fname == '' || 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 + + var req = lsp#create_reqmsg(ftype, 'textDocument/completion') + + # interface CompletionParams + # interface TextDocumentPositionParams + # interface TextDocumentIdentifier + req.params->extend({'textDocument': {'uri': 'file://' .. fname}}) + # interface Position + req.params->extend({'position': {'line': lnum, 'character': col}}) + + lsp#sendto_server(ftype, req) enddef # vim: shiftwidth=2 sts=2 expandtab diff --git a/plugin/lsp.vim b/plugin/lsp.vim index 811efdd..e650a6d 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -9,7 +9,9 @@ autocmd BufWipeOut * call lsp#remove_file(expand(':p'), &filetype) # This takes some time. #autocmd VimLeavePre * call lsp#stop_all_servers() +command! -nargs=0 LspShowServers call lsp#showServers() command! -nargs=0 LspGotoDefinition call lsp#goto_definition(expand('%:p'), &filetype, line('.') - 1, col('.') - 1) command! -nargs=0 LspGotoDeclaration call lsp#goto_declaration(expand('%:p'), &filetype, line('.') - 1, col('.') - 1) -command! -nargs=0 LspGetSignature call lsp#show_signature('') +command! -nargs=0 LspShowSignature call lsp#showSignature() +command! -nargs=0 LspShowDiagnostics call lsp#showDiagnostics()