Skip to content

Commit

Permalink
Merge pull request #4 from UmkaDK/set-tags
Browse files Browse the repository at this point in the history
Prevent taggatron#SetTags() from clobbering an existing &tags value
  • Loading branch information
joonty committed Feb 5, 2014
2 parents 2152f23 + 2344403 commit c87ce12
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 36 deletions.
51 changes: 20 additions & 31 deletions autoload/taggatron.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,39 @@ function! taggatron#CreateTags(cmdset,forceCreate)
call taggatron#debug("Creating tags for file type ".&filetype)
call taggatron#debug(a:cmdset)
call taggatron#debug(s:taggatron_cmd_entry)

" Define local support variables
let l:cset = {}
let l:eset = a:cmdset
let l:cwd = fnamemodify(getcwd(), ':p')

" Initialise l:cset variable
call extend(l:cset,s:taggatron_cmd_entry)
call extend(l:cset,l:eset)
if !has_key(l:cset,'tagfile')

" Detect missing tagfile
if !has_key(l:cset,'tagfile')
call taggatron#error("Missing tag file destination from tag commands for file type ".&filetype)
return
endif
let l:cwd = getcwd()
if !has_key(l:cset,'files')

" Identify files to be scanned
if !has_key(l:cset,'files')
let l:cset['files'] = l:cwd
if has_key(l:cset,'filesappend')
let l:cset['files'] = l:cset['files'].l:cset['filesappend']
endif
endif

" Identify the value for the ctag's --language switch
if !has_key(l:cset,"lang")
let l:cset['lang'] = &filetype
endif

" Generate ctags command
let l:cmdstr = l:cset['cmd'] . " " . l:cset["args"] . " --languages=" . l:cset['lang']

" Run ctags to either (re)create or update tag file
if !filereadable(l:cset['tagfile']) || a:forceCreate == 1
let l:cmdstr = l:cmdstr ." -f ".l:cset['tagfile'] . " " .l:cset['files']
call taggatron#debug("Executing command: ".l:cmdstr)
Expand All @@ -39,34 +53,9 @@ function! taggatron#CreateTags(cmdset,forceCreate)
call taggatron#UpdateTags(l:cset['cmd'],l:cwd,l:cset['tagfile'])
endif

let l:tagfile = taggatron#makeAbsoluteFilePath(l:cset['tagfile'],l:cwd)

exec "set tags=".g:tagdefaults.",".l:tagfile

endfunction

function! taggatron#makeAbsoluteFilePath(file,cwd)
if has("unix")
if a:file =~ "^/.*"
return a:file
else
if a:cwd =~ "^.*/$"
return a:cwd.a:file
else
return a:cwd."/".a:file
endif
endif
else
if a:file =~ "^[a-zA-Z]:.*"
return a:file
else
if a:cwd =~ "^.*\\$"
return a:cwd.a:file
else
return a:cwd."\\".a:file
endif
endif
endif
" Ensure that generated tags are picked up by the editor
let l:tagfile = fnamemodify(l:cwd.l:cset['tagfile'], ':p')
call taggatron#SetTags(l:tagfile)
endfunction

function! taggatron#error(str)
Expand Down
58 changes: 53 additions & 5 deletions plugin/taggatron.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,67 @@ endif
if !exists("g:taggatron_verbose")
let g:taggatron_verbose = 0
endif
if !exists("g:taggatron_enabled")
let g:taggatron_enabled = 1
endif

" Include all default tags
if len(g:tagdefaults) > 0
call taggatron#debug("Adding default tags: ".g:tagdefaults)
exec "setlocal tags+=".g:tagdefaults
endif

autocmd BufWritePost * call taggatron#CheckCommandList(0)
command! TagUpdate call taggatron#CheckCommandList(1)
command! -nargs=1 SetTags call taggatron#SetTags(<f-args>)

function! taggatron#SetTags(tags)
call taggatron#debug("Setting tag files: ".a:tags)
exec "set tags=".a:tags
let g:tagdefaults = a:tags
function! taggatron#SetTags(files)
" Define local support variables
let l:files = type(a:files) == 1 ? [a:files] : a:files
let l:tagfiles = []
let l:cwd = fnamemodify(getcwd(), ':p')

" Fail if l:files is not a list
if type(l:files) != 3
call taggatron#error("Invalid tag file format, request to SetTags ignored.")
return

" Exit early if list is empty
elseif empty(l:files)
call taggatron#debug("No tag files defined, exiting early.")
return
endif

" Create a list of all tag files currently loaded (absolute path)
for l:tagfile in tagfiles()
call add(l:tagfiles, fnamemodify(l:cwd.l:tagfile, ':p'))
endfor

" Process all tag files one by one
for l:file in l:files
" Skip non-existent and unreadable file
if !filereadable(l:file)
call taggatron#debug("Skipping non-existent or unreadable tag file: ".l:file)
continue
endif

" Ensure absolute path to the current file
let l:file = fnamemodify(l:file, ':p')

" Only add current file to tags if it hasn't been already found
if index(l:tagfiles, l:file) == -1
call taggatron#debug("Adding tag file: ".l:file)
exec "setlocal tags+=".l:file
endif
endfor
endfunction

function! taggatron#CheckCommandList(forceCreate)
if g:taggatron_enabled != 1
call taggatron#debug("Tag file generation disabled (taggatron_enabled: " . g:taggatron_enabled . ")")
return
endif

let l:cwd = getcwd()
call taggatron#debug("Current directory: ".l:cwd)
if expand("%:p:h") =~ l:cwd . ".*"
Expand All @@ -32,5 +81,4 @@ function! taggatron#CheckCommandList(forceCreate)
else
call taggatron#debug("Not creating tags: file is not in current directory")
endif

endfunction

0 comments on commit c87ce12

Please sign in to comment.