Modified the Black and Isort dialogs such, that only files that need a change are processed after a diff or check run by pressing the relevant button. eric7

Wed, 02 Nov 2022 10:11:18 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 02 Nov 2022 10:11:18 +0100
branch
eric7
changeset 9460
0d1b5d0fd815
parent 9459
f9c6a8f86195
child 9461
24c0c1066090

Modified the Black and Isort dialogs such, that only files that need a change are processed after a diff or check run by pressing the relevant button.

src/eric7/CodeFormatting/BlackFormattingDialog.py file | annotate | diff | comparison | revisions
src/eric7/CodeFormatting/IsortFormattingDialog.py file | annotate | diff | comparison | revisions
--- a/src/eric7/CodeFormatting/BlackFormattingDialog.py	Tue Nov 01 19:46:49 2022 +0100
+++ b/src/eric7/CodeFormatting/BlackFormattingDialog.py	Wed Nov 02 10:11:18 2022 +0100
@@ -42,11 +42,13 @@
     results.
     """
 
-    DataTypeRole = Qt.ItemDataRole.UserRole
-    DataRole = Qt.ItemDataRole.UserRole + 1
+    DataRole = Qt.ItemDataRole.UserRole
+    DataTypeRole = Qt.ItemDataRole.UserRole + 1
+    FileNameRole = Qt.ItemDataRole.UserRole + 2
+    StatusRole = Qt.ItemDataRole.UserRole + 3
 
+    FileNameColumn = 1
     StatusColumn = 0
-    FileNameColumn = 1
 
     def __init__(
         self,
@@ -253,8 +255,15 @@
         """
         Private slot handling the selection of the 'Format Code' button.
         """
+        files = []
+        for row in range(self.resultsList.topLevelItemCount()):
+            itm = self.resultsList.topLevelItem(row)
+            if itm.data(0, BlackFormattingDialog.StatusRole) == "changed":
+                files.append(itm.data(0, BlackFormattingDialog.FileNameRole))
+        if files:
+            self.__filesList = files
+
         self.__config["__action__"] = BlackFormattingAction.Format
-
         self.__performAction()
 
     @pyqtSlot(QTreeWidgetItem, int)
@@ -565,10 +574,17 @@
         if status != "ignored":
             self.__statistics.processedCount += 1
 
-        if self.__project:
-            filename = self.__project.getRelativePath(filename)
-
-        itm = QTreeWidgetItem(self.resultsList, [statusMsg, filename])
+        itm = QTreeWidgetItem(
+            self.resultsList,
+            [
+                statusMsg,
+                self.__project.getRelativePath(filename)
+                if self.__project
+                else filename,
+            ],
+        )
+        itm.setData(0, BlackFormattingDialog.StatusRole, status)
+        itm.setData(0, BlackFormattingDialog.FileNameRole, filename)
         if data:
             itm.setData(
                 0, BlackFormattingDialog.DataTypeRole, "error" if isError else "diff"
--- a/src/eric7/CodeFormatting/IsortFormattingDialog.py	Tue Nov 01 19:46:49 2022 +0100
+++ b/src/eric7/CodeFormatting/IsortFormattingDialog.py	Wed Nov 02 10:11:18 2022 +0100
@@ -42,11 +42,13 @@
     results.
     """
 
-    DataTypeRole = Qt.ItemDataRole.UserRole
-    DataRole = Qt.ItemDataRole.UserRole + 1
+    DataRole = Qt.ItemDataRole.UserRole
+    DataTypeRole = Qt.ItemDataRole.UserRole + 1
+    FileNameRole = Qt.ItemDataRole.UserRole + 2
+    StatusRole = Qt.ItemDataRole.UserRole + 3
 
+    FileNameColumn = 1
     StatusColumn = 0
-    FileNameColumn = 1
 
     def __init__(
         self,
@@ -248,8 +250,15 @@
         """
         Private slot handling the selection of the 'Sort Imports' button.
         """
+        files = []
+        for row in range(self.resultsList.topLevelItemCount()):
+            itm = self.resultsList.topLevelItem(row)
+            if itm.data(0, IsortFormattingDialog.StatusRole) == "changed":
+                files.append(itm.data(0, IsortFormattingDialog.FileNameRole))
+        if files:
+            self.__filesList = files
+
         self.__config["__action__"] = IsortFormattingAction.Sort
-
         self.__performAction()
 
     @pyqtSlot(QTreeWidgetItem, int)
@@ -357,7 +366,17 @@
         if self.__project:
             filename = self.__project.getRelativePath(filename)
 
-        itm = QTreeWidgetItem(self.resultsList, [statusMsg, filename])
+        itm = QTreeWidgetItem(
+            self.resultsList,
+            [
+                statusMsg,
+                self.__project.getRelativePath(filename)
+                if self.__project
+                else filename,
+            ],
+        )
+        itm.setData(0, IsortFormattingDialog.StatusRole, status)
+        itm.setData(0, IsortFormattingDialog.FileNameRole, filename)
         if data:
             itm.setData(
                 0, IsortFormattingDialog.DataTypeRole, "error" if isError else "diff"

eric ide

mercurial