Commits

abudden committed d22ace3

Allow overriding languages for a given file.

  • Participants
  • Parent commits ef98468

Comments (0)

Files changed (4)

File autoload/TagHighlight/Option.vim

 			" Probably a multi-entry dict that has automatically been
 			" split: rejoin
 			let result = eval(join(opt, ', '))
+		elseif type(opt) == type("")
+			let result = eval(opt)
 		else
-			let result = eval(opt)
+			let result = opt
 		endif
 	elseif option['Type'] == 'int'
 		let result = str2nr(opt)

File autoload/TagHighlight/ReadTypes.vim

 	endif
 	let extension = fnamemodify(file, ':e')
 
-	return s:ReadTypesImplementation('ExtensionLookup', extension, 's:ExtensionCheck')
+	return s:ReadTypesImplementation('Extension', 'ExtensionLookup', extension, 's:ExtensionCheck')
 endfunction
 
 function! TagHighlight#ReadTypes#ReadTypesBySyntax()
 		let syn = &syntax
 	endif
 
-	return s:ReadTypesImplementation('SyntaxLookup', syn, 's:InListCheck')
+	return s:ReadTypesImplementation('Syntax', 'SyntaxLookup', syn, 's:InListCheck')
 endfunction
 
 function! TagHighlight#ReadTypes#ReadTypesByFileType()
 		let ft = &filetype
 	endif
 
-	return s:ReadTypesImplementation('FileTypeLookup', ft, 's:InListCheck')
+	return s:ReadTypesImplementation('FileType', 'FileTypeLookup', ft, 's:InListCheck')
 endfunction
 function! s:ExtensionCheck(this, expected)
 	let regex = '^'.a:expected.'$'
 	return 0
 endfunction
 
-function! s:ReadTypesImplementation(lookup, reference, check_function)
+function! s:ReadTypesImplementation(type, lookup, reference, check_function)
 	let result = 0
 	if TagHighlight#Debug#DebugLevelIncludes('Information')
 		call TagHLDebug("Reading types with " . a:lookup . " at " . strftime("%Y%m%d-%H%M%S"), "Information")
 	endif
-	for key in keys(g:TagHighlightPrivate[a:lookup])
-		if eval(a:check_function . '(a:reference, key)') == 1
-			call s:ReadTypes(g:TagHighlightPrivate[a:lookup][key])
-			let result = 1
+	let user_overrides = TagHighlight#Option#GetOption(a:type . 'LanguageOverrides')
+	for dictionary in [user_overrides, g:TagHighlightPrivate[a:lookup]]
+		for key in keys(dictionary)
+			if eval(a:check_function . '(a:reference, key)') == 1
+				call s:ReadTypes(dictionary[key])
+				let result = 1
+				break
+			endif
+		endfor
+		if result
+			break
 		endif
 	endfor
 	return result
 
 	call TagHighlight#Option#LoadOptionFileIfPresent()
 
+	if len(a:suffix) == 0
+		return
+	endif
+
 	let file = expand('<afile>')
 	if len(file) == 0
 		let file = expand('%')

File doc/TagHighlight.txt

 		LanguageDetectionMethods         Link:|TagHL-LanguageDetectionMethods|
 			How to identify programming language and which |autocmd|s to run.
 
+		ExtensionLanguageOverrides       Link:|TagHL-ExtensionLanguageOverrides|
+			Force a particular file extension to match a chosen language.
+			
+		FileTypeLanguageOverrides        Link:|TagHL-FileTypeLanguageOverrides|
+			Force a particular file type to match a chosen language.
+
+		SyntaxLanguageOverrides          Link:|TagHL-SyntaxLanguageOverrides|
+			Force a particular Vim syntax to match a chosen language.
+
 	What to include in the generated types file
 
 		IncludeLocals                    Link:|TagHL-IncludeLocals|
 		Option Type: Boolean
 		Default: False (generate tags)
 
+	ExtensionLanguageOverrides           *TagHL-ExtensionLanguageOverrides*
+		If there are any entries in this dictionary, they will be used to
+		force a particular file extension to be treated as representing a
+		particular programming language.  The dictionary key is a Vim regular
+		expression that matches the file extension and the dictionary value is
+		the "suffix" defined for that language (the bit in between "types_"
+		and ".taghl" in the generated type highlighter file).
+
+		For example, to make all .svn files be treated as C files by the
+		highlighter (so that function names mentioned in your commit log are
+		highlighted), you could use something like this:
+>
+			let g:TagHighlightSettings['ExtensionLanguageOverrides'] =
+					\ {'svn': 'c'}
+<
+		To disable an extension from matching against a language, set the
+		dictionary value to an empty string ("").
+
+		This override will only work if 'Extension' is in the list of
+		|TagHL-LanguageDetectionMethods|.
+
+	FileTypeLanguageOverrides            *TagHL-FileTypeLanguageOverrides*
+		If there are any entries in this dictionary, they will be used to
+		force a particular file type to be treated as representing a
+		particular programming language.  The dictionary key is a Vim file
+		type (as reported by ":set ft?") and the dictionary value is the
+		"suffix" defined for that language (the bit in between "types_"
+		and ".taghl" in the generated type highlighter file).
+
+		To disable a file type from matching against a language, set the
+		dictionary value to an empty string ("").
+
+		This override will only work if 'FileType' is in the list of
+		|TagHL-LanguageDetectionMethods|.
+
 	ForcedPythonVariant                  *TagHL-ForcedPythonVariant*
 		There are a number of different python variants that are supported for
 		running the python part of the plugin.  If this option is set to one
 					   for matches with the VimExtensionMatcher in the
 					   language definition files.
 
-			Syntax: Look at syntax that has been selected by Vim and check for
-			        matches with the VimSyntaxes list in the language
-			        definition files.
-
 			FileType: Look at the file type that has been selected by Vim and
 			          check for matches with the VimFileTypes list in the
 			          language definition files.
 
+			Syntax: Look at syntax that has been selected by Vim and check for
+			        matches with the VimSyntaxes list in the language
+			        definition files.  In most cases, you probably want to use
+			        FileType rather than Syntax.
+
 		When calling the either of the core commands (|:ReadTypes| and
 		|:UpdateTypesFile|), they are checked in the order specified in the
 		option.  In addition, there are |autocommand|s defined that will load
 		Option Type: String
 		Default: None (use normal scanning method)
 
+	SyntaxLanguageOverrides              *TagHL-SyntaxLanguageOverrides*
+		If there are any entries in this dictionary, they will be used to
+		force a particular file that is being highlighted with a specified
+		syntax to be treated as representing a particular programming
+		language.  The dictionary key is a Vim syntax name (as reported by
+		":set syntax?") and the dictionary value is the "suffix" defined for
+		that language (the bit in between "types_" and ".taghl" in the
+		generated type highlighter file).  In most cases, you probably want to
+		use |TagHL-FileTypeLanguageOverrides|.
+
+		To disable a syntax type from matching against a language, set the
+		dictionary value to an empty string ("").
+
+		This override will only work if 'Syntax' is in the list of
+		|TagHL-LanguageDetectionMethods|.
+
 	TagFileDirModePriority               *TagHL-TagFileDirModePriority*
 		This option is used to determine which directories to look in for a
 		tag file or in which directory to place a tag file when generating new
 ==============================================================================
 6. TagHighlight History                *TagHighlight-history*               {{{1
 
+2.1.2:  xxxx xxxxxx 2011   : Allow overriding the file-language relationship
+                             without having to edit the language definitions.
+
 2.1.1:  23rd August 2011   : Addition of LanguageDetectionMethods option to
                              allow loading of types when FileType or Syntax
                              are set (thanks to Hong Xu for the suggestion).

File plugin/TagHighlight/data/options.txt

 	Default:Extension
 	Help:Specify methods to use for determining language for the file when reading highlight definitions.
 
+filetype_language_overrides:
+	VimOptionMap:FileTypeLanguageOverrides
+	Type:dict
+	Default:{}
+	Help:Match file types to languages manually (if FileType language detection method is enabled).
+
+extension_language_overrides:
+	VimOptionMap:ExtensionLanguageOverrides
+	Type:dict
+	Default:{}
+	Help:Match extensions to languages manually (if Extension language detection method is enabled).
+
+syntax_language_overrides:
+	VimOptionMap:SyntaxLanguageOverrides
+	Type:dict
+	Default:{}
+	Help:Match syntaxes to languages manually (if Syntax language detection method is enabled).
+
 disable_type_parsing:
 	VimOptionMap:DisableTypeParsing
 	Type:bool