Plug-in docu updated, now the return values of the syntax checker is a dictionary BgService

Mon, 17 Mar 2014 20:32:35 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Mon, 17 Mar 2014 20:32:35 +0100
branch
BgService
changeset 3412
9364dab2d472
parent 3241
957673fc463a
child 3413
5e63f809732a

Plug-in docu updated, now the return values of the syntax checker is a dictionary
with the two keys 'error' and 'warnings'.

Documentation/eric5-plugin.odt file | annotate | diff | comparison | revisions
Documentation/eric5-plugin.pdf file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheck.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckService.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckerDialog.py file | annotate | diff | comparison | revisions
Plugins/PluginSyntaxChecker.py file | annotate | diff | comparison | revisions
QScintilla/Editor.py file | annotate | diff | comparison | revisions
Binary file Documentation/eric5-plugin.odt has changed
Binary file Documentation/eric5-plugin.pdf has changed
--- a/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheck.py	Fri Jan 31 22:11:45 2014 +0100
+++ b/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheck.py	Mon Mar 17 20:32:35 2014 +0100
@@ -71,23 +71,21 @@
     return flags
 
 
-def syntaxAndPyflakesCheck(filename, codestring="", checkFlakes=True,
+def syntaxAndPyflakesCheck(filename, codestring, checkFlakes=True,
                            ignoreStarImportWarnings=False):
     """
     Function to compile one Python source file to Python bytecode
     and to perform a pyflakes check.
     
     @param filename source filename (string)
-    @keyparam codestring string containing the code to compile (string)
+    @param codestring string containing the code to compile (string)
     @keyparam checkFlakes flag indicating to do a pyflakes check (boolean)
     @keyparam ignoreStarImportWarnings flag indicating to
         ignore 'star import' warnings (boolean)
-    @return A tuple indicating status (True = an error was found), the
-        file name, the line number, the index number, the code string
-        and the error message (boolean, string, string, string, string,
-        string). If checkFlakes is True, a list of strings containing the
-        warnings (marker, file name, line number, message)
-        The values are only valid, if the status is True.
+    @return dictionary with the keys 'error' and 'warnings' which
+            hold a list containing details about the error/ warnings
+            (file name, line number, column, codestring (only at syntax
+            errors), the message, a list with arguments for the message)
     """
     try:
         import builtins
@@ -100,7 +98,7 @@
         else:
             file_enc = filename
         
-        # It also encoded the code back to avoid 'Encoding declaration in
+        # It also encode the code back to avoid 'Encoding declaration in
         # unicode string' exception on Python2
         codestring = normalizeCode(codestring)
         
@@ -108,13 +106,13 @@
             try:
                 import quixote.ptl_compile
             except ImportError:
-                return (True, filename, 0, 0, '',
-                        'Quixote plugin not found.', [])
+                return [{'error': (filename, 0, 0, '',
+                        'Quixote plugin not found.')}]
             template = quixote.ptl_compile.Template(codestring, file_enc)
             template.compile()
-        
-        # ast.PyCF_ONLY_AST = 1024, speed optimisation
-        module = builtins.compile(codestring, file_enc, 'exec',  1024)
+        else:
+            # ast.PyCF_ONLY_AST = 1024, speed optimisation
+            module = builtins.compile(codestring, file_enc, 'exec', 1024)
     except SyntaxError as detail:
         index = 0
         code = ""
@@ -139,7 +137,7 @@
             fn = detail.filename
             line = detail.lineno or 1
             error = detail.msg
-        return (True, fn, int(line), index, code, error, [])
+        return [{'error': (fn, int(line), index, code.strip(), error)}]
     except ValueError as detail:
         try:
             fn = detail.filename
@@ -149,21 +147,21 @@
             fn = filename
             line = 1
             error = str(detail)
-        return (True, fn, line, 0, "", error, [])
+        return [{'error': (fn, line, 0, "", error)}]
     except Exception as detail:
         try:
             fn = detail.filename
             line = detail.lineno
             error = detail.msg
-            return (True, fn, line, 0, "", error, [])
+            return [{'error': (fn, line, 0, "", error)}]
         except:         # this catchall is intentional
             pass
     
     # pyflakes
     if not checkFlakes:
-        return (False, "", -1, -1, "", "", [])
+        return [{}]
     
-    strings = []
+    results = []
     lines = codestring.splitlines()
     try:
         warnings = Checker(module, filename)
@@ -176,13 +174,12 @@
             _fn, lineno, col, message, msg_args = warning.getMessageData()
             if "__IGNORE_WARNING__" not in extractLineFlags(
                     lines[lineno - 1].strip()):
-                strings.append([
-                    "FLAKES_WARNING", _fn, lineno, col, message, msg_args])
+                results.append((_fn, lineno, col, "", message, msg_args))
     except SyntaxError as err:
         if err.text.strip():
             msg = err.text.strip()
         else:
             msg = err.msg
-        strings.append(["FLAKES_ERROR", filename, err.lineno, 0, msg, ()])
+        results.append((filename, err.lineno, 0, "FLAKES_ERROR", msg, []))
     
-    return (False, "", -1, -1, "", "", strings)
+    return [{'warnings': results}]
--- a/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckService.py	Fri Jan 31 22:11:45 2014 +0100
+++ b/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckService.py	Mon Mar 17 20:32:35 2014 +0100
@@ -24,14 +24,14 @@
     Plugins can add other languages to the syntax check by calling addLanguage
     and support of an extra checker module on the client side which has to
     connect directly to the background service.
+    
+    @signal syntaxChecked(str, dict) emited when the syntax check was done.
     """
-    syntaxChecked = pyqtSignal(str, bool, int, int, str, str, list)
-
+    syntaxChecked = pyqtSignal(str, dict)
+    
     def __init__(self):
         """
         Contructor of SyntaxCheckService.
-        
-        @param backgroundService to connect to (BackgroundService class)
         """
         super(SyntaxCheckService, self).__init__()
         self.backgroundService = e5App().getObject("BackgroundService")
@@ -41,6 +41,8 @@
         """
         Private methode to determine the language of the file.
         
+        @param filename of the sourcefile (str)
+        @param source code of the file (str)
         @return language of the file or None if not found (str or None)
         """
         pyVer = determinePythonVersion(filename, source)
@@ -103,7 +105,7 @@
                 extensions.add(ext)
         return extensions
 
-    def syntaxCheck(self, lang, filename, source=""):
+    def syntaxCheck(self, lang, filename, source):
         """
         Method to prepare to compile one Python source file to Python bytecode
         and to perform a pyflakes check in another task.
@@ -111,7 +113,7 @@
         @param lang language of the file or None to determine by internal
             algorithm (str or None)
         @param filename source filename (string)
-        @keyparam source string containing the code to check (string)
+        @param source string containing the code to check (string)
         """
         if not lang:
             lang = self.__determineLanguage(filename, source)
--- a/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckerDialog.py	Fri Jan 31 22:11:45 2014 +0100
+++ b/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckerDialog.py	Mon Mar 17 20:32:35 2014 +0100
@@ -67,6 +67,7 @@
         
         self.syntaxCheckService = e5App().getObject('SyntaxCheckService')
         self.syntaxCheckService.syntaxChecked.connect(self.__processResult)
+        self.filename = None
         
     def __resort(self):
         """
@@ -215,38 +216,34 @@
         
         self.syntaxCheckService.syntaxCheck(None, self.filename, self.source)
 
-    def __processResult(
-            self, fn, nok, line, index, code, error, warnings):
+    def __processResult(self, fn, problems):
         """
         Slot to display the reported messages.
         
-        If checkFlakes is True, warnings contains a list of strings containing
-        the warnings (marker, file name, line number, message)
-        The values are only valid, if nok is False.
-        
         @param fn filename of the checked file (str)
-        @param nok flag if an error in the source was found (boolean)
-        @param line number where the error occured (int)
-        @param index the column where the error occured (int)
-        @param code the part of the code where the error occured (str)
-        @param error the name of the error (str)
-        @param warnings a list of strings containing the warnings
-            (marker, file name, line number, col, message)
+        @param problems dictionary with the keys 'error' and 'warnings' which
+            hold a list containing details about the error/ warnings
+            (file name, line number, column, codestring (only at syntax
+            errors), the message) (dict)
         """
         # Check if it's the requested file, otherwise ignore signal
         if fn != self.filename:
             return
-        
-        if nok:
+
+        error = problems.get('error')
+        if error:
             self.noResults = False
-            self.__createResultItem(
-                fn, line, index, error, code.strip(), False)
-        else:
+            _fn, lineno, col, code, msg = error
+            self.__createResultItem(_fn, lineno, col, msg, code, False)
+        
+        warnings = problems.get('warnings', [])
+        if warnings:
             source = self.source.splitlines()
-            for marker, _fn, lineno, col, msg in warnings:
-                self.noResults = False
-                scr_line = source[lineno - 1].strip()
-                self.__createResultItem(_fn, lineno, col, msg, scr_line, True)
+        for _fn, lineno, col, code, msg in warnings:
+            self.noResults = False
+            scr_line = source[lineno - 1].strip()
+            self.__createResultItem(_fn, lineno, col, msg, scr_line, True)
+
         self.progress += 1
         self.checkProgress.setValue(self.progress)
         QApplication.processEvents()
@@ -341,7 +338,7 @@
             editor = vm.getOpenEditor(fn)
             
             if itm.data(0, self.warningRole):
-                editor.toggleWarning(lineno, True, error)
+                editor.toggleWarning(lineno, 0, True, error)
             else:
                 editor.toggleSyntaxError(lineno, index, True, error, show=True)
         else:
@@ -355,7 +352,7 @@
                 index = citm.data(0, self.indexRole)
                 error = citm.data(0, self.errorRole)
                 if citm.data(0, self.warningRole):
-                    editor.toggleWarning(lineno, True, error)
+                    editor.toggleWarning(lineno, 0, True, error)
                 else:
                     editor.toggleSyntaxError(
                         lineno, index, True, error, show=True)
@@ -386,7 +383,7 @@
                 index = citm.data(0, self.indexRole)
                 error = citm.data(0, self.errorRole)
                 if citm.data(0, self.warningRole):
-                    editor.toggleWarning(lineno, True, error)
+                    editor.toggleWarning(lineno, 0, True, error)
                 else:
                     editor.toggleSyntaxError(
                         lineno, index, True, error, show=True)
--- a/Plugins/PluginSyntaxChecker.py	Fri Jan 31 22:11:45 2014 +0100
+++ b/Plugins/PluginSyntaxChecker.py	Mon Mar 17 20:32:35 2014 +0100
@@ -61,21 +61,23 @@
         path = os.path.join(ericPath, 'Plugins', 'CheckerPlugins',
                             'SyntaxChecker')
         
-        self.syntaxCheckService.addLanguage('Python2', path, 'SyntaxCheck',
-                self.__getPythonOptions,
-                lambda: Preferences.getPython("PythonExtensions"),
-                self.__translateSyntaxCheck,
-                lambda fx, lng, fn, msg: \
-                    self.syntaxCheckService.syntaxChecked.emit(
-                        fn, True, fn, 0, 0, '', msg, []))
+        self.syntaxCheckService.addLanguage(
+            'Python2', path, 'SyntaxCheck',
+            self.__getPythonOptions,
+            lambda: Preferences.getPython("PythonExtensions"),
+            self.__translateSyntaxCheck,
+            lambda fx, lng, fn, msg:
+                self.syntaxCheckService.syntaxChecked.emit(
+                    fn, {'error': (fn, 0, 0, '', msg)}))
         
-        self.syntaxCheckService.addLanguage('Python3', path, 'SyntaxCheck',
-                self.__getPythonOptions,
-                lambda: Preferences.getPython("Python3Extensions"),
-                self.__translateSyntaxCheck,
-                lambda fx, lng, fn, msg: \
-                    self.syntaxCheckService.syntaxChecked.emit(
-                        fn, True, fn, 0, 0, '', msg, []))
+        self.syntaxCheckService.addLanguage(
+            'Python3', path, 'SyntaxCheck',
+            self.__getPythonOptions,
+            lambda: Preferences.getPython("Python3Extensions"),
+            self.__translateSyntaxCheck,
+            lambda fx, lng, fn, msg:
+                self.syntaxCheckService.syntaxChecked.emit(
+                    fn, {'error': (fn, 0, 0, '', msg)}))
 
     def __initialize(self):
         """
@@ -103,8 +105,7 @@
             "IgnoreStarImportWarnings")
         return checkFlakes, ignoreStarImportWarnings
 
-    def __translateSyntaxCheck(
-            self, fn, nok, fname, line, index, code, error, warnings):
+    def __translateSyntaxCheck(self, fn, problems):
         """
         Slot to translate the resulting messages.
         
@@ -113,15 +114,12 @@
         The values are only valid, if nok is False.
         
         @param fn filename of the checked file (str)
-        @param nok flag if an error in the source was found (boolean)
-        @param fname filename of the checked file (str)  # TODO: remove dubl.
-        @param line number where the error occured (int)
-        @param index the column where the error occured (int)
-        @param code the part of the code where the error occured (str)
-        @param error the name of the error (str)
-        @param warnings a list of strings containing the warnings
-            (marker, file name, line number, col, message, list(msg_args))
+        @param problems dictionary with the keys 'error' and 'warnings' which
+            hold a list containing details about the error/ warnings
+            (file name, line number, column, codestring (only at syntax
+            errors), the message, a list with arguments for the message)
         """
+        warnings = problems.get('warnings', [])
         for warning in warnings:
             # Translate messages
             msg_args = warning.pop()
@@ -132,8 +130,8 @@
                 translated = translated[1:]
             warning[4] = translated.replace(" u'", " '")
         
-        self.syntaxCheckService.syntaxChecked.emit(
-            fn, nok, line, index, code, error, warnings)
+        problems['warnings'] = warnings
+        self.syntaxCheckService.syntaxChecked.emit(fn, problems)
 
     def activate(self):
         """
--- a/QScintilla/Editor.py	Fri Jan 31 22:11:45 2014 +0100
+++ b/QScintilla/Editor.py	Mon Mar 17 20:32:35 2014 +0100
@@ -4972,23 +4972,15 @@
             self.syntaxCheckService.syntaxCheck(
                 self.filetype, self.fileName or "(Unnamed)", self.text())
 
-    def __processResult(
-            self, fn, nok, line, index, code, error, warnings):
+    def __processResult(self, fn, problems):
         """
         Slot to report the resulting messages.
         
-        If checkFlakes is True, warnings contains a list of strings containing
-        the warnings (marker, file name, line number, message)
-        The values are only valid, if nok is False.
-        
         @param fn filename of the checked file (str)
-        @param nok flag if an error in the source was found (boolean)
-        @param line number where the error occured (int)
-        @param index the column where the error occured (int)
-        @param code the part of the code where the error occured (str)
-        @param error the name of the error (str)
-        @param warnings a list of strings containing the warnings
-            (marker, file name, line number, col, message)
+        @param problems dictionary with the keys 'error' and 'warnings' which
+            hold a list containing details about the error/ warnings
+            (file name, line number, column, codestring (only at syntax
+            errors), the message) (dict)
         """
         # Check if it's the requested file, otherwise ignore signal
         if fn != self.fileName and (
@@ -4998,12 +4990,15 @@
         self.clearSyntaxError()
         self.clearFlakesWarnings()
         
-        if nok:
-            self.toggleSyntaxError(line, index, True, error)
-        else:
-            for marker, _fn, lineno, col, msg in warnings:
-                self.toggleWarning(lineno, True, msg)
-
+        error = problems.get('error')
+        if error:
+            _fn, lineno, col, code, msg = error
+            self.toggleSyntaxError(lineno, col, True, msg)
+        
+        warnings = problems.get('warnings', [])
+        for _fn, lineno, col, code, msg in warnings:
+            self.toggleWarning(lineno, col, True, msg)
+ 
     def __initOnlineSyntaxCheck(self):
         """
         Private slot to initialize the online syntax check.
@@ -5387,13 +5382,15 @@
     ## Warning handling methods below
     ###########################################################################
     
-    def toggleWarning(self, line, warning, msg="", warningType=WarningCode):
+    def toggleWarning(
+            self, line, col, warning, msg="", warningType=WarningCode):
         """
         Public method to toggle a warning indicator.
         
         Note: This method is used to set pyflakes and code style warnings.
         
         @param line line number of the warning
+        @param col column of the warning
         @param warning flag indicating if the warning marker should be
             set or deleted (boolean)
         @param msg warning message (string)

eric ide

mercurial