14 |
14 |
15 from E5Gui import E5MessageBox |
15 from E5Gui import E5MessageBox |
16 |
16 |
17 import Utilities |
17 import Utilities |
18 |
18 |
19 Pep8FixableIssues = ["E101", "W191", "E201", "E202", "E203", "W291", "W292", "W293", "E301", "E303", "E304", "W391", "W603"] |
19 Pep8FixableIssues = ["E101", "W191", "E201", "E202", "E203", "E211", "E221", "E222", "E225", "E231", "E241", "E251", "E261", "E262", "W291", "W292", "W293", "E301", "E303", "E304", "W391", "W603"] |
20 |
20 |
21 class Pep8Fixer(QObject): |
21 class Pep8Fixer(QObject): |
22 """ |
22 """ |
23 Class implementing a fixer for certain PEP 8 issues. |
23 Class implementing a fixer for certain PEP 8 issues. |
24 """ |
24 """ |
50 self.__fixes = { |
50 self.__fixes = { |
51 "E101" : self.__fixTabs, |
51 "E101" : self.__fixTabs, |
52 "W191" : self.__fixTabs, |
52 "W191" : self.__fixTabs, |
53 "E201" : self.__fixWhitespaceAfter, |
53 "E201" : self.__fixWhitespaceAfter, |
54 "E202" : self.__fixWhitespaceBefore, |
54 "E202" : self.__fixWhitespaceBefore, |
55 "E203" : self.__fixWhitespaceBefore, |
55 "E203" : self.__fixWhitespaceBefore, |
|
56 "E211" : self.__fixWhitespaceBefore, |
|
57 "E221" : self.__fixWhitespaceAroundOperator, |
|
58 "E222" : self.__fixWhitespaceAroundOperator, |
|
59 "E225" : self.__fixMissingWhitespaceAroundOperator, |
|
60 "E231" : self.__fixMissingWhitespaceAfter, |
|
61 "E241" : self.__fixWhitespaceAroundOperator, |
|
62 "E251" : self.__fixWhitespaceAroundEquals, |
|
63 "E261" : self.__fixWhitespaceBeforeInline, |
|
64 "E262" : self.__fixWhitespaceAfterInline, |
56 "W291" : self.__fixWhitespace, |
65 "W291" : self.__fixWhitespace, |
57 "W292" : self.__fixNewline, |
66 "W292" : self.__fixNewline, |
58 "W293" : self.__fixWhitespace, |
67 "W293" : self.__fixWhitespace, |
59 "E301" : self.__fixOneBlankLine, |
68 "E301" : self.__fixOneBlankLine, |
60 "E303" : self.__fixTooManyBlankLines, |
69 "E303" : self.__fixTooManyBlankLines, |
301 self.__source[line][pos + 1:] |
310 self.__source[line][pos + 1:] |
302 return (True, self.trUtf8("Superfluous whitespace removed.")) |
311 return (True, self.trUtf8("Superfluous whitespace removed.")) |
303 |
312 |
304 def __fixWhitespaceBefore(self, code, line, pos, apply=False): |
313 def __fixWhitespaceBefore(self, code, line, pos, apply=False): |
305 """ |
314 """ |
306 Private method to fix superfluous whitespace before '}])' |
315 Private method to fix superfluous whitespace before '}])', |
307 and ',;:'. |
316 ',;:' and '(['. |
308 |
317 |
309 @param code code of the issue (string) |
318 @param code code of the issue (string) |
310 @param line line number of the issue (integer) |
319 @param line line number of the issue (integer) |
311 @param pos position inside line (integer) |
320 @param pos position inside line (integer) |
312 @keyparam apply flag indicating, that the fix should be applied |
321 @keyparam apply flag indicating, that the fix should be applied |
319 while self.__source[line][pos] in [" ", "\t"]: |
328 while self.__source[line][pos] in [" ", "\t"]: |
320 self.__source[line] = self.__source[line][:pos] + \ |
329 self.__source[line] = self.__source[line][:pos] + \ |
321 self.__source[line][pos + 1:] |
330 self.__source[line][pos + 1:] |
322 pos -= 1 |
331 pos -= 1 |
323 return (True, self.trUtf8("Superfluous whitespace removed.")) |
332 return (True, self.trUtf8("Superfluous whitespace removed.")) |
|
333 |
|
334 def __fixMissingWhitespaceAfter(self, code, line, pos, apply=False): |
|
335 """ |
|
336 Private method to fix missing whitespace after ',;:'. |
|
337 |
|
338 @param code code of the issue (string) |
|
339 @param line line number of the issue (integer) |
|
340 @param pos position inside line (integer) |
|
341 @keyparam apply flag indicating, that the fix should be applied |
|
342 (boolean) |
|
343 @return flag indicating an applied fix (boolean) and a message for |
|
344 the fix (string) |
|
345 """ |
|
346 line = line - 1 |
|
347 self.__source[line] = self.__source[line][:pos] + \ |
|
348 " " + \ |
|
349 self.__source[line][pos:] |
|
350 return (True, self.trUtf8("Missing whitespace added.")) |
|
351 |
|
352 def __fixWhitespaceAroundOperator(self, code, line, pos, apply=False): |
|
353 """ |
|
354 Private method to fix extraneous whitespace around operator. |
|
355 |
|
356 @param code code of the issue (string) |
|
357 @param line line number of the issue (integer) |
|
358 @param pos position inside line (integer) |
|
359 @keyparam apply flag indicating, that the fix should be applied |
|
360 (boolean) |
|
361 @return flag indicating an applied fix (boolean) and a message for |
|
362 the fix (string) |
|
363 """ |
|
364 line = line - 1 |
|
365 while self.__source[line][pos] in [" ", "\t"]: |
|
366 self.__source[line] = self.__source[line][:pos] + \ |
|
367 self.__source[line][pos + 1:] |
|
368 return (True, self.trUtf8("Extraneous whitespace removed.")) |
|
369 |
|
370 def __fixMissingWhitespaceAroundOperator(self, code, line, pos, |
|
371 apply=False): |
|
372 """ |
|
373 Private method to fix missing whitespace after ',;:'. |
|
374 |
|
375 @param code code of the issue (string) |
|
376 @param line line number of the issue (integer) |
|
377 @param pos position inside line (integer) |
|
378 @keyparam apply flag indicating, that the fix should be applied |
|
379 (boolean) |
|
380 @return flag indicating an applied fix (boolean) and a message for |
|
381 the fix (string) |
|
382 """ |
|
383 line = line - 1 |
|
384 pos = pos - 1 |
|
385 self.__source[line] = self.__source[line][:pos] + \ |
|
386 " " + \ |
|
387 self.__source[line][pos:] |
|
388 return (True, self.trUtf8("Missing whitespace added.")) |
|
389 |
|
390 def __fixWhitespaceAroundEquals(self, code, line, pos, apply=False): |
|
391 """ |
|
392 Private method to fix extraneous whitespace around keyword and |
|
393 default parameter equals. |
|
394 |
|
395 @param code code of the issue (string) |
|
396 @param line line number of the issue (integer) |
|
397 @param pos position inside line (integer) |
|
398 @keyparam apply flag indicating, that the fix should be applied |
|
399 (boolean) |
|
400 @return flag indicating an applied fix (boolean) and a message for |
|
401 the fix (string) |
|
402 """ |
|
403 line = line - 1 |
|
404 if self.__source[line][pos + 1] == " ": |
|
405 self.__source[line] = self.__source[line][:pos + 1] + \ |
|
406 self.__source[line][pos + 2:] |
|
407 if self.__source[line][pos - 1] == " ": |
|
408 self.__source[line] = self.__source[line][:pos - 1] + \ |
|
409 self.__source[line][pos:] |
|
410 return (True, self.trUtf8("Extraneous whitespace removed.")) |
|
411 |
|
412 def __fixWhitespaceBeforeInline(self, code, line, pos, apply=False): |
|
413 """ |
|
414 Private method to fix missing whitespace before inline comment. |
|
415 |
|
416 @param code code of the issue (string) |
|
417 @param line line number of the issue (integer) |
|
418 @param pos position inside line (integer) |
|
419 @keyparam apply flag indicating, that the fix should be applied |
|
420 (boolean) |
|
421 @return flag indicating an applied fix (boolean) and a message for |
|
422 the fix (string) |
|
423 """ |
|
424 line = line - 1 |
|
425 pos = pos - 1 |
|
426 if self.__source[line][pos] == " ": |
|
427 count = 1 |
|
428 else: |
|
429 count = 2 |
|
430 self.__source[line] = self.__source[line][:pos] + \ |
|
431 count * " " + \ |
|
432 self.__source[line][pos:] |
|
433 return (True, self.trUtf8("Missing whitespace added.")) |
|
434 |
|
435 def __fixWhitespaceAfterInline(self, code, line, pos, apply=False): |
|
436 """ |
|
437 Private method to fix whitespace after inline comment. |
|
438 |
|
439 @param code code of the issue (string) |
|
440 @param line line number of the issue (integer) |
|
441 @param pos position inside line (integer) |
|
442 @keyparam apply flag indicating, that the fix should be applied |
|
443 (boolean) |
|
444 @return flag indicating an applied fix (boolean) and a message for |
|
445 the fix (string) |
|
446 """ |
|
447 line = line - 1 |
|
448 if self.__source[line][pos] == " ": |
|
449 pos += 1 |
|
450 while self.__source[line][pos] == " ": |
|
451 self.__source[line] = self.__source[line][:pos] + \ |
|
452 self.__source[line][pos + 1:] |
|
453 else: |
|
454 self.__source[line] = self.__source[line][:pos] + \ |
|
455 " " + \ |
|
456 self.__source[line][pos:] |
|
457 return (True, self.trUtf8( |
|
458 "Whitespace after inline comment sign corrected.")) |