From 5bd20f02ebfd7c7bf5a33a2cd31a7d76a54911dd Mon Sep 17 00:00:00 2001 From: Peter Odding Date: Sun, 6 Jun 2010 13:40:11 +0200 Subject: Wrapped public facing functions in try/catch blocks The plug-in used an inconsistent mix of error handling using both :throw and :echoerr. The public facing functions have now been wrapped in try/catch blocks and all internal functions :throw error message strings. --- autoload.vim | 181 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 99 insertions(+), 82 deletions(-) (limited to 'autoload.vim') diff --git a/autoload.vim b/autoload.vim index 600135a..4ec3f2a 100644 --- a/autoload.vim +++ b/autoload.vim @@ -4,34 +4,84 @@ " URL: http://peterodding.com/code/vim/easytags function! easytags#autoload() " {{{1 - - " Update the entries for the current file in the global tags file? - let start = xolox#timer#start() - if getftime(expand('%')) > getftime(easytags#get_tagsfile()) - UpdateTags - call xolox#timer#stop(start, "easytags.vim: Automatically updated tags in %s second(s)") - endif - - " Apply highlighting of tags in global tags file to current buffer? - if &eventignore !~? '\' + try + " Update the entries for the current file in the global tags file? let start = xolox#timer#start() - if !exists('b:easytags_last_highlighted') - HighlightTags - else - for tagfile in tagfiles() - if getftime(tagfile) > b:easytags_last_highlighted - HighlightTags - break - endif - endfor + if getftime(expand('%')) > getftime(easytags#get_tagsfile()) + UpdateTags + call xolox#timer#stop(start, "easytags.vim: Automatically updated tags in %s second(s)") endif - let b:easytags_last_highlighted = localtime() - call xolox#timer#stop(start, "easytags.vim: Automatically highlighted tags in %s second(s)") - endif - + " Apply highlighting of tags in global tags file to current buffer? + if &eventignore !~? '\' + let start = xolox#timer#start() + if !exists('b:easytags_last_highlighted') + HighlightTags + else + for tagfile in tagfiles() + if getftime(tagfile) > b:easytags_last_highlighted + HighlightTags + break + endif + endfor + endif + let b:easytags_last_highlighted = localtime() + call xolox#timer#stop(start, "easytags.vim: Automatically highlighted tags in %s second(s)") + endif + catch + call xolox#warning("easytags.vim: %s (at %s)", v:exception, v:throwpoint) + endtry endfunction function! easytags#update_cmd(filter_invalid_tags) " {{{1 + try + let supported_filetype = index(easytags#supported_filetypes(), &ft) >= 0 + if supported_filetype || a:filter_invalid_tags + let start = xolox#timer#start() + let tagsfile = easytags#get_tagsfile() + let filename = expand('%:p') + if g:easytags_resolve_links + let filename = resolve(filename) + endif + let command = [g:easytags_cmd, '-f', shellescape(tagsfile)] + if filereadable(tagsfile) + call add(command, '-a') + let start_filter = xolox#timer#start() + let lines = readfile(tagsfile) + let filters = [] + if supported_filetype + let filename_pattern = '\s' . xolox#escape#pattern(filename) . '\s' + call add(filters, 'v:val !~ filename_pattern') + endif + if a:filter_invalid_tags + call add(filters, 'filereadable(get(split(v:val, "\t"), 1))') + endif + let filter = 'v:val =~ "^!_TAG_" || (' . join(filters, ' && ') . ')' + let filtered = filter(copy(lines), filter) + if lines != filtered + if writefile(filtered, tagsfile) != 0 + throw "Failed to filter tags file!" + endif + endif + call xolox#timer#stop(start_filter, "easytags.vim: Filtered tags file in %s second(s)") + endif + if supported_filetype + call add(command, '--language-force=' . easytags#to_ctags_ft(&ft)) + call add(command, shellescape(filename)) + let listing = system(join(command)) + if v:shell_error + throw "Failed to update tags file!" + endif + endif + call xolox#timer#stop(start, "easytags.vim: Updated tags in %s second(s)") + return 1 + endif + return 0 + catch + call xolox#warning("easytags.vim: %s (at %s)", v:exception, v:throwpoint) + endtry +endfunction + +function! easytags#supported_filetypes() " {{{1 if !exists('s:supported_filetypes') let start = xolox#timer#start() let listing = system(g:easytags_cmd . ' --list-languages') @@ -42,73 +92,40 @@ function! easytags#update_cmd(filter_invalid_tags) " {{{1 call map(s:supported_filetypes, 'easytags#to_vim_ft(v:val)') call xolox#timer#stop(start, "easytags.vim: Parsed language mappings in %s second(s)") endif - let supported_filetype = index(s:supported_filetypes, &ft) >= 0 - if supported_filetype || a:filter_invalid_tags - let start = xolox#timer#start() - let tagsfile = easytags#get_tagsfile() - let filename = expand('%:p') - if g:easytags_resolve_links - let filename = resolve(filename) - endif - let command = [g:easytags_cmd, '-f', shellescape(tagsfile)] - if filereadable(tagsfile) - call add(command, '-a') - let start_filter = xolox#timer#start() - let lines = readfile(tagsfile) - let filters = [] - if supported_filetype - call add(filters, 'v:val !~ ' . string('\s' . xolox#escape#pattern(filename) . '\s')) - endif - if a:filter_invalid_tags - call add(filters, 'filereadable(get(split(v:val, "\t"), 1))') - endif - let filter = 'v:val =~ "^!_TAG_" || (' . join(filters, ' && ') . ')' - let filtered = filter(copy(lines), filter) - if lines != filtered - call writefile(filtered, tagsfile) - endif - call xolox#timer#stop(start_filter, "easytags.vim: Filtered tags file in %s second(s)") - endif - if supported_filetype - call add(command, '--language-force=' . easytags#to_ctags_ft(&ft)) - call add(command, shellescape(filename)) - let listing = system(join(command)) - if v:shell_error - let message = "Failed to update tags file! (%s)" - throw printf(message, listing) - endif - endif - call xolox#timer#stop(start, "easytags.vim: Updated tags in %s second(s)") - endif + return s:supported_filetypes endfunction function! easytags#highlight_cmd() " {{{1 - if exists('g:syntax_on') && has_key(s:tagkinds, &ft) - let start = xolox#timer#start() - let taglist = filter(taglist('.'), "get(v:val, 'language', '') ==? &ft") - for tagkind in s:tagkinds[&ft] - let hlgroup_tagged = tagkind.hlgroup . 'Tag' - if hlexists(hlgroup_tagged) - execute 'syntax clear' hlgroup_tagged - else - execute 'highlight def link' hlgroup_tagged tagkind.hlgroup - endif - let matches = filter(copy(taglist), tagkind.filter) - call map(matches, 'xolox#escape#pattern(get(v:val, "name"))') - let pattern = tagkind.pattern_prefix . '\%(' . join(xolox#unique(matches), '\|') . '\)' . tagkind.pattern_suffix - let command = 'syntax match %s /%s/ containedin=ALLBUT,.*String.*,.*Comment.*' - execute printf(command, hlgroup_tagged, escape(pattern, '/')) - endfor - redraw - call xolox#timer#stop(start, "easytags.vim: Highlighted tags in %s second(s)") - endif + try + if exists('g:syntax_on') && has_key(s:tagkinds, &ft) + let start = xolox#timer#start() + let taglist = filter(taglist('.'), "get(v:val, 'language', '') ==? &ft") + for tagkind in s:tagkinds[&ft] + let hlgroup_tagged = tagkind.hlgroup . 'Tag' + if hlexists(hlgroup_tagged) + execute 'syntax clear' hlgroup_tagged + else + execute 'highlight def link' hlgroup_tagged tagkind.hlgroup + endif + let matches = filter(copy(taglist), tagkind.filter) + call map(matches, 'xolox#escape#pattern(get(v:val, "name"))') + let pattern = tagkind.pattern_prefix . '\%(' . join(xolox#unique(matches), '\|') . '\)' . tagkind.pattern_suffix + let command = 'syntax match %s /%s/ containedin=ALLBUT,.*String.*,.*Comment.*' + execute printf(command, hlgroup_tagged, escape(pattern, '/')) + endfor + redraw + call xolox#timer#stop(start, "easytags.vim: Highlighted tags in %s second(s)") + endif + catch + call xolox#warning("easytags.vim: %s (at %s)", v:exception, v:throwpoint) + endtry endfunction function! easytags#get_tagsfile() " {{{1 let tagsfile = expand(g:easytags_file) if filereadable(tagsfile) && filewritable(tagsfile) != 1 - let message = "easytags.vim: The tags file isn't writable! (%s)" - echoerr printf(message, fnamemodify(directory, ':~')) + let message = "The tags file isn't writable! (%s)" + throw printf(message, tagsfile) endif return tagsfile endfunction -- cgit v1.2.3