add LspDocumentSymbolSearch using quickpick (#1029)

This commit is contained in:
Prabir Shrestha
2021-01-09 12:14:26 -08:00
committed by GitHub
parent c1a9a73864
commit e699b0fcb6
4 changed files with 84 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ function! s:on_lsp_buffer_enabled() abort
setlocal signcolumn=yes setlocal signcolumn=yes
if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif
nmap <buffer> gd <plug>(lsp-definition) nmap <buffer> gd <plug>(lsp-definition)
nmap <buffer> gs <plug>(lsp-document-symbol-search)
nmap <buffer> gr <plug>(lsp-references) nmap <buffer> gr <plug>(lsp-references)
nmap <buffer> gi <plug>(lsp-implementation) nmap <buffer> gi <plug>(lsp-implementation)
nmap <buffer> gt <plug>(lsp-type-definition) nmap <buffer> gt <plug>(lsp-type-definition)

View File

@@ -0,0 +1,76 @@
" https://microsoft.github.io/language-server-protocol/specification#textDocument_documentSymbol
" options - {
" bufnr: bufnr('%') " optional
" server - 'server_name' " optional
" }
function! lsp#internal#document_symbol#search#do(options) abort
let l:bufnr = get(a:options, 'bufnr', bufnr('%'))
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
let l:filetype = getbufvar(l:bufnr, '&filetype')
call lsp#utils#error('textDocument/documentSymbol not supported for ' . l:filetype)
return
endif
redraw | echo 'Retrieving document symbols ...'
call lsp#internal#ui#quickpick#open({
\ 'items': [],
\ 'busy': 1,
\ 'input': '',
\ 'key': 'text',
\ 'on_accept': function('s:on_accept'),
\ 'on_close': function('s:on_close'),
\ })
let s:Dispose = lsp#callbag#pipe(
\ lsp#callbag#fromList(l:servers),
\ lsp#callbag#flatMap({server->
\ lsp#callbag#pipe(
\ lsp#request(server, {
\ 'method': 'textDocument/documentSymbol',
\ 'params': {
\ 'textDocument': lsp#get_text_document_identifier(l:bufnr),
\ },
\ }),
\ 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#subscribe({
\ 'complete':{->lsp#internal#ui#quickpick#busy(0)},
\ 'error':{e->s:on_error(e)},
\ }),
\ )
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, ...) 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('LspDocumentSymbolSearch error', a:e)
endfunction

View File

@@ -98,6 +98,7 @@ CONTENTS *vim-lsp-contents*
LspDocumentRangeFormat |:LspDocumentRangeFormat| LspDocumentRangeFormat |:LspDocumentRangeFormat|
LspDocumentRangeFormatSync |:LspDocumentRangeFormatSync| LspDocumentRangeFormatSync |:LspDocumentRangeFormatSync|
LspDocumentSymbol |:LspDocumentSymbol| LspDocumentSymbol |:LspDocumentSymbol|
LspDocumentSymbolSearch |:LspDocumentSymbolSearch|
LspHover |:LspHover| LspHover |:LspHover|
LspNextDiagnostic |:LspNextDiagnostic| LspNextDiagnostic |:LspNextDiagnostic|
LspNextError |:LspNextError| LspNextError |:LspNextError|
@@ -1418,6 +1419,10 @@ LspDocumentSymbol *:LspDocumentSymbol*
Gets the symbols for the current document. Gets the symbols for the current document.
LspDocumentSymbolSearch *:LspDocumentSymbolSearch*
Search the symbols for the current document and navigate.
LspHover *:LspHover* LspHover *:LspHover*
Gets the hover information and displays it in the |preview-window|. Gets the hover information and displays it in the |preview-window|.

View File

@@ -88,6 +88,7 @@ command! LspPeekDeclaration call lsp#ui#vim#declaration(1)
command! LspDefinition call lsp#ui#vim#definition(0, <q-mods>) command! LspDefinition call lsp#ui#vim#definition(0, <q-mods>)
command! LspPeekDefinition call lsp#ui#vim#definition(1) command! LspPeekDefinition call lsp#ui#vim#definition(1)
command! LspDocumentSymbol call lsp#ui#vim#document_symbol() command! LspDocumentSymbol call lsp#ui#vim#document_symbol()
command! LspDocumentSymbolSearch call lsp#internal#document_symbol#search#do({})
command! -nargs=? LspDocumentDiagnostics call lsp#internal#diagnostics#document_diagnostics_command#do( command! -nargs=? LspDocumentDiagnostics call lsp#internal#diagnostics#document_diagnostics_command#do(
\ extend({}, lsp#utils#args#_parse(<q-args>, { \ extend({}, lsp#utils#args#_parse(<q-args>, {
\ 'buffers': {'type': type('')}, \ 'buffers': {'type': type('')},
@@ -135,6 +136,7 @@ nnoremap <plug>(lsp-peek-declaration) :<c-u>call lsp#ui#vim#declaration(1)<cr>
nnoremap <plug>(lsp-definition) :<c-u>call lsp#ui#vim#definition(0)<cr> nnoremap <plug>(lsp-definition) :<c-u>call lsp#ui#vim#definition(0)<cr>
nnoremap <plug>(lsp-peek-definition) :<c-u>call lsp#ui#vim#definition(1)<cr> nnoremap <plug>(lsp-peek-definition) :<c-u>call lsp#ui#vim#definition(1)<cr>
nnoremap <plug>(lsp-document-symbol) :<c-u>call lsp#ui#vim#document_symbol()<cr> nnoremap <plug>(lsp-document-symbol) :<c-u>call lsp#ui#vim#document_symbol()<cr>
nnoremap <plug>(lsp-document-symbol-search) :<c-u>call lsp#internal#document_symbol#search#do({})<cr>
nnoremap <plug>(lsp-document-diagnostics) :<c-u>call lsp#internal#diagnostics#document_diagnostics_command#do({})<cr> nnoremap <plug>(lsp-document-diagnostics) :<c-u>call lsp#internal#diagnostics#document_diagnostics_command#do({})<cr>
nnoremap <plug>(lsp-hover) :<c-u>call lsp#ui#vim#hover#get_hover_under_cursor()<cr> nnoremap <plug>(lsp-hover) :<c-u>call lsp#ui#vim#hover#get_hover_under_cursor()<cr>
nnoremap <plug>(lsp-preview-close) :<c-u>call lsp#ui#vim#output#closepreview()<cr> nnoremap <plug>(lsp-preview-close) :<c-u>call lsp#ui#vim#output#closepreview()<cr>