diff options
author | mattn <mattn.jp@gmail.com> | 2020-02-13 02:22:29 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-13 02:22:29 +0900 |
commit | cad34decf8caa4214b5973741cd0528458145928 (patch) | |
tree | bde91c9f6d7080b4e2f6e0d231515ced333d2504 | |
parent | 145cae42dbfcdecd9cd6b9e6de28edf6bfd6e9b8 (diff) | |
parent | bfa93528251ceb21cd18bebf0fa6cef84184e653 (diff) | |
download | vim-lsp-settings-cad34decf8caa4214b5973741cd0528458145928.tar.gz vim-lsp-settings-cad34decf8caa4214b5973741cd0528458145928.tar.bz2 vim-lsp-settings-cad34decf8caa4214b5973741cd0528458145928.zip |
Merge pull request #153 from mattn/add-LspSwitchSourceHeader
Add LspSwitchSourceHeader based on @kent-tri's patch
-rw-r--r-- | settings/clangd.vim | 79 | ||||
-rw-r--r-- | settings/metals.vim | 2 |
2 files changed, 80 insertions, 1 deletions
diff --git a/settings/clangd.vim b/settings/clangd.vim index a23bb1b..4a48686 100644 --- a/settings/clangd.vim +++ b/settings/clangd.vim @@ -11,3 +11,82 @@ augroup vimlsp_settings_clangd \ 'workspace_config': lsp_settings#get('clangd', 'workspace_config', {}), \ } augroup END + +function! s:handle_document_switch_source_header(ctx, server, type, has_extension, data) abort "ctx = {counter, list, last_command_id} + if a:ctx['last_command_id'] != lsp#_last_command() + return + endif + + if lsp#client#is_error(a:data['response']) || !has_key(a:data['response'], 'result') + call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response'])) + elseif type(a:data['response']['result']) ==# v:t_string + let a:ctx['list'] = a:ctx['list'] + [lsp#utils#uri_to_path(a:data['response']['result'])] + else + call lsp#utils#error('No switchable header file found') + return + endif + + if a:ctx['counter'] == a:has_extension + if empty(a:ctx['list']) + call lsp#utils#error('No ' . a:type .' found') + else + call lsp#utils#tagstack#_update() + + let l:loc = { + \ 'filename': a:ctx['list'][0], + \ 'lnum': 0, + \ 'col': 0, + \ } + + call lsp#utils#location#_open_vim_list_item(l:loc) + echo 'Retrieved ' . a:type + redraw + endif + endif +endfunction + +function! s:document_switch_source_header() abort + let l:servers = lsp#get_whitelisted_servers() + + let l:has_extension = 0 + for l:server in l:servers + if stridx(l:server, 'clangd') != -1 + let l:has_extension += 1 + endif + endfor + + let l:command_id = lsp#_new_command() + call setqflist([]) + + if l:has_extension == 0 + call lsp#utils#error('Switching between source/header not supported for '.&filetype) + return + endif + + let l:ctx = { 'counter': l:has_extension, 'list':[], 'last_command_id': l:command_id } + for l:server in l:servers + if stridx(l:server, 'clangd') == -1 + continue + endif + + call lsp#send_request(l:server, { + \ 'method': 'textDocument/switchSourceHeader', + \ 'params': { + \ 'uri': lsp#utils#get_buffer_uri(), + \ }, + \ 'on_notification': function('s:handle_document_switch_source_header', [l:ctx, l:server, 'header/source', l:has_extension]), + \ }) + endfor + + echo 'Switching between source/header ...' +endfunction + +function! s:on_lsp_buffer_enabled() abort + command! LspDocumentSwitchSourceHeader call <SID>document_switch_source_header() + nnoremap <plug>(lsp-switch-source-header) :<c-u>call <SID>document_switch_source_header()<cr> +endfunction + +augroup lsp_install_clangd + au! + autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled() +augroup END diff --git a/settings/metals.vim b/settings/metals.vim index d0cdd53..376d5f5 100644 --- a/settings/metals.vim +++ b/settings/metals.vim @@ -7,7 +7,7 @@ augroup vimlsp_settings_metals \ 'initialization_options': lsp_settings#get('metals', 'initialization_options', v:null), \ 'whitelist': lsp_settings#get('metals', 'whitelist', ['scala', 'sbt']), \ 'blacklist': lsp_settings#get('metals', 'blacklist', []), - \ 'config': lsp_settings#get('metals', 'config', {'typed_pattern': '\k\zs'}), + \ 'config': lsp_settings#get('metals', 'config', {}), \ 'workspace_config': lsp_settings#get('metals', 'workspace_config', {}), \ } augroup END |