lsp: add workspace/symbol (#12224)

* lsp: add workspace/symbol
* refactor symbol callback
* set hierarchical symbol support to true
* add documentation and default mapping

Co-authored-by: Hirokazu Hata <h.hata.ai.t@gmail.com>
This commit is contained in:
Christian Clason 2020-05-02 17:56:05 +02:00 committed by GitHub
parent 2f42e4d0c8
commit ea347b18d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 1 deletions

View File

@ -49,6 +49,7 @@ go-to-definition, hover, etc. Example config: >
nnoremap <silent> 1gD <cmd>lua vim.lsp.buf.type_definition()<CR> nnoremap <silent> 1gD <cmd>lua vim.lsp.buf.type_definition()<CR>
nnoremap <silent> gr <cmd>lua vim.lsp.buf.references()<CR> nnoremap <silent> gr <cmd>lua vim.lsp.buf.references()<CR>
nnoremap <silent> g0 <cmd>lua vim.lsp.buf.document_symbol()<CR> nnoremap <silent> g0 <cmd>lua vim.lsp.buf.document_symbol()<CR>
nnoremap <silent> gW <cmd>lua vim.lsp.buf.workspace_symbol()<CR>
Nvim provides the |vim.lsp.omnifunc| 'omnifunc' handler which allows Nvim provides the |vim.lsp.omnifunc| 'omnifunc' handler which allows
|i_CTRL-X_CTRL-O| to consume LSP completion. Example config (note the use of |i_CTRL-X_CTRL-O| to consume LSP completion. Example config (note the use of
@ -783,6 +784,13 @@ signature_help() *vim.lsp.buf.signature_help()*
type_definition() *vim.lsp.buf.type_definition()* type_definition() *vim.lsp.buf.type_definition()*
TODO: Documentation TODO: Documentation
workspace_symbol({query}) *vim.lsp.buf.workspace_symbol()*
Lists all symbols in the current workspace in the quickfix
window. The list is filtered against the optional argument
{query}; if the argument is omitted from the call, the user
is prompted to enter a string on the command line. An empty
string means no filtering is done.
============================================================================== ==============================================================================
Lua module: vim.lsp.callbacks *lsp-callbacks* Lua module: vim.lsp.callbacks *lsp-callbacks*

View File

@ -533,6 +533,7 @@ function lsp.start_client(config)
or (not client.resolved_capabilities.goto_definition and method == 'textDocument/definition') or (not client.resolved_capabilities.goto_definition and method == 'textDocument/definition')
or (not client.resolved_capabilities.implementation and method == 'textDocument/implementation') or (not client.resolved_capabilities.implementation and method == 'textDocument/implementation')
or (not client.resolved_capabilities.document_symbol and method == 'textDocument/documentSymbol') or (not client.resolved_capabilities.document_symbol and method == 'textDocument/documentSymbol')
or (not client.resolved_capabilities.workspace_symbol and method == 'textDocument/workspaceSymbol')
then then
callback(unsupported_method(method), method, nil, client_id, bufnr) callback(unsupported_method(method), method, nil, client_id, bufnr)
return return

View File

@ -137,6 +137,12 @@ function M.document_symbol()
request('textDocument/documentSymbol', params) request('textDocument/documentSymbol', params)
end end
function M.workspace_symbol(query)
query = query or npcall(vfn.input, "Query: ")
local params = {query = query}
request('workspace/symbol', params)
end
--- Send request to server to resolve document highlights for the --- Send request to server to resolve document highlights for the
--- current text document position. This request can be associated --- current text document position. This request can be associated
--- to key mapping or to events such as `CursorHold`, eg: --- to key mapping or to events such as `CursorHold`, eg:

View File

@ -54,13 +54,15 @@ M['textDocument/references'] = function(_, _, result)
api.nvim_command("wincmd p") api.nvim_command("wincmd p")
end end
M['textDocument/documentSymbol'] = function(_, _, result, _, bufnr) local symbol_callback = function(_, _, result, _, bufnr)
if not result or vim.tbl_isempty(result) then return end if not result or vim.tbl_isempty(result) then return end
util.set_qflist(util.symbols_to_items(result, bufnr)) util.set_qflist(util.symbols_to_items(result, bufnr))
api.nvim_command("copen") api.nvim_command("copen")
api.nvim_command("wincmd p") api.nvim_command("wincmd p")
end end
M['textDocument/documentSymbol'] = symbol_callback
M['workspace/symbol'] = symbol_callback
M['textDocument/rename'] = function(_, _, result) M['textDocument/rename'] = function(_, _, result)
if not result then return end if not result then return end

View File

@ -688,6 +688,19 @@ function protocol.make_client_capabilities()
}; };
hierarchicalDocumentSymbolSupport = true; hierarchicalDocumentSymbolSupport = true;
}; };
workspaceSymbol = {
dynamicRegistration = false;
symbolKind = {
valueSet = (function()
local res = {}
for k in pairs(protocol.SymbolKind) do
if type(k) == 'number' then table.insert(res, k) end
end
return res
end)();
};
hierarchicalWorkspaceSymbolSupport = true;
};
}; };
workspace = nil; workspace = nil;
experimental = nil; experimental = nil;