Skip to content

cscope_maps: add gtags support for cscope_maps#360

Open
epheien wants to merge 2 commits intoludovicchabant:masterfrom
epheien:master
Open

cscope_maps: add gtags support for cscope_maps#360
epheien wants to merge 2 commits intoludovicchabant:masterfrom
epheien:master

Conversation

@epheien
Copy link
Copy Markdown

@epheien epheien commented May 20, 2024

cscope has not been updated for a long time, so supporting gtags is a must-do

@bqv
Copy link
Copy Markdown

bqv commented Nov 25, 2024

@ludovicchabant merge?

@smemsh
Copy link
Copy Markdown

smemsh commented Nov 25, 2024

Isn't there already g:gtags_cscope? With gutentags_plus it seems to work ok, you do need to make your own maps though. I'm using this:

" we want gscope, ie gutentags_plus, to manage cscope for us
let g:gutentags_auto_add_gtags_cscope = 0
let g:gutentags_auto_add_cscope = 0
let g:gutentags_auto_add_pycscope = 0
" ...
let g:gutentags_modules = []
if executable('ctags') | let g:gutentags_modules += ['ctags'] | endif
if executable('gtags') | let g:gutentags_modules += ['gtags_cscope']
elseif executable('cscope') | let g:gutentags_modules += ['cscope'] | endif
if empty(g:gutentags_modules) | let g:gutentags_dont_load = 1 | endif
" ...
" manage our own maps
let g:gutentags_plus_nomap = 1

and then for my maps:

"
" like the cscope interface, but uses gutentags-generated db
"

if !exists("g:gscope_done") | let g:gscope_done = 1 | else | finish | endif
if !has("cscope") || !has("channel") | finish | endif

nnoremap csa <plug>GscopeFindAssign
nnoremap csc <plug>GscopeFindCallingFunc
nnoremap csd <plug>GscopeFindCalledFunc
nnoremap cse <plug>GscopeFindEgrep
nnoremap csf <plug>GscopeFindFile
nnoremap csg <plug>GscopeFindDefinition
nnoremap csi <plug>GscopeFindInclude
nnoremap css <plug>GscopeFindSymbol
nnoremap cst <plug>GscopeFindText
nnoremap csz <plug>GscopeFindCtag

" cs[acdefgistz]/:
"
function! s:GsgAbbrev(cschar) abort
	return (((getcmdtype() == ":" && (getcmdline() =~ ('^cs' . a:cschar)))
		\ ?  'GscopeFind ' : 'cs') . a:cschar)
endfunction
let s:chars = "acdefgistz"
"if !has('patch-8.2.2658') | finish | endif " :for x in String
"for s:abb in s:chars
for s:abb in split(s:chars, '\zs')
exec 'cnoreabbrev <expr> cs' . s:abb . ' <SID>GsgAbbrev("' . s:abb . '")'
endfor

" cs[rhuk]/:
"
function! s:GsAbbrev(cschar, cmd) abort
	return ((getcmdtype() == ":" && (getcmdline() =~ ('^cs' . a:cschar)))
		\ ? a:cmd : ('cs' . a:cschar))
endfunction
for [cschar, cmd] in [
	\ ['r', 'call gutentags#rescan()'],
	\ ['h', 'call Gscope_menu()'],
	\ ['u', 'GutentagsUpdate'],
	\ ['k', 'GscopeKill'],
\ ]
	let s:abb = 'cs' . cschar
	let s:abbfunc = 'GsAbbrev("' . cschar . '", "' . cmd . '")'
	let s:gsfunc = expand('<SID>') . s:abbfunc
	exec 'nnoremap ' . s:abb . ' :' . cmd . '<return>'
	exec 'cnoreabbrev <expr> ' . s:abb . ' ' . s:gsfunc
endfor

" menu for csh
"
function! Gscope_menu ()
	echo
	\"gscope find:\n"
	\"  (a)ssigns   find places this is assigned to\n"
	\"  (c)allers   find functions calling this function\n"
	\"  (d)epends   find functions this one depends on\n"
	\"  (e)grep     find this egrep pattern\n"
	\"  (f)ile      find this file\n"
	\"  (g)lobal    find this definition\n"
	\"  (i)nclude   find files #including this file\n"
	\"  (s)ymbol    find this C symbol\n"
	\"  (t)o        find assignments to\n"
	\"  (z)tags     find matching ctags\n"
	\"? "
	let gotchar = getchar()
	execute "normal cs" . nr2char(gotchar)
endf

" gutentags_plus takes care of 'cscope add' and db management.  restart every
" time we change working directory.  this lets us start with an empty buffer
"
if !has('patch-8.0.1459') | finish | endif
augroup DirChangedGroup
	au!
	autocmd DirChanged global
	\ if exists('*gutentags#rescan') | call gutentags#rescan() | endif
augroup END

works for me without any patch. I am not sure if I'd need the maps if I didn't set g:gutentags_plus_nomap. Also I'm not certain if this is solving the same problem as this PR? but I'm using gtags-cscope and it seems to work ok.

@bqv
Copy link
Copy Markdown

bqv commented Nov 25, 2024

cscope_maps is for the purpose of neovim, where things don't quite work well

@epheien
Copy link
Copy Markdown
Author

epheien commented Nov 27, 2024

The cscope integration has been removed from nvim.
And cscope_map.nvim is an implementation of a relatively mature solution in plug-in form.

I see that the patch related to cscope_map has been merged into the main branch, but it only supports cscope and not gtags, which doesn't seem very reasonable, so I made this PR to support gtags.

@bqv
Copy link
Copy Markdown

bqv commented Nov 27, 2024

Can gtags be made to place its files in gutentags_cache_dir, not the project root?

@smemsh
Copy link
Copy Markdown

smemsh commented Dec 2, 2024

Can gtags be made to place its files in gutentags_cache_dir, not the project root?

it looks like the patch uses gutentags#get_cachefile() which does seem to use the cache dir if defined

@bqv
Copy link
Copy Markdown

bqv commented Dec 2, 2024

My bad, dodgy config. I had

g:gutentags_cscope_executable_maps = 'gtags-cscope'

rather than

'gtags'

Copy link
Copy Markdown

@radiantshaw radiantshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this. Just added one comment for my understanding of how this works.

endif
endfunction

function! gutentags#cscope_maps#generate_gtags(proj_dir, tags_file, gen_opts) abort
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also build the inverted index? Apologies if it already does. I'm new to writing/reading Vim plugins.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After adding this comment, I realised that I can just check this behaviour by running it locally. I'll do that and then resolve this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants