removed all but vam submodule to let autoinstall work
[stack/conf/vim.git] / addons / TagHighlight / autoload / TagHighlight / ReadTypes.vim
1 " Tag Highlighter:
2 "   Author:  A. S. Budden <abudden _at_ gmail _dot_ com>
3 " Copyright: Copyright (C) 2009-2012 A. S. Budden
4 "            Permission is hereby granted to use and distribute this code,
5 "            with or without modifications, provided that this copyright
6 "            notice is copied with it. Like anything else that's free,
7 "            the TagHighlight plugin is provided *as is* and comes with no
8 "            warranty of any kind, either expressed or implied. By using
9 "            this plugin, you agree that in no event will the copyright
10 "            holder be liable for any damages resulting from the use
11 "            of this software.
12
13 " ---------------------------------------------------------------------
14 try
15         if &cp || v:version < 700 || (exists('g:loaded_TagHLReadTypes') && (g:plugin_development_mode != 1))
16                 throw "Already loaded"
17         endif
18 catch
19         finish
20 endtry
21 let g:loaded_TagHLReadTypes = 1
22
23 let s:all_ft_methods = ['Extension', 'Syntax', 'FileType']
24
25 function! TagHighlight#ReadTypes#ReadTypesByOption()
26         call TagHighlight#Option#LoadOptionFileIfPresent()
27         let ft_methods = TagHighlight#Option#GetOption('LanguageDetectionMethods')
28         for method in ft_methods
29                 if index(s:all_ft_methods, method) == -1
30                         echoerr "Invalid detection method: " . method . ": valid values are " .
31                                                 \ string(s:all_ft_methods)
32                 endif
33                 if eval('TagHighlight#ReadTypes#ReadTypesBy'.method.'()') == 1
34                         call TagHLDebug("Success with method " . method, "Information")
35                         break
36                 else
37                         call TagHLDebug("Could not find types with method " . method, "Information")
38                 endif
39         endfor
40 endfunction
41
42 function! TagHighlight#ReadTypes#ReadTypesByExtension()
43         call TagHLDebug("Reading Types by Extension", "Information")
44         if ! s:MethodListed('Extension')
45                 call TagHLDebug("Read Types by Extension not specified", "Information")
46                 return 0
47         endif
48
49         let file = expand('<afile>')
50         if len(file) == 0
51                 let file = expand('%')
52         endif
53         let extension = fnamemodify(file, ':e')
54
55         return s:ReadTypesImplementation('Extension', 'ExtensionLookup', extension, 's:ExtensionCheck')
56 endfunction
57
58 function! TagHighlight#ReadTypes#ReadTypesBySyntax()
59         call TagHLDebug("Reading Types by Syntax", "Information")
60         if ! s:MethodListed('Syntax')
61                 call TagHLDebug("Read Types by Syntax not specified", "Information")
62                 return 0
63         endif
64
65         let result = 0
66         let syn = expand('<amatch>')
67         if len(syn) == 0
68                 let syn = &syntax
69         endif
70
71         return s:ReadTypesImplementation('Syntax', 'SyntaxLookup', syn, 's:InListCheck')
72 endfunction
73
74 function! TagHighlight#ReadTypes#ReadTypesByFileType()
75         call TagHLDebug("Reading Types by FileType", "Information")
76         if ! s:MethodListed('FileType')
77                 call TagHLDebug("Read Types by FileType not specified", "Information")
78                 return 0
79         endif
80
81         let result = 0
82         let ft = expand('<amatch>')
83         if len(ft) == 0
84                 let ft = &filetype
85         endif
86
87         return s:ReadTypesImplementation('FileType', 'FileTypeLookup', ft, 's:InListCheck')
88 endfunction
89 function! s:ExtensionCheck(this, expected)
90         let regex = '^'.a:expected.'$'
91         if a:this =~ regex
92                 return 1
93         endif
94         return 0
95 endfunction
96
97 function! s:InListCheck(this, expected)
98         let split_list = split(a:expected, ",")
99         if index(split_list, a:this) != -1
100                 return 1
101         endif
102         return 0
103 endfunction
104
105 function! s:MethodListed(method)
106         let ft_methods = TagHighlight#Option#GetOption('LanguageDetectionMethods')
107         if index(ft_methods, a:method) != -1
108                 return 1
109         endif
110         return 0
111 endfunction
112
113 function! s:ReadTypesImplementation(type, lookup, reference, check_function)
114         let result = 0
115         if TagHighlight#Debug#DebugLevelIncludes('Information')
116                 call TagHLDebug("Reading types with " . a:lookup . " at " . strftime("%Y%m%d-%H%M%S"), "Information")
117         endif
118         let user_overrides = TagHighlight#Option#GetOption(a:type . 'LanguageOverrides')
119         for dictionary in [user_overrides, g:TagHighlightPrivate[a:lookup]]
120                 for key in keys(dictionary)
121                         if eval(a:check_function . '(a:reference, key)') == 1
122                                 call s:ReadTypes(dictionary[key])
123                                 let result = 1
124                                 break
125                         endif
126                 endfor
127                 if result
128                         break
129                 endif
130         endfor
131         return result
132 endfunction
133
134 function! s:ReadTypes(suffix)
135         let savedView = winsaveview()
136
137         call TagHighlight#TagManager#InitialiseBufferTags()
138
139         call TagHighlight#Option#LoadOptionFileIfPresent()
140
141         if len(a:suffix) == 0
142                 return
143         endif
144
145         let file = expand('<afile>')
146         if len(file) == 0
147                 let file = expand('%')
148         endif
149
150         call TagHLDebug("Reading types of suffix " . a:suffix . " for file " . file, "Information")
151
152         if TagHighlight#Option#GetOption('DisableTypeParsing') == 1
153                 call TagHLDebug("Type file parsing disabled", 'Status')
154                 return
155         endif
156
157         let fullname = fnamemodify(file, ':p')
158
159         let skiplist = TagHighlight#Option#GetOption('ParsingSkipList')
160         call TagHLDebug("Skip List is " . string(skiplist) . " (length " . len(skiplist) . ")", 'Information')
161         if len(skiplist) > 0
162                 let basename = fnamemodify(file, ':p:t')
163                 call TagHLDebug("Checking skip list against b(".basename.");f(".fullname.")", "Information")
164                 if index(skiplist, basename) != -1
165                         call TagHLDebug("Skipping file due to basename match", 'Status')
166                         return
167                 endif
168                 if index(skiplist, fullname) != -1
169                         call TagHLDebug("Skipping file due to fullname match", 'Status')
170                         return
171                 endif
172         endif
173         "
174         " Call Pre Read hooks (if any)
175         let preread_hooks = TagHighlight#Option#GetOption('PreReadHooks')
176         for preread_hook in preread_hooks
177                 call TagHLDebug("Calling pre-read hook " . preread_hook, 'Information')
178                 exe 'call' preread_hook . '(fullname, a:suffix)'
179         endfor
180
181         call TagHLDebug("Searching for types file", 'Status')
182
183         " Clear any existing syntax entries
184         for group in g:TagHighlightPrivate['AllTypes']
185                 exe 'syn clear' group
186         endfor
187
188         let b:TagHighlightLoadedLibraries = []
189         
190         let type_files = TagHighlight#ReadTypes#FindTypeFiles(a:suffix)
191         for fname in type_files
192                 call TagHLDebug("Loading type highlighter file " . fname, 'Information')
193                 exe 'so' fname
194                 let b:TagHighlightLoadedLibraries +=
195                                         \ [{
196                                         \     'Name': 'Local',
197                                         \     'Filename': fnamemodify(fname, ':t'),
198                                         \     'Path': fnamemodify(fname, ':p'),
199                                         \ }]
200         endfor
201
202         " Load user libraries
203         let user_library_files = TagHighlight#Libraries#FindUserLibraries()
204         for lib in user_library_files
205                 call TagHLDebug("Loading user library type highlighter file " . lib['Path'], 'Information')
206                 exe 'so' lib['Path']
207                 let b:TagHighlightLoadedLibraries += [lib]
208         endfor
209
210         " Now load any libraries that are relevant
211         let library_files = TagHighlight#Libraries#FindLibraryFiles(a:suffix)
212         for lib in library_files
213                 call TagHLDebug("Loading standard library type highlighter file " . lib['Path'], 'Information')
214                 exe 'so' lib['Path']
215                 let b:TagHighlightLoadedLibraries += [lib]
216         endfor
217
218         " Set up tags for all loaded libraries
219         call TagHighlight#TagManager#ConfigureTags()
220
221         " Handle any special cases
222         if has_key(g:TagHighlightPrivate['SpecialSyntaxHandlers'], a:suffix)
223                 for handler in g:TagHighlightPrivate['SpecialSyntaxHandlers'][a:suffix]
224                         call TagHLDebug("Calling special handler " . handler, 'Information')
225                         exe 'call' handler . '()'
226                 endfor
227         endif
228
229         " Call Post Read Hooks (if any)
230         let postread_hooks = TagHighlight#Option#GetOption('PostReadHooks')
231         for postread_hook in postread_hooks
232                 call TagHLDebug("Calling post-read hook " . postread_hook, 'Information')
233                 exe 'call' postread_hook . '(fullname, a:suffix)'
234         endfor
235
236         " Restore the view
237         call winrestview(savedView)
238         call TagHLDebug("ReadTypes complete", "Information")
239 endfunction
240
241 function! TagHighlight#ReadTypes#FindTypeFiles(suffix)
242         let results = []
243         let search_result = TagHighlight#Find#LocateFile('TYPES', a:suffix)
244         if search_result['Found'] == 1 && search_result['Exists'] == 1
245                 let results += [search_result['FullPath']]
246         endif
247         return results
248 endfunction