From 40e266a131d8bfc9f2f48c5e40846436a9c9e64f Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Fri, 8 Apr 2022 00:42:37 +0800 Subject: Use tagstack for ycm (vim) --- .vim/after/plugin/youcompleteme.vim | 8 ++++---- .vim/plugin/util.vim | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 .vim/plugin/util.vim diff --git a/.vim/after/plugin/youcompleteme.vim b/.vim/after/plugin/youcompleteme.vim index b4e30a8..1c30617 100644 --- a/.vim/after/plugin/youcompleteme.vim +++ b/.vim/after/plugin/youcompleteme.vim @@ -33,10 +33,10 @@ let g:ycm_filetype_blacklist = { \ 'vimwiki': 1, \ } -nnoremap gd :YcmCompleter GoTo -nnoremap gy :YcmCompleter GoToType -nnoremap gr :YcmCompleter GoToReferences -nnoremap gi :YcmCompleter GoToImplementation +nnoremap gd :call PushTagStackOnCommand('YcmCompleter GoTo') +nnoremap gy :call PushTagStackOnCommand('YcmCompleter GoToType') +nnoremap gr :call PushTagStackOnCommand('YcmCompleter GoToReferences') +nnoremap gi :call PushTagStackOnCommand('YcmCompleter GoToImplementation') nnoremap rn :YcmCompleter RefactorRename nmap fs (YCMFindSymbolInDocument) nmap fS (YCMFindSymbolInWorkspace) diff --git a/.vim/plugin/util.vim b/.vim/plugin/util.vim new file mode 100644 index 0000000..de2dadd --- /dev/null +++ b/.vim/plugin/util.vim @@ -0,0 +1,36 @@ +function! PushTagStackOnCommand(command_name) abort + call s:PushTagStackBefore() + exec a:command_name + call s:PushTagStackAfter() +endfunction + +function! PushTagStackOnFunction(function_name) abort + call s:PushTagStackBefore() + call a:function_name() + call s:PushTagStackAfter() +endfunction + +function! s:PushTagStackBefore() abort + let dotag = &tagstack && exists('*gettagstack') && exists('*settagstack') + if dotag + let from = [bufnr('%'), line('.'), col('.'), 0] + let tagname = expand('') + let stack = gettagstack() + if stack.curidx > 1 + let stack.items = stack.items[0:stack.curidx-2] + else + let stack.items = [] + endif + let stack.items += [{'from': from, 'tagname': tagname}] + let stack.curidx = len(stack.items) + call settagstack(win_getid(), stack) + endif +endfunction + +function! s:PushTagStackAfter() abort + let dotag = &tagstack && exists('*gettagstack') && exists('*settagstack') + if dotag + let curidx = gettagstack().curidx + 1 + call settagstack(win_getid(), {'curidx': curidx}) + endif +endfunction -- cgit v1.2.3