Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py

changeset 5588
6ba512d9f46a
parent 5389
9b1c800daff3
child 5616
adcffadf4962
equal deleted inserted replaced
5587:ea526b78ee6c 5588:6ba512d9f46a
586 self.__source[line] = docstring 586 self.__source[line] = docstring
587 self.__source[line + 1] = "" 587 self.__source[line + 1] = ""
588 # Single line docstring put on one line. 588 # Single line docstring put on one line.
589 return (1, "FD121", 0) 589 return (1, "FD121", 0)
590 else: 590 else:
591 id = self.__getID() 591 fixId = self.__getID()
592 self.__stack.append((id, code, line, pos)) 592 self.__stack.append((fixId, code, line, pos))
593 return (-1, "", id) 593 return (-1, "", fixId)
594 594
595 def __fixD131(self, code, line, pos): 595 def __fixD131(self, code, line, pos):
596 """ 596 """
597 Private method to fix a docstring summary not ending with a 597 Private method to fix a docstring summary not ending with a
598 period. 598 period.
648 line = line - 1 648 line = line - 1
649 self.__source[line - 1] = "" 649 self.__source[line - 1] = ""
650 # Blank line before function/method docstring removed. 650 # Blank line before function/method docstring removed.
651 return (1, "FD141", 0) 651 return (1, "FD141", 0)
652 else: 652 else:
653 id = self.__getID() 653 fixId = self.__getID()
654 self.__stack.append((id, code, line, pos)) 654 self.__stack.append((fixId, code, line, pos))
655 return (-1, "", id) 655 return (-1, "", fixId)
656 656
657 def __fixD142(self, code, line, pos, apply=False): 657 def __fixD142(self, code, line, pos, apply=False):
658 """ 658 """
659 Private method to fix a class docstring not preceded by a 659 Private method to fix a class docstring not preceded by a
660 blank line. 660 blank line.
674 line = line - 1 674 line = line - 1
675 self.__source[line] = self.__eol + self.__source[line] 675 self.__source[line] = self.__eol + self.__source[line]
676 # Blank line inserted before class docstring. 676 # Blank line inserted before class docstring.
677 return (1, "FD142", 0) 677 return (1, "FD142", 0)
678 else: 678 else:
679 id = self.__getID() 679 fixId = self.__getID()
680 self.__stack.append((id, code, line, pos)) 680 self.__stack.append((fixId, code, line, pos))
681 return (-1, "", id) 681 return (-1, "", fixId)
682 682
683 def __fixD143(self, code, line, pos, apply=False): 683 def __fixD143(self, code, line, pos, apply=False):
684 """ 684 """
685 Private method to fix a class docstring not followed by a 685 Private method to fix a class docstring not followed by a
686 blank line. 686 blank line.
700 line = line - 1 700 line = line - 1
701 self.__source[line] += self.__eol 701 self.__source[line] += self.__eol
702 # Blank line inserted after class docstring. 702 # Blank line inserted after class docstring.
703 return (1, "FD143", 0) 703 return (1, "FD143", 0)
704 else: 704 else:
705 id = self.__getID() 705 fixId = self.__getID()
706 self.__stack.append((id, code, line, pos)) 706 self.__stack.append((fixId, code, line, pos))
707 return (-1, "", id) 707 return (-1, "", fixId)
708 708
709 def __fixD144(self, code, line, pos, apply=False): 709 def __fixD144(self, code, line, pos, apply=False):
710 """ 710 """
711 Private method to fix a docstring summary not followed by a 711 Private method to fix a docstring summary not followed by a
712 blank line. 712 blank line.
730 730
731 self.__source[line] += self.__eol 731 self.__source[line] += self.__eol
732 # Blank line inserted after docstring summary. 732 # Blank line inserted after docstring summary.
733 return (1, "FD144", 0) 733 return (1, "FD144", 0)
734 else: 734 else:
735 id = self.__getID() 735 fixId = self.__getID()
736 self.__stack.append((id, code, line, pos)) 736 self.__stack.append((fixId, code, line, pos))
737 return (-1, "", id) 737 return (-1, "", fixId)
738 738
739 def __fixD145(self, code, line, pos, apply=False): 739 def __fixD145(self, code, line, pos, apply=False):
740 """ 740 """
741 Private method to fix the last paragraph of a multi-line docstring 741 Private method to fix the last paragraph of a multi-line docstring
742 not followed by a blank line. 742 not followed by a blank line.
756 line = line - 1 756 line = line - 1
757 self.__source[line] = self.__eol + self.__source[line] 757 self.__source[line] = self.__eol + self.__source[line]
758 # Blank line inserted after last paragraph of docstring. 758 # Blank line inserted after last paragraph of docstring.
759 return (1, "FD145", 0) 759 return (1, "FD145", 0)
760 else: 760 else:
761 id = self.__getID() 761 fixId = self.__getID()
762 self.__stack.append((id, code, line, pos)) 762 self.__stack.append((fixId, code, line, pos))
763 return (-1, "", id) 763 return (-1, "", fixId)
764 764
765 def __fixD221(self, code, line, pos, apply=False): 765 def __fixD221(self, code, line, pos, apply=False):
766 """ 766 """
767 Private method to fix leading and trailing quotes of docstring 767 Private method to fix leading and trailing quotes of docstring
768 not on separate lines. 768 not on separate lines.
800 else: 800 else:
801 # Trailing quotes put on separate line. 801 # Trailing quotes put on separate line.
802 msg = "FD222" 802 msg = "FD222"
803 return (1, msg, 0) 803 return (1, msg, 0)
804 else: 804 else:
805 id = self.__getID() 805 fixId = self.__getID()
806 self.__stack.append((id, code, line, pos)) 806 self.__stack.append((fixId, code, line, pos))
807 return (-1, "", id) 807 return (-1, "", fixId)
808 808
809 def __fixD242(self, code, line, pos, apply=False): 809 def __fixD242(self, code, line, pos, apply=False):
810 """ 810 """
811 Private method to fix a class or function/method docstring preceded 811 Private method to fix a class or function/method docstring preceded
812 by a blank line. 812 by a blank line.
831 else: 831 else:
832 # Blank line before function/method docstring removed. 832 # Blank line before function/method docstring removed.
833 msg = "FD244" 833 msg = "FD244"
834 return (1, msg, 0) 834 return (1, msg, 0)
835 else: 835 else:
836 id = self.__getID() 836 fixId = self.__getID()
837 self.__stack.append((id, code, line, pos)) 837 self.__stack.append((fixId, code, line, pos))
838 return (-1, "", id) 838 return (-1, "", fixId)
839 839
840 def __fixD243(self, code, line, pos, apply=False): 840 def __fixD243(self, code, line, pos, apply=False):
841 """ 841 """
842 Private method to fix a class or function/method docstring followed 842 Private method to fix a class or function/method docstring followed
843 by a blank line. 843 by a blank line.
862 else: 862 else:
863 # Blank line after function/method docstring removed. 863 # Blank line after function/method docstring removed.
864 msg = "FD245" 864 msg = "FD245"
865 return (1, msg, 0) 865 return (1, msg, 0)
866 else: 866 else:
867 id = self.__getID() 867 fixId = self.__getID()
868 self.__stack.append((id, code, line, pos)) 868 self.__stack.append((fixId, code, line, pos))
869 return (-1, "", id) 869 return (-1, "", fixId)
870 870
871 def __fixD247(self, code, line, pos, apply=False): 871 def __fixD247(self, code, line, pos, apply=False):
872 """ 872 """
873 Private method to fix a last paragraph of a docstring followed 873 Private method to fix a last paragraph of a docstring followed
874 by a blank line. 874 by a blank line.
888 line = line - 1 888 line = line - 1
889 self.__source[line - 1] = "" 889 self.__source[line - 1] = ""
890 # Blank line after last paragraph removed. 890 # Blank line after last paragraph removed.
891 return (1, "FD247", 0) 891 return (1, "FD247", 0)
892 else: 892 else:
893 id = self.__getID() 893 fixId = self.__getID()
894 self.__stack.append((id, code, line, pos)) 894 self.__stack.append((fixId, code, line, pos))
895 return (-1, "", id) 895 return (-1, "", fixId)
896 896
897 def __fixE101(self, code, line, pos): 897 def __fixE101(self, code, line, pos):
898 """ 898 """
899 Private method to fix obsolete tab usage and indentation errors. 899 Private method to fix obsolete tab usage and indentation errors.
900 900
952 # Indentation of closing bracket corrected. 952 # Indentation of closing bracket corrected.
953 msg = "FE124" 953 msg = "FE124"
954 return (1, msg, 0) 954 return (1, msg, 0)
955 return (0, "", 0) 955 return (0, "", 0)
956 else: 956 else:
957 id = self.__getID() 957 fixId = self.__getID()
958 self.__stackLogical.append((id, code, line, pos)) 958 self.__stackLogical.append((fixId, code, line, pos))
959 return (-1, "", id) 959 return (-1, "", fixId)
960 960
961 def __fixE122(self, code, line, pos, apply=False): 961 def __fixE122(self, code, line, pos, apply=False):
962 """ 962 """
963 Private method to fix a missing indentation of continuation lines. 963 Private method to fix a missing indentation of continuation lines.
964 964
987 self.__indentWord + text.lstrip() 987 self.__indentWord + text.lstrip()
988 # Missing indentation of continuation line corrected. 988 # Missing indentation of continuation line corrected.
989 return (1, "FE122", 0) 989 return (1, "FE122", 0)
990 return (0, "", 0) 990 return (0, "", 0)
991 else: 991 else:
992 id = self.__getID() 992 fixId = self.__getID()
993 self.__stackLogical.append((id, code, line, pos)) 993 self.__stackLogical.append((fixId, code, line, pos))
994 return (-1, "", id) 994 return (-1, "", fixId)
995 995
996 def __fixE123(self, code, line, pos, apply=False): 996 def __fixE123(self, code, line, pos, apply=False):
997 """ 997 """
998 Private method to fix the indentation of a closing bracket lines. 998 Private method to fix the indentation of a closing bracket lines.
999 999
1025 if changed: 1025 if changed:
1026 # Closing bracket aligned to opening bracket. 1026 # Closing bracket aligned to opening bracket.
1027 return (1, "FE123", 0) 1027 return (1, "FE123", 0)
1028 return (0, "", 0) 1028 return (0, "", 0)
1029 else: 1029 else:
1030 id = self.__getID() 1030 fixId = self.__getID()
1031 self.__stackLogical.append((id, code, line, pos)) 1031 self.__stackLogical.append((fixId, code, line, pos))
1032 return (-1, "", id) 1032 return (-1, "", fixId)
1033 1033
1034 def __fixE125(self, code, line, pos, apply=False): 1034 def __fixE125(self, code, line, pos, apply=False):
1035 """ 1035 """
1036 Private method to fix the indentation of continuation lines not 1036 Private method to fix the indentation of continuation lines not
1037 distinguishable from next logical line. 1037 distinguishable from next logical line.
1059 self.__indentWord + text.lstrip() 1059 self.__indentWord + text.lstrip()
1060 # Indentation level changed. 1060 # Indentation level changed.
1061 return (1, "FE125", 0) 1061 return (1, "FE125", 0)
1062 return (0, "", 0) 1062 return (0, "", 0)
1063 else: 1063 else:
1064 id = self.__getID() 1064 fixId = self.__getID()
1065 self.__stackLogical.append((id, code, line, pos)) 1065 self.__stackLogical.append((fixId, code, line, pos))
1066 return (-1, "", id) 1066 return (-1, "", fixId)
1067 1067
1068 def __fixE126(self, code, line, pos, apply=False): 1068 def __fixE126(self, code, line, pos, apply=False):
1069 """ 1069 """
1070 Private method to fix over-indented/under-indented hanging 1070 Private method to fix over-indented/under-indented hanging
1071 indentation. 1071 indentation.
1099 if changed: 1099 if changed:
1100 # Indentation level of hanging indentation changed. 1100 # Indentation level of hanging indentation changed.
1101 return (1, "FE126", 0) 1101 return (1, "FE126", 0)
1102 return (0, "", 0) 1102 return (0, "", 0)
1103 else: 1103 else:
1104 id = self.__getID() 1104 fixId = self.__getID()
1105 self.__stackLogical.append((id, code, line, pos)) 1105 self.__stackLogical.append((fixId, code, line, pos))
1106 return (-1, "", id) 1106 return (-1, "", fixId)
1107 1107
1108 def __fixE127(self, code, line, pos, apply=False): 1108 def __fixE127(self, code, line, pos, apply=False):
1109 """ 1109 """
1110 Private method to fix over/under indented lines. 1110 Private method to fix over/under indented lines.
1111 1111
1154 if changed: 1154 if changed:
1155 # Visual indentation corrected. 1155 # Visual indentation corrected.
1156 return (1, "FE127", 0) 1156 return (1, "FE127", 0)
1157 return (0, "", 0) 1157 return (0, "", 0)
1158 else: 1158 else:
1159 id = self.__getID() 1159 fixId = self.__getID()
1160 self.__stackLogical.append((id, code, line, pos)) 1160 self.__stackLogical.append((fixId, code, line, pos))
1161 return (-1, "", id) 1161 return (-1, "", fixId)
1162 1162
1163 def __fixE201(self, code, line, pos): 1163 def __fixE201(self, code, line, pos):
1164 """ 1164 """
1165 Private method to fix extraneous whitespace. 1165 Private method to fix extraneous whitespace.
1166 1166
1353 if apply: 1353 if apply:
1354 self.__source.insert(line - 1, self.__eol) 1354 self.__source.insert(line - 1, self.__eol)
1355 # One blank line inserted. 1355 # One blank line inserted.
1356 return (1, "FE301", 0) 1356 return (1, "FE301", 0)
1357 else: 1357 else:
1358 id = self.__getID() 1358 fixId = self.__getID()
1359 self.__stack.append((id, code, line, pos)) 1359 self.__stack.append((fixId, code, line, pos))
1360 return (-1, "", id) 1360 return (-1, "", fixId)
1361 1361
1362 def __fixE302(self, code, line, pos, apply=False): 1362 def __fixE302(self, code, line, pos, apply=False):
1363 """ 1363 """
1364 Private method to fix the need for two blank lines. 1364 Private method to fix the need for two blank lines.
1365 1365
1403 # %n superfluous line(s) removed. 1403 # %n superfluous line(s) removed.
1404 return (1, ("FE302-", blanks - 2), 0) 1404 return (1, ("FE302-", blanks - 2), 0)
1405 else: 1405 else:
1406 return (0, "", 0) 1406 return (0, "", 0)
1407 else: 1407 else:
1408 id = self.__getID() 1408 fixId = self.__getID()
1409 self.__stack.append((id, code, line, pos)) 1409 self.__stack.append((fixId, code, line, pos))
1410 return (-1, "", id) 1410 return (-1, "", fixId)
1411 1411
1412 def __fixE303(self, code, line, pos, apply=False): 1412 def __fixE303(self, code, line, pos, apply=False):
1413 """ 1413 """
1414 Private method to fix superfluous blank lines. 1414 Private method to fix superfluous blank lines.
1415 1415
1433 else: 1433 else:
1434 break 1434 break
1435 # Superfluous blank lines removed. 1435 # Superfluous blank lines removed.
1436 return (1, "FE303", 0) 1436 return (1, "FE303", 0)
1437 else: 1437 else:
1438 id = self.__getID() 1438 fixId = self.__getID()
1439 self.__stack.append((id, code, line, pos)) 1439 self.__stack.append((fixId, code, line, pos))
1440 return (-1, "", id) 1440 return (-1, "", fixId)
1441 1441
1442 def __fixE304(self, code, line, pos, apply=False): 1442 def __fixE304(self, code, line, pos, apply=False):
1443 """ 1443 """
1444 Private method to fix superfluous blank lines after a function 1444 Private method to fix superfluous blank lines after a function
1445 decorator. 1445 decorator.
1464 else: 1464 else:
1465 break 1465 break
1466 # Superfluous blank lines after function decorator removed. 1466 # Superfluous blank lines after function decorator removed.
1467 return (1, "FE304", 0) 1467 return (1, "FE304", 0)
1468 else: 1468 else:
1469 id = self.__getID() 1469 fixId = self.__getID()
1470 self.__stack.append((id, code, line, pos)) 1470 self.__stack.append((fixId, code, line, pos))
1471 return (-1, "", id) 1471 return (-1, "", fixId)
1472 1472
1473 def __fixE401(self, code, line, pos, apply=False): 1473 def __fixE401(self, code, line, pos, apply=False):
1474 """ 1474 """
1475 Private method to fix multiple imports on one line. 1475 Private method to fix multiple imports on one line.
1476 1476
1501 self.__getIndent(text) + "import " + text[pos:].lstrip("\t ,") 1501 self.__getIndent(text) + "import " + text[pos:].lstrip("\t ,")
1502 self.__source[line] = newText 1502 self.__source[line] = newText
1503 # Imports were put on separate lines. 1503 # Imports were put on separate lines.
1504 return (1, "FE401", 0) 1504 return (1, "FE401", 0)
1505 else: 1505 else:
1506 id = self.__getID() 1506 fixId = self.__getID()
1507 self.__stack.append((id, code, line, pos)) 1507 self.__stack.append((fixId, code, line, pos))
1508 return (-1, "", id) 1508 return (-1, "", fixId)
1509 1509
1510 def __fixE501(self, code, line, pos, apply=False): 1510 def __fixE501(self, code, line, pos, apply=False):
1511 """ 1511 """
1512 Private method to fix the long lines by breaking them. 1512 Private method to fix the long lines by breaking them.
1513 1513
1551 # Long lines have been shortened. 1551 # Long lines have been shortened.
1552 return (1, "FE501", 0) 1552 return (1, "FE501", 0)
1553 else: 1553 else:
1554 return (0, "", 0) 1554 return (0, "", 0)
1555 else: 1555 else:
1556 id = self.__getID() 1556 fixId = self.__getID()
1557 self.__stack.append((id, code, line, pos)) 1557 self.__stack.append((fixId, code, line, pos))
1558 return (-1, "", id) 1558 return (-1, "", fixId)
1559 1559
1560 def __fixE502(self, code, line, pos): 1560 def __fixE502(self, code, line, pos):
1561 """ 1561 """
1562 Private method to fix redundant backslash within brackets. 1562 Private method to fix redundant backslash within brackets.
1563 1563
1600 self.__eol 1600 self.__eol
1601 self.__source[line] = newText 1601 self.__source[line] = newText
1602 # Compound statement corrected. 1602 # Compound statement corrected.
1603 return (1, "FE701", 0) 1603 return (1, "FE701", 0)
1604 else: 1604 else:
1605 id = self.__getID() 1605 fixId = self.__getID()
1606 self.__stack.append((id, code, line, pos)) 1606 self.__stack.append((fixId, code, line, pos))
1607 return (-1, "", id) 1607 return (-1, "", fixId)
1608 1608
1609 def __fixE702(self, code, line, pos, apply=False): 1609 def __fixE702(self, code, line, pos, apply=False):
1610 """ 1610 """
1611 Private method to fix semicolon-separated compound statements. 1611 Private method to fix semicolon-separated compound statements.
1612 1612
1636 second = text[pos:].lstrip("\n\r \t;") 1636 second = text[pos:].lstrip("\n\r \t;")
1637 self.__source[line] = first + self.__getIndent(text) + second 1637 self.__source[line] = first + self.__getIndent(text) + second
1638 # Compound statement corrected. 1638 # Compound statement corrected.
1639 return (1, "FE702", 0) 1639 return (1, "FE702", 0)
1640 else: 1640 else:
1641 id = self.__getID() 1641 fixId = self.__getID()
1642 self.__stack.append((id, code, line, pos)) 1642 self.__stack.append((fixId, code, line, pos))
1643 return (-1, "", id) 1643 return (-1, "", fixId)
1644 1644
1645 def __fixE711(self, code, line, pos): 1645 def __fixE711(self, code, line, pos):
1646 """ 1646 """
1647 Private method to fix comparison with None. 1647 Private method to fix comparison with None.
1648 1648
1718 newText = left + center + right 1718 newText = left + center + right
1719 self.__source[line] = newText 1719 self.__source[line] = newText
1720 # '{0}' argument added. 1720 # '{0}' argument added.
1721 return (1, ("FN804", (arg,)), 0) 1721 return (1, ("FN804", (arg,)), 0)
1722 else: 1722 else:
1723 id = self.__getID() 1723 fixId = self.__getID()
1724 self.__stack.append((id, code, line, pos)) 1724 self.__stack.append((fixId, code, line, pos))
1725 return (-1, "", id) 1725 return (-1, "", fixId)
1726 1726
1727 def __fixN806(self, code, line, pos, apply=False): 1727 def __fixN806(self, code, line, pos, apply=False):
1728 """ 1728 """
1729 Private method to fix a wrong first argument of static methods. 1729 Private method to fix a wrong first argument of static methods.
1730 1730
1779 self.__source[line] = indent + right 1779 self.__source[line] = indent + right
1780 1780
1781 # '{0}' argument removed. 1781 # '{0}' argument removed.
1782 return (1, ("FN806", arg), 0) 1782 return (1, ("FN806", arg), 0)
1783 else: 1783 else:
1784 id = self.__getID() 1784 fixId = self.__getID()
1785 self.__stack.append((id, code, line, pos)) 1785 self.__stack.append((fixId, code, line, pos))
1786 return (-1, "", id) 1786 return (-1, "", fixId)
1787 1787
1788 def __fixW291(self, code, line, pos): 1788 def __fixW291(self, code, line, pos):
1789 """ 1789 """
1790 Private method to fix trailing whitespace. 1790 Private method to fix trailing whitespace.
1791 1791
2669 offset = 0 2669 offset = 0
2670 if lines[0].rstrip()[-1] not in '([{': 2670 if lines[0].rstrip()[-1] not in '([{':
2671 for symbol in '([{': 2671 for symbol in '([{':
2672 offset = max(offset, 1 + lines[0].find(symbol)) 2672 offset = max(offset, 1 + lines[0].find(symbol))
2673 2673
2674 maxLength = max([offset + len(x.strip()) for x in lines]) 2674 maxLength = max(offset + len(x.strip()) for x in lines)
2675 rank += maxLength 2675 rank += maxLength
2676 rank += len(lines) 2676 rank += len(lines)
2677 2677
2678 badStartingSymbol = { 2678 badStartingSymbol = {
2679 '(': ')', 2679 '(': ')',

eric ide

mercurial