src/eric7/CodeFormatting/BlackFormattingDialog.py

branch
eric7
changeset 9344
52990830b13f
parent 9292
a5c8a0213fe3
child 9348
f61d71d95cb1
diff -r 7180fb8677e5 -r 52990830b13f src/eric7/CodeFormatting/BlackFormattingDialog.py
--- a/src/eric7/CodeFormatting/BlackFormattingDialog.py	Tue Sep 20 18:33:03 2022 +0200
+++ b/src/eric7/CodeFormatting/BlackFormattingDialog.py	Wed Sep 21 10:38:52 2022 +0200
@@ -74,32 +74,48 @@
         self.setupUi(self)
 
         self.progressBar.setMaximum(len(filesList))
-        self.progressBar.setValue(0)
 
         self.resultsList.header().setSortIndicator(1, Qt.SortOrder.AscendingOrder)
 
-        self.statisticsGroup.setVisible(False)
-
-        self.__statistics = BlackStatistics()
-
         self.__config = copy.deepcopy(configuration)
         self.__config["__action__"] = action  # needed by the workers
         self.__project = project
-        self.__action = action
 
-        self.__cancelled = False
+        self.__filesList = filesList[:]
+
         self.__diffDialog = None
 
         self.__allFilter = self.tr("<all>")
 
+        self.__formatButton = self.buttonBox.addButton(
+            self.tr("Format Code"), QDialogButtonBox.ButtonRole.ActionRole
+        )
+        self.__formatButton.setVisible(False)
+
+        self.show()
+        QCoreApplication.processEvents()
+
+        self.__performAction()
+
+    def __performAction(self):
+        """
+        Private method to exceute the requested formatting action.
+        """
+        self.progressBar.setValue(0)
+
+        self.statisticsGroup.setVisible(False)
+        self.__statistics = BlackStatistics()
+
+        self.__cancelled = False
+
+        self.statusFilterComboBox.clear()
+        self.resultsList.clear()
+
         self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(True)
         self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(False)
         self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setDefault(True)
 
-        self.show()
-        QCoreApplication.processEvents()
-
-        files = self.__filterFiles(filesList)
+        files = self.__filterFiles(self.__filesList)
         if len(files) > 1:
             self.__formatManyFiles(files)
         elif len(files) == 1:
@@ -182,6 +198,11 @@
 
         self.progressBar.setVisible(False)
 
+        self.__formatButton.setVisible(
+            self.__config["__action__"] is not BlackFormattingAction.Format
+            and self.__statistics.changeCount > 0
+        )
+
         self.__updateStatistics()
         self.__populateStatusFilterCombo()
 
@@ -192,7 +213,7 @@
         """
         self.reformattedLabel.setText(
             self.tr("Reformatted:")
-            if self.__action is BlackFormattingAction.Format
+            if self.__config["__action__"] is BlackFormattingAction.Format
             else self.tr("Would Reformat:")
         )
 
@@ -223,6 +244,17 @@
             self.__cancelled = True
         elif button == self.buttonBox.button(QDialogButtonBox.StandardButton.Close):
             self.accept()
+        elif button is self.__formatButton:
+            self.__formatButtonClicked()
+
+    @pyqtSlot()
+    def __formatButtonClicked(self):
+        """
+        Private slot handling the selection of the 'Format Code' button.
+        """
+        self.__config["__action__"] = BlackFormattingAction.Format
+
+        self.__performAction()
 
     @pyqtSlot(QTreeWidgetItem, int)
     def on_resultsList_itemDoubleClicked(self, item, column):
@@ -313,11 +345,15 @@
             taskQueue.put("STOP")
 
         for worker in workers:
-            worker.join()
+            worker.join(2)  # 2 seconds timeout just in case
+            # TODO: monitor this change
+            if worker.exitcode is None:
+                worker.terminate()
             worker.close()
 
         taskQueue.close()
-        doneQueue.close()
+        ##        doneQueue.close()
+        # TODO: monitor this change
 
         self.__finish()
 
@@ -407,7 +443,7 @@
             magic_trailing_comma=not self.__config["skip-magic-trailing-comma"],
         )
 
-        if self.__action is BlackFormattingAction.Diff:
+        if self.__config["__action__"] is BlackFormattingAction.Diff:
             relSrc = self.__project.getRelativePath(str(file)) if self.__project else ""
             self.__diffFormatFile(
                 pathlib.Path(file), fast=False, mode=mode, report=report, relSrc=relSrc
@@ -501,7 +537,7 @@
         if status == "changed":
             statusMsg = (
                 self.tr("would reformat")
-                if self.__action
+                if self.__config["__action__"]
                 in (BlackFormattingAction.Check, BlackFormattingAction.Diff)
                 else self.tr("reformatted")
             )

eric ide

mercurial