Skip to content
This repository
Browse code

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.
  • Loading branch information...
commit 5bd20f02ebfd7c7bf5a33a2cd31a7d76a54911dd 1 parent 8d13bd2
Peter Odding authored June 06, 2010

Showing 1 changed file with 99 additions and 82 deletions. Show diff stats Hide diff stats

  1. 181  autoload.vim
181  autoload.vim
@@ -4,34 +4,84 @@
4 4
 " URL: http://peterodding.com/code/vim/easytags
5 5
 
6 6
 function! easytags#autoload() " {{{1
7  
-
8  
-  " Update the entries for the current file in the global tags file?
9  
-  let start = xolox#timer#start()
10  
-  if getftime(expand('%')) > getftime(easytags#get_tagsfile())
11  
-    UpdateTags
12  
-    call xolox#timer#stop(start, "easytags.vim: Automatically updated tags in %s second(s)")
13  
-  endif
14  
-
15  
-  " Apply highlighting of tags in global tags file to current buffer?
16  
-  if &eventignore !~? '\<syntax\>'
  7
+  try
  8
+    " Update the entries for the current file in the global tags file?
17 9
     let start = xolox#timer#start()
18  
-    if !exists('b:easytags_last_highlighted')
19  
-      HighlightTags
20  
-    else
21  
-      for tagfile in tagfiles()
22  
-        if getftime(tagfile) > b:easytags_last_highlighted
23  
-          HighlightTags
24  
-          break
25  
-        endif
26  
-      endfor
  10
+    if getftime(expand('%')) > getftime(easytags#get_tagsfile())
  11
+      UpdateTags
  12
+      call xolox#timer#stop(start, "easytags.vim: Automatically updated tags in %s second(s)")
27 13
     endif
28  
-    let b:easytags_last_highlighted = localtime()
29  
-    call xolox#timer#stop(start, "easytags.vim: Automatically highlighted tags in %s second(s)")
30  
-  endif
31  
-
  14
+    " Apply highlighting of tags in global tags file to current buffer?
  15
+    if &eventignore !~? '\<syntax\>'
  16
+      let start = xolox#timer#start()
  17
+      if !exists('b:easytags_last_highlighted')
  18
+        HighlightTags
  19
+      else
  20
+        for tagfile in tagfiles()
  21
+          if getftime(tagfile) > b:easytags_last_highlighted
  22
+            HighlightTags
  23
+            break
  24
+          endif
  25
+        endfor
  26
+      endif
  27
+      let b:easytags_last_highlighted = localtime()
  28
+      call xolox#timer#stop(start, "easytags.vim: Automatically highlighted tags in %s second(s)")
  29
+    endif
  30
+  catch
  31
+    call xolox#warning("easytags.vim: %s (at %s)", v:exception, v:throwpoint)
  32
+  endtry
32 33
 endfunction
33 34
 
34 35
 function! easytags#update_cmd(filter_invalid_tags) " {{{1
  36
+  try
  37
+    let supported_filetype = index(easytags#supported_filetypes(), &ft) >= 0
  38
+    if supported_filetype || a:filter_invalid_tags
  39
+      let start = xolox#timer#start()
  40
+      let tagsfile = easytags#get_tagsfile()
  41
+      let filename = expand('%:p')
  42
+      if g:easytags_resolve_links
  43
+        let filename = resolve(filename)
  44
+      endif
  45
+      let command = [g:easytags_cmd, '-f', shellescape(tagsfile)]
  46
+      if filereadable(tagsfile)
  47
+        call add(command, '-a')
  48
+        let start_filter = xolox#timer#start()
  49
+        let lines = readfile(tagsfile)
  50
+        let filters = []
  51
+        if supported_filetype
  52
+          let filename_pattern = '\s' . xolox#escape#pattern(filename) . '\s'
  53
+          call add(filters, 'v:val !~ filename_pattern')
  54
+        endif
  55
+        if a:filter_invalid_tags
  56
+          call add(filters, 'filereadable(get(split(v:val, "\t"), 1))')
  57
+        endif
  58
+        let filter = 'v:val =~ "^!_TAG_" || (' . join(filters, ' && ') . ')'
  59
+        let filtered = filter(copy(lines), filter)
  60
+        if lines != filtered
  61
+          if writefile(filtered, tagsfile) != 0
  62
+            throw "Failed to filter tags file!"
  63
+          endif
  64
+        endif
  65
+        call xolox#timer#stop(start_filter, "easytags.vim: Filtered tags file in %s second(s)")
  66
+      endif
  67
+      if supported_filetype
  68
+        call add(command, '--language-force=' . easytags#to_ctags_ft(&ft))
  69
+        call add(command, shellescape(filename))
  70
+        let listing = system(join(command))
  71
+        if v:shell_error
  72
+          throw "Failed to update tags file!"
  73
+        endif
  74
+      endif
  75
+      call xolox#timer#stop(start, "easytags.vim: Updated tags in %s second(s)")
  76
+      return 1
  77
+    endif
  78
+    return 0
  79
+  catch
  80
+    call xolox#warning("easytags.vim: %s (at %s)", v:exception, v:throwpoint)
  81
+  endtry
  82
+endfunction
  83
+
  84
+function! easytags#supported_filetypes() " {{{1
35 85
   if !exists('s:supported_filetypes')
36 86
     let start = xolox#timer#start()
37 87
     let listing = system(g:easytags_cmd . ' --list-languages')
@@ -42,73 +92,40 @@ function! easytags#update_cmd(filter_invalid_tags) " {{{1
42 92
     call map(s:supported_filetypes, 'easytags#to_vim_ft(v:val)')
43 93
     call xolox#timer#stop(start, "easytags.vim: Parsed language mappings in %s second(s)")
44 94
   endif
45  
-  let supported_filetype = index(s:supported_filetypes, &ft) >= 0
46  
-  if supported_filetype || a:filter_invalid_tags
47  
-    let start = xolox#timer#start()
48  
-    let tagsfile = easytags#get_tagsfile()
49  
-    let filename = expand('%:p')
50  
-    if g:easytags_resolve_links
51  
-      let filename = resolve(filename)
52  
-    endif
53  
-    let command = [g:easytags_cmd, '-f', shellescape(tagsfile)]
54  
-    if filereadable(tagsfile)
55  
-      call add(command, '-a')
56  
-      let start_filter = xolox#timer#start()
57  
-      let lines = readfile(tagsfile)
58  
-      let filters = []
59  
-      if supported_filetype
60  
-        call add(filters, 'v:val !~ ' . string('\s' . xolox#escape#pattern(filename) . '\s'))
61  
-      endif
62  
-      if a:filter_invalid_tags
63  
-        call add(filters, 'filereadable(get(split(v:val, "\t"), 1))')
64  
-      endif
65  
-      let filter = 'v:val =~ "^!_TAG_" || (' . join(filters, ' && ') . ')'
66  
-      let filtered = filter(copy(lines), filter)
67  
-      if lines != filtered
68  
-        call writefile(filtered, tagsfile)
69  
-      endif
70  
-      call xolox#timer#stop(start_filter, "easytags.vim: Filtered tags file in %s second(s)")
71  
-    endif
72  
-    if supported_filetype
73  
-      call add(command, '--language-force=' . easytags#to_ctags_ft(&ft))
74  
-      call add(command, shellescape(filename))
75  
-      let listing = system(join(command))
76  
-      if v:shell_error
77  
-        let message = "Failed to update tags file! (%s)"
78  
-        throw printf(message, listing)
79  
-      endif
80  
-    endif
81  
-    call xolox#timer#stop(start, "easytags.vim: Updated tags in %s second(s)")
82  
-  endif
  95
+  return s:supported_filetypes
83 96
 endfunction
84 97
 
85 98
 function! easytags#highlight_cmd() " {{{1
86  
-  if exists('g:syntax_on') && has_key(s:tagkinds, &ft)
87  
-    let start = xolox#timer#start()
88  
-    let taglist = filter(taglist('.'), "get(v:val, 'language', '') ==? &ft")
89  
-    for tagkind in s:tagkinds[&ft]
90  
-      let hlgroup_tagged = tagkind.hlgroup . 'Tag'
91  
-      if hlexists(hlgroup_tagged)
92  
-        execute 'syntax clear' hlgroup_tagged
93  
-      else
94  
-        execute 'highlight def link' hlgroup_tagged tagkind.hlgroup
95  
-      endif
96  
-      let matches = filter(copy(taglist), tagkind.filter)
97  
-      call map(matches, 'xolox#escape#pattern(get(v:val, "name"))')
98  
-      let pattern = tagkind.pattern_prefix . '\%(' . join(xolox#unique(matches), '\|') . '\)' . tagkind.pattern_suffix
99  
-      let command = 'syntax match %s /%s/ containedin=ALLBUT,.*String.*,.*Comment.*'
100  
-      execute printf(command, hlgroup_tagged, escape(pattern, '/'))
101  
-    endfor
102  
-    redraw
103  
-    call xolox#timer#stop(start, "easytags.vim: Highlighted tags in %s second(s)")
104  
-  endif
  99
+  try
  100
+    if exists('g:syntax_on') && has_key(s:tagkinds, &ft)
  101
+      let start = xolox#timer#start()
  102
+      let taglist = filter(taglist('.'), "get(v:val, 'language', '') ==? &ft")
  103
+      for tagkind in s:tagkinds[&ft]
  104
+        let hlgroup_tagged = tagkind.hlgroup . 'Tag'
  105
+        if hlexists(hlgroup_tagged)
  106
+          execute 'syntax clear' hlgroup_tagged
  107
+        else
  108
+          execute 'highlight def link' hlgroup_tagged tagkind.hlgroup
  109
+        endif
  110
+        let matches = filter(copy(taglist), tagkind.filter)
  111
+        call map(matches, 'xolox#escape#pattern(get(v:val, "name"))')
  112
+        let pattern = tagkind.pattern_prefix . '\%(' . join(xolox#unique(matches), '\|') . '\)' . tagkind.pattern_suffix
  113
+        let command = 'syntax match %s /%s/ containedin=ALLBUT,.*String.*,.*Comment.*'
  114
+        execute printf(command, hlgroup_tagged, escape(pattern, '/'))
  115
+      endfor
  116
+      redraw
  117
+      call xolox#timer#stop(start, "easytags.vim: Highlighted tags in %s second(s)")
  118
+    endif
  119
+  catch
  120
+    call xolox#warning("easytags.vim: %s (at %s)", v:exception, v:throwpoint)
  121
+  endtry
105 122
 endfunction
106 123
 
107 124
 function! easytags#get_tagsfile() " {{{1
108 125
   let tagsfile = expand(g:easytags_file)
109 126
   if filereadable(tagsfile) && filewritable(tagsfile) != 1
110  
-    let message = "easytags.vim: The tags file isn't writable! (%s)"
111  
-    echoerr printf(message, fnamemodify(directory, ':~'))
  127
+    let message = "The tags file isn't writable! (%s)"
  128
+    throw printf(message, tagsfile)
112 129
   endif
113 130
   return tagsfile
114 131
 endfunction

0 notes on commit 5bd20f0

Please sign in to comment.
Something went wrong with that request. Please try again.