Plugins/CheckerPlugins/Pep8/Pep8Fixer.py

changeset 2876
bfa39cf40277
parent 2875
1267f0663801
child 2878
61042247f793
equal deleted inserted replaced
2875:1267f0663801 2876:bfa39cf40277
19 from . import pep8 19 from . import pep8
20 20
21 import Utilities 21 import Utilities
22 22
23 Pep8FixableIssues = ["E101", "E111", "E121", "E122", "E123", "E124", 23 Pep8FixableIssues = ["E101", "E111", "E121", "E122", "E123", "E124",
24 "E125", "E126", "E127", "E128", "E133", "W191", "E201", "E202", "E203", 24 "E125", "E126", "E127", "E128", "E133", "W191",
25 "E211", "E221", "E222", "E223", "E224", "E225", 25 "E201", "E202", "E203", "E211", "E221", "E222",
26 "E226", "E227", "E228", "E231", "E241", "E242", 26 "E223", "E224", "E225", "E226", "E227", "E228",
27 "E251", "E261", "E262", "E271", "E272", "E273", 27 "E231", "E241", "E242", "E251", "E261", "E262",
28 "E274", "W291", "W292", "W293", "E301", "E302", 28 "E271", "E272", "E273", "E274", "W291", "W292",
29 "E303", "E304", "W391", "E401", "E502", "W603", 29 "W293", "E301", "E302", "E303", "E304", "W391",
30 "E701", "E702", "E703", "E711", "E712" 30 "E401", "E502", "W603", "E701", "E702", "E703",
31 "E711", "E712"
31 ] 32 ]
32 33
33 34
34 class Pep8Fixer(QObject): 35 class Pep8Fixer(QObject):
35 """ 36 """
36 Class implementing a fixer for certain PEP 8 issues. 37 Class implementing a fixer for certain PEP 8 issues.
37 """ 38 """
38 def __init__(self, project, filename, sourceLines, fixCodes, inPlace): 39 def __init__(self, project, filename, sourceLines, fixCodes, noFixCodes,
40 maxLineLength, inPlace):
39 """ 41 """
40 Constructor 42 Constructor
41 43
42 @param project reference to the project object (Project) 44 @param project reference to the project object (Project)
43 @param filename name of the file to be fixed (string) 45 @param filename name of the file to be fixed (string)
44 @param sourceLines list of source lines including eol marker 46 @param sourceLines list of source lines including eol marker
45 (list of string) 47 (list of string)
46 @param fixCodes list of codes to be fixed as a comma separated 48 @param fixCodes list of codes to be fixed as a comma separated
47 string (string) 49 string (string)
50 @param noFixCodes list of codes not to be fixed as a comma
51 separated string (string)
52 @param maxLineLength maximum allowed line length (integer)
48 @param inPlace flag indicating to modify the file in place (boolean) 53 @param inPlace flag indicating to modify the file in place (boolean)
49 """ 54 """
50 super().__init__() 55 super().__init__()
51 56
52 self.__project = project 57 self.__project = project
53 self.__filename = filename 58 self.__filename = filename
54 self.__origName = "" 59 self.__origName = ""
55 self.__source = sourceLines[:] # save a copy 60 self.__source = sourceLines[:] # save a copy
56 self.__fixCodes = [c.strip() for c in fixCodes.split(",") if c.strip()] 61 self.__fixCodes = [c.strip() for c in fixCodes.split(",") if c.strip()]
62 self.__noFixCodes = [c.strip() for c in noFixCodes.split(",") if c.strip()]
63 self.__maxLineLength = maxLineLength
57 self.fixed = 0 64 self.fixed = 0
58 65
59 self.__reindenter = None 66 self.__reindenter = None
60 self.__eol = "" 67 self.__eol = ""
61 self.__indentWord = self.__getIndentWord() 68 self.__indentWord = self.__getIndentWord()
144 except (IOError, Utilities.CodingError, UnicodeError) as err: 151 except (IOError, Utilities.CodingError, UnicodeError) as err:
145 E5MessageBox.critical(self, 152 E5MessageBox.critical(self,
146 self.trUtf8("Fix PEP 8 issues"), 153 self.trUtf8("Fix PEP 8 issues"),
147 self.trUtf8( 154 self.trUtf8(
148 """<p>Could not save the file <b>{0}</b>.""" 155 """<p>Could not save the file <b>{0}</b>."""
149 """ Skipping it.</p><p>Reason: {1}</p>""")\ 156 """ Skipping it.</p><p>Reason: {1}</p>""")
150 .format(self.__filename, str(err)) 157 .format(self.__filename, str(err))
151 ) 158 )
152 return False 159 return False
153 160
154 return True 161 return True
164 the fix (string) 171 the fix (string)
165 """ 172 """
166 code = message.split(None, 1)[0].strip() 173 code = message.split(None, 1)[0].strip()
167 174
168 if line <= len(self.__source) and \ 175 if line <= len(self.__source) and \
176 code not in self.__noFixCodes and \
169 (code in self.__fixCodes or len(self.__fixCodes) == 0) and \ 177 (code in self.__fixCodes or len(self.__fixCodes) == 0) and \
170 code in self.__fixes: 178 code in self.__fixes:
171 res = self.__fixes[code](code, line, pos) 179 res = self.__fixes[code](code, line, pos)
172 if res[0]: 180 if res[0]:
173 self.__modified = True 181 self.__modified = True
1051 after = self.after = [] 1059 after = self.after = []
1052 # Copy over initial empty lines -- there's nothing to do until 1060 # Copy over initial empty lines -- there's nothing to do until
1053 # we see a line with *something* on it. 1061 # we see a line with *something* on it.
1054 i = stats[0][0] 1062 i = stats[0][0]
1055 after.extend(lines[1:i]) 1063 after.extend(lines[1:i])
1056 for i in range(len(stats)-1): 1064 for i in range(len(stats) - 1):
1057 thisstmt, thislevel = stats[i] 1065 thisstmt, thislevel = stats[i]
1058 nextstmt = stats[i+1][0] 1066 nextstmt = stats[i + 1][0]
1059 have = self.__getlspace(lines[thisstmt]) 1067 have = self.__getlspace(lines[thisstmt])
1060 want = thislevel * 4 1068 want = thislevel * 4
1061 if want < 0: 1069 if want < 0:
1062 # A comment line. 1070 # A comment line.
1063 if have: 1071 if have:
1078 # in which case we should shift it like its base 1086 # in which case we should shift it like its base
1079 # line got shifted. 1087 # line got shifted.
1080 for j in range(i - 1, -1, -1): 1088 for j in range(i - 1, -1, -1):
1081 jline, jlevel = stats[j] 1089 jline, jlevel = stats[j]
1082 if jlevel >= 0: 1090 if jlevel >= 0:
1083 want = have + self.__getlspace(after[jline-1]) - \ 1091 want = have + self.__getlspace(after[jline - 1]) - \
1084 self.__getlspace(lines[jline]) 1092 self.__getlspace(lines[jline])
1085 break 1093 break
1086 if want < 0: 1094 if want < 0:
1087 # Still no luck -- leave it alone. 1095 # Still no luck -- leave it alone.
1088 want = have 1096 want = have

eric ide

mercurial