Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py

branch
BgService
changeset 3209
c5432abceb25
parent 3145
a9de05d4a22f
child 3228
f489068e51e8
equal deleted inserted replaced
3177:5af61402d74d 3209:c5432abceb25
5 5
6 """ 6 """
7 Module implementing a class to fix certain code style issues. 7 Module implementing a class to fix certain code style issues.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 try:
11 11 # Python 2
12 from StringIO import StringIO # __IGNORE_EXCEPTION__
13 except ImportError:
14 # Python 3
15 from io import StringIO # __IGNORE_WARNING__
12 import os 16 import os
13 import re 17 import re
18 import sys
14 import tokenize 19 import tokenize
15 import io 20
16 21 import pep8
17 from PyQt4.QtCore import QObject 22
18 23 # Tell 'lupdate' which strings to keep for translation.
19 from E5Gui import E5MessageBox 24 QT_TRANSLATE_NOOP = lambda mod, txt: txt
20
21 from . import pep8
22
23 import Utilities
24 25
25 FixableCodeStyleIssues = [ 26 FixableCodeStyleIssues = [
26 "D111", "D112", "D113", "D121", "D131", "D141", 27 "D111", "D112", "D113", "D121", "D131", "D141",
27 "D142", "D143", "D144", "D145", 28 "D142", "D143", "D144", "D145",
28 "D221", "D222", "D231", "D242", "D243", "D244", 29 "D221", "D222", "D231", "D242", "D243", "D244",
38 "N804", "N805", "N806", 39 "N804", "N805", "N806",
39 "W191", "W291", "W292", "W293", "W391", "W603", 40 "W191", "W291", "W292", "W293", "W391", "W603",
40 ] 41 ]
41 42
42 43
43 class CodeStyleFixer(QObject): 44 class CodeStyleFixer(object):
44 """ 45 """
45 Class implementing a fixer for certain code style issues. 46 Class implementing a fixer for certain code style issues.
46 """ 47 """
47 def __init__(self, project, filename, sourceLines, fixCodes, noFixCodes, 48 def __init__(self, filename, sourceLines, fixCodes, noFixCodes,
48 maxLineLength, inPlace): 49 maxLineLength, inPlace, eol):
49 """ 50 """
50 Constructor 51 Constructor
51 52
52 @param project reference to the project object (Project)
53 @param filename name of the file to be fixed (string) 53 @param filename name of the file to be fixed (string)
54 @param sourceLines list of source lines including eol marker 54 @param sourceLines list of source lines including eol marker
55 (list of string) 55 (list of string)
56 @param fixCodes list of codes to be fixed as a comma separated 56 @param fixCodes list of codes to be fixed as a comma separated
57 string (string) 57 string (string)
58 @param noFixCodes list of codes not to be fixed as a comma 58 @param noFixCodes list of codes not to be fixed as a comma
59 separated string (string) 59 separated string (string)
60 @param maxLineLength maximum allowed line length (integer) 60 @param maxLineLength maximum allowed line length (integer)
61 @param inPlace flag indicating to modify the file in place (boolean) 61 @param inPlace flag indicating to modify the file in place (boolean)
62 @param eol end of line character(s) (string)
62 """ 63 """
63 super(CodeStyleFixer, self).__init__() 64 super(CodeStyleFixer, self).__init__()
64 65
65 self.__project = project
66 self.__filename = filename 66 self.__filename = filename
67 self.__origName = "" 67 self.__origName = ""
68 self.__source = sourceLines[:] # save a copy 68 self.__source = sourceLines[:] # save a copy
69 self.__fixCodes = [c.strip() for c in fixCodes.split(",") if c.strip()] 69 self.__fixCodes = [c.strip() for c in fixCodes.split(",") if c.strip()]
70 self.__noFixCodes = [ 70 self.__noFixCodes = [
71 c.strip() for c in noFixCodes.split(",") if c.strip()] 71 c.strip() for c in noFixCodes.split(",") if c.strip()]
72 self.__maxLineLength = maxLineLength 72 self.__maxLineLength = maxLineLength
73 self.fixed = 0 73 self.fixed = 0
74 74
75 self.__reindenter = None 75 self.__reindenter = None
76 self.__eol = ""
77 self.__indentWord = self.__getIndentWord() 76 self.__indentWord = self.__getIndentWord()
78 77
79 if not inPlace: 78 if inPlace:
79 # TODO: Do a backup before any changes
80 pass
81 else:
80 self.__origName = self.__filename 82 self.__origName = self.__filename
81 self.__filename = os.path.join( 83 self.__filename = os.path.join(
82 os.path.dirname(self.__filename), 84 os.path.dirname(self.__filename),
83 "fixed_" + os.path.basename(self.__filename)) 85 "fixed_" + os.path.basename(self.__filename))
84 86 self.__eol = eol
87
85 self.__fixes = { 88 self.__fixes = {
86 "D111": self.__fixD111, 89 "D111": self.__fixD111,
87 "D112": self.__fixD112, 90 "D112": self.__fixD112,
88 "D113": self.__fixD112, 91 "D113": self.__fixD112,
89 "D121": self.__fixD121, 92 "D121": self.__fixD121,
174 Public method to save the modified file. 177 Public method to save the modified file.
175 178
176 @param encoding encoding of the source file (string) 179 @param encoding encoding of the source file (string)
177 @return flag indicating success (boolean) 180 @return flag indicating success (boolean)
178 """ 181 """
182 import codecs
183
179 if not self.__modified: 184 if not self.__modified:
180 # no need to write 185 # no need to write
181 return True 186 return True
182 187
183 txt = "".join(self.__source) 188 txt = "".join(self.__source)
184 try: 189 try:
185 Utilities.writeEncodedFile(self.__filename, txt, encoding) 190 if sys.version_info[0] == 3:
186 except (IOError, Utilities.CodingError, UnicodeError) as err: 191 txt = txt.encode(encoding)
187 E5MessageBox.critical( 192 if encoding == 'utf-8-bom':
188 self, 193 txt = codecs.BOM_UTF8 + txt
189 self.trUtf8("Fix Code Style Issues"), 194
190 self.trUtf8( 195 with open(self.__filename, "wb") as fp:
191 """<p>Could not save the file <b>{0}</b>.""" 196 fp.write(txt)
192 """ Skipping it.</p><p>Reason: {1}</p>""") 197 except (IOError, UnicodeError): # as err:
193 .format(self.__filename, str(err)) 198 # E5MessageBox.critical(
194 ) 199 # self,
200 # self.trUtf8("Fix Code Style Issues"),
201 # self.trUtf8(
202 # """<p>Could not save the file <b>{0}</b>."""
203 # """ Skipping it.</p><p>Reason: {1}</p>""")
204 # .format(self.__filename, str(err))
205 # )
195 return False 206 return False
196 207
197 return True 208 return True
198 209
199 def __codeMatch(self, code): 210 def __codeMatch(self, code):
284 @return ID for a deferred fix (integer) 295 @return ID for a deferred fix (integer)
285 """ 296 """
286 self.__lastID += 1 297 self.__lastID += 1
287 return self.__lastID 298 return self.__lastID
288 299
289 def __getEol(self):
290 """
291 Private method to get the applicable eol string.
292
293 @return eol string (string)
294 """
295 if not self.__eol:
296 if self.__origName:
297 fn = self.__origName
298 else:
299 fn = self.__filename
300
301 if self.__project.isOpen() and self.__project.isProjectFile(fn):
302 self.__eol = self.__project.getEolString()
303 else:
304 self.__eol = Utilities.linesep()
305 return self.__eol
306
307 def __findLogical(self): 300 def __findLogical(self):
308 """ 301 """
309 Private method to extract the index of all the starts and ends of 302 Private method to extract the index of all the starts and ends of
310 lines. 303 lines.
311 304
313 of lines 306 of lines
314 """ 307 """
315 logical_start = [] 308 logical_start = []
316 logical_end = [] 309 logical_end = []
317 last_newline = True 310 last_newline = True
318 sio = io.StringIO("".join(self.__source)) 311 sio = StringIO("".join(self.__source))
319 parens = 0 312 parens = 0
320 for t in tokenize.generate_tokens(sio.readline): 313 for t in tokenize.generate_tokens(sio.readline):
321 if t[0] in [tokenize.COMMENT, tokenize.DEDENT, 314 if t[0] in [tokenize.COMMENT, tokenize.DEDENT,
322 tokenize.INDENT, tokenize.NL, 315 tokenize.INDENT, tokenize.NL,
323 tokenize.ENDMARKER]: 316 tokenize.ENDMARKER]:
372 """ 365 """
373 Private method to determine the indentation type. 366 Private method to determine the indentation type.
374 367
375 @return string to be used for an indentation (string) 368 @return string to be used for an indentation (string)
376 """ 369 """
377 sio = io.StringIO("".join(self.__source)) 370 sio = StringIO("".join(self.__source))
378 indentWord = " " # default in case of failure 371 indentWord = " " # default in case of failure
379 try: 372 try:
380 for token in tokenize.generate_tokens(sio.readline): 373 for token in tokenize.generate_tokens(sio.readline):
381 if token[0] == tokenize.INDENT: 374 if token[0] == tokenize.INDENT:
382 indentWord = token[1] 375 indentWord = token[1]
403 string and a set of line numbers belonging to a multi line 396 string and a set of line numbers belonging to a multi line
404 documentation string (tuple of two set of integer) 397 documentation string (tuple of two set of integer)
405 """ 398 """
406 if self.__multiLineNumbers is None: 399 if self.__multiLineNumbers is None:
407 source = "".join(self.__source) 400 source = "".join(self.__source)
408 sio = io.StringIO(source) 401 sio = StringIO(source)
409 self.__multiLineNumbers = set() 402 self.__multiLineNumbers = set()
410 self.__docLineNumbers = set() 403 self.__docLineNumbers = set()
411 previousTokenType = '' 404 previousTokenType = ''
412 try: 405 try:
413 for t in tokenize.generate_tokens(sio.readline): 406 for t in tokenize.generate_tokens(sio.readline):
509 left, right = self.__source[line].rsplit("'''", 1) 502 left, right = self.__source[line].rsplit("'''", 1)
510 self.__source[line] = left + '"""' + right 503 self.__source[line] = left + '"""' + right
511 break 504 break
512 line += 1 505 line += 1
513 506
514 return ( 507 return (1, QT_TRANSLATE_NOOP(
515 1, 508 'CodeStyleFixer',
516 self.trUtf8( 509 "Triple single quotes converted to triple double quotes."), 0)
517 "Triple single quotes converted to triple double quotes."),
518 0)
519 510
520 def __fixD112(self, code, line, pos): 511 def __fixD112(self, code, line, pos):
521 """ 512 """
522 Private method to fix docstring 'r' or 'u' in leading quotes. 513 Private method to fix docstring 'r' or 'u' in leading quotes.
523 514
539 return (0, "", 0) 530 return (0, "", 0)
540 531
541 newText = self.__getIndent(self.__source[line]) + \ 532 newText = self.__getIndent(self.__source[line]) + \
542 insertChar + self.__source[line].lstrip() 533 insertChar + self.__source[line].lstrip()
543 self.__source[line] = newText 534 self.__source[line] = newText
544 return ( 535 return (1, QT_TRANSLATE_NOOP(
545 1, 536 'CodeStyleFixer',
546 self.trUtf8('Introductory quotes corrected to be {0}"""') 537 'Introductory quotes corrected to be {0}"""') +
547 .format(insertChar), 538 '@@{0}'.format(insertChar), 0)
548 0)
549 539
550 def __fixD121(self, code, line, pos, apply=False): 540 def __fixD121(self, code, line, pos, apply=False):
551 """ 541 """
552 Private method to fix a single line docstring on multiple lines. 542 Private method to fix a single line docstring on multiple lines.
553 543
570 return (0, "", 0) 560 return (0, "", 0)
571 561
572 docstring = self.__source[line].rstrip() + \ 562 docstring = self.__source[line].rstrip() + \
573 self.__source[line + 1].strip() 563 self.__source[line + 1].strip()
574 if docstring.endswith('"""'): 564 if docstring.endswith('"""'):
575 docstring += self.__getEol() 565 docstring += self.__eol
576 else: 566 else:
577 docstring += self.__source[line + 2].lstrip() 567 docstring += self.__source[line + 2].lstrip()
578 self.__source[line + 2] = "" 568 self.__source[line + 2] = ""
579 569
580 self.__source[line] = docstring 570 self.__source[line] = docstring
581 self.__source[line + 1] = "" 571 self.__source[line + 1] = ""
582 return ( 572 return (
583 1, 573 1,
584 self.trUtf8("Single line docstring put on one line."), 574 QT_TRANSLATE_NOOP(
575 'CodeStyleFixer',
576 "Single line docstring put on one line."),
585 0) 577 0)
586 else: 578 else:
587 id = self.__getID() 579 id = self.__getID()
588 self.__stack.append((id, code, line, pos)) 580 self.__stack.append((id, code, line, pos))
589 return (-1, "", id) 581 return (-1, "", id)
606 newText = "" 598 newText = ""
607 if self.__source[line].rstrip().endswith(('"""', "'''")) and \ 599 if self.__source[line].rstrip().endswith(('"""', "'''")) and \
608 self.__source[line].lstrip().startswith(('"""', 'r"""', 'u"""')): 600 self.__source[line].lstrip().startswith(('"""', 'r"""', 'u"""')):
609 # it is a one-liner 601 # it is a one-liner
610 newText = self.__source[line].rstrip()[:-3].rstrip() + "." + \ 602 newText = self.__source[line].rstrip()[:-3].rstrip() + "." + \
611 self.__source[line].rstrip()[-3:] + self.__getEol() 603 self.__source[line].rstrip()[-3:] + self.__eol
612 else: 604 else:
613 if line < len(self.__source) - 1 and \ 605 if line < len(self.__source) - 1 and \
614 (not self.__source[line + 1].strip() or 606 (not self.__source[line + 1].strip() or
615 self.__source[line + 1].lstrip().startswith("@") or 607 self.__source[line + 1].lstrip().startswith("@") or
616 (self.__source[line + 1].strip() in ('"""', "'''") and 608 (self.__source[line + 1].strip() in ('"""', "'''") and
617 not self.__source[line].lstrip().startswith("@"))): 609 not self.__source[line].lstrip().startswith("@"))):
618 newText = self.__source[line].rstrip() + "." + self.__getEol() 610 newText = self.__source[line].rstrip() + "." + self.__eol
619 611
620 if newText: 612 if newText:
621 self.__source[line] = newText 613 self.__source[line] = newText
622 return (1, self.trUtf8("Period added to summary line."), 0) 614 return (
615 1,
616 QT_TRANSLATE_NOOP(
617 'CodeStyleFixer',
618 "Period added to summary line."),
619 0)
623 else: 620 else:
624 return (0, "", 0) 621 return (0, "", 0)
625 622
626 def __fixD141(self, code, line, pos, apply=False): 623 def __fixD141(self, code, line, pos, apply=False):
627 """ 624 """
642 if apply: 639 if apply:
643 line = line - 1 640 line = line - 1
644 self.__source[line - 1] = "" 641 self.__source[line - 1] = ""
645 return ( 642 return (
646 1, 643 1,
647 self.trUtf8( 644 QT_TRANSLATE_NOOP(
645 'CodeStyleFixer',
648 "Blank line before function/method docstring removed."), 646 "Blank line before function/method docstring removed."),
649 0) 647 0)
650 else: 648 else:
651 id = self.__getID() 649 id = self.__getID()
652 self.__stack.append((id, code, line, pos)) 650 self.__stack.append((id, code, line, pos))
668 a message for the fix (string) and an ID for a deferred 666 a message for the fix (string) and an ID for a deferred
669 fix (integer) 667 fix (integer)
670 """ 668 """
671 if apply: 669 if apply:
672 line = line - 1 670 line = line - 1
673 self.__source[line] = self.__getEol() + self.__source[line] 671 self.__source[line] = self.__eol + self.__source[line]
674 return ( 672 return (1, QT_TRANSLATE_NOOP(
675 1, 673 'CodeStyleFixer',
676 self.trUtf8("Blank line inserted before class docstring."), 674 "Blank line inserted before class docstring."), 0)
677 0)
678 else: 675 else:
679 id = self.__getID() 676 id = self.__getID()
680 self.__stack.append((id, code, line, pos)) 677 self.__stack.append((id, code, line, pos))
681 return (-1, "", id) 678 return (-1, "", id)
682 679
696 a message for the fix (string) and an ID for a deferred 693 a message for the fix (string) and an ID for a deferred
697 fix (integer) 694 fix (integer)
698 """ 695 """
699 if apply: 696 if apply:
700 line = line - 1 697 line = line - 1
701 self.__source[line] += self.__getEol() 698 self.__source[line] += self.__eol
702 return ( 699 return (1, QT_TRANSLATE_NOOP(
703 1, 700 'CodeStyleFixer',
704 self.trUtf8("Blank line inserted after class docstring."), 701 "Blank line inserted after class docstring."), 0)
705 0)
706 else: 702 else:
707 id = self.__getID() 703 id = self.__getID()
708 self.__stack.append((id, code, line, pos)) 704 self.__stack.append((id, code, line, pos))
709 return (-1, "", id) 705 return (-1, "", id)
710 706
728 line = line - 1 724 line = line - 1
729 if not self.__source[line].rstrip().endswith("."): 725 if not self.__source[line].rstrip().endswith("."):
730 # only correct summary lines can be fixed here 726 # only correct summary lines can be fixed here
731 return (0, "", 0) 727 return (0, "", 0)
732 728
733 self.__source[line] += self.__getEol() 729 self.__source[line] += self.__eol
734 return ( 730 return (1, QT_TRANSLATE_NOOP(
735 1, 731 'CodeStyleFixer',
736 self.trUtf8("Blank line inserted after docstring summary."), 732 "Blank line inserted after docstring summary."), 0)
737 0)
738 else: 733 else:
739 id = self.__getID() 734 id = self.__getID()
740 self.__stack.append((id, code, line, pos)) 735 self.__stack.append((id, code, line, pos))
741 return (-1, "", id) 736 return (-1, "", id)
742 737
756 a message for the fix (string) and an ID for a deferred 751 a message for the fix (string) and an ID for a deferred
757 fix (integer) 752 fix (integer)
758 """ 753 """
759 if apply: 754 if apply:
760 line = line - 1 755 line = line - 1
761 self.__source[line] = self.__getEol() + self.__source[line] 756 self.__source[line] = self.__eol + self.__source[line]
762 return ( 757 return (1, QT_TRANSLATE_NOOP(
763 1, 758 'CodeStyleFixer', "Blank line inserted after last paragraph"
764 self.trUtf8("Blank line inserted after last paragraph" 759 " of docstring."), 0)
765 " of docstring."),
766 0)
767 else: 760 else:
768 id = self.__getID() 761 id = self.__getID()
769 self.__stack.append((id, code, line, pos)) 762 self.__stack.append((id, code, line, pos))
770 return (-1, "", id) 763 return (-1, "", id)
771 764
796 else: 789 else:
797 first, second = source[:3], source[3:].strip() 790 first, second = source[:3], source[3:].strip()
798 else: 791 else:
799 # trailing 792 # trailing
800 first, second = source[:-3].strip(), source[-3:] 793 first, second = source[:-3].strip(), source[-3:]
801 newText = indent + first + self.__getEol() + \ 794 newText = indent + first + self.__eol + \
802 indent + second + self.__getEol() 795 indent + second + self.__eol
803 self.__source[line] = newText 796 self.__source[line] = newText
804 if code == "D221": 797 if code == "D221":
805 msg = self.trUtf8("Leading quotes put on separate line.") 798 msg = QT_TRANSLATE_NOOP(
799 'CodeStyleFixer', "Leading quotes put on separate line.")
806 else: 800 else:
807 msg = self.trUtf8("Trailing quotes put on separate line.") 801 msg = QT_TRANSLATE_NOOP(
802 'CodeStyleFixer', "Trailing quotes put on separate line.")
808 return (1, msg, 0) 803 return (1, msg, 0)
809 else: 804 else:
810 id = self.__getID() 805 id = self.__getID()
811 self.__stack.append((id, code, line, pos)) 806 self.__stack.append((id, code, line, pos))
812 return (-1, "", id) 807 return (-1, "", id)
829 """ 824 """
830 if apply: 825 if apply:
831 line = line - 1 826 line = line - 1
832 self.__source[line - 1] = "" 827 self.__source[line - 1] = ""
833 if code == "D242": 828 if code == "D242":
834 msg = self.trUtf8("Blank line before class docstring removed.") 829 msg = QT_TRANSLATE_NOOP(
830 'CodeStyleFixer',
831 "Blank line before class docstring removed.")
835 else: 832 else:
836 msg = self.trUtf8( 833 msg = QT_TRANSLATE_NOOP(
834 'CodeStyleFixer',
837 "Blank line before function/method docstring removed.") 835 "Blank line before function/method docstring removed.")
838 return (1, msg, 0) 836 return (1, msg, 0)
839 else: 837 else:
840 id = self.__getID() 838 id = self.__getID()
841 self.__stack.append((id, code, line, pos)) 839 self.__stack.append((id, code, line, pos))
859 """ 857 """
860 if apply: 858 if apply:
861 line = line - 1 859 line = line - 1
862 self.__source[line + 1] = "" 860 self.__source[line + 1] = ""
863 if code == "D243": 861 if code == "D243":
864 msg = self.trUtf8("Blank line after class docstring removed.") 862 msg = QT_TRANSLATE_NOOP(
863 'CodeStyleFixer',
864 "Blank line after class docstring removed.")
865 else: 865 else:
866 msg = self.trUtf8( 866 msg = QT_TRANSLATE_NOOP(
867 'CodeStyleFixer',
867 "Blank line after function/method docstring removed.") 868 "Blank line after function/method docstring removed.")
868 return (1, msg, 0) 869 return (1, msg, 0)
869 else: 870 else:
870 id = self.__getID() 871 id = self.__getID()
871 self.__stack.append((id, code, line, pos)) 872 self.__stack.append((id, code, line, pos))
888 fix (integer) 889 fix (integer)
889 """ 890 """
890 if apply: 891 if apply:
891 line = line - 1 892 line = line - 1
892 self.__source[line - 1] = "" 893 self.__source[line - 1] = ""
893 return ( 894 return (1, QT_TRANSLATE_NOOP(
894 1, 895 'CodeStyleFixer', "Blank line after last paragraph removed."),
895 self.trUtf8("Blank line after last paragraph removed."),
896 0) 896 0)
897 else: 897 else:
898 id = self.__getID() 898 id = self.__getID()
899 self.__stack.append((id, code, line, pos)) 899 self.__stack.append((id, code, line, pos))
900 return (-1, "", id) 900 return (-1, "", id)
917 self.__reindenter.run() 917 self.__reindenter.run()
918 fixedLine = self.__reindenter.fixedLine(line - 1) 918 fixedLine = self.__reindenter.fixedLine(line - 1)
919 if fixedLine is not None and fixedLine != self.__source[line - 1]: 919 if fixedLine is not None and fixedLine != self.__source[line - 1]:
920 self.__source[line - 1] = fixedLine 920 self.__source[line - 1] = fixedLine
921 if code in ["E101", "W191"]: 921 if code in ["E101", "W191"]:
922 msg = self.trUtf8("Tab converted to 4 spaces.") 922 msg = QT_TRANSLATE_NOOP(
923 'CodeStyleFixer', "Tab converted to 4 spaces.")
923 else: 924 else:
924 msg = self.trUtf8( 925 msg = QT_TRANSLATE_NOOP(
926 'CodeStyleFixer',
925 "Indentation adjusted to be a multiple of four.") 927 "Indentation adjusted to be a multiple of four.")
926 return (1, msg, 0) 928 return (1, msg, 0)
927 else: 929 else:
928 return (0, "", 0) 930 return (0, "", 0)
929 931
948 if logical: 950 if logical:
949 # Fix by adjusting initial indent level. 951 # Fix by adjusting initial indent level.
950 changed = self.__fixReindent(line, pos, logical) 952 changed = self.__fixReindent(line, pos, logical)
951 if changed: 953 if changed:
952 if code == "E121": 954 if code == "E121":
953 msg = self.trUtf8( 955 msg = QT_TRANSLATE_NOOP(
956 'CodeStyleFixer',
954 "Indentation of continuation line corrected.") 957 "Indentation of continuation line corrected.")
955 elif code == "E124": 958 elif code == "E124":
956 msg = self.trUtf8( 959 msg = QT_TRANSLATE_NOOP(
960 'CodeStyleFixer',
957 "Indentation of closing bracket corrected.") 961 "Indentation of closing bracket corrected.")
958 return (1, msg, 0) 962 return (1, msg, 0)
959 return (0, "", 0) 963 return (0, "", 0)
960 else: 964 else:
961 id = self.__getID() 965 id = self.__getID()
987 line = line - 1 991 line = line - 1
988 text = self.__source[line] 992 text = self.__source[line]
989 indentation = self.__getIndent(text) 993 indentation = self.__getIndent(text)
990 self.__source[line] = indentation + \ 994 self.__source[line] = indentation + \
991 self.__indentWord + text.lstrip() 995 self.__indentWord + text.lstrip()
992 return ( 996 return (1, QT_TRANSLATE_NOOP(
993 1, 997 'CodeStyleFixer', "Missing indentation of continuation"
994 self.trUtf8( 998 " line corrected."), 0)
995 "Missing indentation of continuation line corrected."),
996 0)
997 return (0, "", 0) 999 return (0, "", 0)
998 else: 1000 else:
999 id = self.__getID() 1001 id = self.__getID()
1000 self.__stackLogical.append((id, code, line, pos)) 1002 self.__stackLogical.append((id, code, line, pos))
1001 return (-1, "", id) 1003 return (-1, "", id)
1028 changed = self.__fixReindent(line, pos, logical) 1030 changed = self.__fixReindent(line, pos, logical)
1029 else: 1031 else:
1030 self.__source[row] = newText 1032 self.__source[row] = newText
1031 changed = True 1033 changed = True
1032 if changed: 1034 if changed:
1033 return (1, self.trUtf8( 1035 return (1, QT_TRANSLATE_NOOP('CodeStyleFixer',
1034 "Closing bracket aligned to opening bracket."), 1036 "Closing bracket aligned to opening bracket."), 0)
1035 0)
1036 return (0, "", 0) 1037 return (0, "", 0)
1037 else: 1038 else:
1038 id = self.__getID() 1039 id = self.__getID()
1039 self.__stackLogical.append((id, code, line, pos)) 1040 self.__stackLogical.append((id, code, line, pos))
1040 return (-1, "", id) 1041 return (-1, "", id)
1063 if not modified: 1064 if not modified:
1064 row = line - 1 1065 row = line - 1
1065 text = self.__source[row] 1066 text = self.__source[row]
1066 self.__source[row] = self.__getIndent(text) + \ 1067 self.__source[row] = self.__getIndent(text) + \
1067 self.__indentWord + text.lstrip() 1068 self.__indentWord + text.lstrip()
1068 return (1, self.trUtf8("Indentation level changed."), 0) 1069 return (1, QT_TRANSLATE_NOOP(
1070 'CodeStyleFixer', "Indentation level changed."), 0)
1069 return (0, "", 0) 1071 return (0, "", 0)
1070 else: 1072 else:
1071 id = self.__getID() 1073 id = self.__getID()
1072 self.__stackLogical.append((id, code, line, pos)) 1074 self.__stackLogical.append((id, code, line, pos))
1073 return (-1, "", id) 1075 return (-1, "", id)
1102 changed = self.__fixReindent(line, pos, logical) 1104 changed = self.__fixReindent(line, pos, logical)
1103 else: 1105 else:
1104 self.__source[row] = newText 1106 self.__source[row] = newText
1105 changed = True 1107 changed = True
1106 if changed: 1108 if changed:
1107 return (1, self.trUtf8( 1109 return (1, QT_TRANSLATE_NOOP(
1108 "Indentation level of hanging indentation changed."), 1110 'CodeStyleFixer',
1109 0) 1111 "Indentation level of hanging indentation "
1112 "changed."), 0)
1110 return (0, "", 0) 1113 return (0, "", 0)
1111 else: 1114 else:
1112 id = self.__getID() 1115 id = self.__getID()
1113 self.__stackLogical.append((id, code, line, pos)) 1116 self.__stackLogical.append((id, code, line, pos))
1114 return (-1, "", id) 1117 return (-1, "", id)
1158 changed = self.__fixReindent(line, pos, logical) 1161 changed = self.__fixReindent(line, pos, logical)
1159 else: 1162 else:
1160 self.__source[row] = newText 1163 self.__source[row] = newText
1161 changed = True 1164 changed = True
1162 if changed: 1165 if changed:
1163 return (1, self.trUtf8("Visual indentation corrected."), 0) 1166 return (1, QT_TRANSLATE_NOOP(
1167 'CodeStyleFixer', "Visual indentation corrected."),
1168 0)
1164 return (0, "", 0) 1169 return (0, "", 0)
1165 else: 1170 else:
1166 id = self.__getID() 1171 id = self.__getID()
1167 self.__stackLogical.append((id, code, line, pos)) 1172 self.__stackLogical.append((id, code, line, pos))
1168 return (-1, "", id) 1173 return (-1, "", id)
1189 newText = self.__fixWhitespace(text, pos, '') 1194 newText = self.__fixWhitespace(text, pos, '')
1190 if newText == text: 1195 if newText == text:
1191 return (0, "", 0) 1196 return (0, "", 0)
1192 1197
1193 self.__source[line] = newText 1198 self.__source[line] = newText
1194 return (1, self.trUtf8("Extraneous whitespace removed."), 0) 1199 return (1, QT_TRANSLATE_NOOP(
1200 'CodeStyleFixer', "Extraneous whitespace removed."), 0)
1195 1201
1196 def __fixE221(self, code, line, pos): 1202 def __fixE221(self, code, line, pos):
1197 """ 1203 """
1198 Private method to fix extraneous whitespace around operator or 1204 Private method to fix extraneous whitespace around operator or
1199 keyword. 1205 keyword.
1218 if newText == text: 1224 if newText == text:
1219 return (0, "", 0) 1225 return (0, "", 0)
1220 1226
1221 self.__source[line] = newText 1227 self.__source[line] = newText
1222 if code in ["E225", "E226", "E227", "E228"]: 1228 if code in ["E225", "E226", "E227", "E228"]:
1223 return (1, self.trUtf8("Missing whitespace added."), 0) 1229 return (1, QT_TRANSLATE_NOOP(
1224 else: 1230 'CodeStyleFixer', "Missing whitespace added."), 0)
1225 return (1, self.trUtf8("Extraneous whitespace removed."), 0) 1231 else:
1232 return (1, QT_TRANSLATE_NOOP(
1233 'CodeStyleFixer', "Extraneous whitespace removed."), 0)
1226 1234
1227 def __fixE231(self, code, line, pos): 1235 def __fixE231(self, code, line, pos):
1228 """ 1236 """
1229 Private method to fix missing whitespace after ',;:'. 1237 Private method to fix missing whitespace after ',;:'.
1230 1238
1239 """ 1247 """
1240 line = line - 1 1248 line = line - 1
1241 pos = pos + 1 1249 pos = pos + 1
1242 self.__source[line] = self.__source[line][:pos] + \ 1250 self.__source[line] = self.__source[line][:pos] + \
1243 " " + self.__source[line][pos:] 1251 " " + self.__source[line][pos:]
1244 return (1, self.trUtf8("Missing whitespace added."), 0) 1252 return (1, QT_TRANSLATE_NOOP(
1253 'CodeStyleFixer', "Missing whitespace added."), 0)
1245 1254
1246 def __fixE251(self, code, line, pos): 1255 def __fixE251(self, code, line, pos):
1247 """ 1256 """
1248 Private method to fix extraneous whitespace around keyword and 1257 Private method to fix extraneous whitespace around keyword and
1249 default parameter equals. 1258 default parameter equals.
1276 if newText.endswith(('=\\\n', '=\\\r\n', '=\\\r')): 1285 if newText.endswith(('=\\\n', '=\\\r\n', '=\\\r')):
1277 self.__source[line] = newText.rstrip("\n\r \t\\") 1286 self.__source[line] = newText.rstrip("\n\r \t\\")
1278 self.__source[line + 1] = self.__source[line + 1].lstrip() 1287 self.__source[line + 1] = self.__source[line + 1].lstrip()
1279 else: 1288 else:
1280 self.__source[line] = newText 1289 self.__source[line] = newText
1281 return (1, self.trUtf8("Extraneous whitespace removed."), 0) 1290 return (1, QT_TRANSLATE_NOOP(
1291 'CodeStyleFixer', "Extraneous whitespace removed."), 0)
1282 1292
1283 def __fixE261(self, code, line, pos): 1293 def __fixE261(self, code, line, pos):
1284 """ 1294 """
1285 Private method to fix whitespace before or after inline comment. 1295 Private method to fix whitespace before or after inline comment.
1286 1296
1297 text = self.__source[line] 1307 text = self.__source[line]
1298 left = text[:pos].rstrip(' \t#') 1308 left = text[:pos].rstrip(' \t#')
1299 right = text[pos:].lstrip(' \t#') 1309 right = text[pos:].lstrip(' \t#')
1300 newText = left + (" # " + right if right.strip() else right) 1310 newText = left + (" # " + right if right.strip() else right)
1301 self.__source[line] = newText 1311 self.__source[line] = newText
1302 return (1, self.trUtf8("Whitespace around comment sign corrected."), 0) 1312 return (1, QT_TRANSLATE_NOOP(
1313 'CodeStyleFixer', "Whitespace around comment sign corrected."),
1314 0)
1303 1315
1304 def __fixE301(self, code, line, pos, apply=False): 1316 def __fixE301(self, code, line, pos, apply=False):
1305 """ 1317 """
1306 Private method to fix the need for one blank line. 1318 Private method to fix the need for one blank line.
1307 1319
1315 @return value indicating an applied/deferred fix (-1, 0, 1), 1327 @return value indicating an applied/deferred fix (-1, 0, 1),
1316 a message for the fix (string) and an ID for a deferred 1328 a message for the fix (string) and an ID for a deferred
1317 fix (integer) 1329 fix (integer)
1318 """ 1330 """
1319 if apply: 1331 if apply:
1320 self.__source.insert(line - 1, self.__getEol()) 1332 self.__source.insert(line - 1, self.__eol)
1321 return (1, self.trUtf8("One blank line inserted."), 0) 1333 return (1, QT_TRANSLATE_NOOP(
1334 'CodeStyleFixer', "One blank line inserted."), 0)
1322 else: 1335 else:
1323 id = self.__getID() 1336 id = self.__getID()
1324 self.__stack.append((id, code, line, pos)) 1337 self.__stack.append((id, code, line, pos))
1325 return (-1, "", id) 1338 return (-1, "", id)
1326 1339
1353 1366
1354 line -= 1 1367 line -= 1
1355 if delta < 0: 1368 if delta < 0:
1356 # insert blank lines (one or two) 1369 # insert blank lines (one or two)
1357 while delta < 0: 1370 while delta < 0:
1358 self.__source.insert(line, self.__getEol()) 1371 self.__source.insert(line, self.__eol)
1359 delta += 1 1372 delta += 1
1360 changed = True 1373 changed = True
1361 elif delta > 0: 1374 elif delta > 0:
1362 # delete superfluous blank lines 1375 # delete superfluous blank lines
1363 while delta > 0: 1376 while delta > 0:
1368 else: 1381 else:
1369 changed = False 1382 changed = False
1370 1383
1371 if changed: 1384 if changed:
1372 if delta < 0: 1385 if delta < 0:
1373 msg = self.trUtf8( 1386 msg = QT_TRANSLATE_NOOP(
1374 "%n blank line(s) inserted.", "", -delta) 1387 'CodeStyleFixer', "{0} blank line(s) inserted.") + \
1388 '@@{0}'.format(-delta)
1375 elif delta > 0: 1389 elif delta > 0:
1376 msg = self.trUtf8( 1390 msg = QT_TRANSLATE_NOOP(
1377 "%n superfluous lines removed", "", delta) 1391 'CodeStyleFixer',
1392 "{0} superfluous line(s) removed.") + \
1393 '@@{0}'.format(delta)
1378 else: 1394 else:
1379 msg = "" 1395 msg = ""
1380 return (1, msg, 0) 1396 return (1, msg, 0)
1381 return (0, "", 0) 1397 return (0, "", 0)
1382 else: 1398 else:
1405 if self.__source[index].strip() == "": 1421 if self.__source[index].strip() == "":
1406 del self.__source[index] 1422 del self.__source[index]
1407 index -= 1 1423 index -= 1
1408 else: 1424 else:
1409 break 1425 break
1410 return (1, self.trUtf8("Superfluous blank lines removed."), 0) 1426 return (1, QT_TRANSLATE_NOOP(
1427 'CodeStyleFixer', "Superfluous blank lines removed."), 0)
1411 else: 1428 else:
1412 id = self.__getID() 1429 id = self.__getID()
1413 self.__stack.append((id, code, line, pos)) 1430 self.__stack.append((id, code, line, pos))
1414 return (-1, "", id) 1431 return (-1, "", id)
1415 1432
1435 if self.__source[index].strip() == "": 1452 if self.__source[index].strip() == "":
1436 del self.__source[index] 1453 del self.__source[index]
1437 index -= 1 1454 index -= 1
1438 else: 1455 else:
1439 break 1456 break
1440 return (1, self.trUtf8( 1457 return (1, QT_TRANSLATE_NOOP('CodeStyleFixer',
1441 "Superfluous blank lines after function decorator removed."), 1458 "Superfluous blank lines after function decorator "
1442 0) 1459 "removed."), 0)
1443 else: 1460 else:
1444 id = self.__getID() 1461 id = self.__getID()
1445 self.__stack.append((id, code, line, pos)) 1462 self.__stack.append((id, code, line, pos))
1446 return (-1, "", id) 1463 return (-1, "", id)
1447 1464
1470 # statement followed by a semicolon and some unrelated 1487 # statement followed by a semicolon and some unrelated
1471 # statement with commas in it. 1488 # statement with commas in it.
1472 if ';' in text: 1489 if ';' in text:
1473 return (0, "", 0) 1490 return (0, "", 0)
1474 1491
1475 newText = text[:pos].rstrip("\t ,") + self.__getEol() + \ 1492 newText = text[:pos].rstrip("\t ,") + self.__eol + \
1476 self.__getIndent(text) + "import " + text[pos:].lstrip("\t ,") 1493 self.__getIndent(text) + "import " + text[pos:].lstrip("\t ,")
1477 self.__source[line] = newText 1494 self.__source[line] = newText
1478 return (1, self.trUtf8("Imports were put on separate lines."), 0) 1495 return (1, QT_TRANSLATE_NOOP(
1496 'CodeStyleFixer', "Imports were put on separate lines."),
1497 0)
1479 else: 1498 else:
1480 id = self.__getID() 1499 id = self.__getID()
1481 self.__stack.append((id, code, line, pos)) 1500 self.__stack.append((id, code, line, pos))
1482 return (-1, "", id) 1501 return (-1, "", id)
1483 1502
1510 nextText = self.__source[line + 1] 1529 nextText = self.__source[line + 1]
1511 else: 1530 else:
1512 nextText = "" 1531 nextText = ""
1513 shortener = LineShortener( 1532 shortener = LineShortener(
1514 text, prevText, nextText, 1533 text, prevText, nextText,
1515 maxLength=self.__maxLineLength, eol=self.__getEol(), 1534 maxLength=self.__maxLineLength, eol=self.__eol,
1516 indentWord=self.__indentWord, isDocString=isDocString) 1535 indentWord=self.__indentWord, isDocString=isDocString)
1517 changed, newText, newNextText = shortener.shorten() 1536 changed, newText, newNextText = shortener.shorten()
1518 if changed: 1537 if changed:
1519 if newText != text: 1538 if newText != text:
1520 self.__source[line] = newText 1539 self.__source[line] = newText
1521 if newNextText and newNextText != nextText: 1540 if newNextText and newNextText != nextText:
1522 if newNextText == " ": 1541 if newNextText == " ":
1523 newNextText = "" 1542 newNextText = ""
1524 self.__source[line + 1] = newNextText 1543 self.__source[line + 1] = newNextText
1525 return (1, self.trUtf8("Long lines have been shortened."), 0) 1544 return (1, QT_TRANSLATE_NOOP(
1545 'CodeStyleFixer', "Long lines have been shortened."),
1546 0)
1526 else: 1547 else:
1527 return (0, "", 0) 1548 return (0, "", 0)
1528 else: 1549 else:
1529 id = self.__getID() 1550 id = self.__getID()
1530 self.__stack.append((id, code, line, pos)) 1551 self.__stack.append((id, code, line, pos))
1542 @return value indicating an applied/deferred fix (-1, 0, 1), 1563 @return value indicating an applied/deferred fix (-1, 0, 1),
1543 a message for the fix (string) and an ID for a deferred 1564 a message for the fix (string) and an ID for a deferred
1544 fix (integer) 1565 fix (integer)
1545 """ 1566 """
1546 self.__source[line - 1] = \ 1567 self.__source[line - 1] = \
1547 self.__source[line - 1].rstrip("\n\r \t\\") + self.__getEol() 1568 self.__source[line - 1].rstrip("\n\r \t\\") + self.__eol
1548 return (1, self.trUtf8("Redundant backslash in brackets removed."), 0) 1569 return (1, QT_TRANSLATE_NOOP(
1570 'CodeStyleFixer', "Redundant backslash in brackets removed."),
1571 0)
1549 1572
1550 def __fixE701(self, code, line, pos, apply=False): 1573 def __fixE701(self, code, line, pos, apply=False):
1551 """ 1574 """
1552 Private method to fix colon-separated compound statements. 1575 Private method to fix colon-separated compound statements.
1553 1576
1565 if apply: 1588 if apply:
1566 line = line - 1 1589 line = line - 1
1567 text = self.__source[line] 1590 text = self.__source[line]
1568 pos = pos + 1 1591 pos = pos + 1
1569 1592
1570 newText = text[:pos] + self.__getEol() + self.__getIndent(text) + \ 1593 newText = text[:pos] + self.__eol + self.__getIndent(text) + \
1571 self.__indentWord + text[pos:].lstrip("\n\r \t\\") + \ 1594 self.__indentWord + text[pos:].lstrip("\n\r \t\\") + \
1572 self.__getEol() 1595 self.__eol
1573 self.__source[line] = newText 1596 self.__source[line] = newText
1574 return (1, self.trUtf8("Compound statement corrected."), 0) 1597 return (1, QT_TRANSLATE_NOOP(
1598 'CodeStyleFixer', "Compound statement corrected."), 0)
1575 else: 1599 else:
1576 id = self.__getID() 1600 id = self.__getID()
1577 self.__stack.append((id, code, line, pos)) 1601 self.__stack.append((id, code, line, pos))
1578 return (-1, "", id) 1602 return (-1, "", id)
1579 1603
1599 if text.rstrip().endswith("\\"): 1623 if text.rstrip().endswith("\\"):
1600 # normalize '1; \\\n2' into '1; 2' 1624 # normalize '1; \\\n2' into '1; 2'
1601 self.__source[line] = text.rstrip("\n\r \t\\") 1625 self.__source[line] = text.rstrip("\n\r \t\\")
1602 self.__source[line + 1] = self.__source[line + 1].lstrip() 1626 self.__source[line + 1] = self.__source[line + 1].lstrip()
1603 elif text.rstrip().endswith(";"): 1627 elif text.rstrip().endswith(";"):
1604 self.__source[line] = text.rstrip("\n\r \t;") + self.__getEol() 1628 self.__source[line] = text.rstrip("\n\r \t;") + self.__eol
1605 else: 1629 else:
1606 first = text[:pos].rstrip("\n\r \t;") + self.__getEol() 1630 first = text[:pos].rstrip("\n\r \t;") + self.__eol
1607 second = text[pos:].lstrip("\n\r \t;") 1631 second = text[pos:].lstrip("\n\r \t;")
1608 self.__source[line] = first + self.__getIndent(text) + second 1632 self.__source[line] = first + self.__getIndent(text) + second
1609 return (1, self.trUtf8("Compound statement corrected."), 0) 1633 return (1, QT_TRANSLATE_NOOP(
1634 'CodeStyleFixer', "Compound statement corrected."), 0)
1610 else: 1635 else:
1611 id = self.__getID() 1636 id = self.__getID()
1612 self.__stack.append((id, code, line, pos)) 1637 self.__stack.append((id, code, line, pos))
1613 return (-1, "", id) 1638 return (-1, "", id)
1614 1639
1645 center = "is not" 1670 center = "is not"
1646 else: 1671 else:
1647 return (0, "", 0) 1672 return (0, "", 0)
1648 1673
1649 self.__source[line] = " ".join([left, center, right]) 1674 self.__source[line] = " ".join([left, center, right])
1650 return (1, self.trUtf8("Comparison to None/True/False corrected."), 0) 1675 return (1, QT_TRANSLATE_NOOP(
1676 'CodeStyleFixer', "Comparison to None/True/False corrected."),
1677 0)
1651 1678
1652 def __fixN804(self, code, line, pos, apply=False): 1679 def __fixN804(self, code, line, pos, apply=False):
1653 """ 1680 """
1654 Private method to fix a wrong first argument of normal and 1681 Private method to fix a wrong first argument of normal and
1655 class methods. 1682 class methods.
1673 else: 1700 else:
1674 arg = "self" 1701 arg = "self"
1675 1702
1676 if text.rstrip().endswith("("): 1703 if text.rstrip().endswith("("):
1677 newText = text + self.__getIndent(text) + \ 1704 newText = text + self.__getIndent(text) + \
1678 self.__indentWord + arg + "," + self.__getEol() 1705 self.__indentWord + arg + "," + self.__eol
1679 else: 1706 else:
1680 index = text.find("(") + 1 1707 index = text.find("(") + 1
1681 left = text[:index] 1708 left = text[:index]
1682 right = text[index:] 1709 right = text[index:]
1683 if right.startswith(")"): 1710 if right.startswith(")"):
1684 center = arg 1711 center = arg
1685 else: 1712 else:
1686 center = arg + ", " 1713 center = arg + ", "
1687 newText = left + center + right 1714 newText = left + center + right
1688 self.__source[line] = newText 1715 self.__source[line] = newText
1689 return (1, self.trUtf8("'{0}' argument added.").format(arg), 0) 1716 return (1, QT_TRANSLATE_NOOP(
1717 'CodeStyleFixer', "'{0}' argument added.") +
1718 '@@{0}'.format(arg), 0)
1690 else: 1719 else:
1691 id = self.__getID() 1720 id = self.__getID()
1692 self.__stack.append((id, code, line, pos)) 1721 self.__stack.append((id, code, line, pos))
1693 return (-1, "", id) 1722 return (-1, "", id)
1694 1723
1744 self.__source[line - 1].rstrip() + right 1773 self.__source[line - 1].rstrip() + right
1745 self.__source[line] = "" 1774 self.__source[line] = ""
1746 else: 1775 else:
1747 self.__source[line] = indent + right 1776 self.__source[line] = indent + right
1748 1777
1749 return (1, self.trUtf8("'{0}' argument removed.").format(arg), 0) 1778 return (1, QT_TRANSLATE_NOOP(
1779 'CodeStyleFixer', "'{0}' argument removed.") +
1780 '@@{0}'.format(arg), 0)
1750 else: 1781 else:
1751 id = self.__getID() 1782 id = self.__getID()
1752 self.__stack.append((id, code, line, pos)) 1783 self.__stack.append((id, code, line, pos))
1753 return (-1, "", id) 1784 return (-1, "", id)
1754 1785
1765 a message for the fix (string) and an ID for a deferred 1796 a message for the fix (string) and an ID for a deferred
1766 fix (integer) 1797 fix (integer)
1767 """ 1798 """
1768 self.__source[line - 1] = re.sub(r'[\t ]+(\r?)$', r"\1", 1799 self.__source[line - 1] = re.sub(r'[\t ]+(\r?)$', r"\1",
1769 self.__source[line - 1]) 1800 self.__source[line - 1])
1770 return (1, self.trUtf8("Whitespace stripped from end of line."), 0) 1801 return (1, QT_TRANSLATE_NOOP(
1802 'CodeStyleFixer', "Whitespace stripped from end of line."), 0)
1771 1803
1772 def __fixW292(self, code, line, pos): 1804 def __fixW292(self, code, line, pos):
1773 """ 1805 """
1774 Private method to fix a missing newline at the end of file. 1806 Private method to fix a missing newline at the end of file.
1775 1807
1780 @param pos position inside line (integer) 1812 @param pos position inside line (integer)
1781 @return value indicating an applied/deferred fix (-1, 0, 1), 1813 @return value indicating an applied/deferred fix (-1, 0, 1),
1782 a message for the fix (string) and an ID for a deferred 1814 a message for the fix (string) and an ID for a deferred
1783 fix (integer) 1815 fix (integer)
1784 """ 1816 """
1785 self.__source[line - 1] += self.__getEol() 1817 self.__source[line - 1] += self.__eol
1786 return (1, self.trUtf8("newline added to end of file."), 0) 1818 return (1, QT_TRANSLATE_NOOP(
1819 'CodeStyleFixer', "newline added to end of file."), 0)
1787 1820
1788 def __fixW391(self, code, line, pos): 1821 def __fixW391(self, code, line, pos):
1789 """ 1822 """
1790 Private method to fix trailing blank lines. 1823 Private method to fix trailing blank lines.
1791 1824
1803 if self.__source[index].strip() == "": 1836 if self.__source[index].strip() == "":
1804 del self.__source[index] 1837 del self.__source[index]
1805 index -= 1 1838 index -= 1
1806 else: 1839 else:
1807 break 1840 break
1808 return (1, self.trUtf8( 1841 return (1, QT_TRANSLATE_NOOP(
1809 "Superfluous trailing blank lines removed from end of file."), 0) 1842 'CodeStyleFixer',
1843 "Superfluous trailing blank lines removed from end of file."),
1844 0)
1810 1845
1811 def __fixW603(self, code, line, pos): 1846 def __fixW603(self, code, line, pos):
1812 """ 1847 """
1813 Private method to fix the not equal notation. 1848 Private method to fix the not equal notation.
1814 1849
1820 @return value indicating an applied/deferred fix (-1, 0, 1), 1855 @return value indicating an applied/deferred fix (-1, 0, 1),
1821 a message for the fix (string) and an ID for a deferred 1856 a message for the fix (string) and an ID for a deferred
1822 fix (integer) 1857 fix (integer)
1823 """ 1858 """
1824 self.__source[line - 1] = self.__source[line - 1].replace("<>", "!=") 1859 self.__source[line - 1] = self.__source[line - 1].replace("<>", "!=")
1825 return (1, self.trUtf8("'<>' replaced by '!='."), 0) 1860 return (1, QT_TRANSLATE_NOOP(
1861 'CodeStyleFixer', "'<>' replaced by '!='."), 0)
1826 1862
1827 1863
1828 class Reindenter(object): 1864 class Reindenter(object):
1829 """ 1865 """
1830 Class to reindent badly-indented code to uniformly use four-space 1866 Class to reindent badly-indented code to uniformly use four-space
2041 (list of strings) 2077 (list of strings)
2042 """ 2078 """
2043 self.lines = physical_lines 2079 self.lines = physical_lines
2044 self.tokens = [] 2080 self.tokens = []
2045 self.rel_indent = None 2081 self.rel_indent = None
2046 sio = io.StringIO(''.join(physical_lines)) 2082 sio = StringIO(''.join(physical_lines))
2047 for t in tokenize.generate_tokens(sio.readline): 2083 for t in tokenize.generate_tokens(sio.readline):
2048 if not len(self.tokens) and t[0] in self.SKIP_TOKENS: 2084 if not len(self.tokens) and t[0] in self.SKIP_TOKENS:
2049 continue 2085 continue
2050 if t[0] != tokenize.ENDMARKER: 2086 if t[0] != tokenize.ENDMARKER:
2051 self.tokens.append(t) 2087 self.tokens.append(t)
2336 return True, newText, newNext 2372 return True, newText, newNext
2337 2373
2338 indent = self.__getIndent(self.__text) 2374 indent = self.__getIndent(self.__text)
2339 source = self.__text[len(indent):] 2375 source = self.__text[len(indent):]
2340 assert source.lstrip() == source 2376 assert source.lstrip() == source
2341 sio = io.StringIO(source) 2377 sio = StringIO(source)
2342 2378
2343 # Check for multi line string. 2379 # Check for multi line string.
2344 try: 2380 try:
2345 tokens = list(tokenize.generate_tokens(sio.readline)) 2381 tokens = list(tokenize.generate_tokens(sio.readline))
2346 except (SyntaxError, tokenize.TokenError): 2382 except (SyntaxError, tokenize.TokenError):

eric ide

mercurial