Plugins/CheckerPlugins/Pep8/Pep8Dialog.py

changeset 2891
6843b7d23279
parent 2890
86b03a0c94bc
child 2894
8e4264045fc9
equal deleted inserted replaced
2890:86b03a0c94bc 2891:6843b7d23279
124 @param pos character position of issue (integer or string) 124 @param pos character position of issue (integer or string)
125 @param message message text (string) 125 @param message message text (string)
126 @param fixed flag indicating a fixed issue (boolean) 126 @param fixed flag indicating a fixed issue (boolean)
127 @param autofixing flag indicating, that we are fixing issues 127 @param autofixing flag indicating, that we are fixing issues
128 automatically (boolean) 128 automatically (boolean)
129 @return reference to the created item (QTreeWidgetItem)
129 """ 130 """
130 from .Pep8Fixer import Pep8FixableIssues 131 from .Pep8Fixer import Pep8FixableIssues
131 132
132 if self.__lastFileItem is None: 133 if self.__lastFileItem is None:
133 # It's a new file 134 # It's a new file
161 itm.setData(0, self.lineRole, int(line)) 162 itm.setData(0, self.lineRole, int(line))
162 itm.setData(0, self.positionRole, int(pos)) 163 itm.setData(0, self.positionRole, int(pos))
163 itm.setData(0, self.messageRole, message) 164 itm.setData(0, self.messageRole, message)
164 itm.setData(0, self.fixableRole, fixable) 165 itm.setData(0, self.fixableRole, fixable)
165 itm.setData(0, self.codeRole, code) 166 itm.setData(0, self.codeRole, code)
167
168 return itm
166 169
167 def __modifyFixedResultItem(self, itm, text, fixed): 170 def __modifyFixedResultItem(self, itm, text, fixed):
168 """ 171 """
169 Private method to modify a result list entry to show its 172 Private method to modify a result list entry to show its
170 positive fixed state. 173 positive fixed state.
411 max_line_length=maxLineLength, 414 max_line_length=maxLineLength,
412 hang_closing=hangClosing, 415 hang_closing=hangClosing,
413 ) 416 )
414 report = styleGuide.check_files([file]) 417 report = styleGuide.check_files([file])
415 report.errors.sort(key=lambda a: a[1]) 418 report.errors.sort(key=lambda a: a[1])
419 deferredFixes = {}
416 for error in report.errors: 420 for error in report.errors:
417 fname, lineno, position, text = error 421 fname, lineno, position, text = error
418 if lineno > len(source): 422 if lineno > len(source):
419 lineno = len(source) 423 lineno = len(source)
420 if "__IGNORE_WARNING__" not in Utilities.extractLineFlags( 424 if "__IGNORE_WARNING__" not in Utilities.extractLineFlags(
421 source[lineno - 1].strip()): 425 source[lineno - 1].strip()):
422 self.noResults = False 426 self.noResults = False
423 fixed = False
424 if fixer: 427 if fixer:
425 fixed, msg = fixer.fixIssue(lineno, position, text) 428 res, msg, id_ = fixer.fixIssue(lineno, position, text)
426 if fixed: 429 if res == 1:
427 text += "\n" + \ 430 text += "\n" + \
428 self.trUtf8("Fix: {0}").format(msg) 431 self.trUtf8("Fix: {0}").format(msg)
429 self.__createResultItem( 432 self.__createResultItem(
430 fname, lineno, position, text, fixed, fixIssues) 433 fname, lineno, position, text, True, True)
431 fixer and fixer.saveFile(encoding) 434 elif res == 0:
435 self.__createResultItem(
436 fname, lineno, position, text, False, True)
437 else:
438 itm = self.__createResultItem(
439 fname, lineno, position,
440 text, False, False)
441 deferredFixes[id_] = itm
442 else:
443 self.__createResultItem(
444 fname, lineno, position, text, False, False)
445 if fixer:
446 deferredResults = fixer.finalize()
447 for id_ in deferredResults:
448 fixed, msg = deferredResults[id_]
449 itm = deferredFixes[id_]
450 if fixed == 1:
451 text = "\n" + self.trUtf8("Fix: {0}").format(msg)
452 self.__modifyFixedResultItem(itm, text, True)
453 else:
454 self.__modifyFixedResultItem(itm, "", False)
455 fixer.saveFile(encoding)
432 self.__updateStatistics(report.counters, fixer) 456 self.__updateStatistics(report.counters, fixer)
433 progress += 1 457 progress += 1
434 finally: 458 finally:
435 # reenable updates of the list 459 # reenable updates of the list
436 self.resultList.setSortingEnabled(True) 460 self.resultList.setSortingEnabled(True)
751 except (UnicodeError, IOError) as msg: 775 except (UnicodeError, IOError) as msg:
752 # skip silently because that should not happen 776 # skip silently because that should not happen
753 progress += 1 777 progress += 1
754 continue 778 continue
755 779
780 deferredFixes = {}
756 fixer = Pep8Fixer(self.__project, file, source, 781 fixer = Pep8Fixer(self.__project, file, source,
757 fixCodes, noFixCodes, maxLineLength, 782 fixCodes, noFixCodes, maxLineLength,
758 True) # always fix in place 783 True) # always fix in place
759 errors = fixesDict[file] 784 errors = fixesDict[file]
760 errors.sort(key=lambda a: a[0][0]) 785 errors.sort(key=lambda a: a[0][0])
761 for error in errors: 786 for error in errors:
762 (lineno, position, text), itm = error 787 (lineno, position, text), itm = error
763 if lineno > len(source): 788 if lineno > len(source):
764 lineno = len(source) 789 lineno = len(source)
765 fixed, msg = fixer.fixIssue(lineno, position, text) 790 fixed, msg, id_ = fixer.fixIssue(lineno, position, text)
766 if fixed: 791 if fixed == 1:
767 text = "\n" + self.trUtf8("Fix: {0}").format(msg) 792 text = "\n" + self.trUtf8("Fix: {0}").format(msg)
793 self.__modifyFixedResultItem(itm, text, True)
794 elif fixed == 0:
795 self.__modifyFixedResultItem(itm, "", False)
768 else: 796 else:
769 text = "" 797 # remember item for the deferred fix
770 self.__modifyFixedResultItem(itm, text, fixed) 798 deferredFixes[id_] = itm
799 deferredResults = fixer.finalize()
800 for id_ in deferredResults:
801 fixed, msg = deferredResults[id_]
802 itm = deferredFixes[id_]
803 if fixed == 1:
804 text = "\n" + self.trUtf8("Fix: {0}").format(msg)
805 self.__modifyFixedResultItem(itm, text, True)
806 else:
807 self.__modifyFixedResultItem(itm, "", False)
771 fixer.saveFile(encoding) 808 fixer.saveFile(encoding)
772 809
773 self.__updateFixerStatistics(fixer) 810 self.__updateFixerStatistics(fixer)
774 progress += 1 811 progress += 1
775 812

eric ide

mercurial