src/eric7/QScintilla/Editor.py

branch
eric7
changeset 9453
e5065dde905d
parent 9436
731d146193e2
child 9465
8a020c34dce2
--- a/src/eric7/QScintilla/Editor.py	Mon Oct 31 14:09:07 2022 +0100
+++ b/src/eric7/QScintilla/Editor.py	Mon Oct 31 15:29:18 2022 +0100
@@ -57,6 +57,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
+
 EditorAutoCompletionListID = 1
 TemplateCompletionListID = 2
 ReferencesListID = 3
@@ -1046,6 +1049,10 @@
         """
         menu = QMenu(self.tr("Code Formatting"))
 
+        #######################################################################
+        ## Black related entries
+        #######################################################################
+
         act = menu.addAction(self.tr("Black"), aboutBlack)
         font = act.font()
         font.setBold(True)
@@ -1062,6 +1069,25 @@
             self.tr("Formatting Diff"),
             lambda: self.__performFormatWithBlack(BlackFormattingAction.Diff),
         )
+        menu.addSeparator()
+
+        #######################################################################
+        ## isort related entries
+        #######################################################################
+
+        act = menu.addAction(self.tr("isort"), aboutIsort)
+        font = act.font()
+        font.setBold(True)
+        act.setFont(font)
+        menu.addAction(
+            self.tr("Sort Imports"),
+            lambda: self.__performImportSortingWithIsort(IsortFormattingAction.Sort),
+        )
+        menu.addAction(
+            self.tr("Imports Sorting Diff"),
+            lambda: self.__performImportSortingWithIsort(IsortFormattingAction.Diff),
+        )
+        menu.addSeparator()
 
         menu.aboutToShow.connect(self.__showContextMenuFormatting)
 
@@ -9061,7 +9087,7 @@
             self.__cancelMouseHoverHelp()
 
     #######################################################################
-    ## Methods implementing the Black code formatting interface
+    ## Methods implementing the code formatting interface
     #######################################################################
 
     def __performFormatWithBlack(self, action):
@@ -9099,3 +9125,39 @@
                     config, [self.fileName], project=self.project, action=action
                 )
                 formattingDialog.exec()
+
+    def __performImportSortingWithIsort(self, action):
+        """
+        Private method to sort the import statements 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
+
+        if not self.isModified() or self.saveFile():
+            withProject = (
+                self.fileName
+                and self.project.isOpen()
+                and self.project.isProjectSource(self.fileName)
+            )
+            dlg = IsortConfigurationDialog(withProject=withProject)
+            if dlg.exec() == QDialog.DialogCode.Accepted:
+                config = dlg.getConfiguration()
+
+                formattingDialog = IsortFormattingDialog(
+                    config, [self.fileName], project=self.project, action=action
+                )
+                formattingDialog.exec()

eric ide

mercurial