Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py

branch
BgService
changeset 3413
5e63f809732a
parent 3228
f489068e51e8
child 3456
96232974dcdb
equal deleted inserted replaced
3412:9364dab2d472 3413:5e63f809732a
22 # Python3. So ignore it. 22 # Python3. So ignore it.
23 try: 23 try:
24 import pep8 24 import pep8
25 except ImportError: 25 except ImportError:
26 pass 26 pass
27
28 # Tell 'lupdate' which strings to keep for translation.
29 QT_TRANSLATE_NOOP = lambda mod, txt: txt
30 27
31 FixableCodeStyleIssues = [ 28 FixableCodeStyleIssues = [
32 "D111", "D112", "D113", "D121", "D131", "D141", 29 "D111", "D112", "D113", "D121", "D131", "D141",
33 "D142", "D143", "D144", "D145", 30 "D142", "D143", "D144", "D145",
34 "D221", "D222", "D231", "D242", "D243", "D244", 31 "D221", "D222", "D231", "D242", "D243", "D244",
180 def saveFile(self, encoding): 177 def saveFile(self, encoding):
181 """ 178 """
182 Public method to save the modified file. 179 Public method to save the modified file.
183 180
184 @param encoding encoding of the source file (string) 181 @param encoding encoding of the source file (string)
185 @return flag indicating success (boolean) 182 @return error message on failure (tuple of str)
186 """ 183 """
187 import codecs 184 import codecs
188 185
189 if not self.__modified: 186 if not self.__modified:
190 # no need to write 187 # no need to write
191 return True 188 return
192 189
193 txt = "".join(self.__source) 190 txt = "".join(self.__source)
194 try: 191 try:
195 if sys.version_info[0] == 3: 192 if sys.version_info[0] == 3:
196 txt = txt.encode(encoding) 193 txt = txt.encode(encoding)
197 if encoding == 'utf-8-bom': 194 if encoding == 'utf-8-bom':
198 txt = codecs.BOM_UTF8 + txt 195 txt = codecs.BOM_UTF8 + txt
199 196
200 with open(self.__filename, "wb") as fp: 197 with open(self.__filename, "wb") as fp:
201 fp.write(txt) 198 fp.write(txt)
202 except (IOError, UnicodeError): # as err: 199 except (IOError, UnicodeError) as err:
203 # E5MessageBox.critical( 200 # Could not save the file! Skipping it. Reason: {0}
204 # self, 201 return ("FWRITE_ERROR", (str(err),))
205 # self.trUtf8("Fix Code Style Issues"), 202 return
206 # self.trUtf8(
207 # """<p>Could not save the file <b>{0}</b>."""
208 # """ Skipping it.</p><p>Reason: {1}</p>""")
209 # .format(self.__filename, str(err))
210 # )
211 return False
212
213 return True
214 203
215 def __codeMatch(self, code): 204 def __codeMatch(self, code):
216 """ 205 """
217 Private method to check, if the code should be fixed. 206 Private method to check, if the code should be fixed.
218 207
251 @param message message text (string) 240 @param message message text (string)
252 @return value indicating an applied/deferred fix (-1, 0, 1), 241 @return value indicating an applied/deferred fix (-1, 0, 1),
253 a message for the fix (string) and an ID for a deferred 242 a message for the fix (string) and an ID for a deferred
254 fix (integer) 243 fix (integer)
255 """ 244 """
256 code = message.split(None, 1)[0].strip() 245 if isinstance(message, (tuple, list)):
246 code = message[0].strip()
247 else:
248 code = message.split(None, 1)[0].strip()
257 249
258 if line <= len(self.__source) and \ 250 if line <= len(self.__source) and \
259 self.__codeMatch(code) and \ 251 self.__codeMatch(code) and \
260 code in self.__fixes: 252 code in self.__fixes:
261 res = self.__fixes[code](code, line, pos) 253 res = self.__fixes[code](code, line, pos)
507 left, right = self.__source[line].rsplit("'''", 1) 499 left, right = self.__source[line].rsplit("'''", 1)
508 self.__source[line] = left + '"""' + right 500 self.__source[line] = left + '"""' + right
509 break 501 break
510 line += 1 502 line += 1
511 503
512 return (1, QT_TRANSLATE_NOOP( 504 # Triple single quotes converted to triple double quotes.
513 'CodeStyleFixer', 505 return (1, "FD111", 0)
514 "Triple single quotes converted to triple double quotes."), 0)
515 506
516 def __fixD112(self, code, line, pos): 507 def __fixD112(self, code, line, pos):
517 """ 508 """
518 Private method to fix docstring 'r' or 'u' in leading quotes. 509 Private method to fix docstring 'r' or 'u' in leading quotes.
519 510
535 return (0, "", 0) 526 return (0, "", 0)
536 527
537 newText = self.__getIndent(self.__source[line]) + \ 528 newText = self.__getIndent(self.__source[line]) + \
538 insertChar + self.__source[line].lstrip() 529 insertChar + self.__source[line].lstrip()
539 self.__source[line] = newText 530 self.__source[line] = newText
540 return (1, QT_TRANSLATE_NOOP( 531 # Introductory quotes corrected to be {0}"""
541 'CodeStyleFixer', 532 return (1, ('FD112', (insertChar,)), 0)
542 'Introductory quotes corrected to be {0}"""') +
543 '@@{0}'.format(insertChar), 0)
544 533
545 def __fixD121(self, code, line, pos, apply=False): 534 def __fixD121(self, code, line, pos, apply=False):
546 """ 535 """
547 Private method to fix a single line docstring on multiple lines. 536 Private method to fix a single line docstring on multiple lines.
548 537
572 docstring += self.__source[line + 2].lstrip() 561 docstring += self.__source[line + 2].lstrip()
573 self.__source[line + 2] = "" 562 self.__source[line + 2] = ""
574 563
575 self.__source[line] = docstring 564 self.__source[line] = docstring
576 self.__source[line + 1] = "" 565 self.__source[line + 1] = ""
577 return ( 566 # Single line docstring put on one line.
578 1, 567 return (1, "FD121", 0)
579 QT_TRANSLATE_NOOP(
580 'CodeStyleFixer',
581 "Single line docstring put on one line."),
582 0)
583 else: 568 else:
584 id = self.__getID() 569 id = self.__getID()
585 self.__stack.append((id, code, line, pos)) 570 self.__stack.append((id, code, line, pos))
586 return (-1, "", id) 571 return (-1, "", id)
587 572
614 not self.__source[line].lstrip().startswith("@"))): 599 not self.__source[line].lstrip().startswith("@"))):
615 newText = self.__source[line].rstrip() + "." + self.__eol 600 newText = self.__source[line].rstrip() + "." + self.__eol
616 601
617 if newText: 602 if newText:
618 self.__source[line] = newText 603 self.__source[line] = newText
619 return ( 604 # Period added to summary line.
620 1, 605 return (1, "FD131", 0)
621 QT_TRANSLATE_NOOP(
622 'CodeStyleFixer',
623 "Period added to summary line."),
624 0)
625 else: 606 else:
626 return (0, "", 0) 607 return (0, "", 0)
627 608
628 def __fixD141(self, code, line, pos, apply=False): 609 def __fixD141(self, code, line, pos, apply=False):
629 """ 610 """
642 fix (integer) 623 fix (integer)
643 """ 624 """
644 if apply: 625 if apply:
645 line = line - 1 626 line = line - 1
646 self.__source[line - 1] = "" 627 self.__source[line - 1] = ""
647 return ( 628 # Blank line before function/method docstring removed.
648 1, 629 return (1, "FD141", 0)
649 QT_TRANSLATE_NOOP(
650 'CodeStyleFixer',
651 "Blank line before function/method docstring removed."),
652 0)
653 else: 630 else:
654 id = self.__getID() 631 id = self.__getID()
655 self.__stack.append((id, code, line, pos)) 632 self.__stack.append((id, code, line, pos))
656 return (-1, "", id) 633 return (-1, "", id)
657 634
672 fix (integer) 649 fix (integer)
673 """ 650 """
674 if apply: 651 if apply:
675 line = line - 1 652 line = line - 1
676 self.__source[line] = self.__eol + self.__source[line] 653 self.__source[line] = self.__eol + self.__source[line]
677 return (1, QT_TRANSLATE_NOOP( 654 # Blank line inserted before class docstring.
678 'CodeStyleFixer', 655 return (1, "FD142", 0)
679 "Blank line inserted before class docstring."), 0)
680 else: 656 else:
681 id = self.__getID() 657 id = self.__getID()
682 self.__stack.append((id, code, line, pos)) 658 self.__stack.append((id, code, line, pos))
683 return (-1, "", id) 659 return (-1, "", id)
684 660
699 fix (integer) 675 fix (integer)
700 """ 676 """
701 if apply: 677 if apply:
702 line = line - 1 678 line = line - 1
703 self.__source[line] += self.__eol 679 self.__source[line] += self.__eol
704 return (1, QT_TRANSLATE_NOOP( 680 # Blank line inserted after class docstring.
705 'CodeStyleFixer', 681 return (1, "FD143", 0)
706 "Blank line inserted after class docstring."), 0)
707 else: 682 else:
708 id = self.__getID() 683 id = self.__getID()
709 self.__stack.append((id, code, line, pos)) 684 self.__stack.append((id, code, line, pos))
710 return (-1, "", id) 685 return (-1, "", id)
711 686
730 if not self.__source[line].rstrip().endswith("."): 705 if not self.__source[line].rstrip().endswith("."):
731 # only correct summary lines can be fixed here 706 # only correct summary lines can be fixed here
732 return (0, "", 0) 707 return (0, "", 0)
733 708
734 self.__source[line] += self.__eol 709 self.__source[line] += self.__eol
735 return (1, QT_TRANSLATE_NOOP( 710 # Blank line inserted after docstring summary.
736 'CodeStyleFixer', 711 return (1, "FD144", 0)
737 "Blank line inserted after docstring summary."), 0)
738 else: 712 else:
739 id = self.__getID() 713 id = self.__getID()
740 self.__stack.append((id, code, line, pos)) 714 self.__stack.append((id, code, line, pos))
741 return (-1, "", id) 715 return (-1, "", id)
742 716
757 fix (integer) 731 fix (integer)
758 """ 732 """
759 if apply: 733 if apply:
760 line = line - 1 734 line = line - 1
761 self.__source[line] = self.__eol + self.__source[line] 735 self.__source[line] = self.__eol + self.__source[line]
762 return (1, QT_TRANSLATE_NOOP( 736 # Blank line inserted after last paragraph of docstring.
763 'CodeStyleFixer', "Blank line inserted after last paragraph" 737 return (1, "FD145", 0)
764 " of docstring."), 0)
765 else: 738 else:
766 id = self.__getID() 739 id = self.__getID()
767 self.__stack.append((id, code, line, pos)) 740 self.__stack.append((id, code, line, pos))
768 return (-1, "", id) 741 return (-1, "", id)
769 742
798 first, second = source[:-3].strip(), source[-3:] 771 first, second = source[:-3].strip(), source[-3:]
799 newText = indent + first + self.__eol + \ 772 newText = indent + first + self.__eol + \
800 indent + second + self.__eol 773 indent + second + self.__eol
801 self.__source[line] = newText 774 self.__source[line] = newText
802 if code == "D221": 775 if code == "D221":
803 msg = QT_TRANSLATE_NOOP( 776 # Leading quotes put on separate line.
804 'CodeStyleFixer', "Leading quotes put on separate line.") 777 msg = "FD221"
805 else: 778 else:
806 msg = QT_TRANSLATE_NOOP( 779 # Trailing quotes put on separate line.
807 'CodeStyleFixer', "Trailing quotes put on separate line.") 780 msg = "FD222"
808 return (1, msg, 0) 781 return (1, msg, 0)
809 else: 782 else:
810 id = self.__getID() 783 id = self.__getID()
811 self.__stack.append((id, code, line, pos)) 784 self.__stack.append((id, code, line, pos))
812 return (-1, "", id) 785 return (-1, "", id)
829 """ 802 """
830 if apply: 803 if apply:
831 line = line - 1 804 line = line - 1
832 self.__source[line - 1] = "" 805 self.__source[line - 1] = ""
833 if code == "D242": 806 if code == "D242":
834 msg = QT_TRANSLATE_NOOP( 807 # Blank line before class docstring removed.
835 'CodeStyleFixer', 808 msg = "FD242"
836 "Blank line before class docstring removed.")
837 else: 809 else:
838 msg = QT_TRANSLATE_NOOP( 810 # Blank line before function/method docstring removed.
839 'CodeStyleFixer', 811 msg = "FD244"
840 "Blank line before function/method docstring removed.")
841 return (1, msg, 0) 812 return (1, msg, 0)
842 else: 813 else:
843 id = self.__getID() 814 id = self.__getID()
844 self.__stack.append((id, code, line, pos)) 815 self.__stack.append((id, code, line, pos))
845 return (-1, "", id) 816 return (-1, "", id)
862 """ 833 """
863 if apply: 834 if apply:
864 line = line - 1 835 line = line - 1
865 self.__source[line + 1] = "" 836 self.__source[line + 1] = ""
866 if code == "D243": 837 if code == "D243":
867 msg = QT_TRANSLATE_NOOP( 838 # Blank line after class docstring removed.
868 'CodeStyleFixer', 839 msg = "FD243"
869 "Blank line after class docstring removed.")
870 else: 840 else:
871 msg = QT_TRANSLATE_NOOP( 841 # Blank line after function/method docstring removed.
872 'CodeStyleFixer', 842 msg = "FD245"
873 "Blank line after function/method docstring removed.")
874 return (1, msg, 0) 843 return (1, msg, 0)
875 else: 844 else:
876 id = self.__getID() 845 id = self.__getID()
877 self.__stack.append((id, code, line, pos)) 846 self.__stack.append((id, code, line, pos))
878 return (-1, "", id) 847 return (-1, "", id)
894 fix (integer) 863 fix (integer)
895 """ 864 """
896 if apply: 865 if apply:
897 line = line - 1 866 line = line - 1
898 self.__source[line - 1] = "" 867 self.__source[line - 1] = ""
899 return (1, QT_TRANSLATE_NOOP( 868 # Blank line after last paragraph removed.
900 'CodeStyleFixer', "Blank line after last paragraph removed."), 869 return (1, "FD247", 0)
901 0)
902 else: 870 else:
903 id = self.__getID() 871 id = self.__getID()
904 self.__stack.append((id, code, line, pos)) 872 self.__stack.append((id, code, line, pos))
905 return (-1, "", id) 873 return (-1, "", id)
906 874
922 self.__reindenter.run() 890 self.__reindenter.run()
923 fixedLine = self.__reindenter.fixedLine(line - 1) 891 fixedLine = self.__reindenter.fixedLine(line - 1)
924 if fixedLine is not None and fixedLine != self.__source[line - 1]: 892 if fixedLine is not None and fixedLine != self.__source[line - 1]:
925 self.__source[line - 1] = fixedLine 893 self.__source[line - 1] = fixedLine
926 if code in ["E101", "W191"]: 894 if code in ["E101", "W191"]:
927 msg = QT_TRANSLATE_NOOP( 895 # Tab converted to 4 spaces.
928 'CodeStyleFixer', "Tab converted to 4 spaces.") 896 msg = "FE101"
929 else: 897 else:
930 msg = QT_TRANSLATE_NOOP( 898 # Indentation adjusted to be a multiple of four.
931 'CodeStyleFixer', 899 msg = "FE111"
932 "Indentation adjusted to be a multiple of four.")
933 return (1, msg, 0) 900 return (1, msg, 0)
934 else: 901 else:
935 return (0, "", 0) 902 return (0, "", 0)
936 903
937 def __fixE121(self, code, line, pos, apply=False): 904 def __fixE121(self, code, line, pos, apply=False):
955 if logical: 922 if logical:
956 # Fix by adjusting initial indent level. 923 # Fix by adjusting initial indent level.
957 changed = self.__fixReindent(line, pos, logical) 924 changed = self.__fixReindent(line, pos, logical)
958 if changed: 925 if changed:
959 if code == "E121": 926 if code == "E121":
960 msg = QT_TRANSLATE_NOOP( 927 # Indentation of continuation line corrected.
961 'CodeStyleFixer', 928 msg = "FE121"
962 "Indentation of continuation line corrected.")
963 elif code == "E124": 929 elif code == "E124":
964 msg = QT_TRANSLATE_NOOP( 930 # Indentation of closing bracket corrected.
965 'CodeStyleFixer', 931 msg = "FE124"
966 "Indentation of closing bracket corrected.")
967 return (1, msg, 0) 932 return (1, msg, 0)
968 return (0, "", 0) 933 return (0, "", 0)
969 else: 934 else:
970 id = self.__getID() 935 id = self.__getID()
971 self.__stackLogical.append((id, code, line, pos)) 936 self.__stackLogical.append((id, code, line, pos))
996 line = line - 1 961 line = line - 1
997 text = self.__source[line] 962 text = self.__source[line]
998 indentation = self.__getIndent(text) 963 indentation = self.__getIndent(text)
999 self.__source[line] = indentation + \ 964 self.__source[line] = indentation + \
1000 self.__indentWord + text.lstrip() 965 self.__indentWord + text.lstrip()
1001 return (1, QT_TRANSLATE_NOOP( 966 # Missing indentation of continuation line corrected.
1002 'CodeStyleFixer', "Missing indentation of continuation" 967 return (1, "FE122", 0)
1003 " line corrected."), 0)
1004 return (0, "", 0) 968 return (0, "", 0)
1005 else: 969 else:
1006 id = self.__getID() 970 id = self.__getID()
1007 self.__stackLogical.append((id, code, line, pos)) 971 self.__stackLogical.append((id, code, line, pos))
1008 return (-1, "", id) 972 return (-1, "", id)
1035 changed = self.__fixReindent(line, pos, logical) 999 changed = self.__fixReindent(line, pos, logical)
1036 else: 1000 else:
1037 self.__source[row] = newText 1001 self.__source[row] = newText
1038 changed = True 1002 changed = True
1039 if changed: 1003 if changed:
1040 return (1, QT_TRANSLATE_NOOP('CodeStyleFixer', 1004 # Closing bracket aligned to opening bracket.
1041 "Closing bracket aligned to opening bracket."), 0) 1005 return (1, "FE123", 0)
1042 return (0, "", 0) 1006 return (0, "", 0)
1043 else: 1007 else:
1044 id = self.__getID() 1008 id = self.__getID()
1045 self.__stackLogical.append((id, code, line, pos)) 1009 self.__stackLogical.append((id, code, line, pos))
1046 return (-1, "", id) 1010 return (-1, "", id)
1069 if not modified: 1033 if not modified:
1070 row = line - 1 1034 row = line - 1
1071 text = self.__source[row] 1035 text = self.__source[row]
1072 self.__source[row] = self.__getIndent(text) + \ 1036 self.__source[row] = self.__getIndent(text) + \
1073 self.__indentWord + text.lstrip() 1037 self.__indentWord + text.lstrip()
1074 return (1, QT_TRANSLATE_NOOP( 1038 # Indentation level changed.
1075 'CodeStyleFixer', "Indentation level changed."), 0) 1039 return (1, "FE125", 0)
1076 return (0, "", 0) 1040 return (0, "", 0)
1077 else: 1041 else:
1078 id = self.__getID() 1042 id = self.__getID()
1079 self.__stackLogical.append((id, code, line, pos)) 1043 self.__stackLogical.append((id, code, line, pos))
1080 return (-1, "", id) 1044 return (-1, "", id)
1109 changed = self.__fixReindent(line, pos, logical) 1073 changed = self.__fixReindent(line, pos, logical)
1110 else: 1074 else:
1111 self.__source[row] = newText 1075 self.__source[row] = newText
1112 changed = True 1076 changed = True
1113 if changed: 1077 if changed:
1114 return (1, QT_TRANSLATE_NOOP( 1078 # Indentation level of hanging indentation changed.
1115 'CodeStyleFixer', 1079 return (1, "FE126", 0)
1116 "Indentation level of hanging indentation "
1117 "changed."), 0)
1118 return (0, "", 0) 1080 return (0, "", 0)
1119 else: 1081 else:
1120 id = self.__getID() 1082 id = self.__getID()
1121 self.__stackLogical.append((id, code, line, pos)) 1083 self.__stackLogical.append((id, code, line, pos))
1122 return (-1, "", id) 1084 return (-1, "", id)
1166 changed = self.__fixReindent(line, pos, logical) 1128 changed = self.__fixReindent(line, pos, logical)
1167 else: 1129 else:
1168 self.__source[row] = newText 1130 self.__source[row] = newText
1169 changed = True 1131 changed = True
1170 if changed: 1132 if changed:
1171 return (1, QT_TRANSLATE_NOOP( 1133 # Visual indentation corrected.
1172 'CodeStyleFixer', "Visual indentation corrected."), 1134 return (1, "FE127", 0)
1173 0)
1174 return (0, "", 0) 1135 return (0, "", 0)
1175 else: 1136 else:
1176 id = self.__getID() 1137 id = self.__getID()
1177 self.__stackLogical.append((id, code, line, pos)) 1138 self.__stackLogical.append((id, code, line, pos))
1178 return (-1, "", id) 1139 return (-1, "", id)
1199 newText = self.__fixWhitespace(text, pos, '') 1160 newText = self.__fixWhitespace(text, pos, '')
1200 if newText == text: 1161 if newText == text:
1201 return (0, "", 0) 1162 return (0, "", 0)
1202 1163
1203 self.__source[line] = newText 1164 self.__source[line] = newText
1204 return (1, QT_TRANSLATE_NOOP( 1165 # Extraneous whitespace removed.
1205 'CodeStyleFixer', "Extraneous whitespace removed."), 0) 1166 return (1, "FE201", 0)
1206 1167
1207 def __fixE221(self, code, line, pos): 1168 def __fixE221(self, code, line, pos):
1208 """ 1169 """
1209 Private method to fix extraneous whitespace around operator or 1170 Private method to fix extraneous whitespace around operator or
1210 keyword. 1171 keyword.
1229 if newText == text: 1190 if newText == text:
1230 return (0, "", 0) 1191 return (0, "", 0)
1231 1192
1232 self.__source[line] = newText 1193 self.__source[line] = newText
1233 if code in ["E225", "E226", "E227", "E228"]: 1194 if code in ["E225", "E226", "E227", "E228"]:
1234 return (1, QT_TRANSLATE_NOOP( 1195 # Missing whitespace added.
1235 'CodeStyleFixer', "Missing whitespace added."), 0) 1196 return (1, "", 0)
1236 else: 1197 else:
1237 return (1, QT_TRANSLATE_NOOP( 1198 # Extraneous whitespace removed.
1238 'CodeStyleFixer', "Extraneous whitespace removed."), 0) 1199 return (1, "", 0)
1239 1200
1240 def __fixE231(self, code, line, pos): 1201 def __fixE231(self, code, line, pos):
1241 """ 1202 """
1242 Private method to fix missing whitespace after ',;:'. 1203 Private method to fix missing whitespace after ',;:'.
1243 1204
1252 """ 1213 """
1253 line = line - 1 1214 line = line - 1
1254 pos = pos + 1 1215 pos = pos + 1
1255 self.__source[line] = self.__source[line][:pos] + \ 1216 self.__source[line] = self.__source[line][:pos] + \
1256 " " + self.__source[line][pos:] 1217 " " + self.__source[line][pos:]
1257 return (1, QT_TRANSLATE_NOOP( 1218 # Missing whitespace added.
1258 'CodeStyleFixer', "Missing whitespace added."), 0) 1219 return (1, "FE231", 0)
1259 1220
1260 def __fixE251(self, code, line, pos): 1221 def __fixE251(self, code, line, pos):
1261 """ 1222 """
1262 Private method to fix extraneous whitespace around keyword and 1223 Private method to fix extraneous whitespace around keyword and
1263 default parameter equals. 1224 default parameter equals.
1290 if newText.endswith(('=\\\n', '=\\\r\n', '=\\\r')): 1251 if newText.endswith(('=\\\n', '=\\\r\n', '=\\\r')):
1291 self.__source[line] = newText.rstrip("\n\r \t\\") 1252 self.__source[line] = newText.rstrip("\n\r \t\\")
1292 self.__source[line + 1] = self.__source[line + 1].lstrip() 1253 self.__source[line + 1] = self.__source[line + 1].lstrip()
1293 else: 1254 else:
1294 self.__source[line] = newText 1255 self.__source[line] = newText
1295 return (1, QT_TRANSLATE_NOOP( 1256 # Extraneous whitespace removed.
1296 'CodeStyleFixer', "Extraneous whitespace removed."), 0) 1257 return (1, "FE251", 0)
1297 1258
1298 def __fixE261(self, code, line, pos): 1259 def __fixE261(self, code, line, pos):
1299 """ 1260 """
1300 Private method to fix whitespace before or after inline comment. 1261 Private method to fix whitespace before or after inline comment.
1301 1262
1312 text = self.__source[line] 1273 text = self.__source[line]
1313 left = text[:pos].rstrip(' \t#') 1274 left = text[:pos].rstrip(' \t#')
1314 right = text[pos:].lstrip(' \t#') 1275 right = text[pos:].lstrip(' \t#')
1315 newText = left + (" # " + right if right.strip() else right) 1276 newText = left + (" # " + right if right.strip() else right)
1316 self.__source[line] = newText 1277 self.__source[line] = newText
1317 return (1, QT_TRANSLATE_NOOP( 1278 # Whitespace around comment sign corrected.
1318 'CodeStyleFixer', "Whitespace around comment sign corrected."), 1279 return (1, "FE261", 0)
1319 0)
1320 1280
1321 def __fixE301(self, code, line, pos, apply=False): 1281 def __fixE301(self, code, line, pos, apply=False):
1322 """ 1282 """
1323 Private method to fix the need for one blank line. 1283 Private method to fix the need for one blank line.
1324 1284
1333 a message for the fix (string) and an ID for a deferred 1293 a message for the fix (string) and an ID for a deferred
1334 fix (integer) 1294 fix (integer)
1335 """ 1295 """
1336 if apply: 1296 if apply:
1337 self.__source.insert(line - 1, self.__eol) 1297 self.__source.insert(line - 1, self.__eol)
1338 return (1, QT_TRANSLATE_NOOP( 1298 # One blank line inserted.
1339 'CodeStyleFixer', "One blank line inserted."), 0) 1299 return (1, "FE301", 0)
1340 else: 1300 else:
1341 id = self.__getID() 1301 id = self.__getID()
1342 self.__stack.append((id, code, line, pos)) 1302 self.__stack.append((id, code, line, pos))
1343 return (-1, "", id) 1303 return (-1, "", id)
1344 1304
1373 if delta < 0: 1333 if delta < 0:
1374 # insert blank lines (one or two) 1334 # insert blank lines (one or two)
1375 while delta < 0: 1335 while delta < 0:
1376 self.__source.insert(line, self.__eol) 1336 self.__source.insert(line, self.__eol)
1377 delta += 1 1337 delta += 1
1378 changed = True 1338 # %n blank line(s) inserted.
1339 return (1, ("FE302+", 2 - blanks), 0)
1379 elif delta > 0: 1340 elif delta > 0:
1380 # delete superfluous blank lines 1341 # delete superfluous blank lines
1381 while delta > 0: 1342 while delta > 0:
1382 del self.__source[line - 1] 1343 del self.__source[line - 1]
1383 line -= 1 1344 line -= 1
1384 delta -= 1 1345 delta -= 1
1385 changed = True 1346 # %n superfluous line(s) removed.
1347 return (1, ("FE302-", blanks - 2), 0)
1386 else: 1348 else:
1387 changed = False 1349 return (0, "", 0)
1388
1389 if changed:
1390 if delta < 0:
1391 msg = QT_TRANSLATE_NOOP(
1392 'CodeStyleFixer', "{0} blank line(s) inserted.") + \
1393 '@@{0}'.format(-delta)
1394 elif delta > 0:
1395 msg = QT_TRANSLATE_NOOP(
1396 'CodeStyleFixer',
1397 "{0} superfluous line(s) removed.") + \
1398 '@@{0}'.format(delta)
1399 else:
1400 msg = ""
1401 return (1, msg, 0)
1402 return (0, "", 0)
1403 else: 1350 else:
1404 id = self.__getID() 1351 id = self.__getID()
1405 self.__stack.append((id, code, line, pos)) 1352 self.__stack.append((id, code, line, pos))
1406 return (-1, "", id) 1353 return (-1, "", id)
1407 1354
1426 if self.__source[index].strip() == "": 1373 if self.__source[index].strip() == "":
1427 del self.__source[index] 1374 del self.__source[index]
1428 index -= 1 1375 index -= 1
1429 else: 1376 else:
1430 break 1377 break
1431 return (1, QT_TRANSLATE_NOOP( 1378 # Superfluous blank lines removed.
1432 'CodeStyleFixer', "Superfluous blank lines removed."), 0) 1379 return (1, "FE303", 0)
1433 else: 1380 else:
1434 id = self.__getID() 1381 id = self.__getID()
1435 self.__stack.append((id, code, line, pos)) 1382 self.__stack.append((id, code, line, pos))
1436 return (-1, "", id) 1383 return (-1, "", id)
1437 1384
1457 if self.__source[index].strip() == "": 1404 if self.__source[index].strip() == "":
1458 del self.__source[index] 1405 del self.__source[index]
1459 index -= 1 1406 index -= 1
1460 else: 1407 else:
1461 break 1408 break
1462 return (1, QT_TRANSLATE_NOOP('CodeStyleFixer', 1409 # Superfluous blank lines after function decorator removed.
1463 "Superfluous blank lines after function decorator " 1410 return (1, "FE304", 0)
1464 "removed."), 0)
1465 else: 1411 else:
1466 id = self.__getID() 1412 id = self.__getID()
1467 self.__stack.append((id, code, line, pos)) 1413 self.__stack.append((id, code, line, pos))
1468 return (-1, "", id) 1414 return (-1, "", id)
1469 1415
1495 return (0, "", 0) 1441 return (0, "", 0)
1496 1442
1497 newText = text[:pos].rstrip("\t ,") + self.__eol + \ 1443 newText = text[:pos].rstrip("\t ,") + self.__eol + \
1498 self.__getIndent(text) + "import " + text[pos:].lstrip("\t ,") 1444 self.__getIndent(text) + "import " + text[pos:].lstrip("\t ,")
1499 self.__source[line] = newText 1445 self.__source[line] = newText
1500 return (1, QT_TRANSLATE_NOOP( 1446 # Imports were put on separate lines.
1501 'CodeStyleFixer', "Imports were put on separate lines."), 1447 return (1, "FE401", 0)
1502 0)
1503 else: 1448 else:
1504 id = self.__getID() 1449 id = self.__getID()
1505 self.__stack.append((id, code, line, pos)) 1450 self.__stack.append((id, code, line, pos))
1506 return (-1, "", id) 1451 return (-1, "", id)
1507 1452
1544 self.__source[line] = newText 1489 self.__source[line] = newText
1545 if newNextText and newNextText != nextText: 1490 if newNextText and newNextText != nextText:
1546 if newNextText == " ": 1491 if newNextText == " ":
1547 newNextText = "" 1492 newNextText = ""
1548 self.__source[line + 1] = newNextText 1493 self.__source[line + 1] = newNextText
1549 return (1, QT_TRANSLATE_NOOP( 1494 # Long lines have been shortened.
1550 'CodeStyleFixer', "Long lines have been shortened."), 1495 return (1, "FE501", 0)
1551 0)
1552 else: 1496 else:
1553 return (0, "", 0) 1497 return (0, "", 0)
1554 else: 1498 else:
1555 id = self.__getID() 1499 id = self.__getID()
1556 self.__stack.append((id, code, line, pos)) 1500 self.__stack.append((id, code, line, pos))
1569 a message for the fix (string) and an ID for a deferred 1513 a message for the fix (string) and an ID for a deferred
1570 fix (integer) 1514 fix (integer)
1571 """ 1515 """
1572 self.__source[line - 1] = \ 1516 self.__source[line - 1] = \
1573 self.__source[line - 1].rstrip("\n\r \t\\") + self.__eol 1517 self.__source[line - 1].rstrip("\n\r \t\\") + self.__eol
1574 return (1, QT_TRANSLATE_NOOP( 1518 # Redundant backslash in brackets removed.
1575 'CodeStyleFixer', "Redundant backslash in brackets removed."), 1519 return (1, "FE502", 0)
1576 0)
1577 1520
1578 def __fixE701(self, code, line, pos, apply=False): 1521 def __fixE701(self, code, line, pos, apply=False):
1579 """ 1522 """
1580 Private method to fix colon-separated compound statements. 1523 Private method to fix colon-separated compound statements.
1581 1524
1597 1540
1598 newText = text[:pos] + self.__eol + self.__getIndent(text) + \ 1541 newText = text[:pos] + self.__eol + self.__getIndent(text) + \
1599 self.__indentWord + text[pos:].lstrip("\n\r \t\\") + \ 1542 self.__indentWord + text[pos:].lstrip("\n\r \t\\") + \
1600 self.__eol 1543 self.__eol
1601 self.__source[line] = newText 1544 self.__source[line] = newText
1602 return (1, QT_TRANSLATE_NOOP( 1545 # Compound statement corrected.
1603 'CodeStyleFixer', "Compound statement corrected."), 0) 1546 return (1, "FE701", 0)
1604 else: 1547 else:
1605 id = self.__getID() 1548 id = self.__getID()
1606 self.__stack.append((id, code, line, pos)) 1549 self.__stack.append((id, code, line, pos))
1607 return (-1, "", id) 1550 return (-1, "", id)
1608 1551
1633 self.__source[line] = text.rstrip("\n\r \t;") + self.__eol 1576 self.__source[line] = text.rstrip("\n\r \t;") + self.__eol
1634 else: 1577 else:
1635 first = text[:pos].rstrip("\n\r \t;") + self.__eol 1578 first = text[:pos].rstrip("\n\r \t;") + self.__eol
1636 second = text[pos:].lstrip("\n\r \t;") 1579 second = text[pos:].lstrip("\n\r \t;")
1637 self.__source[line] = first + self.__getIndent(text) + second 1580 self.__source[line] = first + self.__getIndent(text) + second
1638 return (1, QT_TRANSLATE_NOOP( 1581 # Compound statement corrected.
1639 'CodeStyleFixer', "Compound statement corrected."), 0) 1582 return (1, "FE702", 0)
1640 else: 1583 else:
1641 id = self.__getID() 1584 id = self.__getID()
1642 self.__stack.append((id, code, line, pos)) 1585 self.__stack.append((id, code, line, pos))
1643 return (-1, "", id) 1586 return (-1, "", id)
1644 1587
1675 center = "is not" 1618 center = "is not"
1676 else: 1619 else:
1677 return (0, "", 0) 1620 return (0, "", 0)
1678 1621
1679 self.__source[line] = " ".join([left, center, right]) 1622 self.__source[line] = " ".join([left, center, right])
1680 return (1, QT_TRANSLATE_NOOP( 1623 # Comparison to None/True/False corrected.
1681 'CodeStyleFixer', "Comparison to None/True/False corrected."), 1624 return (1, "FE711", 0)
1682 0)
1683 1625
1684 def __fixN804(self, code, line, pos, apply=False): 1626 def __fixN804(self, code, line, pos, apply=False):
1685 """ 1627 """
1686 Private method to fix a wrong first argument of normal and 1628 Private method to fix a wrong first argument of normal and
1687 class methods. 1629 class methods.
1716 center = arg 1658 center = arg
1717 else: 1659 else:
1718 center = arg + ", " 1660 center = arg + ", "
1719 newText = left + center + right 1661 newText = left + center + right
1720 self.__source[line] = newText 1662 self.__source[line] = newText
1721 return (1, QT_TRANSLATE_NOOP( 1663 # '{0}' argument added.
1722 'CodeStyleFixer', "'{0}' argument added.") + 1664 return (1, ("FN804", (arg,)), 0)
1723 '@@{0}'.format(arg), 0)
1724 else: 1665 else:
1725 id = self.__getID() 1666 id = self.__getID()
1726 self.__stack.append((id, code, line, pos)) 1667 self.__stack.append((id, code, line, pos))
1727 return (-1, "", id) 1668 return (-1, "", id)
1728 1669
1778 self.__source[line - 1].rstrip() + right 1719 self.__source[line - 1].rstrip() + right
1779 self.__source[line] = "" 1720 self.__source[line] = ""
1780 else: 1721 else:
1781 self.__source[line] = indent + right 1722 self.__source[line] = indent + right
1782 1723
1783 return (1, QT_TRANSLATE_NOOP( 1724 # '{0}' argument removed.
1784 'CodeStyleFixer', "'{0}' argument removed.") + 1725 return (1, ("FN806", arg), 0)
1785 '@@{0}'.format(arg), 0)
1786 else: 1726 else:
1787 id = self.__getID() 1727 id = self.__getID()
1788 self.__stack.append((id, code, line, pos)) 1728 self.__stack.append((id, code, line, pos))
1789 return (-1, "", id) 1729 return (-1, "", id)
1790 1730
1801 a message for the fix (string) and an ID for a deferred 1741 a message for the fix (string) and an ID for a deferred
1802 fix (integer) 1742 fix (integer)
1803 """ 1743 """
1804 self.__source[line - 1] = re.sub(r'[\t ]+(\r?)$', r"\1", 1744 self.__source[line - 1] = re.sub(r'[\t ]+(\r?)$', r"\1",
1805 self.__source[line - 1]) 1745 self.__source[line - 1])
1806 return (1, QT_TRANSLATE_NOOP( 1746 # Whitespace stripped from end of line.
1807 'CodeStyleFixer', "Whitespace stripped from end of line."), 0) 1747 return (1, "FW291", 0)
1808 1748
1809 def __fixW292(self, code, line, pos): 1749 def __fixW292(self, code, line, pos):
1810 """ 1750 """
1811 Private method to fix a missing newline at the end of file. 1751 Private method to fix a missing newline at the end of file.
1812 1752
1818 @return value indicating an applied/deferred fix (-1, 0, 1), 1758 @return value indicating an applied/deferred fix (-1, 0, 1),
1819 a message for the fix (string) and an ID for a deferred 1759 a message for the fix (string) and an ID for a deferred
1820 fix (integer) 1760 fix (integer)
1821 """ 1761 """
1822 self.__source[line - 1] += self.__eol 1762 self.__source[line - 1] += self.__eol
1823 return (1, QT_TRANSLATE_NOOP( 1763 # newline added to end of file.
1824 'CodeStyleFixer', "newline added to end of file."), 0) 1764 return (1, "FW292", 0)
1825 1765
1826 def __fixW391(self, code, line, pos): 1766 def __fixW391(self, code, line, pos):
1827 """ 1767 """
1828 Private method to fix trailing blank lines. 1768 Private method to fix trailing blank lines.
1829 1769
1841 if self.__source[index].strip() == "": 1781 if self.__source[index].strip() == "":
1842 del self.__source[index] 1782 del self.__source[index]
1843 index -= 1 1783 index -= 1
1844 else: 1784 else:
1845 break 1785 break
1846 return (1, QT_TRANSLATE_NOOP( 1786 # Superfluous trailing blank lines removed from end of file.
1847 'CodeStyleFixer', 1787 return (1, "FW391", 0)
1848 "Superfluous trailing blank lines removed from end of file."),
1849 0)
1850 1788
1851 def __fixW603(self, code, line, pos): 1789 def __fixW603(self, code, line, pos):
1852 """ 1790 """
1853 Private method to fix the not equal notation. 1791 Private method to fix the not equal notation.
1854 1792
1860 @return value indicating an applied/deferred fix (-1, 0, 1), 1798 @return value indicating an applied/deferred fix (-1, 0, 1),
1861 a message for the fix (string) and an ID for a deferred 1799 a message for the fix (string) and an ID for a deferred
1862 fix (integer) 1800 fix (integer)
1863 """ 1801 """
1864 self.__source[line - 1] = self.__source[line - 1].replace("<>", "!=") 1802 self.__source[line - 1] = self.__source[line - 1].replace("<>", "!=")
1865 return (1, QT_TRANSLATE_NOOP( 1803 # '<>' replaced by '!='.
1866 'CodeStyleFixer', "'<>' replaced by '!='."), 0) 1804 return (1, "FW603", 0)
1867 1805
1868 1806
1869 class Reindenter(object): 1807 class Reindenter(object):
1870 """ 1808 """
1871 Class to reindent badly-indented code to uniformly use four-space 1809 Class to reindent badly-indented code to uniformly use four-space

eric ide

mercurial