diff options
author | Raphael <glephunter@gmail.com> | 2023-08-03 19:03:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-03 04:03:48 -0700 |
commit | f1772272b4fda43c093fc495f54b5e7c11968d62 (patch) | |
tree | aca0ec70aec49ea58b651759c5821c5a0aacd07a | |
parent | 214b125132778c5d51d4d7e673d31a9be835e150 (diff) | |
download | rneovim-f1772272b4fda43c093fc495f54b5e7c11968d62.tar.gz rneovim-f1772272b4fda43c093fc495f54b5e7c11968d62.tar.bz2 rneovim-f1772272b4fda43c093fc495f54b5e7c11968d62.zip |
refactor(lsp): use protocol.Methods instead of strings #24537
-rw-r--r-- | runtime/lua/vim/lsp.lua | 101 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/_watchfiles.lua | 3 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/buf.lua | 54 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/codelens.lua | 5 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/diagnostic.lua | 9 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/handlers.lua | 71 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/inlay_hint.lua | 15 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/semantic_tokens.lua | 5 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/tagfunc.lua | 5 |
9 files changed, 138 insertions, 130 deletions
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index edf69af718..b4c853d3d4 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -3,6 +3,7 @@ local default_handlers = require('vim.lsp.handlers') local log = require('vim.lsp.log') local lsp_rpc = require('vim.lsp.rpc') local protocol = require('vim.lsp.protocol') +local ms = protocol.Methods local util = require('vim.lsp.util') local sync = require('vim.lsp.sync') local semantic_tokens = require('vim.lsp.semantic_tokens') @@ -35,34 +36,34 @@ local lsp = { -- maps request name to the required server_capability in the client. lsp._request_name_to_capability = { - ['textDocument/hover'] = { 'hoverProvider' }, - ['textDocument/signatureHelp'] = { 'signatureHelpProvider' }, - ['textDocument/definition'] = { 'definitionProvider' }, - ['textDocument/implementation'] = { 'implementationProvider' }, - ['textDocument/declaration'] = { 'declarationProvider' }, - ['textDocument/typeDefinition'] = { 'typeDefinitionProvider' }, - ['textDocument/documentSymbol'] = { 'documentSymbolProvider' }, - ['textDocument/prepareCallHierarchy'] = { 'callHierarchyProvider' }, - ['callHierarchy/incomingCalls'] = { 'callHierarchyProvider' }, - ['callHierarchy/outgoingCalls'] = { 'callHierarchyProvider' }, - ['textDocument/rename'] = { 'renameProvider' }, - ['textDocument/prepareRename'] = { 'renameProvider', 'prepareProvider' }, - ['textDocument/codeAction'] = { 'codeActionProvider' }, - ['textDocument/codeLens'] = { 'codeLensProvider' }, - ['codeLens/resolve'] = { 'codeLensProvider', 'resolveProvider' }, - ['codeAction/resolve'] = { 'codeActionProvider', 'resolveProvider' }, - ['workspace/executeCommand'] = { 'executeCommandProvider' }, - ['workspace/symbol'] = { 'workspaceSymbolProvider' }, - ['textDocument/references'] = { 'referencesProvider' }, - ['textDocument/rangeFormatting'] = { 'documentRangeFormattingProvider' }, - ['textDocument/formatting'] = { 'documentFormattingProvider' }, - ['textDocument/completion'] = { 'completionProvider' }, - ['textDocument/documentHighlight'] = { 'documentHighlightProvider' }, - ['textDocument/semanticTokens/full'] = { 'semanticTokensProvider' }, - ['textDocument/semanticTokens/full/delta'] = { 'semanticTokensProvider' }, - ['textDocument/inlayHint'] = { 'inlayHintProvider' }, - ['textDocument/diagnostic'] = { 'diagnosticProvider' }, - ['inlayHint/resolve'] = { 'inlayHintProvider', 'resolveProvider' }, + [ms.textDocument_hover] = { 'hoverProvider' }, + [ms.textDocument_signatureHelp] = { 'signatureHelpProvider' }, + [ms.textDocument_definition] = { 'definitionProvider' }, + [ms.textDocument_implementation] = { 'implementationProvider' }, + [ms.textDocument_declaration] = { 'declarationProvider' }, + [ms.textDocument_typeDefinition] = { 'typeDefinitionProvider' }, + [ms.textDocument_documentSymbol] = { 'documentSymbolProvider' }, + [ms.textDocument_prepareCallHierarchy] = { 'callHierarchyProvider' }, + [ms.callHierarchy_incomingCalls] = { 'callHierarchyProvider' }, + [ms.callHierarchy_outgoingCalls] = { 'callHierarchyProvider' }, + [ms.textDocument_rename] = { 'renameProvider' }, + [ms.textDocument_prepareRename] = { 'renameProvider', 'prepareProvider' }, + [ms.textDocument_codeAction] = { 'codeActionProvider' }, + [ms.textDocument_codeLens] = { 'codeLensProvider' }, + [ms.codeLens_resolve] = { 'codeLensProvider', 'resolveProvider' }, + [ms.codeAction_resolve] = { 'codeActionProvider', 'resolveProvider' }, + [ms.workspace_executeCommand] = { 'executeCommandProvider' }, + [ms.workspace_symbol] = { 'workspaceSymbolProvider' }, + [ms.textDocument_references] = { 'referencesProvider' }, + [ms.textDocument_rangeFormatting] = { 'documentRangeFormattingProvider' }, + [ms.textDocument_formatting] = { 'documentFormattingProvider' }, + [ms.textDocument_completion] = { 'completionProvider' }, + [ms.textDocument_documentHighlight] = { 'documentHighlightProvider' }, + [ms.textDocument_semanticTokens_full] = { 'semanticTokensProvider' }, + [ms.textDocument_semanticTokens_full_delta] = { 'semanticTokensProvider' }, + [ms.textDocument_inlayHint] = { 'inlayHintProvider' }, + [ms.textDocument_diagnostic] = { 'diagnosticProvider' }, + [ms.inlayHint_resolve] = { 'inlayHintProvider', 'resolveProvider' }, } -- TODO improve handling of scratch buffers with LSP attached. @@ -583,7 +584,7 @@ do local uri = vim.uri_from_bufnr(bufnr) for _, client in pairs(state.clients) do if not client.is_stopped() and lsp.buf_is_attached(bufnr, client.id) then - client.notify('textDocument/didChange', { + client.notify(ms.textDocument_didChange, { textDocument = { uri = uri, version = util.buf_versions[bufnr], @@ -701,7 +702,7 @@ local function text_document_did_open_handler(bufnr, client) text = buf_get_full_text(bufnr), }, } - client.notify('textDocument/didOpen', params) + client.notify(ms.textDocument_didOpen, params) util.buf_versions[bufnr] = params.textDocument.version -- Next chance we get, we should re-do the diagnostics @@ -930,17 +931,17 @@ end ---@param client lsp.Client function lsp._set_defaults(client, bufnr) if - client.supports_method('textDocument/definition') and is_empty_or_default(bufnr, 'tagfunc') + client.supports_method(ms.textDocument_definition) and is_empty_or_default(bufnr, 'tagfunc') then vim.bo[bufnr].tagfunc = 'v:lua.vim.lsp.tagfunc' end if - client.supports_method('textDocument/completion') and is_empty_or_default(bufnr, 'omnifunc') + client.supports_method(ms.textDocument_completion) and is_empty_or_default(bufnr, 'omnifunc') then vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc' end if - client.supports_method('textDocument/rangeFormatting') + client.supports_method(ms.textDocument_rangeFormatting) and is_empty_or_default(bufnr, 'formatprg') and is_empty_or_default(bufnr, 'formatexpr') then @@ -948,14 +949,14 @@ function lsp._set_defaults(client, bufnr) end api.nvim_buf_call(bufnr, function() if - client.supports_method('textDocument/hover') + client.supports_method(ms.textDocument_hover) and is_empty_or_default(bufnr, 'keywordprg') and vim.fn.maparg('K', 'n', false, false) == '' then vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr }) end end) - if client.supports_method('textDocument/diagnostic') then + if client.supports_method(ms.textDocument_diagnostic) then lsp.diagnostic._enable(bufnr) end end @@ -1431,7 +1432,7 @@ function lsp.start_client(config) end if next(config.settings) then - client.notify('workspace/didChangeConfiguration', { settings = config.settings }) + client.notify(ms.workspace_didChangeConfiguration, { settings = config.settings }) end if config.on_init then @@ -1573,7 +1574,7 @@ function lsp.start_client(config) ---If it is false, then it will always be false ---(the client has shutdown). function client.notify(method, params) - if method ~= 'textDocument/didChange' then + if method ~= ms.textDocument_didChange then changetracking.flush(client) end @@ -1693,7 +1694,7 @@ function lsp.start_client(config) command = command.command, arguments = command.arguments, } - client.request('workspace/executeCommand', params, handler, context.bufnr) + client.request(ms.workspace_executeCommand, params, handler, context.bufnr) end ---@private @@ -1763,12 +1764,12 @@ local function text_document_did_save_handler(bufnr) local name = api.nvim_buf_get_name(bufnr) local old_name = changetracking._get_and_set_name(client, bufnr, name) if old_name and name ~= old_name then - client.notify('textDocument/didClose', { + client.notify(ms.textDocument_didClose, { textDocument = { uri = vim.uri_from_fname(old_name), }, }) - client.notify('textDocument/didOpen', { + client.notify(ms.textDocument_didOpen, { textDocument = { version = 0, uri = uri, @@ -1784,7 +1785,7 @@ local function text_document_did_save_handler(bufnr) if type(save_capability) == 'table' and save_capability.includeText then included_text = text(bufnr) end - client.notify('textDocument/didSave', { + client.notify(ms.textDocument_didSave, { textDocument = { uri = uri, }, @@ -1835,11 +1836,11 @@ function lsp.buf_attach_client(bufnr, client_id) reason = protocol.TextDocumentSaveReason.Manual, } if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'willSave') then - client.notify('textDocument/willSave', params) + client.notify(ms.textDocument_willSave, params) end if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'willSaveWaitUntil') then local result, err = - client.request_sync('textDocument/willSaveWaitUntil', params, 1000, ctx.buf) + client.request_sync(ms.textDocument_willSaveWaitUntil, params, 1000, ctx.buf) if result and result.result then util.apply_text_edits(result.result, ctx.buf, client.offset_encoding) elseif err then @@ -1865,7 +1866,7 @@ function lsp.buf_attach_client(bufnr, client_id) for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do changetracking.reset_buf(client, bufnr) if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'openClose') then - client.notify('textDocument/didClose', params) + client.notify(ms.textDocument_didClose, params) end text_document_did_open_handler(bufnr, client) end @@ -1875,7 +1876,7 @@ function lsp.buf_attach_client(bufnr, client_id) for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do changetracking.reset_buf(client, bufnr) if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'openClose') then - client.notify('textDocument/didClose', params) + client.notify(ms.textDocument_didClose, params) end client.attached_buffers[bufnr] = nil end @@ -1940,7 +1941,7 @@ function lsp.buf_detach_client(bufnr, client_id) if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'openClose') then local uri = vim.uri_from_bufnr(bufnr) local params = { textDocument = { uri = uri } } - client.notify('textDocument/didClose', params) + client.notify(ms.textDocument_didClose, params) end client.attached_buffers[bufnr] = nil @@ -2294,7 +2295,7 @@ function lsp.omnifunc(findstart, base) end local bufnr = resolve_bufnr() - local clients = lsp.get_clients({ bufnr = bufnr, method = 'textDocument/completion' }) + local clients = lsp.get_clients({ bufnr = bufnr, method = ms.textDocument_completion }) local remaining = #clients if remaining == 0 then return findstart == 1 and -1 or {} @@ -2326,7 +2327,7 @@ function lsp.omnifunc(findstart, base) for _, client in ipairs(clients) do local params = util.make_position_params(win, client.offset_encoding) - client.request('textDocument/completion', params, function(err, result) + client.request(ms.textDocument_completion, params, function(err, result) if err then log.warn(err.message) end @@ -2397,7 +2398,7 @@ function lsp.formatexpr(opts) end local bufnr = api.nvim_get_current_buf() for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do - if client.supports_method('textDocument/rangeFormatting') then + if client.supports_method(ms.textDocument_rangeFormatting) then local params = util.make_formatting_params() local end_line = vim.fn.getline(end_lnum) --[[@as string]] local end_col = util._str_utfindex_enc(end_line, nil, client.offset_encoding) @@ -2412,7 +2413,7 @@ function lsp.formatexpr(opts) }, } local response = - client.request_sync('textDocument/rangeFormatting', params, timeout_ms, bufnr) + client.request_sync(ms.textDocument_rangeFormatting, params, timeout_ms, bufnr) if response and response.result then lsp.util.apply_text_edits(response.result, 0, client.offset_encoding) return 0 diff --git a/runtime/lua/vim/lsp/_watchfiles.lua b/runtime/lua/vim/lsp/_watchfiles.lua index b66f2f6f32..c271dc6e14 100644 --- a/runtime/lua/vim/lsp/_watchfiles.lua +++ b/runtime/lua/vim/lsp/_watchfiles.lua @@ -1,6 +1,7 @@ local bit = require('bit') local watch = require('vim._watch') local protocol = require('vim.lsp.protocol') +local ms = protocol.Methods local lpeg = vim.lpeg local M = {} @@ -190,7 +191,7 @@ function M.register(reg, ctx) if not queue_timers[client_id] then queue_timers[client_id] = vim.defer_fn(function() - client.notify('workspace/didChangeWatchedFiles', { + client.notify(ms.workspace_didChangeWatchedFiles, { changes = change_queues[client_id], }) queue_timers[client_id] = nil diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 5e0e429021..59afaf6fa0 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -2,6 +2,7 @@ local api = vim.api local validate = vim.validate local util = require('vim.lsp.util') local npcall = vim.F.npcall +local ms = require('vim.lsp.protocol').Methods local M = {} @@ -41,7 +42,7 @@ end --- window. Calling the function twice will jump into the floating window. function M.hover() local params = util.make_position_params() - request('textDocument/hover', params) + request(ms.textDocument_hover, params) end local function request_with_options(name, params, options) @@ -64,7 +65,7 @@ end --- - on_list: (function) handler for list results. See |lsp-on-list-handler| function M.declaration(options) local params = util.make_position_params() - request_with_options('textDocument/declaration', params, options) + request_with_options(ms.textDocument_declaration, params, options) end --- Jumps to the definition of the symbol under the cursor. @@ -74,7 +75,7 @@ end --- - on_list: (function) handler for list results. See |lsp-on-list-handler| function M.definition(options) local params = util.make_position_params() - request_with_options('textDocument/definition', params, options) + request_with_options(ms.textDocument_definition, params, options) end --- Jumps to the definition of the type of the symbol under the cursor. @@ -84,7 +85,7 @@ end --- - on_list: (function) handler for list results. See |lsp-on-list-handler| function M.type_definition(options) local params = util.make_position_params() - request_with_options('textDocument/typeDefinition', params, options) + request_with_options(ms.textDocument_typeDefinition, params, options) end --- Lists all the implementations for the symbol under the cursor in the @@ -94,14 +95,14 @@ end --- - on_list: (function) handler for list results. See |lsp-on-list-handler| function M.implementation(options) local params = util.make_position_params() - request_with_options('textDocument/implementation', params, options) + request_with_options(ms.textDocument_implementation, params, options) end --- Displays signature information about the symbol under the cursor in a --- floating window. function M.signature_help() local params = util.make_position_params() - request('textDocument/signatureHelp', params) + request(ms.textDocument_signatureHelp, params) end --- Retrieves the completion items at the current cursor position. Can only be @@ -115,7 +116,7 @@ end function M.completion(context) local params = util.make_position_params() params.context = context - return request('textDocument/completion', params) + return request(ms.textDocument_completion, params) end ---@param bufnr integer @@ -199,7 +200,7 @@ function M.format(options) if not range and mode == 'v' or mode == 'V' then range = range_from_selection(bufnr, mode) end - local method = range and 'textDocument/rangeFormatting' or 'textDocument/formatting' + local method = range and ms.textDocument_rangeFormatting or ms.textDocument_formatting local clients = vim.lsp.get_clients({ id = options.id, @@ -270,7 +271,7 @@ function M.rename(new_name, options) bufnr = bufnr, name = options.name, -- Clients must at least support rename, prepareRename is optional - method = 'textDocument/rename', + method = ms.textDocument_rename, }) if options.filter then clients = vim.tbl_filter(options.filter, clients) @@ -305,17 +306,17 @@ function M.rename(new_name, options) local function rename(name) local params = util.make_position_params(win, client.offset_encoding) params.newName = name - local handler = client.handlers['textDocument/rename'] - or vim.lsp.handlers['textDocument/rename'] - client.request('textDocument/rename', params, function(...) + local handler = client.handlers[ms.textDocument_rename] + or vim.lsp.handlers[ms.textDocument_rename] + client.request(ms.textDocument_rename, params, function(...) handler(...) try_use_client(next(clients, idx)) end, bufnr) end - if client.supports_method('textDocument/prepareRename') then + if client.supports_method(ms.textDocument_prepareRename) then local params = util.make_position_params(win, client.offset_encoding) - client.request('textDocument/prepareRename', params, function(err, result) + client.request(ms.textDocument_prepareRename, params, function(err, result) if err or result == nil then if next(clients, idx) then try_use_client(next(clients, idx)) @@ -354,7 +355,7 @@ function M.rename(new_name, options) end, bufnr) else assert( - client.supports_method('textDocument/rename'), + client.supports_method(ms.textDocument_rename), 'Client must support textDocument/rename' ) if new_name then @@ -390,7 +391,7 @@ function M.references(context, options) params.context = context or { includeDeclaration = true, } - request_with_options('textDocument/references', params, options) + request_with_options(ms.textDocument_references, params, options) end --- Lists all symbols in the current buffer in the quickfix window. @@ -399,7 +400,7 @@ end --- - on_list: (function) handler for list results. See |lsp-on-list-handler| function M.document_symbol(options) local params = { textDocument = util.make_text_document_params() } - request_with_options('textDocument/documentSymbol', params, options) + request_with_options(ms.textDocument_documentSymbol, params, options) end local function pick_call_hierarchy_item(call_hierarchy_items) @@ -423,7 +424,7 @@ end local function call_hierarchy(method) local params = util.make_position_params() - request('textDocument/prepareCallHierarchy', params, function(err, result, ctx) + request(ms.textDocument_prepareCallHierarchy, params, function(err, result, ctx) if err then vim.notify(err.message, vim.log.levels.WARN) return @@ -496,7 +497,7 @@ function M.add_workspace_folder(workspace_folder) end end if not found then - client.notify('workspace/didChangeWorkspaceFolders', params) + client.notify(ms.workspace_didChangeWorkspaceFolders, params) if not client.workspace_folders then client.workspace_folders = {} end @@ -524,7 +525,7 @@ function M.remove_workspace_folder(workspace_folder) for _, client in pairs(vim.lsp.get_clients({ bufnr = bufnr })) do for idx, folder in pairs(client.workspace_folders) do if folder.name == workspace_folder then - client.notify('workspace/didChangeWorkspaceFolders', params) + client.notify(ms.workspace_didChangeWorkspaceFolders, params) client.workspace_folders[idx] = nil return end @@ -548,7 +549,7 @@ function M.workspace_symbol(query, options) return end local params = { query = query } - request_with_options('workspace/symbol', params, options) + request_with_options(ms.workspace_symbol, params, options) end --- Send request to the server to resolve document highlights for the current @@ -567,7 +568,7 @@ end --- |hl-LspReferenceWrite| function M.document_highlight() local params = util.make_position_params() - request('textDocument/documentHighlight', params) + request(ms.textDocument_documentHighlight, params) end --- Removes document highlights from current buffer. @@ -655,7 +656,7 @@ local function on_code_action_results(results, ctx, options) local client = vim.lsp.get_client_by_id(action_tuple[1]) local action = action_tuple[2] - local reg = client.dynamic_capabilities:get('textDocument/codeAction', { bufnr = ctx.bufnr }) + local reg = client.dynamic_capabilities:get(ms.textDocument_codeAction, { bufnr = ctx.bufnr }) local supports_resolve = vim.tbl_get(reg or {}, 'registerOptions', 'resolveProvider') or client.supports_method('codeAction/resolve') @@ -694,9 +695,8 @@ end --- with all aggregated results local function code_action_request(params, options) local bufnr = api.nvim_get_current_buf() - local method = 'textDocument/codeAction' - vim.lsp.buf_request_all(bufnr, method, params, function(results) - local ctx = { bufnr = bufnr, method = method, params = params } + vim.lsp.buf_request_all(bufnr, ms.textDocument_codeAction, params, function(results) + local ctx = { bufnr = bufnr, method = ms.textDocument_codeAction, params = params } on_code_action_results(results, ctx, options) end) end @@ -776,7 +776,7 @@ function M.execute_command(command_params) arguments = command_params.arguments, workDoneToken = command_params.workDoneToken, } - request('workspace/executeCommand', command_params) + request(ms.workspace_executeCommand, command_params) end return M diff --git a/runtime/lua/vim/lsp/codelens.lua b/runtime/lua/vim/lsp/codelens.lua index a516238ae0..d581eb985f 100644 --- a/runtime/lua/vim/lsp/codelens.lua +++ b/runtime/lua/vim/lsp/codelens.lua @@ -1,5 +1,6 @@ local util = require('vim.lsp.util') local log = require('vim.lsp.log') +local ms = require('vim.lsp.protocol').Methods local api = vim.api local M = {} @@ -33,7 +34,7 @@ local function execute_lens(lens, bufnr, client_id) local client = vim.lsp.get_client_by_id(client_id) assert(client, 'Client is required to execute lens, client_id=' .. client_id) client._exec_cmd(lens.command, { bufnr = bufnr }, function(...) - vim.lsp.handlers['workspace/executeCommand'](...) + vim.lsp.handlers[ms.workspace_executeCommand](...) M.refresh() end) end @@ -267,7 +268,7 @@ function M.refresh() return end active_refreshes[bufnr] = true - vim.lsp.buf_request(0, 'textDocument/codeLens', params, M.on_codelens) + vim.lsp.buf_request(0, ms.textDocument_codeLens, params, M.on_codelens) end return M diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua index 44bb90d985..a0568bc09c 100644 --- a/runtime/lua/vim/lsp/diagnostic.lua +++ b/runtime/lua/vim/lsp/diagnostic.lua @@ -2,6 +2,7 @@ local util = require('vim.lsp.util') local protocol = require('vim.lsp.protocol') +local ms = protocol.Methods local api = vim.api @@ -422,13 +423,13 @@ function M._enable(bufnr) buffer = bufnr, callback = function(opts) if - opts.data.method ~= 'textDocument/didChange' - and opts.data.method ~= 'textDocument/didOpen' + opts.data.method ~= ms.textDocument_didChange + and opts.data.method ~= ms.textDocument_didOpen then return end if bufstates[bufnr] and bufstates[bufnr].enabled then - util._refresh('textDocument/diagnostic', { bufnr = bufnr, only_visible = true }) + util._refresh(ms.textDocument_diagnostic, { bufnr = bufnr, only_visible = true }) end end, group = augroup, @@ -437,7 +438,7 @@ function M._enable(bufnr) api.nvim_buf_attach(bufnr, false, { on_reload = function() if bufstates[bufnr] and bufstates[bufnr].enabled then - util._refresh('textDocument/diagnostic', { bufnr = bufnr }) + util._refresh(ms.textDocument_diagnostic, { bufnr = bufnr }) end end, on_detach = function() diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua index d887183972..6fe4b7f939 100644 --- a/runtime/lua/vim/lsp/handlers.lua +++ b/runtime/lua/vim/lsp/handlers.lua @@ -1,5 +1,6 @@ local log = require('vim.lsp.log') local protocol = require('vim.lsp.protocol') +local ms = protocol.Methods local util = require('vim.lsp.util') local api = vim.api @@ -15,14 +16,14 @@ local function err_message(...) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand -M['workspace/executeCommand'] = function(_, _, _, _) +M[ms.workspace_executeCommand] = function(_, _, _, _) -- Error handling is done implicitly by wrapping all handlers; see end of this file end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#progress ---@param result lsp.ProgressParams ---@param ctx lsp.HandlerContext -M['$/progress'] = function(_, result, ctx) +M[ms.dollar_progress] = function(_, result, ctx) local client = vim.lsp.get_client_by_id(ctx.client_id) if not client then err_message('LSP[id=', tostring(ctx.client_id), '] client has shut down during progress update') @@ -58,7 +59,7 @@ end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_workDoneProgress_create ---@param result lsp.WorkDoneProgressCreateParams ---@param ctx lsp.HandlerContext -M['window/workDoneProgress/create'] = function(_, result, ctx) +M[ms.window_workDoneProgress_create] = function(_, result, ctx) local client = vim.lsp.get_client_by_id(ctx.client_id) if not client then err_message('LSP[id=', tostring(ctx.client_id), '] client has shut down during progress update') @@ -70,7 +71,7 @@ end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showMessageRequest ---@param result lsp.ShowMessageRequestParams -M['window/showMessageRequest'] = function(_, result) +M[ms.window_showMessageRequest] = function(_, result) local actions = result.actions or {} local co, is_main = coroutine.running() if co and not is_main then @@ -105,7 +106,7 @@ M['window/showMessageRequest'] = function(_, result) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#client_registerCapability -M['client/registerCapability'] = function(_, result, ctx) +M[ms.client_registerCapability] = function(_, result, ctx) local client_id = ctx.client_id ---@type lsp.Client local client = vim.lsp.get_client_by_id(client_id) @@ -118,7 +119,7 @@ M['client/registerCapability'] = function(_, result, ctx) ---@type string[] local unsupported = {} for _, reg in ipairs(result.registrations) do - if reg.method == 'workspace/didChangeWatchedFiles' then + if reg.method == ms.workspace_didChangeWatchedFiles then require('vim.lsp._watchfiles').register(reg, ctx) elseif not client.dynamic_capabilities:supports_registration(reg.method) then unsupported[#unsupported + 1] = reg.method @@ -136,13 +137,13 @@ M['client/registerCapability'] = function(_, result, ctx) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#client_unregisterCapability -M['client/unregisterCapability'] = function(_, result, ctx) +M[ms.client_unregisterCapability] = function(_, result, ctx) local client_id = ctx.client_id local client = vim.lsp.get_client_by_id(client_id) client.dynamic_capabilities:unregister(result.unregisterations) for _, unreg in ipairs(result.unregisterations) do - if unreg.method == 'workspace/didChangeWatchedFiles' then + if unreg.method == ms.workspace_didChangeWatchedFiles then require('vim.lsp._watchfiles').unregister(unreg, ctx) end end @@ -150,7 +151,7 @@ M['client/unregisterCapability'] = function(_, result, ctx) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit -M['workspace/applyEdit'] = function(_, workspace_edit, ctx) +M[ms.workspace_applyEdit] = function(_, workspace_edit, ctx) assert( workspace_edit, 'workspace/applyEdit must be called with `ApplyWorkspaceEditParams`. Server is violating the specification' @@ -170,7 +171,7 @@ M['workspace/applyEdit'] = function(_, workspace_edit, ctx) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_configuration -M['workspace/configuration'] = function(_, result, ctx) +M[ms.workspace_configuration] = function(_, result, ctx) local client_id = ctx.client_id local client = vim.lsp.get_client_by_id(client_id) if not client then @@ -200,7 +201,7 @@ M['workspace/configuration'] = function(_, result, ctx) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_workspaceFolders -M['workspace/workspaceFolders'] = function(_, _, ctx) +M[ms.workspace_workspaceFolders] = function(_, _, ctx) local client_id = ctx.client_id local client = vim.lsp.get_client_by_id(client_id) if not client then @@ -210,24 +211,24 @@ M['workspace/workspaceFolders'] = function(_, _, ctx) return client.workspace_folders or vim.NIL end -M['textDocument/publishDiagnostics'] = function(...) +M[ms.textDocument_publishDiagnostics] = function(...) return require('vim.lsp.diagnostic').on_publish_diagnostics(...) end -M['textDocument/diagnostic'] = function(...) +M[ms.textDocument_diagnostic] = function(...) return require('vim.lsp.diagnostic').on_diagnostic(...) end -M['textDocument/codeLens'] = function(...) +M[ms.textDocument_codeLens] = function(...) return require('vim.lsp.codelens').on_codelens(...) end -M['textDocument/inlayHint'] = function(...) +M[ms.textDocument_inlayHint] = function(...) return require('vim.lsp.inlay_hint').on_inlayhint(...) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references -M['textDocument/references'] = function(_, result, ctx, config) +M[ms.textDocument_references] = function(_, result, ctx, config) if not result or vim.tbl_isempty(result) then vim.notify('No references found') else @@ -283,7 +284,7 @@ local function response_to_list(map_result, entity, title_fn) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol -M['textDocument/documentSymbol'] = response_to_list( +M[ms.textDocument_documentSymbol] = response_to_list( util.symbols_to_items, 'document symbols', function(ctx) @@ -293,12 +294,12 @@ M['textDocument/documentSymbol'] = response_to_list( ) --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_symbol -M['workspace/symbol'] = response_to_list(util.symbols_to_items, 'symbols', function(ctx) +M[ms.workspace_symbol] = response_to_list(util.symbols_to_items, 'symbols', function(ctx) return string.format("Symbols matching '%s'", ctx.params.query) end) --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename -M['textDocument/rename'] = function(_, result, ctx, _) +M[ms.textDocument_rename] = function(_, result, ctx, _) if not result then vim.notify("Language server couldn't provide rename result", vim.log.levels.INFO) return @@ -308,7 +309,7 @@ M['textDocument/rename'] = function(_, result, ctx, _) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rangeFormatting -M['textDocument/rangeFormatting'] = function(_, result, ctx, _) +M[ms.textDocument_rangeFormatting] = function(_, result, ctx, _) if not result then return end @@ -317,7 +318,7 @@ M['textDocument/rangeFormatting'] = function(_, result, ctx, _) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_formatting -M['textDocument/formatting'] = function(_, result, ctx, _) +M[ms.textDocument_formatting] = function(_, result, ctx, _) if not result then return end @@ -326,7 +327,7 @@ M['textDocument/formatting'] = function(_, result, ctx, _) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion -M['textDocument/completion'] = function(_, result, _, _) +M[ms.textDocument_completion] = function(_, result, _, _) if vim.tbl_isempty(result or {}) then return end @@ -380,7 +381,7 @@ function M.hover(_, result, ctx, config) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_hover -M['textDocument/hover'] = M.hover +M[ms.textDocument_hover] = M.hover --- Jumps to a location. Used as a handler for multiple LSP methods. ---@param _ nil not used @@ -420,13 +421,13 @@ local function location_handler(_, result, ctx, config) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_declaration -M['textDocument/declaration'] = location_handler +M[ms.textDocument_declaration] = location_handler --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition -M['textDocument/definition'] = location_handler +M[ms.textDocument_definition] = location_handler --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_typeDefinition -M['textDocument/typeDefinition'] = location_handler +M[ms.textDocument_typeDefinition] = location_handler --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_implementation -M['textDocument/implementation'] = location_handler +M[ms.textDocument_implementation] = location_handler --- |lsp-handler| for the method "textDocument/signatureHelp". --- The active parameter is highlighted with |hl-LspSignatureActiveParameter|. @@ -477,10 +478,10 @@ function M.signature_help(_, result, ctx, config) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_signatureHelp -M['textDocument/signatureHelp'] = M.signature_help +M[ms.textDocument_signatureHelp] = M.signature_help --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentHighlight -M['textDocument/documentHighlight'] = function(_, result, ctx, _) +M[ms.textDocument_documentHighlight] = function(_, result, ctx, _) if not result then return end @@ -523,13 +524,13 @@ local make_call_hierarchy_handler = function(direction) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#callHierarchy_incomingCalls -M['callHierarchy/incomingCalls'] = make_call_hierarchy_handler('from') +M[ms.callHierarchy_incomingCalls] = make_call_hierarchy_handler('from') --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#callHierarchy_outgoingCalls -M['callHierarchy/outgoingCalls'] = make_call_hierarchy_handler('to') +M[ms.callHierarchy_outgoingCalls] = make_call_hierarchy_handler('to') --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_logMessage -M['window/logMessage'] = function(_, result, ctx, _) +M[ms.window_logMessage] = function(_, result, ctx, _) local message_type = result.type local message = result.message local client_id = ctx.client_id @@ -551,7 +552,7 @@ M['window/logMessage'] = function(_, result, ctx, _) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showMessage -M['window/showMessage'] = function(_, result, ctx, _) +M[ms.window_showMessage] = function(_, result, ctx, _) local message_type = result.type local message = result.message local client_id = ctx.client_id @@ -570,7 +571,7 @@ M['window/showMessage'] = function(_, result, ctx, _) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showDocument -M['window/showDocument'] = function(_, result, ctx, _) +M[ms.window_showDocument] = function(_, result, ctx, _) local uri = result.uri if result.external then @@ -611,7 +612,7 @@ M['window/showDocument'] = function(_, result, ctx, _) end ---@see https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_inlayHint_refresh -M['workspace/inlayHint/refresh'] = function(err, result, ctx, config) +M[ms.workspace_inlayHint_refresh] = function(err, result, ctx, config) return require('vim.lsp.inlay_hint').on_refresh(err, result, ctx, config) end diff --git a/runtime/lua/vim/lsp/inlay_hint.lua b/runtime/lua/vim/lsp/inlay_hint.lua index 2bf0fb9cb2..eb2f59b312 100644 --- a/runtime/lua/vim/lsp/inlay_hint.lua +++ b/runtime/lua/vim/lsp/inlay_hint.lua @@ -1,5 +1,6 @@ local util = require('vim.lsp.util') local log = require('vim.lsp.log') +local ms = require('vim.lsp.protocol').Methods local api = vim.api local M = {} @@ -87,7 +88,7 @@ function M.on_refresh(err, _, ctx, _) if api.nvim_win_get_buf(winid) == bufnr then local bufstate = bufstates[bufnr] if bufstate then - util._refresh('textDocument/inlayHint', { bufnr = bufnr }) + util._refresh(ms.textDocument_inlayHint, { bufnr = bufnr }) break end end @@ -143,24 +144,24 @@ local function enable(bufnr) buffer = bufnr, callback = function(opts) if - opts.data.method ~= 'textDocument/didChange' - and opts.data.method ~= 'textDocument/didOpen' + opts.data.method ~= ms.textDocument_didChange + and opts.data.method ~= ms.textDocument_didOpen then return end if bufstates[bufnr] and bufstates[bufnr].enabled then - util._refresh('textDocument/inlayHint', { bufnr = bufnr }) + util._refresh(ms.textDocument_inlayHint, { bufnr = bufnr }) end end, group = augroup, }) - util._refresh('textDocument/inlayHint', { bufnr = bufnr }) + util._refresh(ms.textDocument_inlayHint, { bufnr = bufnr }) api.nvim_buf_attach(bufnr, false, { on_reload = function(_, cb_bufnr) clear(cb_bufnr) if bufstates[cb_bufnr] and bufstates[cb_bufnr].enabled then bufstates[cb_bufnr].applied = {} - util._refresh('textDocument/inlayHint', { bufnr = cb_bufnr }) + util._refresh(ms.textDocument_inlayHint, { bufnr = cb_bufnr }) end end, on_detach = function(_, cb_bufnr) @@ -176,7 +177,7 @@ local function enable(bufnr) }) else bufstate.enabled = true - util._refresh('textDocument/inlayHint', { bufnr = bufnr }) + util._refresh(ms.textDocument_inlayHint, { bufnr = bufnr }) end end diff --git a/runtime/lua/vim/lsp/semantic_tokens.lua b/runtime/lua/vim/lsp/semantic_tokens.lua index 84723bbc05..bab1b23ee4 100644 --- a/runtime/lua/vim/lsp/semantic_tokens.lua +++ b/runtime/lua/vim/lsp/semantic_tokens.lua @@ -1,6 +1,7 @@ local api = vim.api local bit = require('bit') local handlers = require('vim.lsp.handlers') +local ms = require('vim.lsp.protocol').Methods local util = require('vim.lsp.util') local uv = vim.uv @@ -292,7 +293,7 @@ function STHighlighter:send_request() local hasEditProvider = type(spec) == 'table' and spec.delta local params = { textDocument = util.make_text_document_params(self.bufnr) } - local method = 'textDocument/semanticTokens/full' + local method = ms.textDocument_semanticTokens_full if hasEditProvider and current_result.result_id then method = method .. '/delta' @@ -755,7 +756,7 @@ end --- the BufWinEnter event should take care of it next time it's displayed. --- ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#semanticTokens_refreshRequest -handlers['workspace/semanticTokens/refresh'] = function(err, _, ctx) +handlers[ms.workspace_semanticTokens_refresh] = function(err, _, ctx) if err then return vim.NIL end diff --git a/runtime/lua/vim/lsp/tagfunc.lua b/runtime/lua/vim/lsp/tagfunc.lua index c70eb573c2..957449743b 100644 --- a/runtime/lua/vim/lsp/tagfunc.lua +++ b/runtime/lua/vim/lsp/tagfunc.lua @@ -1,5 +1,6 @@ local lsp = vim.lsp local util = lsp.util +local ms = lsp.protocol.Methods local function mk_tag_item(name, range, uri, offset_encoding) local bufnr = vim.uri_to_bufnr(uri) @@ -14,7 +15,7 @@ end local function query_definition(pattern) local params = util.make_position_params() - local results_by_client, err = lsp.buf_request_sync(0, 'textDocument/definition', params, 1000) + local results_by_client, err = lsp.buf_request_sync(0, ms.textDocument_definition, params, 1000) if err then return {} end @@ -42,7 +43,7 @@ end local function query_workspace_symbols(pattern) local results_by_client, err = - lsp.buf_request_sync(0, 'workspace/symbol', { query = pattern }, 1000) + lsp.buf_request_sync(0, ms.workspace_symbol, { query = pattern }, 1000) if err then return {} end |