|
| 1 | +" https://microsoft.github.io/language-server-protocol/specification#textDocument_documentSymbol |
| 2 | +" options - { |
| 3 | +" bufnr: bufnr('%') " optional |
| 4 | +" server - 'server_name' " optional |
| 5 | +" } |
| 6 | +function! lsp#internal#document_symbol#search#do(options) abort |
| 7 | + let l:bufnr = get(a:options, 'bufnr', bufnr('%')) |
| 8 | + if has_key(a:options, 'server') |
| 9 | + let l:servers = [a:options['server']] |
| 10 | + else |
| 11 | + let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_document_symbol_provider(v:val)') |
| 12 | + endif |
| 13 | + |
| 14 | + if len(l:servers) == 0 |
| 15 | + let l:filetype = getbufvar(l:bufnr, '&filetype') |
| 16 | + call lsp#utils#error('textDocument/documentSymbol not supported for ' . l:filetype) |
| 17 | + return |
| 18 | + endif |
| 19 | + |
| 20 | + redraw | echo 'Retrieving document symbols ...' |
| 21 | + |
| 22 | + call lsp#internal#ui#quickpick#open({ |
| 23 | + \ 'items': [], |
| 24 | + \ 'busy': 1, |
| 25 | + \ 'input': '', |
| 26 | + \ 'key': 'text', |
| 27 | + \ 'on_accept': function('s:on_accept'), |
| 28 | + \ 'on_close': function('s:on_close'), |
| 29 | + \ }) |
| 30 | + |
| 31 | + let s:Dispose = lsp#callbag#pipe( |
| 32 | + \ lsp#callbag#fromList(l:servers), |
| 33 | + \ lsp#callbag#flatMap({server-> |
| 34 | + \ lsp#callbag#pipe( |
| 35 | + \ lsp#request(server, { |
| 36 | + \ 'method': 'textDocument/documentSymbol', |
| 37 | + \ 'params': { |
| 38 | + \ 'textDocument': lsp#get_text_document_identifier(l:bufnr), |
| 39 | + \ }, |
| 40 | + \ }), |
| 41 | + \ lsp#callbag#map({x->{'server': server, 'request': x['request'], 'response': x['response']}}), |
| 42 | + \ ) |
| 43 | + \ }), |
| 44 | + \ lsp#callbag#scan({acc, curr->add(acc, curr)}, []), |
| 45 | + \ lsp#callbag#tap({x->s:update_ui_items(x)}), |
| 46 | + \ lsp#callbag#subscribe({ |
| 47 | + \ 'complete':{->lsp#internal#ui#quickpick#busy(0)}, |
| 48 | + \ 'error':{e->s:on_error(e)}, |
| 49 | + \ }), |
| 50 | + \ ) |
| 51 | +endfunction |
| 52 | + |
| 53 | +function! s:update_ui_items(x) abort |
| 54 | + let l:items = [] |
| 55 | + for l:i in a:x |
| 56 | + let l:items += lsp#ui#vim#utils#symbols_to_loc_list(l:i['server'], l:i) |
| 57 | + endfor |
| 58 | + call lsp#internal#ui#quickpick#items(l:items) |
| 59 | +endfunction |
| 60 | + |
| 61 | +function! s:on_accept(data, ...) abort |
| 62 | + call lsp#internal#ui#quickpick#close() |
| 63 | + call lsp#utils#location#_open_vim_list_item(a:data['items'][0], '') |
| 64 | +endfunction |
| 65 | + |
| 66 | +function! s:on_close(...) abort |
| 67 | + if exists('s:Dispose') |
| 68 | + call s:Dispose() |
| 69 | + unlet s:Dispose |
| 70 | + endif |
| 71 | +endfunction |
| 72 | + |
| 73 | +function! s:on_error(e) abort |
| 74 | + call lsp#internal#ui#quickpick#close() |
| 75 | + call lsp#log('LspDocumentSymbolSearch error', a:e) |
| 76 | +endfunction |
0 commit comments