:LspWorkspaceAddFolder `{folder}`| Add a folder to the workspace
:LspWorkspaceRemoveFolder `{folder}`|Remove a folder from the workspace
:LspWorkspaceListFolders|Show the list of folders in the workspace
+:LspServerRestart|Restart the LSP server for the current buffer
## Similar Vim LSP Plugins
endif
enddef
+# Process a 'shutdown' reply from the LSP server.
+def ProcessShutdownReply(lspserver: dict<any>, req: dict<any>, reply: dict<any>): void
+ return
+enddef
+
# process the 'textDocument/definition' / 'textDocument/declaration' /
# 'textDocument/typeDefinition' and 'textDocument/implementation' replies from
# the LSP server
var lsp_reply_handlers: dict<func> =
{
'initialize': function('ProcessInitializeReply'),
+ 'shutdown': function('ProcessShutdownReply'),
'textDocument/definition': function('ProcessDefDeclReply'),
'textDocument/declaration': function('ProcessDefDeclReply'),
'textDocument/typeDefinition': function('ProcessDefDeclReply'),
if lsp_reply_handlers->has_key(req.method)
lsp_reply_handlers[req.method](lspserver, req, reply)
else
- util.ErrMsg("Error: Unsupported reply received from LSP server: " .. reply->string())
+ util.ErrMsg("Error: Unsupported reply received from LSP server: " .. reply->string() .. " for request: " .. req->string())
endif
enddef
# Notify LSP server to remove a file
export def RemoveFile(bnr: number): void
var lspserver: dict<any> = buf.BufLspServerGet(bnr)
- if lspserver->empty() || !lspserver.running
+ if lspserver->empty()
return
endif
- lspserver.textdocDidClose(bnr)
+ if lspserver.running
+ lspserver.textdocDidClose(bnr)
+ endif
diag.DiagRemoveFile(lspserver, bnr)
buf.BufLspServerRemove(bnr)
enddef
endfor
enddef
+# Restart the LSP server for the current buffer
+export def RestartServer()
+ var lspserver: dict<any> = CurbufGetServerChecked()
+ if lspserver->empty()
+ return
+ endif
+
+ # Stop the server
+ lspserver.stopServer()
+
+ # Remove all the buffers with the same file type as the current buffer
+ var ftype: string = &filetype
+ for binfo in getbufinfo()
+ if getbufvar(binfo.bufnr, '&filetype') == ftype
+ RemoveFile(binfo.bufnr)
+ endif
+ endfor
+
+ # Start the server again
+ lspserver.startServer(true)
+
+ # Add all the buffers with the same file type as the current buffer
+ for binfo in getbufinfo({bufloaded: 1})
+ if getbufvar(binfo.bufnr, '&filetype') == ftype
+ AddFile(binfo.bufnr)
+ endif
+ endfor
+enddef
+
# Register a LSP server for one or more file types
export def AddServer(serverList: list<dict<any>>)
for server in serverList
return
endif
- var newName: string = input("Rename symbol: ", expand('<cword>'))
+ var sym: string = expand('<cword>')
+ var newName: string = input("Rename symbol '" .. sym .. "' to: ", sym)
if newName == ''
return
endif
# LSP server exit callback
def Exit_cb(lspserver: dict<any>, job: job, status: number): void
util.WarnMsg("LSP server exited with status " .. status)
- lspserver.job = v:null
lspserver.running = false
lspserver.ready = false
lspserver.requests = {}
enddef
# Start a LSP server
-def StartServer(lspserver: dict<any>): number
+#
+# If 'isSync' is true, then waits for the server to send the initialize
+# reponse message.
+def StartServer(lspserver: dict<any>, isSync: bool = false): number
if lspserver.running
util.WarnMsg("LSP server for is already running")
return 0
return 1
endif
- # wait for the LSP server to start
+ # wait a little for the LSP server to start
sleep 10m
lspserver.job = job
lspserver.running = true
- lspserver.initServer()
+ lspserver.initServer(isSync)
return 0
enddef
# Request: 'initialize'
# Param: InitializeParams
-def InitServer(lspserver: dict<any>)
+#
+# If 'isSync' is true, then waits for the server to send the initialize
+# reponse message.
+def InitServer(lspserver: dict<any>, isSync: bool = false)
var req = lspserver.createRequest('initialize')
# client capabilities (ClientCapabilities)
req.params->extend(initparams)
lspserver.sendMessage(req)
+ if isSync
+ lspserver.waitForReponse(req)
+ endif
enddef
# Send a "initialized" LSP notification
def ShutdownServer(lspserver: dict<any>): void
var req = lspserver.createRequest('shutdown')
lspserver.sendMessage(req)
+ lspserver.waitForReponse(req)
enddef
# Send a 'exit' notification to the LSP server
return 0
endif
+ # Send the shutdown request to the server
lspserver.shutdownServer()
- # Wait for the server to process the shutodwn request
- sleep 1
-
+ # Notify the server to exit
lspserver.exitServer()
- lspserver.job->job_stop()
- lspserver.job = v:null
+ # Wait for the server to process the exit notification and exit for a
+ # maximum of 2 seconds.
+ var maxCount: number = 1000
+ while lspserver.job->job_status() == 'run' && maxCount > 0
+ sleep 2m
+ maxCount -= 1
+ endwhile
+
+ if lspserver.job->job_status() == 'run'
+ lspserver.job->job_stop()
+ endif
lspserver.running = false
lspserver.ready = false
lspserver.requests = {}
var req = lspserver.createRequest('textDocument/selectionRange')
# interface SelectionRangeParams
# interface TextDocumentIdentifier
- req.params->extend({textDocument: {uri: util.LspFileToUri(fname)}, positions: [s:GetLspPosition()]})
+ req.params->extend({textDocument: {uri: util.LspFileToUri(fname)}, positions: [GetLspPosition()]})
lspserver.sendMessage(req)
lspserver.waitForReponse(req)
Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
For Vim version 8.2.2342 and above
-Last change: Feb 4, 2022
+Last change: Feb 17, 2022
==============================================================================
*lsp-license*
Show the list of folders in the workspace
:LspShowServerCapabilities
Display the list of capabilities of a LSP server.
+:LspServerRestart Restart the LSP server for the current buffer.
==============================================================================
4. Configuration *lsp-configuration*
:LspWorkspaceRemoveFolder {folder}
Remove a folder from the workspace
- *:LspWorkspaceListFolders*
-:LspWorkspaceListFolders
+:LspWorkspaceListFolders *:LspWorkspaceListFolders*
Show the list of folders in the workspace.
- *:LspShowServerCapabilities*
-:LspShowServerCapabilities
+:LspShowServerCapabilities *:LspShowServerCapabilities*
Display the list of capabilities of a LSP server.
The server capabilities are described in the LSP
protocol specification under the "ServerCapabilities"
interface.
+ *:LspServerRestart*
+:LspServerRestart Restart (stop and then start) the LSP server for the
+ current buffer. All the loaded buffers with the same
+ filetype as the current buffer are added back to the
+ server.
+
==============================================================================
6. Insert mode completion
opt.lspOptions = lspoptions.lspOptions
lspf.enableServerTrace = lsp.EnableServerTrace
lspf.addServer = lsp.AddServer
+ lspf.restartServer = lsp.RestartServer
lspf.LspServerReady = lsp.ServerReady
lspf.addFile = lsp.AddFile
lspf.removeFile = lsp.RemoveFile
opt.lspOptions = opt_import.lspOptions
lspf.enableServerTrace = lsp_import.EnableServerTrace
lspf.addServer = lsp_import.AddServer
+ lspf.restartServer = lsp_import.RestartServer
lspf.LspServerReady = lsp_import.ServerReady
lspf.addFile = lsp_import.AddFile
lspf.removeFile = lsp_import.RemoveFile
import {lspOptions, OptionsSet} from '../autoload/lspoptions.vim'
import {EnableServerTrace,
AddServer,
+ RestartServer,
ServerReady,
AddFile,
RemoveFile,
opt.lspOptions = lspOptions
lspf.enableServerTrace = EnableServerTrace
lspf.addServer = AddServer
+ lspf.restartServer = RestartServer
lspf.LspServerReady = ServerReady
lspf.addFile = AddFile
lspf.removeFile = RemoveFile
var TshowServers = lspf.showServers
var TshowServerCapabilities = lspf.showServerCapabilities
+var TrestartServer = lspf.restartServer
var TsetTraceServer = lspf.setTraceServer
var TaddFile = lspf.addFile
var TremoveFile = lspf.removeFile
# LSP commands
command! -nargs=0 -bar LspShowServers call TshowServers()
command! -nargs=0 -bar LspShowServerCapabilities call TshowServerCapabilities()
+command! -nargs=0 -bar LspServerRestart call TrestartServer()
command! -nargs=1 -bar LspSetTrace call TsetTraceServer(<q-args>)
command! -nargs=0 -bar LspGotoDefinition call TgotoDefinition(v:false)
command! -nargs=0 -bar LspGotoDeclaration call TgotoDeclaration(v:false)