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", |
24 "E125", "E126", "E127", "E128", "E133", "E201", |
25 "E201", "E202", "E203", "E211", "E221", "E222", |
25 "E202", "E203", "E211", "E221", "E222", "E223", |
26 "E223", "E224", "E225", "E226", "E227", "E228", |
26 "E224", "E225", "E226", "E227", "E228", "E231", |
27 "E231", "E241", "E242", "E251", "E261", "E262", |
27 "E241", "E242", "E251", "E261", "E262", "E271", |
28 "E271", "E272", "E273", "E274", "W291", "W292", |
28 "E272", "E273", "E274", "E301", "E302", "E303", |
29 "W293", "E301", "E302", "E303", "E304", "W391", |
29 "E304", "E401", "E501", "E502", "E701", "E702", |
30 "E401", "E501", "E502", "W603", "E701", "E702", |
30 "E703", "E711", "E712", |
31 "E703", "E711", "E712" |
31 "N804", "N805", "N806", |
|
32 "W191", "W291", "W292", "W293", "W391", "W603", |
32 ] |
33 ] |
33 |
34 |
34 |
35 |
35 # TODO: add fixes for N804, N805 |
|
36 class Pep8Fixer(QObject): |
36 class Pep8Fixer(QObject): |
37 """ |
37 """ |
38 Class implementing a fixer for certain PEP 8 issues. |
38 Class implementing a fixer for certain PEP 8 issues. |
39 """ |
39 """ |
40 def __init__(self, project, filename, sourceLines, fixCodes, noFixCodes, |
40 def __init__(self, project, filename, sourceLines, fixCodes, noFixCodes, |
85 "E125": self.__fixE125, |
85 "E125": self.__fixE125, |
86 "E126": self.__fixE126, |
86 "E126": self.__fixE126, |
87 "E127": self.__fixE127, |
87 "E127": self.__fixE127, |
88 "E128": self.__fixE127, |
88 "E128": self.__fixE127, |
89 "E133": self.__fixE126, |
89 "E133": self.__fixE126, |
90 "W191": self.__fixE101, |
|
91 "E201": self.__fixE201, |
90 "E201": self.__fixE201, |
92 "E202": self.__fixE201, |
91 "E202": self.__fixE201, |
93 "E203": self.__fixE201, |
92 "E203": self.__fixE201, |
94 "E211": self.__fixE201, |
93 "E211": self.__fixE201, |
95 "E221": self.__fixE221, |
94 "E221": self.__fixE221, |
108 "E262": self.__fixE261, |
107 "E262": self.__fixE261, |
109 "E271": self.__fixE221, |
108 "E271": self.__fixE221, |
110 "E272": self.__fixE221, |
109 "E272": self.__fixE221, |
111 "E273": self.__fixE221, |
110 "E273": self.__fixE221, |
112 "E274": self.__fixE221, |
111 "E274": self.__fixE221, |
113 "W291": self.__fixW291, |
|
114 "W292": self.__fixW292, |
|
115 "W293": self.__fixW291, |
|
116 "E301": self.__fixE301, |
112 "E301": self.__fixE301, |
117 "E302": self.__fixE302, |
113 "E302": self.__fixE302, |
118 "E303": self.__fixE303, |
114 "E303": self.__fixE303, |
119 "E304": self.__fixE304, |
115 "E304": self.__fixE304, |
120 "W391": self.__fixW391, |
|
121 "E401": self.__fixE401, |
116 "E401": self.__fixE401, |
122 "E501": self.__fixE501, |
117 "E501": self.__fixE501, |
123 "E502": self.__fixE502, |
118 "E502": self.__fixE502, |
124 "W603": self.__fixW603, |
|
125 "E701": self.__fixE701, |
119 "E701": self.__fixE701, |
126 "E702": self.__fixE702, |
120 "E702": self.__fixE702, |
127 "E703": self.__fixE702, |
121 "E703": self.__fixE702, |
128 "E711": self.__fixE711, |
122 "E711": self.__fixE711, |
129 "E712": self.__fixE711, |
123 "E712": self.__fixE711, |
|
124 "N804": self.__fixN804, |
|
125 "N805": self.__fixN804, |
|
126 "N806": self.__fixN806, |
|
127 "W191": self.__fixE101, |
|
128 "W291": self.__fixW291, |
|
129 "W292": self.__fixW292, |
|
130 "W293": self.__fixW291, |
|
131 "W391": self.__fixW391, |
|
132 "W603": self.__fixW603, |
130 } |
133 } |
131 self.__modified = False |
134 self.__modified = False |
132 self.__stackLogical = [] # these need to be fixed before the file |
135 self.__stackLogical = [] # these need to be fixed before the file |
133 # is saved but after all other inline |
136 # is saved but after all other inline |
134 # fixes. These work with logical lines. |
137 # fixes. These work with logical lines. |
484 return (0, "", 0) |
487 return (0, "", 0) |
485 |
488 |
486 def __fixE121(self, code, line, pos, apply=False): |
489 def __fixE121(self, code, line, pos, apply=False): |
487 """ |
490 """ |
488 Private method to fix the indentation of continuation lines and |
491 Private method to fix the indentation of continuation lines and |
489 closing brackets (E121,E124). |
492 closing brackets (E121, E124). |
490 |
493 |
491 @param code code of the issue (string) |
494 @param code code of the issue (string) |
492 @param line line number of the issue (integer) |
495 @param line line number of the issue (integer) |
493 @param pos position inside line (integer) |
496 @param pos position inside line (integer) |
494 @keyparam apply flag indicating, that the fix should be applied |
497 @keyparam apply flag indicating, that the fix should be applied |
1162 else: |
1165 else: |
1163 return (0, "", 0) |
1166 return (0, "", 0) |
1164 |
1167 |
1165 self.__source[line] = " ".join([left, center, right]) |
1168 self.__source[line] = " ".join([left, center, right]) |
1166 return (1, self.trUtf8("Comparison to None/True/False corrected."), 0) |
1169 return (1, self.trUtf8("Comparison to None/True/False corrected."), 0) |
|
1170 |
|
1171 def __fixN804(self, code, line, pos, apply=False): |
|
1172 """ |
|
1173 Private method to fix a wrong first argument of normal and |
|
1174 class methods (N804, N805). |
|
1175 |
|
1176 @param code code of the issue (string) |
|
1177 @param line line number of the issue (integer) |
|
1178 @param pos position inside line (integer) |
|
1179 @keyparam apply flag indicating, that the fix should be applied |
|
1180 (boolean) |
|
1181 @return value indicating an applied/deferred fix (-1, 0, 1), |
|
1182 a message for the fix (string) and an ID for a deferred |
|
1183 fix (integer) |
|
1184 """ |
|
1185 if apply: |
|
1186 line = line - 1 |
|
1187 text = self.__source[line] |
|
1188 if code == "N804": |
|
1189 arg = "cls" |
|
1190 else: |
|
1191 arg = "self" |
|
1192 |
|
1193 if text.rstrip().endswith("("): |
|
1194 newText = text + self.__getIndent(text) + \ |
|
1195 self.__indentWord + arg + "," + self.__getEol() |
|
1196 else: |
|
1197 index = text.find("(") + 1 |
|
1198 left = text[:index] |
|
1199 right = text[index:] |
|
1200 if right.startswith(")"): |
|
1201 center = arg |
|
1202 else: |
|
1203 center = arg + ", " |
|
1204 newText = left + center + right |
|
1205 self.__source[line] = newText |
|
1206 return (1, self.trUtf8("'{0}' argument added.").format(arg), 0) |
|
1207 else: |
|
1208 id = self.__getID() |
|
1209 self.__stack.append((id, code, line, pos)) |
|
1210 return (-1, "", id) |
|
1211 |
|
1212 def __fixN806(self, code, line, pos, apply=False): |
|
1213 """ |
|
1214 Private method to fix a wrong first argument of static methods |
|
1215 (N806). |
|
1216 |
|
1217 @param code code of the issue (string) |
|
1218 @param line line number of the issue (integer) |
|
1219 @param pos position inside line (integer) |
|
1220 @keyparam apply flag indicating, that the fix should be applied |
|
1221 (boolean) |
|
1222 @return value indicating an applied/deferred fix (-1, 0, 1), |
|
1223 a message for the fix (string) and an ID for a deferred |
|
1224 fix (integer) |
|
1225 """ |
|
1226 if apply: |
|
1227 line = line - 1 |
|
1228 text = self.__source[line] |
|
1229 index = text.find("(") + 1 |
|
1230 left = text[:index] |
|
1231 right = text[index:] |
|
1232 |
|
1233 if right.startswith(("cls", "self")): |
|
1234 # cls or self are on the definition line |
|
1235 if right.startswith("cls"): |
|
1236 right = right[3:] |
|
1237 arg = "cls" |
|
1238 else: |
|
1239 right = right[4:] |
|
1240 arg = "self" |
|
1241 right = right.lstrip(", ") |
|
1242 newText = left + right |
|
1243 self.__source[line] = newText |
|
1244 else: |
|
1245 # they are on the next line |
|
1246 line = line + 1 |
|
1247 text = self.__source[line] |
|
1248 indent = self.__getIndent(text) |
|
1249 right = text.lstrip() |
|
1250 if right.startswith("cls"): |
|
1251 right = right[3:] |
|
1252 arg = "cls" |
|
1253 else: |
|
1254 right = right[4:] |
|
1255 arg = "self" |
|
1256 right = right.lstrip(", ") |
|
1257 if right.startswith("):"): |
|
1258 # merge with previous line |
|
1259 self.__source[line - 1] = \ |
|
1260 self.__source[line - 1].rstrip() + right |
|
1261 self.__source[line] = "" |
|
1262 else: |
|
1263 self.__source[line] = indent + right |
|
1264 |
|
1265 return (1, self.trUtf8("'{0}' argument removed.").format(arg), 0) |
|
1266 else: |
|
1267 id = self.__getID() |
|
1268 self.__stack.append((id, code, line, pos)) |
|
1269 return (-1, "", id) |
1167 |
1270 |
1168 def __fixW291(self, code, line, pos): |
1271 def __fixW291(self, code, line, pos): |
1169 """ |
1272 """ |
1170 Private method to fix trailing whitespace (W291, W293). |
1273 Private method to fix trailing whitespace (W291, W293). |
1171 |
1274 |