All pastes #987192 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-04-16 09:25 UTC
rendered paste body
" sweettags.vim
" @Author:      Alessandro Decina <alessandro@nnva.org>
" @License:     GPL
" @Created:     12-Apr-2008.
" @Last Change: 13-Apr-2008.
" @Revision:    0.1.0

if exists('s:sweettags') &&
    \ s:config == s:parseConfig() &&
    \ (!exists('g:sweettags_reload') || g:sweettags_reload == 0)
    finish
endif

let s:config_defaults = {
  \ 'projects': [],
  \ }

"load global configuration variables
function! s:ParseConfig()
  let retval = copy(s:config_defaults)

  for key in keys(s:config_defaults)
    let global_option = 'g:sweettags_' . key
    if exists(global_option)
      let retval[key] = {global_option}
    endif
  endfor

  return retval
endfunction

function! s:GetDependencies(project_name)
  let dependencies = {}
  let dep_list = [a:project_name]

  let projects = s:config['projects']

  while 1
    try
      let dep = remove(dep_list, 0)
      if dep != a:project_name
        let dependencies[dep] = 1
      endif

      let deps = []
      try
        call extend(deps, projects[dep]['dependencies'])
      catch /E716/
        " key not found
      endtry

      for item in deps
        if !has_key(dependencies, item)
          call add(dep_list, item)
        endif
      endfor
    catch /E684/
      " dep_list empty
      break
    
    endtry
  endwhile
  
  return keys(dependencies)
endfunction

function! s:TagsBuild(project_name)
  let project = s:config['projects'][a:project_name]

  try
    let source_dir = project['source_dir']
  catch /E716/
    echo 'project ' . a:project_name . ' has no source_dir'
    
    return ''
  endtry

  let output_file = source_dir . '/tags'

  let excludes = []
  if has_key(project, 'ctags_exclude')
    call extend(excludes, project['ctags_exclude'])
  endif

  let excludes = map(excludes, '"--exclude=" . source_dir . "/" . v:val')
  let excludes_str = join(excludes, ' ')

  let ctags = 'ctags -f ' . output_file . ' ' . excludes_str . ' --tag-relative=no -R ' . source_dir

  return ctags
endfunction

function! s:TagsBuildList(lst)
  let commands = map(a:lst, 's:TagsBuild(v:val)')

  let command = '!' . join(commands, '; ')
  echo command
  exe command
endfunction

function! s:TagLoad(project_name)
  let project = s:config['projects'][a:project_name]
 
  try
    let source_dir = project['source_dir']
  catch /E716/
    return
  endtry

  let tags_file = source_dir . '/tags'

  return tags_file
endfunction

function! s:TagsLoadList(lst)
  let tags = map(a:lst, 's:TagLoad(v:val)')

  let &tags .= join(tags, ',')
endfunction

" public API
function! g:SweetTagsBuild(project_name)
  let deps = s:GetDependencies(a:project_name) + [a:project_name]
  
  call s:TagsBuildList(deps)
endfunction

function! g:SweetTagsLoad(project_name, ...)
  let rebuild = 0
  if a:0
    let rebuild = a:1
  endif
  
  let deps = s:GetDependencies(a:project_name) + [a:project_name]

  if rebuild
    call s:TagsBuildList(deps)
  endif

  call s:TagsLoadList(deps)
endfunction

" main
function! s:Main()
  let s:config = s:ParseConfig()
  let g:tags_backup = &tags
endfunction

call s:Main()

" vim: ts=2 sw=2 et