From 94bda9483d386c592cc2e6a9ff2a87462124bd4a Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 10 Feb 2020 10:58:01 +0900 Subject: Add LspSwitchSourceHeader based on @kent-tri's patch See https://github.com/prabirshrestha/vim-lsp/pull/703 --- settings/clangd.vim | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ settings/metals.vim | 2 +- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/settings/clangd.vim b/settings/clangd.vim index a23bb1b..3f114a7 100644 --- a/settings/clangd.vim +++ b/settings/clangd.vim @@ -11,3 +11,75 @@ augroup vimlsp_settings_clangd \ 'workspace_config': lsp_settings#get('clangd', 'workspace_config', {}), \ } augroup END + +function! s:handle_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: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_switch_source_header', [l:ctx, l:server, 'header/source', l:has_extension]), + \ }) + endfor + + echo 'Switching between source/header ...' +endfunction + +command! LspSwitchSourceHeader call switch_source_header() +nnoremap (lsp-switch-source-header) :call switch_source_header() 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 -- cgit v1.2.3-54-g00ecf From 9d8dcb25d85fa75b4944d76ea61d5fff42760cb5 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Thu, 13 Feb 2020 02:05:39 +0900 Subject: Enable when LSP is enabled --- settings/clangd.vim | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/settings/clangd.vim b/settings/clangd.vim index 3f114a7..3b8674a 100644 --- a/settings/clangd.vim +++ b/settings/clangd.vim @@ -81,5 +81,12 @@ function! s:switch_source_header() abort echo 'Switching between source/header ...' endfunction -command! LspSwitchSourceHeader call switch_source_header() -nnoremap (lsp-switch-source-header) :call switch_source_header() +function! s:on_lsp_buffer_enabled() abort + command! LspSwitchSourceHeader call switch_source_header() + nnoremap (lsp-switch-source-header) :call switch_source_header() +endfunction + +augroup lsp_install_clangd + au! + autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled() +augroup END -- cgit v1.2.3-54-g00ecf From bfa93528251ceb21cd18bebf0fa6cef84184e653 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Thu, 13 Feb 2020 02:19:41 +0900 Subject: Prefer 'Document' contains --- settings/clangd.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/settings/clangd.vim b/settings/clangd.vim index 3b8674a..4a48686 100644 --- a/settings/clangd.vim +++ b/settings/clangd.vim @@ -12,7 +12,7 @@ augroup vimlsp_settings_clangd \ } augroup END -function! s:handle_switch_source_header(ctx, server, type, has_extension, data) abort "ctx = {counter, list, last_command_id} +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 @@ -45,7 +45,7 @@ function! s:handle_switch_source_header(ctx, server, type, has_extension, data) endif endfunction -function! s:switch_source_header() abort +function! s:document_switch_source_header() abort let l:servers = lsp#get_whitelisted_servers() let l:has_extension = 0 @@ -74,7 +74,7 @@ function! s:switch_source_header() abort \ 'params': { \ 'uri': lsp#utils#get_buffer_uri(), \ }, - \ 'on_notification': function('s:handle_switch_source_header', [l:ctx, l:server, 'header/source', l:has_extension]), + \ 'on_notification': function('s:handle_document_switch_source_header', [l:ctx, l:server, 'header/source', l:has_extension]), \ }) endfor @@ -82,8 +82,8 @@ function! s:switch_source_header() abort endfunction function! s:on_lsp_buffer_enabled() abort - command! LspSwitchSourceHeader call switch_source_header() - nnoremap (lsp-switch-source-header) :call switch_source_header() + command! LspDocumentSwitchSourceHeader call document_switch_source_header() + nnoremap (lsp-switch-source-header) :call document_switch_source_header() endfunction augroup lsp_install_clangd -- cgit v1.2.3-54-g00ecf