src/eric7/Project/ProjectSourcesBrowser.py

branch
eric7
changeset 9453
e5065dde905d
parent 9436
731d146193e2
child 9462
e65379fdbd97
diff -r 325c6de4b1f5 -r e5065dde905d src/eric7/Project/ProjectSourcesBrowser.py
--- a/src/eric7/Project/ProjectSourcesBrowser.py	Mon Oct 31 14:09:07 2022 +0100
+++ b/src/eric7/Project/ProjectSourcesBrowser.py	Mon Oct 31 15:29:18 2022 +0100
@@ -38,6 +38,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 ProjectSourcesBrowser(ProjectBaseBrowser):
     """
@@ -147,6 +150,20 @@
             self.tr("Formatting Diff"),
             lambda: self.__performFormatWithBlack(BlackFormattingAction.Diff),
         )
+        self.formattingMenu.addSeparator()
+        act = self.formattingMenu.addAction(self.tr("isort"), aboutIsort)
+        font = act.font()
+        font.setBold(True)
+        act.setFont(font)
+        self.formattingMenu.addAction(
+            self.tr("Sort Imports"),
+            lambda: self.__performImportSortingWithIsort(IsortFormattingAction.Sort),
+        )
+        self.formattingMenu.addAction(
+            self.tr("Imports Sorting Diff"),
+            lambda: self.__performImportSortingWithIsort(IsortFormattingAction.Diff),
+        )
+        self.formattingMenu.addSeparator()
         self.formattingMenu.aboutToShow.connect(self.__showContextMenuFormatting)
 
         self.menuShow = QMenu(self.tr("Show"))
@@ -1271,15 +1288,7 @@
 
         files = [
             itm.fileName()
-            for itm in self.getSelectedItems(
-                [
-                    BrowserFileItem,
-                    BrowserClassItem,
-                    BrowserMethodItem,
-                    BrowserClassAttributeItem,
-                    BrowserImportItem,
-                ]
-            )
+            for itm in self.getSelectedItems([BrowserFileItem])
             if itm.isPython3File()
         ]
         if not files:
@@ -1310,3 +1319,59 @@
                 self.tr("Code Formatting"),
                 self.tr("""There are no files left for reformatting."""),
             )
+
+    def __performImportSortingWithIsort(self, action):
+        """
+        Private method to sort the import statements of the selected project sources
+        using the 'isort' tool.
+
+        Following actions are supported.
+        <ul>
+        <li>IsortFormattingAction.Sort - the import statement sorting is performed</li>
+        <li>IsortFormattingAction.Check - a check is performed, if import statement
+            resorting is necessary</li>
+        <li>IsortFormattingAction.Diff - a unified diff of potential import statement
+            changes is generated</li>
+        </ul>
+
+        @param action sorting operation to be performed
+        @type IsortFormattingAction
+        """
+        from eric7.CodeFormatting.IsortConfigurationDialog import (
+            IsortConfigurationDialog,
+        )
+        from eric7.CodeFormatting.IsortFormattingDialog import IsortFormattingDialog
+
+        files = [
+            itm.fileName()
+            for itm in self.getSelectedItems([BrowserFileItem])
+            if itm.isPython3File()
+        ]
+        if not files:
+            # called for a directory
+            itm = self.model().item(self.currentIndex())
+            dirName = itm.dirName()
+            files = [
+                f
+                for f in self.project.getProjectFiles("SOURCES", normalized=True)
+                if f.startswith(dirName)
+            ]
+
+        vm = ericApp().getObject("ViewManager")
+        files = [fn for fn in files if vm.checkFileDirty(fn)]
+
+        if files:
+            dlg = IsortConfigurationDialog(withProject=True)
+            if dlg.exec() == QDialog.DialogCode.Accepted:
+                config = dlg.getConfiguration()
+
+                formattingDialog = IsortFormattingDialog(
+                    config, files, project=self.project, action=action
+                )
+                formattingDialog.exec()
+        else:
+            EricMessageBox.information(
+                self,
+                self.tr("Import Sorting"),
+                self.tr("""There are no files left for import statement sorting."""),
+            )

eric ide

mercurial