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
13 " ---------------------------------------------------------------------
15 if &cp || v:version < 700 || (exists('g:loaded_TagHLReadTypes') && (g:plugin_development_mode != 1))
16 throw "Already loaded"
21 let g:loaded_TagHLReadTypes = 1
23 let s:all_ft_methods = ['Extension', 'Syntax', 'FileType']
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)
33 if eval('TagHighlight#ReadTypes#ReadTypesBy'.method.'()') == 1
34 call TagHLDebug("Success with method " . method, "Information")
37 call TagHLDebug("Could not find types with method " . method, "Information")
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")
49 let file = expand('<afile>')
51 let file = expand('%')
53 let extension = fnamemodify(file, ':e')
55 return s:ReadTypesImplementation('Extension', 'ExtensionLookup', extension, 's:ExtensionCheck')
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")
66 let syn = expand('<amatch>')
71 return s:ReadTypesImplementation('Syntax', 'SyntaxLookup', syn, 's:InListCheck')
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")
82 let ft = expand('<amatch>')
87 return s:ReadTypesImplementation('FileType', 'FileTypeLookup', ft, 's:InListCheck')
89 function! s:ExtensionCheck(this, expected)
90 let regex = '^'.a:expected.'$'
97 function! s:InListCheck(this, expected)
98 let split_list = split(a:expected, ",")
99 if index(split_list, a:this) != -1
105 function! s:MethodListed(method)
106 let ft_methods = TagHighlight#Option#GetOption('LanguageDetectionMethods')
107 if index(ft_methods, a:method) != -1
113 function! s:ReadTypesImplementation(type, lookup, reference, check_function)
115 if TagHighlight#Debug#DebugLevelIncludes('Information')
116 call TagHLDebug("Reading types with " . a:lookup . " at " . strftime("%Y%m%d-%H%M%S"), "Information")
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])
134 function! s:ReadTypes(suffix)
135 let savedView = winsaveview()
137 call TagHighlight#TagManager#InitialiseBufferTags()
139 call TagHighlight#Option#LoadOptionFileIfPresent()
141 if len(a:suffix) == 0
145 let file = expand('<afile>')
147 let file = expand('%')
150 call TagHLDebug("Reading types of suffix " . a:suffix . " for file " . file, "Information")
152 if TagHighlight#Option#GetOption('DisableTypeParsing') == 1
153 call TagHLDebug("Type file parsing disabled", 'Status')
157 let fullname = fnamemodify(file, ':p')
159 let skiplist = TagHighlight#Option#GetOption('ParsingSkipList')
160 call TagHLDebug("Skip List is " . string(skiplist) . " (length " . len(skiplist) . ")", 'Information')
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')
168 if index(skiplist, fullname) != -1
169 call TagHLDebug("Skipping file due to fullname match", 'Status')
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)'
181 call TagHLDebug("Searching for types file", 'Status')
183 " Clear any existing syntax entries
184 for group in g:TagHighlightPrivate['AllTypes']
185 exe 'syn clear' group
188 let b:TagHighlightLoadedLibraries = []
190 let type_files = TagHighlight#ReadTypes#FindTypeFiles(a:suffix)
191 for fname in type_files
192 call TagHLDebug("Loading type highlighter file " . fname, 'Information')
194 let b:TagHighlightLoadedLibraries +=
197 \ 'Filename': fnamemodify(fname, ':t'),
198 \ 'Path': fnamemodify(fname, ':p'),
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')
207 let b:TagHighlightLoadedLibraries += [lib]
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')
215 let b:TagHighlightLoadedLibraries += [lib]
218 " Set up tags for all loaded libraries
219 call TagHighlight#TagManager#ConfigureTags()
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 . '()'
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)'
237 call winrestview(savedView)
238 call TagHLDebug("ReadTypes complete", "Information")
241 function! TagHighlight#ReadTypes#FindTypeFiles(suffix)
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']]