mirror of
https://github.com/prabirshrestha/vim-lsp.git
synced 2025-12-14 20:35:59 +01:00
Workspace symbol search using quickpick (#1035)
This commit is contained in:
94
autoload/lsp/internal/workspace_symbol/search.vim
Normal file
94
autoload/lsp/internal/workspace_symbol/search.vim
Normal file
@@ -0,0 +1,94 @@
|
||||
" https://microsoft.github.io/language-server-protocol/specification#workspace_symbol
|
||||
" options - {
|
||||
" bufnr: bufnr('%') " optional
|
||||
" server - 'server_name' " optional
|
||||
" query: '' " optional
|
||||
" }
|
||||
function! lsp#internal#workspace_symbol#search#do(options) abort
|
||||
if has_key(a:options, 'server')
|
||||
let l:servers = [a:options['server']]
|
||||
else
|
||||
let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_document_symbol_provider(v:val)')
|
||||
endif
|
||||
|
||||
if len(l:servers) == 0
|
||||
echom 'textDocument/workspaceSymbol not supported'
|
||||
call lsp#utils#error('textDocument/workspaceSymbol not supported')
|
||||
return
|
||||
endif
|
||||
|
||||
redraw | echo 'Retrieving workspace symbols ...'
|
||||
|
||||
let l:TextChangeSubject = lsp#callbag#makeSubject()
|
||||
|
||||
" use callbag debounce instead of quickpick debounce
|
||||
call lsp#internal#ui#quickpick#open({
|
||||
\ 'items': [],
|
||||
\ 'input': get(a:options, 'query', ''),
|
||||
\ 'key': 'text',
|
||||
\ 'debounce': 0,
|
||||
\ 'on_change': function('s:on_change', [l:TextChangeSubject]),
|
||||
\ 'on_accept': function('s:on_accept'),
|
||||
\ 'on_close': function('s:on_close'),
|
||||
\ })
|
||||
|
||||
let s:Dispose = lsp#callbag#pipe(
|
||||
\ l:TextChangeSubject,
|
||||
\ lsp#callbag#debounceTime(250),
|
||||
\ lsp#callbag#distinctUntilChanged(),
|
||||
\ lsp#callbag#switchMap({query->
|
||||
\ lsp#callbag#pipe(
|
||||
\ lsp#callbag#fromList(l:servers),
|
||||
\ lsp#callbag#tap({_->lsp#internal#ui#quickpick#busy(1)}),
|
||||
\ lsp#callbag#flatMap({server->
|
||||
\ lsp#callbag#pipe(
|
||||
\ lsp#request(server, {
|
||||
\ 'method': 'workspace/symbol',
|
||||
\ 'params': {
|
||||
\ 'query': query
|
||||
\ }
|
||||
\ }),
|
||||
\ lsp#callbag#map({x->{'server': server, 'request': x['request'], 'response': x['response']}}),
|
||||
\ )
|
||||
\ }),
|
||||
\ lsp#callbag#scan({acc, curr->add(acc, curr)}, []),
|
||||
\ lsp#callbag#tap({x->s:update_ui_items(x)}),
|
||||
\ lsp#callbag#tap({'complete': {->lsp#internal#ui#quickpick#busy(0)}}),
|
||||
\ )
|
||||
\ }),
|
||||
\ lsp#callbag#subscribe({
|
||||
\ 'error': {e->s:on_error(e)},
|
||||
\ }),
|
||||
\ )
|
||||
" Notify empty query. Some servers may not return results when query is empty
|
||||
call l:TextChangeSubject(1, '')
|
||||
endfunction
|
||||
|
||||
function! s:on_change(TextChangeSubject, data, ...) abort
|
||||
call a:TextChangeSubject(1, a:data['input'])
|
||||
endfunction
|
||||
|
||||
function! s:update_ui_items(x) abort
|
||||
let l:items = []
|
||||
for l:i in a:x
|
||||
let l:items += lsp#ui#vim#utils#symbols_to_loc_list(l:i['server'], l:i)
|
||||
endfor
|
||||
call lsp#internal#ui#quickpick#items(l:items)
|
||||
endfunction
|
||||
|
||||
function! s:on_accept(data, name) abort
|
||||
call lsp#internal#ui#quickpick#close()
|
||||
call lsp#utils#location#_open_vim_list_item(a:data['items'][0], '')
|
||||
endfunction
|
||||
|
||||
function! s:on_close(...) abort
|
||||
if exists('s:Dispose')
|
||||
call s:Dispose()
|
||||
unlet s:Dispose
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:on_error(e) abort
|
||||
call lsp#internal#ui#quickpick#close()
|
||||
call lsp#log('LspWorkspaceSymbolSearch error', a:e)
|
||||
endfunction
|
||||
@@ -119,6 +119,7 @@ CONTENTS *vim-lsp-contents*
|
||||
LspTypeDefinition |:LspTypeDefinition|
|
||||
LspTypeHierarchy |:LspTypeHierarchy|
|
||||
LspWorkspaceSymbol |:LspWorkspaceSymbol|
|
||||
LspWorkspaceSymbolSearch |:LspWorkspaceSymbolSearch|
|
||||
LspStatus |:LspStatus|
|
||||
LspStopServer |:LspStopServer|
|
||||
Autocommands |vim-lsp-autocommands|
|
||||
@@ -1537,7 +1538,13 @@ Also see |:LspPeekTypeDefinition|.
|
||||
|
||||
LspWorkspaceSymbol *:LspWorkspaceSymbol*
|
||||
|
||||
Search and show workspace symbols.
|
||||
Search and show workspace symbols in quickfix.
|
||||
Servers may choose to return empty results if the search query is empty.
|
||||
|
||||
LspWorkspaceSymbolSearch *:LspWorkspaceSymbolSearch*
|
||||
|
||||
Search the workspace symbols for all servers and navigate using quickpick.
|
||||
Servers may choose to return empty results if the search query is empty.
|
||||
|
||||
LspStatus *:LspStatus*
|
||||
|
||||
@@ -1633,6 +1640,7 @@ Available plug mappings are following:
|
||||
nnoremap <plug>(lsp-definition)
|
||||
nnoremap <plug>(lsp-peek-definition)
|
||||
nnoremap <plug>(lsp-document-symbol)
|
||||
nnoremap <plug>(lsp-document-symbol-search)
|
||||
nnoremap <plug>(lsp-document-diagnostics)
|
||||
nnoremap <plug>(lsp-hover)
|
||||
nnoremap <plug>(lsp-next-diagnostic)
|
||||
@@ -1654,6 +1662,7 @@ Available plug mappings are following:
|
||||
nnoremap <plug>(lsp-references)
|
||||
nnoremap <plug>(lsp-rename)
|
||||
nnoremap <plug>(lsp-workspace-symbol)
|
||||
nnoremap <plug>(lsp-workspace-symbol-search)
|
||||
nnoremap <plug>(lsp-document-format)
|
||||
vnoremap <plug>(lsp-document-format)
|
||||
nnoremap <plug>(lsp-document-range-format)
|
||||
|
||||
@@ -106,6 +106,7 @@ command! LspTypeDefinition call lsp#ui#vim#type_definition(0, <q-mods>)
|
||||
command! LspTypeHierarchy call lsp#internal#type_hierarchy#show()
|
||||
command! LspPeekTypeDefinition call lsp#ui#vim#type_definition(1)
|
||||
command! -nargs=? LspWorkspaceSymbol call lsp#ui#vim#workspace_symbol(<q-args>)
|
||||
command! -nargs=? LspWorkspaceSymbolSearch call lsp#internal#workspace_symbol#search#do({'query': <q-args>})
|
||||
command! -range LspDocumentFormat call lsp#internal#document_formatting#format({ 'bufnr': bufnr('%') })
|
||||
command! -range -nargs=? LspDocumentFormatSync call lsp#internal#document_formatting#format(
|
||||
\ extend({'bufnr': bufnr('%'), 'sync': 1 }, lsp#utils#args#_parse(<q-args>, {
|
||||
@@ -159,6 +160,7 @@ nnoremap <plug>(lsp-type-definition) :<c-u>call lsp#ui#vim#type_definition(0)<cr
|
||||
nnoremap <plug>(lsp-type-hierarchy) :<c-u>call lsp#internal#type_hierarchy#show()<cr>
|
||||
nnoremap <plug>(lsp-peek-type-definition) :<c-u>call lsp#ui#vim#type_definition(1)<cr>
|
||||
nnoremap <plug>(lsp-workspace-symbol) :<c-u>call lsp#ui#vim#workspace_symbol('')<cr>
|
||||
nnoremap <plug>(lsp-workspace-symbol-search) :<c-u>call lsp#internal#workspace_symbol#search#do({})<cr>
|
||||
nnoremap <plug>(lsp-document-format) :<c-u>call lsp#internal#document_formatting#format({ 'bufnr': bufnr('%') })<cr>
|
||||
vnoremap <plug>(lsp-document-format) :<Home>silent <End>call lsp#internal#document_range_formatting#format({ 'bufnr': bufnr('%') })<cr>
|
||||
nnoremap <plug>(lsp-document-range-format) :<c-u>set opfunc=lsp#internal#document_range_formatting#opfunc<cr>g@
|
||||
|
||||
Reference in New Issue
Block a user