3 # Author: A. S. Budden <abudden _at_ gmail _dot_ com>
4 # Copyright: Copyright (C) 2009-2011 A. S. Budden
5 # Permission is hereby granted to use and distribute this code,
6 # with or without modifications, provided that this copyright
7 # notice is copied with it. Like anything else that's free,
8 # the TagHighlight plugin is provided *as is* and comes with no
9 # warranty of any kind, either expressed or implied. By using
10 # this plugin, you agree that in no event will the copyright
11 # holder be liable for any damages resulting from the use
14 # ---------------------------------------------------------------------
18 from .config import config
19 from .loaddata import LoadDataFile, LoadFile, GlobData
20 from .debug import Debug
25 def __init__(self, options):
26 self.options = options
29 language_list_entries = ['SkipList','Priority']
31 # Import language specific modules: this will make them be parsed
32 # and will add to the registry
33 self.defaults = LoadDataFile('language_defaults.txt', language_list_entries)
35 for language_file in GlobData('languages/*.txt'):
36 language_dict = LoadDataFile(language_file, language_list_entries)
37 language_dict['Filename'] = language_file
38 if 'ReservedKeywords' in language_dict:
39 # This is some weird python magic that takes a list of
40 # strings containing space-separated items and produces
41 # a single list of those items.
42 language_dict['ReservedKeywords'] = \
43 [item for sublist in language_dict['ReservedKeywords'] for item in sublist.split(' ')]
45 language_dict['ReservedKeywords'] = []
46 language_dict = self.VerifyLanguage(language_dict)
47 self.registry[language_dict['FriendlyName']] = language_dict
49 def ReadConfigFile(self, filename):
51 fh = open(filename, 'r')
52 list_entries = ['SkipList','Priority']
55 if line.strip().endswith(':') and line[0] not in [' ','\t',':','#']:
56 key = line.strip()[:-1]
58 elif key is not None and line.startswith('\t'):
59 result[key] += [line.strip()]
60 elif ':' in line and line[0] not in [' ','\t',':','#']:
61 # End of the previous list, so reset key
63 parts = line.strip().split(':',1)
64 if parts[0] in list_entries:
66 result[parts[0]] = parts[1].split(',')
68 result[parts[0]] = [parts[1]]
70 result[parts[0]] = parts[1]
74 def VerifyLanguage(self, language_dict):
78 'PythonExtensionMatcher',
79 'VimExtensionMatcher',
85 for key in required_keys:
86 if key not in language_dict:
87 if key in self.defaults:
88 language_dict[key] = self.defaults[key]
90 raise Exception("Language data from file {filename} is " \
91 "missing required key {key} (no default " \
92 "available).".format(filename=language_dict['Filename'],
96 def GetAllLanguages(self):
97 return list(self.registry.keys())
99 def GetAllLanguageHandlers(self):
100 return list(self.registry.values())
102 def GetLanguageHandler(self, name):
103 return self.registry[name]
105 def GenerateExtensionTable(self):
107 for handler in list(self.registry.values()):
108 extensions = handler.GetVimMatcher()
109 suffix = handler.GetSuffix()
110 results[extensions] = suffix
113 def GenerateFullKindList(self):
116 for language in list(self.kinds.keys()):
117 kinds |= set(self.kinds[language].values())
118 return sorted(list(kinds))
120 def GetKindList(self, language=None):
121 """Explicit list of kinds exported from ctags help."""
122 if self.kinds is None:
123 kind_import = LoadDataFile('kinds.txt')
124 # Generate the kind database with 'ctags_' prefix on the keys
126 for key in kind_import:
128 for kind in kind_import[key]:
129 self.kinds[key]['ctags_'+kind] = kind_import[key][kind]
133 elif language in self.kinds:
134 return self.kinds[language]