From: Yegappan Lakshmanan Date: Mon, 31 Oct 2022 02:38:16 +0000 (-0700) Subject: Simplify code by adding a function for send notifications X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=f0c2efea0f32c47d9d950d810a91f4d340d1171c;p=vim-lsp.git Simplify code by adding a function for send notifications --- diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index 101c03f..c99a35b 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -166,11 +166,11 @@ def InitServer(lspserver: dict) lspserver.rpc_a('initialize', initparams, ServerInitReply) enddef -# Send a "initialized" LSP notification -# Params: InitializedParams +# Send a "initialized" notification to the language server def SendInitializedNotif(lspserver: dict) - var notif: dict = lspserver.createNotification('initialized') - lspserver.sendMessage(notif) + # Notification: 'initialized' + # Params: InitializedParams + lspserver.sendNotification('initialized') enddef # Request: shutdown @@ -179,11 +179,11 @@ def ShutdownServer(lspserver: dict): void lspserver.rpc('shutdown', {}) enddef -# Send a 'exit' notification to the LSP server -# Params: void +# Send a 'exit' notification to the language server def ExitServer(lspserver: dict): void - var notif: dict = lspserver.createNotification('exit') - lspserver.sendMessage(notif) + # Notification: 'exit' + # Params: void + lspserver.sendNotification('exit') enddef # Stop a LSP server @@ -216,12 +216,13 @@ def StopServer(lspserver: dict): number return 0 enddef -# Set the LSP server trace level using the $/setTrace notification -# Support values for "traceVal" are "off", "messages" and "verbose". +# Set the language server trace level using the '$/setTrace' notification. +# Supported values for "traceVal" are "off", "messages" and "verbose". def SetTrace(lspserver: dict, traceVal: string) - var notif: dict = lspserver.createNotification('$/setTrace') - notif.params->extend({value: traceVal}) - lspserver.sendMessage(notif) + # Notification: '$/setTrace' + # Params: SetTraceParams + var params = {value: traceVal} + lspserver.sendNotification('$/setTrace', params) enddef # Return the next id for a LSP server request message @@ -293,6 +294,13 @@ def SendMessage(lspserver: dict, content: dict): void endif enddef +# Send a notification message to the language server +def SendNotification(lspserver: dict, method: string, params: any = {}) + var notif: dict = CreateNotification(lspserver, method) + notif.params->extend(params) + lspserver.sendMessage(notif) +enddef + # Send a sync RPC request message to the LSP server and return the received # reply. In case of an error, an empty Dict is returned. def Rpc(lspserver: dict, method: string, params: any): dict @@ -402,51 +410,41 @@ def WaitForResponse(lspserver: dict, req: dict) endwhile enddef -# Send a LSP "textDocument/didOpen" notification -# Params: DidOpenTextDocumentParams +# Send a file/document opened notification to the language server. def TextdocDidOpen(lspserver: dict, bnr: number, ftype: string): void - var notif: dict = lspserver.createNotification('textDocument/didOpen') - - # interface DidOpenTextDocumentParams - # interface TextDocumentItem + # Notification: 'textDocument/didOpen' + # Params: DidOpenTextDocumentParams var tdi = {} tdi.uri = util.LspBufnrToUri(bnr) tdi.languageId = ftype tdi.version = 1 tdi.text = getbufline(bnr, 1, '$')->join("\n") .. "\n" - notif.params->extend({textDocument: tdi}) - - lspserver.sendMessage(notif) + var params = {textDocument: tdi} + lspserver.sendNotification('textDocument/didOpen', params) enddef -# Send a LSP "textDocument/didClose" notification +# Send a file/document closed notification to the language server. def TextdocDidClose(lspserver: dict, bnr: number): void - var notif: dict = lspserver.createNotification('textDocument/didClose') - - # interface DidCloseTextDocumentParams - # interface TextDocumentIdentifier + # Notification: 'textDocument/didClose' + # Params: DidCloseTextDocumentParams var tdid = {} tdid.uri = util.LspBufnrToUri(bnr) - notif.params->extend({textDocument: tdid}) - - lspserver.sendMessage(notif) + var params = {textDocument: tdid} + lspserver.sendNotification('textDocument/didClose', params) enddef -# Send a LSP "textDocument/didChange" notification +# Send a file/document change notification to the language server. # Params: DidChangeTextDocumentParams def TextdocDidChange(lspserver: dict, bnr: number, start: number, end: number, added: number, changes: list>): void - var notif: dict = lspserver.createNotification('textDocument/didChange') - - # interface DidChangeTextDocumentParams - # interface VersionedTextDocumentIdentifier + # Notification: 'textDocument/didChange' + # Params: DidChangeTextDocumentParams var vtdid: dict = {} vtdid.uri = util.LspBufnrToUri(bnr) # Use Vim 'changedtick' as the LSP document version number vtdid.version = bnr->getbufvar('changedtick') - # interface TextDocumentContentChangeEvent var changeset: list> ##### FIXME: Sending specific buffer changes to the LSP server doesn't @@ -486,9 +484,8 @@ def TextdocDidChange(lspserver: dict, bnr: number, start: number, # endfor changeset->add({text: getbufline(bnr, 1, '$')->join("\n") .. "\n"}) - notif.params->extend({textDocument: vtdid, contentChanges: changeset}) - - lspserver.sendMessage(notif) + var params = {textDocument: vtdid, contentChanges: changeset} + lspserver.sendNotification('textDocument/didChange', params) enddef # Return the current cursor position as a LSP position. @@ -599,7 +596,7 @@ def CompletionReply(lspserver: dict, cItems: any) if lspserver.completionLazyDoc d.info = 'Lazy doc' else - if item->has_key('detail') + if item->has_key('detail') && item.detail != '' # Solve a issue where if a server send a detail field # with a "\n", on the menu will be everything joined with # a "^@" separating it. (example: clangd) @@ -932,6 +929,7 @@ def ShowSignature(lspserver: dict): void signature.SignatureHelp) enddef +# Send a file/document saved notification to the language server def DidSaveFile(lspserver: dict, bnr: number): void # Check whether the LSP server supports the didSave notification if !lspserver.caps->has_key('textDocumentSync') @@ -942,10 +940,10 @@ def DidSaveFile(lspserver: dict, bnr: number): void return endif - var notif: dict = lspserver.createNotification('textDocument/didSave') - # interface: DidSaveTextDocumentParams - notif.params->extend({textDocument: {uri: util.LspBufnrToUri(bnr)}}) - lspserver.sendMessage(notif) + # Notification: 'textDocument/didSave' + # Params: DidSaveTextDocumentParams + var params = {textDocument: {uri: util.LspBufnrToUri(bnr)}} + lspserver.sendNotification('textDocument/didSave', params) enddef # get the hover information @@ -1280,9 +1278,7 @@ def WorkspaceQuerySymbols(lspserver: dict, query: string) symbol.WorkspaceSymbolPopup(lspserver, query, reply.result) enddef -# Add a workspace folder to the LSP server. -# Request: "workspace/didChangeWorkspaceFolders" -# Param: DidChangeWorkspaceFoldersParams +# Add a workspace folder to the language server. def AddWorkspaceFolder(lspserver: dict, dirName: string): void if !lspserver.caps->has_key('workspace') || !lspserver.caps.workspace->has_key('workspaceFolders') @@ -1297,18 +1293,15 @@ def AddWorkspaceFolder(lspserver: dict, dirName: string): void return endif - var notif: dict = - lspserver.createNotification('workspace/didChangeWorkspaceFolders') - # interface DidChangeWorkspaceFoldersParams - notif.params->extend({event: {added: [dirName], removed: []}}) - lspserver.sendMessage(notif) + # Notification: 'workspace/didChangeWorkspaceFolders' + # Params: DidChangeWorkspaceFoldersParams + var params = {event: {added: [dirName], removed: []}} + lspserver.sendNotification('workspace/didChangeWorkspaceFolders', params) lspserver.workspaceFolders->add(dirName) enddef -# Remove a workspace folder from the LSP server. -# Request: "workspace/didChangeWorkspaceFolders" -# Param: DidChangeWorkspaceFoldersParams +# Remove a workspace folder from the language server. def RemoveWorkspaceFolder(lspserver: dict, dirName: string): void if !lspserver.caps->has_key('workspace') || !lspserver.caps.workspace->has_key('workspaceFolders') @@ -1324,11 +1317,10 @@ def RemoveWorkspaceFolder(lspserver: dict, dirName: string): void return endif - var notif: dict = - lspserver.createNotification('workspace/didChangeWorkspaceFolders') - # interface DidChangeWorkspaceFoldersParams - notif.params->extend({event: {added: [], removed: [dirName]}}) - lspserver.sendMessage(notif) + # Notification: "workspace/didChangeWorkspaceFolders" + # Param: DidChangeWorkspaceFoldersParams + var params = {event: {added: [], removed: [dirName]}} + lspserver.sendNotification('workspace/didChangeWorkspaceFolders', params) lspserver.workspaceFolders->remove(idx) enddef @@ -1504,9 +1496,9 @@ export def NewLspServer(path: string, args: list, isSync: bool, initiali nextReqID: function(NextReqID, [lspserver]), createRequest: function(CreateRequest, [lspserver]), createResponse: function(CreateResponse, [lspserver]), - createNotification: function(CreateNotification, [lspserver]), sendResponse: function(SendResponse, [lspserver]), sendMessage: function(SendMessage, [lspserver]), + sendNotification: function(SendNotification, [lspserver]), rpc: function(Rpc, [lspserver]), rpc_a: function(AsyncRpc, [lspserver]), waitForResponse: function(WaitForResponse, [lspserver]),