Plugins/PluginSyntaxChecker.py

branch
BgService
changeset 3241
957673fc463a
parent 3145
a9de05d4a22f
child 3412
9364dab2d472
equal deleted inserted replaced
3228:f489068e51e8 3241:957673fc463a
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 import os 12 import os
13 13
14 from PyQt4.QtCore import QObject 14 from PyQt4.QtCore import QObject
15 15 from PyQt4.QtGui import QApplication
16
17 from E5Gui.E5Action import E5Action
16 from E5Gui.E5Application import e5App 18 from E5Gui.E5Application import e5App
17 19 from eric5config import getConfig
18 from E5Gui.E5Action import E5Action
19 20
20 import Preferences 21 import Preferences
21 22
22 # Start-Of-Header 23 # Start-Of-Header
23 name = "Syntax Checker Plugin" 24 name = "Syntax Checker Plugin"
49 """ 50 """
50 super(SyntaxCheckerPlugin, self).__init__(ui) 51 super(SyntaxCheckerPlugin, self).__init__(ui)
51 self.__ui = ui 52 self.__ui = ui
52 self.__initialize() 53 self.__initialize()
53 54
55 from Plugins.CheckerPlugins.SyntaxChecker.SyntaxCheckService import \
56 SyntaxCheckService
57 self.syntaxCheckService = SyntaxCheckService()
58 e5App().registerObject("SyntaxCheckService", self.syntaxCheckService)
59
60 ericPath = getConfig('ericDir')
61 path = os.path.join(ericPath, 'Plugins', 'CheckerPlugins',
62 'SyntaxChecker')
63
64 self.syntaxCheckService.addLanguage('Python2', path, 'SyntaxCheck',
65 self.__getPythonOptions,
66 lambda: Preferences.getPython("PythonExtensions"),
67 self.__translateSyntaxCheck,
68 lambda fx, lng, fn, msg: \
69 self.syntaxCheckService.syntaxChecked.emit(
70 fn, True, fn, 0, 0, '', msg, []))
71
72 self.syntaxCheckService.addLanguage('Python3', path, 'SyntaxCheck',
73 self.__getPythonOptions,
74 lambda: Preferences.getPython("Python3Extensions"),
75 self.__translateSyntaxCheck,
76 lambda fx, lng, fn, msg: \
77 self.syntaxCheckService.syntaxChecked.emit(
78 fn, True, fn, 0, 0, '', msg, []))
79
54 def __initialize(self): 80 def __initialize(self):
55 """ 81 """
56 Private slot to (re)initialize the plugin. 82 Private slot to (re)initialize the plugin.
57 """ 83 """
58 self.__projectAct = None 84 self.__projectAct = None
63 self.__projectBrowserSyntaxCheckerDialog = None 89 self.__projectBrowserSyntaxCheckerDialog = None
64 90
65 self.__editors = [] 91 self.__editors = []
66 self.__editorAct = None 92 self.__editorAct = None
67 self.__editorSyntaxCheckerDialog = None 93 self.__editorSyntaxCheckerDialog = None
94
95 def __getPythonOptions(self):
96 """
97 Private methode to determine the syntax check options.
98
99 @return state of checkFlakes and ignoreStarImportWarnings (bool, bool)
100 """
101 checkFlakes = Preferences.getFlakes("IncludeInSyntaxCheck")
102 ignoreStarImportWarnings = Preferences.getFlakes(
103 "IgnoreStarImportWarnings")
104 return checkFlakes, ignoreStarImportWarnings
105
106 def __translateSyntaxCheck(
107 self, fn, nok, fname, line, index, code, error, warnings):
108 """
109 Slot to translate the resulting messages.
110
111 If checkFlakes is True, warnings contains a list of strings containing
112 the warnings (marker, file name, line number, message)
113 The values are only valid, if nok is False.
114
115 @param fn filename of the checked file (str)
116 @param nok flag if an error in the source was found (boolean)
117 @param fname filename of the checked file (str) # TODO: remove dubl.
118 @param line number where the error occured (int)
119 @param index the column where the error occured (int)
120 @param code the part of the code where the error occured (str)
121 @param error the name of the error (str)
122 @param warnings a list of strings containing the warnings
123 (marker, file name, line number, col, message, list(msg_args))
124 """
125 for warning in warnings:
126 # Translate messages
127 msg_args = warning.pop()
128 translated = QApplication.translate(
129 'py3Flakes', warning[4]).format(*msg_args)
130 # Avoid leading "u" at Python2 unicode strings
131 if translated.startswith("u'"):
132 translated = translated[1:]
133 warning[4] = translated.replace(" u'", " '")
134
135 self.syntaxCheckService.syntaxChecked.emit(
136 fn, nok, line, index, code, error, warnings)
68 137
69 def activate(self): 138 def activate(self):
70 """ 139 """
71 Public method to activate this plugin. 140 Public method to activate this plugin.
72 141
150 @param menu reference to the menu (QMenu) 219 @param menu reference to the menu (QMenu)
151 """ 220 """
152 if menuName == "Checks" and self.__projectAct is not None: 221 if menuName == "Checks" and self.__projectAct is not None:
153 self.__projectAct.setEnabled( 222 self.__projectAct.setEnabled(
154 e5App().getObject("Project").getProjectLanguage() in 223 e5App().getObject("Project").getProjectLanguage() in
155 ["Python3", "Python2", "Python"]) 224 self.syntaxCheckService.getLanguages())
156 225
157 def __projectBrowserShowMenu(self, menuName, menu): 226 def __projectBrowserShowMenu(self, menuName, menu):
158 """ 227 """
159 Private slot called, when the the project browser menu or a submenu is 228 Private slot called, when the the project browser menu or a submenu is
160 about to be shown. 229 about to be shown.
162 @param menuName name of the menu to be shown (string) 231 @param menuName name of the menu to be shown (string)
163 @param menu reference to the menu (QMenu) 232 @param menu reference to the menu (QMenu)
164 """ 233 """
165 if menuName == "Checks" and \ 234 if menuName == "Checks" and \
166 e5App().getObject("Project").getProjectLanguage() in \ 235 e5App().getObject("Project").getProjectLanguage() in \
167 ["Python3", "Python2", "Python"]: 236 self.syntaxCheckService.getLanguages():
168 self.__projectBrowserMenu = menu 237 self.__projectBrowserMenu = menu
169 if self.__projectBrowserAct is None: 238 if self.__projectBrowserAct is None:
170 self.__projectBrowserAct = E5Action( 239 self.__projectBrowserAct = E5Action(
171 self.trUtf8('Check Syntax'), 240 self.trUtf8('Check Syntax'),
172 self.trUtf8('&Syntax...'), 0, 0, 241 self.trUtf8('&Syntax...'), 0, 0,
185 Public slot used to check the project files for syntax errors. 254 Public slot used to check the project files for syntax errors.
186 """ 255 """
187 project = e5App().getObject("Project") 256 project = e5App().getObject("Project")
188 project.saveAllScripts() 257 project.saveAllScripts()
189 ppath = project.getProjectPath() 258 ppath = project.getProjectPath()
259 extensions = tuple(self.syntaxCheckService.getExtensions())
190 files = [os.path.join(ppath, file) 260 files = [os.path.join(ppath, file)
191 for file in project.pdata["SOURCES"] 261 for file in project.pdata["SOURCES"]
192 if file.endswith( 262 if file.endswith(extensions)]
193 tuple(Preferences.getPython("Python3Extensions")) +
194 tuple(Preferences.getPython("PythonExtensions")))]
195 263
196 from CheckerPlugins.SyntaxChecker.SyntaxCheckerDialog import \ 264 from CheckerPlugins.SyntaxChecker.SyntaxCheckerDialog import \
197 SyntaxCheckerDialog 265 SyntaxCheckerDialog
198 self.__projectSyntaxCheckerDialog = SyntaxCheckerDialog() 266 self.__projectSyntaxCheckerDialog = SyntaxCheckerDialog()
199 self.__projectSyntaxCheckerDialog.show() 267 self.__projectSyntaxCheckerDialog.show()
252 """ 320 """
253 if menuName == "Checks": 321 if menuName == "Checks":
254 if not self.__editorAct in menu.actions(): 322 if not self.__editorAct in menu.actions():
255 menu.addAction(self.__editorAct) 323 menu.addAction(self.__editorAct)
256 self.__editorAct.setEnabled( 324 self.__editorAct.setEnabled(
257 editor.isPy3File() or editor.isPy2File()) 325 editor.getLanguage() in self.syntaxCheckService.getLanguages())
258 326
259 def __editorSyntaxCheck(self): 327 def __editorSyntaxCheck(self):
260 """ 328 """
261 Private slot to handle the syntax check context menu action of the 329 Private slot to handle the syntax check context menu action of the
262 editors. 330 editors.

eric ide

mercurial