aboutsummaryrefslogtreecommitdiff
path: root/settings/clangd.vim
diff options
context:
space:
mode:
Diffstat (limited to 'settings/clangd.vim')
-rw-r--r--settings/clangd.vim72
1 files changed, 72 insertions, 0 deletions
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 <SID>switch_source_header()
+nnoremap <plug>(lsp-switch-source-header) :<c-u>call <SID>switch_source_header()<cr>