QScintilla/Lexers/__init__.py

changeset 2217
e80c74f2a25a
parent 1509
c0b5e693b0eb
child 2218
1940b0e0a992
equal deleted inserted replaced
2216:3372cf8877c0 2217:e80c74f2a25a
10 from PyQt4.QtGui import QApplication 10 from PyQt4.QtGui import QApplication
11 11
12 from QScintilla.QsciScintillaCompat import QSCINTILLA_VERSION 12 from QScintilla.QsciScintillaCompat import QSCINTILLA_VERSION
13 13
14 import Preferences 14 import Preferences
15 import UI.PixmapCache
15 16
16 # The lexer registry 17 # The lexer registry
17 # Dictionary with the language name as key. Each entry is a list with 18 # Dictionary with the language name as key. Each entry is a list with
18 # 0. display string (string) 19 # 0. display string (string)
19 # 1. dummy filename to derive lexer name (string) 20 # 1. dummy filename to derive lexer name (string)
21 # This function must take a reference to the parent as argument. 22 # This function must take a reference to the parent as argument.
22 # 3. list of open file filters (list of strings) 23 # 3. list of open file filters (list of strings)
23 # 4. list of save file filters (list of strings) 24 # 4. list of save file filters (list of strings)
24 # 5. default lexer associations (list of strings of filename wildcard patterns 25 # 5. default lexer associations (list of strings of filename wildcard patterns
25 # to be associated with the lexer) 26 # to be associated with the lexer)
27 # 6. name of an icon file (string)
26 LexerRegistry = {} 28 LexerRegistry = {}
27 29
28 30
29 def registerLexer(name, displayString, filenameSample, getLexerFunc, 31 def registerLexer(name, displayString, filenameSample, getLexerFunc,
30 openFilters=[], saveFilters=[], 32 openFilters=[], saveFilters=[],
31 defaultAssocs=[]): 33 defaultAssocs=[], iconFileName=""):
32 """ 34 """
33 Module function to register a custom QScintilla lexer. 35 Module function to register a custom QScintilla lexer.
34 36
35 @param name lexer language name (string) 37 @param name lexer language name (string)
36 @param displayString display string (string) 38 @param displayString display string (string)
39 This function must take a reference to the parent as it's only argument. 41 This function must take a reference to the parent as it's only argument.
40 @keyparam openFilters list of open file filters (list of strings) 42 @keyparam openFilters list of open file filters (list of strings)
41 @keyparam saveFilters list of save file filters (list of strings) 43 @keyparam saveFilters list of save file filters (list of strings)
42 @keyparam defaultAssocs default lexer associations (list of strings of filename 44 @keyparam defaultAssocs default lexer associations (list of strings of filename
43 wildcard patterns to be associated with the lexer) 45 wildcard patterns to be associated with the lexer)
46 @keyparam iconFileName name of an icon file (string)
44 @exception KeyError raised when the given name is already in use 47 @exception KeyError raised when the given name is already in use
45 """ 48 """
46 global LexerRegistry 49 global LexerRegistry
47 if name in LexerRegistry: 50 if name in LexerRegistry:
48 raise KeyError('Lexer "{0}" already registered.'.format(name)) 51 raise KeyError('Lexer "{0}" already registered.'.format(name))
49 else: 52 else:
50 LexerRegistry[name] = [displayString, filenameSample, getLexerFunc, 53 LexerRegistry[name] = [displayString, filenameSample, getLexerFunc,
51 openFilters, saveFilters, defaultAssocs[:]] 54 openFilters, saveFilters, defaultAssocs[:],
55 iconFileName]
52 56
53 57
54 def unregisterLexer(name): 58 def unregisterLexer(name):
55 """ 59 """
56 Module function to unregister a custom QScintilla lexer. 60 Module function to unregister a custom QScintilla lexer.
66 Module function to get a dictionary of supported lexer languages. 70 Module function to get a dictionary of supported lexer languages.
67 71
68 @return dictionary of supported lexer languages. The keys are the 72 @return dictionary of supported lexer languages. The keys are the
69 internal language names. The items are lists of two entries. 73 internal language names. The items are lists of two entries.
70 The first is the display string for the language, the second 74 The first is the display string for the language, the second
71 is a dummy file name, which can be used to derive the lexer. 75 is a dummy file name, which can be used to derive the lexer, and
72 (string, string) 76 the third is the name of an icon file.
77 (string, string, string)
73 """ 78 """
74 supportedLanguages = { 79 supportedLanguages = {
75 "Bash": [QApplication.translate('Lexers', "Bash"), 'dummy.sh'], 80 "Bash": [QApplication.translate('Lexers', "Bash"), 'dummy.sh',
76 "Batch": [QApplication.translate('Lexers', "Batch"), 'dummy.bat'], 81 "lexerBash.png"],
77 "C++": [QApplication.translate('Lexers', "C/C++"), 'dummy.cpp'], 82 "Batch": [QApplication.translate('Lexers', "Batch"), 'dummy.bat',
78 "C#": [QApplication.translate('Lexers', "C#"), 'dummy.cs'], 83 "lexerBatch.png"],
79 "CMake": [QApplication.translate('Lexers', "CMake"), 'dummy.cmake'], 84 "C++": [QApplication.translate('Lexers', "C/C++"), 'dummy.cpp',
80 "CSS": [QApplication.translate('Lexers', "CSS"), 'dummy.css'], 85 "lexerCPP.png"],
81 "D": [QApplication.translate('Lexers', "D"), 'dummy.d'], 86 "C#": [QApplication.translate('Lexers', "C#"), 'dummy.cs',
82 "Diff": [QApplication.translate('Lexers', "Diff"), 'dummy.diff'], 87 "lexerCsharp.png"],
83 "Fortran": [QApplication.translate('Lexers', "Fortran"), 'dummy.f95'], 88 "CMake": [QApplication.translate('Lexers', "CMake"), 'dummy.cmake',
84 "Fortran77": [QApplication.translate('Lexers', "Fortran77"), 'dummy.f'], 89 "lexerCMake.png"],
85 "HTML": [QApplication.translate('Lexers', "HTML/PHP/XML"), 'dummy.html'], 90 "CSS": [QApplication.translate('Lexers', "CSS"), 'dummy.css',
86 "IDL": [QApplication.translate('Lexers', "IDL"), 'dummy.idl'], 91 "lexerCSS.png"],
87 "Java": [QApplication.translate('Lexers', "Java"), 'dummy.java'], 92 "D": [QApplication.translate('Lexers', "D"), 'dummy.d',
88 "JavaScript": [QApplication.translate('Lexers', "JavaScript"), 'dummy.js'], 93 "lexerD.png"],
89 "Lua": [QApplication.translate('Lexers', "Lua"), 'dummy.lua'], 94 "Diff": [QApplication.translate('Lexers', "Diff"), 'dummy.diff',
90 "Makefile": [QApplication.translate('Lexers', "Makefile"), 'dummy.mak'], 95 "lexerDiff.png"],
91 "Pascal": [QApplication.translate('Lexers', "Pascal"), 'dummy.pas'], 96 "Fortran": [QApplication.translate('Lexers', "Fortran"), 'dummy.f95',
92 "Perl": [QApplication.translate('Lexers', "Perl"), 'dummy.pl'], 97 "lexerFortran.png"],
93 "PostScript": [QApplication.translate('Lexers', "PostScript"), 'dummy.ps'], 98 "Fortran77": [QApplication.translate('Lexers', "Fortran77"), 'dummy.f',
94 "Povray": [QApplication.translate('Lexers', "Povray"), 'dummy.pov'], 99 "lexerFortran.png"],
95 "Properties": [QApplication.translate('Lexers', "Properties"), 'dummy.ini'], 100 "HTML": [QApplication.translate('Lexers', "HTML/PHP/XML"), 'dummy.html',
96 "Python2": [QApplication.translate('Lexers', "Python2"), 'dummy.py'], 101 "lexerHTML.png"],
97 "Python3": [QApplication.translate('Lexers', "Python3"), 'dummy.py'], 102 "IDL": [QApplication.translate('Lexers', "IDL"), 'dummy.idl',
98 "Ruby": [QApplication.translate('Lexers', "Ruby"), 'dummy.rb'], 103 "lexerIDL.png"],
99 "SQL": [QApplication.translate('Lexers', "SQL"), 'dummy.sql'], 104 "Java": [QApplication.translate('Lexers', "Java"), 'dummy.java',
100 "TCL": [QApplication.translate('Lexers', "TCL"), 'dummy.tcl'], 105 "lexerJava.png"],
101 "TeX": [QApplication.translate('Lexers', "TeX"), 'dummy.tex'], 106 "JavaScript": [QApplication.translate('Lexers', "JavaScript"), 'dummy.js',
102 "VHDL": [QApplication.translate('Lexers', "VHDL"), 'dummy.vhd'], 107 "lexerJavaScript.png"],
103 "XML": [QApplication.translate('Lexers', "XML"), 'dummy.xml'], 108 "Lua": [QApplication.translate('Lexers', "Lua"), 'dummy.lua',
104 "YAML": [QApplication.translate('Lexers', "YAML"), 'dummy.yml'], 109 "lexerLua.png"],
110 "Makefile": [QApplication.translate('Lexers', "Makefile"), 'dummy.mak',
111 "lexerMakefile.png"],
112 "Pascal": [QApplication.translate('Lexers', "Pascal"), 'dummy.pas',
113 "lexerPascal.png"],
114 "Perl": [QApplication.translate('Lexers', "Perl"), 'dummy.pl',
115 "lexerPerl.png"],
116 "PostScript": [QApplication.translate('Lexers', "PostScript"), 'dummy.ps',
117 "lexerPostscript.png"],
118 "Povray": [QApplication.translate('Lexers', "Povray"), 'dummy.pov',
119 "lexerPOV.png"],
120 "Properties": [QApplication.translate('Lexers', "Properties"), 'dummy.ini',
121 "lexerProperties.png"],
122 "Python2": [QApplication.translate('Lexers', "Python2"), 'dummy.py',
123 "lexerPython.png"],
124 "Python3": [QApplication.translate('Lexers', "Python3"), 'dummy.py',
125 "lexerPython.png"],
126 "Ruby": [QApplication.translate('Lexers', "Ruby"), 'dummy.rb',
127 "lexerRuby.png"],
128 "SQL": [QApplication.translate('Lexers', "SQL"), 'dummy.sql',
129 "lexerSQL.png"],
130 "TCL": [QApplication.translate('Lexers', "TCL"), 'dummy.tcl',
131 "lexerTCL.png"],
132 "TeX": [QApplication.translate('Lexers', "TeX"), 'dummy.tex',
133 "lexerTeX.png"],
134 "VHDL": [QApplication.translate('Lexers', "VHDL"), 'dummy.vhd',
135 "lexerVHDL.png"],
136 "XML": [QApplication.translate('Lexers', "XML"), 'dummy.xml',
137 "lexerXML.png"],
138 "YAML": [QApplication.translate('Lexers', "YAML"), 'dummy.yml',
139 "lexerYAML.png"],
105 } 140 }
106 141
107 if QSCINTILLA_VERSION() >= 0x020501: 142 if QSCINTILLA_VERSION() >= 0x020501:
108 supportedLanguages.update({ 143 supportedLanguages.update({
109 "Matlab": [QApplication.translate('Lexers', "Matlab"), 'dummy.m.matlab'], 144 "Matlab": [QApplication.translate('Lexers', "Matlab"), 'dummy.m.matlab',
110 "Octave": [QApplication.translate('Lexers', "Octave"), 'dummy.m.octave'], 145 "lexerMatlab.png"],
146 "Octave": [QApplication.translate('Lexers', "Octave"), 'dummy.m.octave',
147 "lexerOctave.png"],
111 }) 148 })
112 149
113 for name in LexerRegistry: 150 for name in LexerRegistry:
114 supportedLanguages[name] = LexerRegistry[name][:2] 151 supportedLanguages[name] = LexerRegistry[name][:2] + [LexerRegistry[name][6]]
115 152
116 supportedLanguages["Guessed"] = \ 153 supportedLanguages["Guessed"] = \
117 [QApplication.translate('Lexers', "Pygments"), 'dummy.pygments'] 154 [QApplication.translate('Lexers', "Pygments"), 'dummy.pygments', ""]
118 155
119 return supportedLanguages 156 return supportedLanguages
157
158
159 def getLanguageIcon(language, pixmap):
160 """
161 Module function to get an icon for a language.
162
163 @param language language of the lexer (string)
164 @param pixmap flag indicating to return a pixmap (boolean)
165 @return icon for the language (QPixmap or QIcon)
166 """
167 supportedLanguages = getSupportedLanguages()
168 if language in supportedLanguages:
169 iconFileName = supportedLanguages[language][2]
170 else:
171 iconFileName = ""
172 if pixmap:
173 return UI.PixmapCache.getPixmap(iconFileName)
174 else:
175 return UI.PixmapCache.getIcon(iconFileName)
120 176
121 177
122 def getLexer(language, parent=None, pyname=""): 178 def getLexer(language, parent=None, pyname=""):
123 """ 179 """
124 Module function to instantiate a lexer object for a given language. 180 Module function to instantiate a lexer object for a given language.

eric ide

mercurial