Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py

changeset 6264
04a671fa4adb
parent 6188
5a6ae3be31e6
child 6265
56bd09c4c297
diff -r 4dd53711d869 -r 04a671fa4adb Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py
--- a/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py	Sat Apr 21 17:11:05 2018 +0200
+++ b/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py	Sat Apr 21 17:43:41 2018 +0200
@@ -36,8 +36,9 @@
     "E224", "E225", "E226", "E227", "E228", "E231",
     "E241", "E242", "E251", "E261", "E262", "E271",
     "E272", "E273", "E274", "E301", "E302", "E303",
-    "E304", "E401", "E501", "E502", "E701", "E702",
-    "E703", "E711", "E712",
+    "E304", "E305", "E306", "E307", "E308", "E401",
+    "E501", "E502", "E701", "E702", "E703", "E711",
+    "E712",
     "N804", "N805", "N806",
     "W191", "W291", "W292", "W293", "W391", "W603",
 ]
@@ -48,22 +49,33 @@
     Class implementing a fixer for certain code style issues.
     """
     def __init__(self, filename, sourceLines, fixCodes, noFixCodes,
-                 maxLineLength, inPlace, eol, backup=False):
+                 maxLineLength, blankLines, inPlace, eol, backup=False):
         """
         Constructor
         
-        @param filename name of the file to be fixed (string)
+        @param filename name of the file to be fixed
+        @type str
         @param sourceLines list of source lines including eol marker
-            (list of string)
+        @type list of str
         @param fixCodes list of codes to be fixed as a comma separated
-            string (string)
+            string
+        @type str
         @param noFixCodes list of codes not to be fixed as a comma
-            separated string (string)
-        @param maxLineLength maximum allowed line length (integer)
-        @param inPlace flag indicating to modify the file in place (boolean)
-        @param eol end of line character(s) (string)
+            separated string
+        @type str
+        @param maxLineLength maximum allowed line length
+        @type int
+        @param blanklines tuple containg the number of blank lines before
+            a top level class or function and before a method or nested class
+            or function
+        @type tuple of (int, int)
+        @param inPlace flag indicating to modify the file in place
+        @type bool
+        @param eol end of line character(s)
+        @type str
         @param backup flag indicating to create a backup before fixing
-            anything (boolean)
+            anything
+        @type bool
         """
         super(CodeStyleFixer, self).__init__()
         
@@ -74,6 +86,10 @@
         self.__noFixCodes = [
             c.strip() for c in noFixCodes.split(",") if c.strip()]
         self.__maxLineLength = maxLineLength
+        self.__blankLines = {
+            "toplevel": blankLines[0],
+            "method": blankLines[1],
+        }
         self.fixed = 0
         
         self.__reindenter = None
@@ -142,10 +158,14 @@
             "E272": self.__fixE221,
             "E273": self.__fixE221,
             "E274": self.__fixE221,
-            "E301": self.__fixE301,
-            "E302": self.__fixE302,
-            "E303": self.__fixE303,
+            "E301": self.__fixBlankLinesBefore,
+            "E302": self.__fixBlankLinesBefore,
+            "E303": self.__fixBlankLinesBefore,
             "E304": self.__fixE304,
+            "E305": self.__fixBlankLinesBefore,
+            "E306": self.__fixBlankLinesBefore,
+            "E307": self.__fixBlankLinesBefore,
+            "E308": self.__fixBlankLinesBefore,
             "E401": self.__fixE401,
             "E501": self.__fixE501,
             "E502": self.__fixE502,
@@ -1335,35 +1355,12 @@
         # Whitespace around comment sign corrected.
         return (1, "FE261", 0)
     
-    def __fixE301(self, code, line, pos, apply=False):
-        """
-        Private method to fix the need for one blank line.
-       
-        Codes: E301
-        
-        @param code code of the issue (string)
-        @param line line number of the issue (integer)
-        @param pos position inside line (integer)
-        @keyparam apply flag indicating, that the fix should be applied
-            (boolean)
-        @return value indicating an applied/deferred fix (-1, 0, 1),
-            a message for the fix (string) and an ID for a deferred
-            fix (integer)
+    def __fixBlankLinesBefore(self, code, line, pos, apply=False):
         """
-        if apply:
-            self.__source.insert(line - 1, self.__eol)
-            # One blank line inserted.
-            return (1, "FE301", 0)
-        else:
-            fixId = self.__getID()
-            self.__stack.append((fixId, code, line, pos))
-            return (-1, "", fixId)
-    
-    def __fixE302(self, code, line, pos, apply=False):
-        """
-        Private method to fix the need for two blank lines.
+        Private method to fix the need for blank lines before class, function
+        and method definitions.
        
-        Codes: E302
+        Codes: E301, E302, E303, E305, E306, E307, E308
         
         @param code code of the issue (string)
         @param line line number of the issue (integer)
@@ -1375,6 +1372,13 @@
             fix (integer)
         """
         if apply:
+            if code in ["E301", "E306", "E307"]:
+                blankLinesBefore = self.__blankLines["method"]
+            elif code == "E308":
+                blankLinesBefore = 1
+            else:
+                blankLinesBefore = self.__blankLines["toplevel"]
+            
             # count blank lines
             index = line - 1
             blanks = 0
@@ -1384,7 +1388,7 @@
                     index -= 1
                 else:
                     break
-            delta = blanks - 2
+            delta = blanks - blankLinesBefore
             
             line -= 1
             if delta < 0:
@@ -1393,7 +1397,7 @@
                     self.__source.insert(line, self.__eol)
                     delta += 1
                 # %n blank line(s) inserted.
-                return (1, ("FE302+", 2 - blanks), 0)
+                return (1, ("FE302+", blankLinesBefore - blanks), 0)
             elif delta > 0:
                 # delete superfluous blank lines
                 while delta > 0:
@@ -1401,7 +1405,7 @@
                     line -= 1
                     delta -= 1
                 # %n superfluous line(s) removed.
-                return (1, ("FE302-", blanks - 2), 0)
+                return (1, ("FE302-", blanks - blankLinesBefore), 0)
             else:
                 return (0, "", 0)
         else:
@@ -1409,36 +1413,6 @@
             self.__stack.append((fixId, code, line, pos))
             return (-1, "", fixId)
     
-    def __fixE303(self, code, line, pos, apply=False):
-        """
-        Private method to fix superfluous blank lines.
-       
-        Codes: E303
-        
-        @param code code of the issue (string)
-        @param line line number of the issue (integer)
-        @param pos position inside line (integer)
-        @keyparam apply flag indicating, that the fix should be applied
-            (boolean)
-        @return value indicating an applied/deferred fix (-1, 0, 1),
-            a message for the fix (string) and an ID for a deferred
-            fix (integer)
-        """
-        if apply:
-            index = line - 3
-            while index:
-                if self.__source[index].strip() == "":
-                    del self.__source[index]
-                    index -= 1
-                else:
-                    break
-            # Superfluous blank lines removed.
-            return (1, "FE303", 0)
-        else:
-            fixId = self.__getID()
-            self.__stack.append((fixId, code, line, pos))
-            return (-1, "", fixId)
-    
     def __fixE304(self, code, line, pos, apply=False):
         """
         Private method to fix superfluous blank lines after a function

eric ide

mercurial