view autoload/gutentags/cscope.vim @ 251:e61d20280c6c

Fix some more spaces-in-paths issues. When using the tags cache directory, the project root is passed to ctags. Escaping this path didn't work correctly when it has spaces: - We remove the quotes around it on *nix because `job_start()` doesn't like those, but then we need to escape the spaces with backslashes otherwise the script doesn't understand those parameters. - Once the escaping is gone in the script, we need to quote them but it looks like sh doesn't like double quotes in the middle of an env var or something, so we need to put the project root in a separate env var.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 25 Oct 2019 23:52:12 -0700
parents ac312dc3c111
children 52be4cf89810
line wrap: on
line source

" Cscope module for Gutentags

if !has('cscope')
    throw "Can't enable the cscope module for Gutentags, this Vim has ".
                \"no support for cscope files."
endif

" Global Options {{{

if !exists('g:gutentags_cscope_executable')
    let g:gutentags_cscope_executable = 'cscope'
endif

if !exists('g:gutentags_scopefile')
    let g:gutentags_scopefile = 'cscope.out'
endif

if !exists('g:gutentags_auto_add_cscope')
    let g:gutentags_auto_add_cscope = 1
endif

if !exists('g:gutentags_cscope_build_inverted_index')
    let g:gutentags_cscope_build_inverted_index = 0
endif

" }}}

" Gutentags Module Interface {{{

let s:runner_exe = gutentags#get_plat_file('update_scopedb')
let s:unix_redir = (&shellredir =~# '%s') ? &shellredir : &shellredir . ' %s'
let s:added_dbs = []

function! gutentags#cscope#init(project_root) abort
    let l:dbfile_path = gutentags#get_cachefile(
                \a:project_root, g:gutentags_scopefile)
    let b:gutentags_files['cscope'] = l:dbfile_path

    if g:gutentags_auto_add_cscope && filereadable(l:dbfile_path)
        if index(s:added_dbs, l:dbfile_path) < 0
            call add(s:added_dbs, l:dbfile_path)
            silent! execute 'cs add ' . fnameescape(l:dbfile_path)
        endif
    endif
endfunction

function! gutentags#cscope#generate(proj_dir, tags_file, gen_opts) abort
    let l:cmd = [s:runner_exe]
    let l:cmd += ['-e', g:gutentags_cscope_executable]
    let l:cmd += ['-p', a:proj_dir]
    let l:cmd += ['-f', a:tags_file]
    let l:file_list_cmd =
        \ gutentags#get_project_file_list_cmd(a:proj_dir)
    if !empty(l:file_list_cmd)
        let l:cmd += ['-L', '"' . l:file_list_cmd . '"']
    endif
    if g:gutentags_cscope_build_inverted_index
        let l:cmd += ['-I']
    endif
    let l:cmd = gutentags#make_args(l:cmd)

    call gutentags#trace("Running: " . string(l:cmd))
    call gutentags#trace("In:      " . getcwd())
    if !g:gutentags_fake
		let l:job_opts = gutentags#build_default_job_options('cscope')
        let l:job = gutentags#start_job(l:cmd, l:job_opts)
        call gutentags#add_job('cscope', a:tags_file, l:job)
    else
        call gutentags#trace("(fake... not actually running)")
    endif
endfunction

function! gutentags#cscope#on_job_exit(job, exit_val) abort
    let l:job_idx = gutentags#find_job_index_by_data('cscope', a:job)
    let l:dbfile_path = gutentags#get_job_tags_file('cscope', l:job_idx)
    call gutentags#remove_job('cscope', l:job_idx)

    if a:exit_val == 0
        if index(s:added_dbs, l:dbfile_path) < 0
            call add(s:added_dbs, l:dbfile_path)
            silent! execute 'cs add ' . fnameescape(l:dbfile_path)
        else
            silent! execute 'cs reset'
        endif
    else
        call gutentags#warning(
                    \"cscope job failed, returned: ".
                    \string(a:exit_val))
    endif
endfunction

" }}}