Finished implementing fixer method for eric style docstring issues.

Fri, 27 Sep 2013 18:36:07 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 27 Sep 2013 18:36:07 +0200
changeset 2937
de26bc76d6ee
parent 2936
2ff273068a10
child 2938
42d82e978d27

Finished implementing fixer method for eric style docstring issues.

Plugins/CheckerPlugins/Pep8/Pep257Checker.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/Pep8/Pep8Fixer.py file | annotate | diff | comparison | revisions
--- a/Plugins/CheckerPlugins/Pep8/Pep257Checker.py	Wed Sep 25 19:50:48 2013 +0200
+++ b/Plugins/CheckerPlugins/Pep8/Pep257Checker.py	Fri Sep 27 18:36:07 2013 +0200
@@ -112,7 +112,7 @@
         "D141", "D142", "D143", "D144", "D145",
         
         "D203", "D205",
-        "D221",
+        "D221", "D222",
         "D231", "D234", "D235", "D236", "D237", "D238",
         "D242", "D243", "D244", "D245", "D246", "D247",
     ]
@@ -173,7 +173,11 @@
         "D205": QT_TRANSLATE_NOOP(
             "Pep257Checker", "private class is missing a docstring"),
         "D221": QT_TRANSLATE_NOOP(
-            "Pep257Checker", "one-liner docstring not on three lines"),
+            "Pep257Checker",
+            "leading quotes of docstring not on separate line"),
+        "D222": QT_TRANSLATE_NOOP(
+            "Pep257Checker",
+            "trailing quotes of docstring not on separate line"),
         "D231": QT_TRANSLATE_NOOP(
             "Pep257Checker", "docstring summary does not end with a period"),
         "D234": QT_TRANSLATE_NOOP(
@@ -313,11 +317,11 @@
                     (self.__checkTripleDoubleQuotes, ("D111",)),
                     (self.__checkBackslashes, ("D112",)),
                     (self.__checkUnicode, ("D113",)),
-                    (self.__checkEricOneLiner, ("D221",)),
                     (self.__checkIndent, ("D122",)),
                     (self.__checkEricEndsWithPeriod, ("D231",)),
                     (self.__checkEricBlankAfterSummary, ("D246",)),
                     (self.__checkEricNBlankAfterLastParagraph, ("D247",)),
+                    (self.__checkEricQuotesOnSeparateLines, ("D222", "D223"))
                 ],
             }
         
@@ -1058,10 +1062,10 @@
     ## Checking functionality below (eric specific ones)
     ##################################################################
 
-    def __checkEricOneLiner(self, docstringContext, context):
+    def __checkEricQuotesOnSeparateLines(self, docstringContext, context):
         """
-        Private method to check, that one-liner docstrings are on
-        three lines (quotes, docstring, quotes).
+        Private method to check, that leading and trailing quotes are on
+        a line by themselves.
         
         @param docstringContext docstring context (Pep257Context)
         @param context context of the docstring (Pep257Context)
@@ -1070,10 +1074,10 @@
             return
         
         lines = docstringContext.source()
-        if len(lines) != 3:
-            nonEmptyLines = [l for l in lines if l.strip().strip('\'"')]
-            if len(nonEmptyLines) == 1:
-                self.__error(docstringContext.start(), 0, "D221")
+        if lines[0].strip().strip('ru"'):
+            self.__error(docstringContext.start(), 0, "D221")
+        if lines[-1].strip().strip('"'):
+            self.__error(docstringContext.end(), 0, "D222")
     
     def __checkEricEndsWithPeriod(self, docstringContext, context):
         """
@@ -1086,6 +1090,8 @@
             return
         
         summaryLines, lineNumber = self.__getSummaryLines(docstringContext)
+        if summaryLines[-1].lstrip().startswith("@"):
+            summaryLines.pop(-1)
         summary = " ".join([s.strip() for s in summaryLines if s])
         if not summary.endswith(".") and \
                 not summary.split(None, 1)[0].lower() == "constructor":
--- a/Plugins/CheckerPlugins/Pep8/Pep8Fixer.py	Wed Sep 25 19:50:48 2013 +0200
+++ b/Plugins/CheckerPlugins/Pep8/Pep8Fixer.py	Fri Sep 27 18:36:07 2013 +0200
@@ -20,12 +20,10 @@
 
 import Utilities
 
-# TODO: add fixers for these issues
-# D221, D231, D246,
-
 Pep8FixableIssues = ["D111", "D112", "D113", "D121", "D131", "D141",
                      "D142", "D143", "D144", "D145",
-                     "D242", "D243", "D244", "D245", "D247",
+                     "D221", "D222", "D231", "D242", "D243", "D244",
+                     "D245", "D246", "D247",
                      "E101", "E111", "E121", "E122", "E123", "E124",
                      "E125", "E126", "E127", "E128", "E133", "E201",
                      "E202", "E203", "E211", "E221", "E222", "E223",
@@ -92,10 +90,14 @@
             "D143": self.__fixD143,
             "D144": self.__fixD144,
             "D145": self.__fixD145,
+            "D221": self.__fixD221,
+            "D222": self.__fixD221,
+            "D231": self.__fixD131,
             "D242": self.__fixD242,
             "D243": self.__fixD243,
             "D244": self.__fixD242,
             "D245": self.__fixD243,
+            "D246": self.__fixD144,
             "D247": self.__fixD247,
             "E101": self.__fixE101,
             "E111": self.__fixE101,
@@ -577,7 +579,8 @@
     
     def __fixD131(self, code, line, pos):
         """
-        Private method to fix a single line docstring on multiple lines (D121).
+        Private method to fix a docstring summary not ending with a
+        period (D131).
         
         @param code code of the issue (string)
         @param line line number of the issue (integer)
@@ -591,12 +594,14 @@
         if self.__source[line].rstrip().endswith(('"""', "'''")) and \
            self.__source[line].lstrip().startswith(('"""', 'r"""', 'u"""')):
             # it is a one-liner
-            newText = self.__source[line].rstrip()[:-3] + "." + \
+            newText = self.__source[line].rstrip()[:-3].rstrip() + "." + \
                 self.__source[line].rstrip()[-3:] + self.__getEol()
         else:
             if line < len(self.__source) - 1 and \
                 (not self.__source[line + 1].strip() or
-                    self.__source[line + 1].strip() in ('"""', "'''")):
+                 self.__source[line + 1].lstrip().startswith("@") or
+                 (self.__source[line + 1].strip() in ('"""', "'''") and
+                  not self.__source[line].lstrip().startswith("@"))):
                 newText = self.__source[line].rstrip() + "." + self.__getEol()
         
         if newText:
@@ -741,6 +746,46 @@
             self.__stack.append((id, code, line, pos))
             return (-1, "", id)
     
+    def __fixD221(self, code, line, pos, apply=False):
+        """
+        Private method to fix leading and trailing quotes of docstring
+        not on separate lines (D221, D222).
+        
+        @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:
+            line = line - 1
+            indent = self.__getIndent(self.__source[line])
+            source = self.__source[line].strip()
+            if code == "D221":
+                # leading
+                if source.startswith(("r", "u")):
+                    first, second = source[:4], source[4:].strip()
+                else:
+                    first, second = source[:3], source[3:].strip()
+            else:
+                # trailing
+                first, second = source[:-3].strip(), source[-3:]
+            newText = indent + first + self.__getEol() + \
+                indent + second + self.__getEol()
+            self.__source[line] = newText
+            if code == "D221":
+                msg = self.trUtf8("Leading quotes put on separate line.")
+            else:
+                msg = self.trUtf8("Trailing quotes put on separate line.")
+            return (1, msg, 0)
+        else:
+            id = self.__getID()
+            self.__stack.append((id, code, line, pos))
+            return (-1, "", id)
+    
     def __fixD242(self, code, line, pos, apply=False):
         """
         Private method to fix a class or function/method docstring preceded

eric ide

mercurial