kotp
1/2/2017 - 2:39 PM

Vim configuration for interacting with cscope databases (focus on Ruby).

Vim configuration for interacting with cscope databases (focus on Ruby).

#!/bin/sh

# NOTE: this is really .git/hooks/cscope but Gist doesn't like paths.

set -e
trap "rm -f .git/cscope.out.$$" EXIT
find . \( -name "*.rb" -o -name "*.erb" -o -name "*.haml" \) -print \
  -o -path "./.git" -prune \
  -o -path "./tmp" -prune \
  -o -path "./coverage" -prune \
  -o -path "./vendor" -prune | cscope -R -b -i - -f .git/cscope.out.$$
mv .git/cscope.out.$$ .git/cscope.out
if has("cscope")
  "TODO: turn this all into a plugin.

  set nocscopetag
  set cscopequickfix=s-,c-,d-,i-,t-,e-
  set nocscopeverbose
  if filereadable(".git/cscope.out")
    cscope add .git/cscope.out
  endif
  set cscopeverbose

  "TODO: figure out which of these are useless in Ruby and disable them.
  nnoremap <Leader>fs :cscope find s <C-R>=expand("<cword>")<CR><CR>:botright cwindow<CR>
  nnoremap <Leader>fg :cscope find g <C-R>=expand("<cword>")<CR><CR>:botright cwindow<CR>
  nnoremap <Leader>fc :cscope find c <C-R>=expand("<cword>")<CR><CR>:botright cwindow<CR>
  nnoremap <Leader>ft :cscope find t <C-R>=expand("<cword>")<CR><CR>:botright cwindow<CR>
  nnoremap <Leader>fe :cscope find e <C-R>=expand("<cword>")<CR><CR>:botright cwindow<CR>
  nnoremap <Leader>ff :cscope find f <C-R>=expand("<cfile>")<CR><CR>:botright cwindow<CR>
  nnoremap <Leader>fd :cscope find d <C-R>=expand("<cword>")<CR><CR>:botright cwindow<CR>
  nnoremap <Leader>fi :cscope find i ^<C-R>=expand("<cfile>")<CR>$<CR>:botright cwindow<CR>
  "TODO: figure out how to get cstag output in quickfix or a popup menu.
  map <C-_> :cstag <C-R>=expand("<cword>")<CR><CR>

  function! CscopeRebuild()
    cscope kill .git/cscope.out
    silent execute "!./.git/hooks/cscope"
    if v:shell_error
      redraw!
      echohl ErrorMsg | echo "Unable to run cscope command." | echohl None
    else
      if filereadable(".git/cscope.out")
        redraw!
        cscope add .git/cscope.out
      else
        redraw!
        echohl ErrorMsg | echo "Unable to read cscope database." | echohl None
      endif
    endif
  endfunction

  command! Cscope call CscopeRebuild()
endif