src/eric7/Project/Project.py

branch
eric7
changeset 9453
e5065dde905d
parent 9436
731d146193e2
child 9473
3f23dbf37dbe
diff -r 325c6de4b1f5 -r e5065dde905d src/eric7/Project/Project.py
--- a/src/eric7/Project/Project.py	Mon Oct 31 14:09:07 2022 +0100
+++ b/src/eric7/Project/Project.py	Mon Oct 31 15:29:18 2022 +0100
@@ -59,6 +59,9 @@
 from eric7.CodeFormatting.BlackFormattingAction import BlackFormattingAction
 from eric7.CodeFormatting.BlackUtilities import aboutBlack
 
+from eric7.CodeFormatting.IsortFormattingAction import IsortFormattingAction
+from eric7.CodeFormatting.IsortUtilities import aboutIsort
+
 
 class Project(QObject):
     """
@@ -4867,7 +4870,7 @@
         self.actions.append(self.createSBOMAct)
 
         ###################################################################
-        ## Project Tools - code formatting actions
+        ## Project Tools - code formatting actions - Black
         ###################################################################
 
         self.blackFormattingGrp = createActionGroup(self)
@@ -4993,6 +4996,108 @@
         self.actions.append(self.blackConfigureAct)
 
         ###################################################################
+        ## Project Tools - code formatting actions - isort
+        ###################################################################
+
+        self.isortFormattingGrp = createActionGroup(self)
+
+        self.isortAboutAct = EricAction(
+            self.tr("About isort"),
+            self.tr("&isort"),
+            0,
+            0,
+            self.isortFormattingGrp,
+            "project_isort_about",
+        )
+        self.isortAboutAct.setStatusTip(self.tr("Show some information about 'isort'."))
+        self.isortAboutAct.setWhatsThis(
+            self.tr(
+                "<b>isort</b>"
+                "<p>This shows some information about the installed 'isort' tool.</p>"
+            )
+        )
+        self.isortAboutAct.triggered.connect(aboutIsort)
+        self.actions.append(self.isortAboutAct)
+        font = self.isortAboutAct.font()
+        font.setBold(True)
+        self.isortAboutAct.setFont(font)
+
+        self.isortSortImportsAct = EricAction(
+            self.tr("Sort Imports"),
+            self.tr("Sort Imports"),
+            0,
+            0,
+            self.isortFormattingGrp,
+            "project_isort_sort_imports",
+        )
+        self.isortSortImportsAct.setStatusTip(
+            self.tr("Sort the import statements of the project sources with 'isort'.")
+        )
+        self.isortSortImportsAct.setWhatsThis(
+            self.tr(
+                "<b>Sort Imports</b>"
+                "<p>This shows a dialog to enter parameters for the imports sorting"
+                " run and sorts the import statements of the project sources using"
+                " 'isort'.</p>"
+            )
+        )
+        self.isortSortImportsAct.triggered.connect(
+            lambda: self.__performImportSortingWithIsort(IsortFormattingAction.Sort)
+        )
+        self.actions.append(self.isortSortImportsAct)
+
+        self.isortDiffSortingAct = EricAction(
+            self.tr("Imports Sorting Diff"),
+            self.tr("Imports Sorting Diff"),
+            0,
+            0,
+            self.isortFormattingGrp,
+            "project_isort_diff_code",
+        )
+        self.isortDiffSortingAct.setStatusTip(
+            self.tr(
+                "Generate a unified diff of potential project source imports"
+                " resorting with 'isort'."
+            )
+        )
+        self.isortDiffSortingAct.setWhatsThis(
+            self.tr(
+                "<b>Imports Sorting Diff</b>"
+                "<p>This shows a dialog to enter parameters for the imports sorting"
+                " diff run and generates a unified diff of potential project source"
+                " changes using 'isort'.</p>"
+            )
+        )
+        self.isortDiffSortingAct.triggered.connect(
+            lambda: self.__performImportSortingWithIsort(IsortFormattingAction.Diff)
+        )
+        self.actions.append(self.isortDiffSortingAct)
+
+        self.isortConfigureAct = EricAction(
+            self.tr("Configure"),
+            self.tr("Configure"),
+            0,
+            0,
+            self.isortFormattingGrp,
+            "project_isort_configure",
+        )
+        self.isortConfigureAct.setStatusTip(
+            self.tr(
+                "Enter the parameters for resorting the project sources import"
+                " statements with 'isort'."
+            )
+        )
+        self.isortConfigureAct.setWhatsThis(
+            self.tr(
+                "<b>Configure</b>"
+                "<p>This shows a dialog to enter the parameters for resorting the"
+                " import statements of the project sources with 'isort'.</p>"
+            )
+        )
+        self.isortConfigureAct.triggered.connect(self.__configureIsort)
+        self.actions.append(self.isortConfigureAct)
+
+        ###################################################################
         ## Project - embedded environment actions
         ###################################################################
 
@@ -5205,6 +5310,8 @@
         self.formattingMenu.setTearOffEnabled(True)
         self.formattingMenu.addActions(self.blackFormattingGrp.actions())
         self.formattingMenu.addSeparator()
+        self.formattingMenu.addActions(self.isortFormattingGrp.actions())
+        self.formattingMenu.addSeparator()
 
         # build the project main menu
         menu.setTearOffEnabled(True)
@@ -6787,6 +6894,56 @@
             dlg.getConfiguration(saveToProject=True)
             # The data is saved to the project as a side effect.
 
+    def __performImportSortingWithIsort(self, action):
+        """
+        Private method to format the project sources import statements using the
+        'isort' tool.
+
+        Following actions are supported.
+        <ul>
+        <li>IsortFormattingAction.Format - the imports reformatting is performed</li>
+        <li>IsortFormattingAction.Check - a check is performed, if imports formatting
+            is necessary</li>
+        <li>IsortFormattingAction.Diff - a unified diff of potential imports formatting
+            changes is generated</li>
+        </ul>
+
+        @param action formatting operation to be performed
+        @type IsortFormattingAction
+        """
+        from eric7.CodeFormatting.IsortConfigurationDialog import (
+            IsortConfigurationDialog,
+        )
+        from eric7.CodeFormatting.IsortFormattingDialog import IsortFormattingDialog
+
+        if ericApp().getObject("ViewManager").checkAllDirty():
+            dlg = IsortConfigurationDialog(withProject=True)
+            if dlg.exec() == QDialog.DialogCode.Accepted:
+                config = dlg.getConfiguration(saveToProject=True)
+
+                isortDialog = IsortFormattingDialog(
+                    config,
+                    self.getProjectFiles("SOURCES", normalized=True),
+                    project=self,
+                    action=action,
+                )
+                isortDialog.exec()
+
+    @pyqtSlot()
+    def __configureIsort(self):
+        """
+        Private slot to enter the parameters for formatting the import statements of the
+        project sources with 'isort'.
+        """
+        from eric7.CodeFormatting.IsortConfigurationDialog import (
+            IsortConfigurationDialog,
+        )
+
+        dlg = IsortConfigurationDialog(withProject=True, onlyProject=True)
+        if dlg.exec() == QDialog.DialogCode.Accepted:
+            dlg.getConfiguration(saveToProject=True)
+            # The data is saved to the project as a side effect.
+
     #########################################################################
     ## Below are methods implementing the 'Embedded Environment' support
     #########################################################################

eric ide

mercurial