src/eric7/QScintilla/Editor.py

branch
eric7
changeset 9214
bd28e56047d7
parent 9209
b99e7fd55fd3
child 9221
bf71ee032bb4
equal deleted inserted replaced
9213:2bf743848d2f 9214:bd28e56047d7
45 from Utilities import MouseUtilities 45 from Utilities import MouseUtilities
46 46
47 import UI.PixmapCache 47 import UI.PixmapCache
48 48
49 from UI import PythonDisViewer 49 from UI import PythonDisViewer
50
51 from CodeFormatting.BlackFormattingAction import BlackFormattingAction
50 52
51 EditorAutoCompletionListID = 1 53 EditorAutoCompletionListID = 1
52 TemplateCompletionListID = 2 54 TemplateCompletionListID = 2
53 ReferencesListID = 3 55 ReferencesListID = 3
54 56
814 else: 816 else:
815 self.checksMenu = self.__initContextMenuChecks() 817 self.checksMenu = self.__initContextMenuChecks()
816 self.menuShow = self.__initContextMenuShow() 818 self.menuShow = self.__initContextMenuShow()
817 self.graphicsMenu = self.__initContextMenuGraphics() 819 self.graphicsMenu = self.__initContextMenuGraphics()
818 self.autocompletionMenu = self.__initContextMenuAutocompletion() 820 self.autocompletionMenu = self.__initContextMenuAutocompletion()
821 self.codeFormattingMenu = self.__initContextMenuFormatting()
819 self.__menus["Checks"] = self.checksMenu 822 self.__menus["Checks"] = self.checksMenu
820 self.__menus["Show"] = self.menuShow 823 self.__menus["Show"] = self.menuShow
821 self.__menus["Graphics"] = self.graphicsMenu 824 self.__menus["Graphics"] = self.graphicsMenu
822 self.__menus["Autocompletion"] = self.autocompletionMenu 825 self.__menus["Autocompletion"] = self.autocompletionMenu
826 self.__menus["Formatting"] = self.codeFormattingMenu
823 self.toolsMenu = self.__initContextMenuTools() 827 self.toolsMenu = self.__initContextMenuTools()
824 self.__menus["Tools"] = self.toolsMenu 828 self.__menus["Tools"] = self.toolsMenu
825 self.eolMenu = self.__initContextMenuEol() 829 self.eolMenu = self.__initContextMenuEol()
826 self.__menus["Eol"] = self.eolMenu 830 self.__menus["Eol"] = self.eolMenu
827 self.encodingsMenu = self.__initContextMenuEncodings() 831 self.encodingsMenu = self.__initContextMenuEncodings()
915 self.menu.addSeparator() 919 self.menu.addSeparator()
916 if self.isResourcesFile: 920 if self.isResourcesFile:
917 self.menu.addMenu(self.resourcesMenu) 921 self.menu.addMenu(self.resourcesMenu)
918 else: 922 else:
919 self.menuActs["Check"] = self.menu.addMenu(self.checksMenu) 923 self.menuActs["Check"] = self.menu.addMenu(self.checksMenu)
920 self.menu.addSeparator() 924 self.menuActs["Formatting"] = self.menu.addMenu(self.codeFormattingMenu)
921 self.menuActs["Show"] = self.menu.addMenu(self.menuShow) 925 self.menuActs["Show"] = self.menu.addMenu(self.menuShow)
922 self.menu.addSeparator()
923 self.menuActs["Diagrams"] = self.menu.addMenu(self.graphicsMenu) 926 self.menuActs["Diagrams"] = self.menu.addMenu(self.graphicsMenu)
924 self.menu.addSeparator() 927 self.menu.addSeparator()
925 self.menuActs["Tools"] = self.menu.addMenu(self.toolsMenu) 928 self.menuActs["Tools"] = self.menu.addMenu(self.toolsMenu)
926 self.menu.addSeparator() 929 self.menu.addSeparator()
927 self.menu.addAction( 930 self.menu.addAction(
990 """ 993 """
991 menu = QMenu(self.tr('Check')) 994 menu = QMenu(self.tr('Check'))
992 menu.aboutToShow.connect(self.__showContextMenuChecks) 995 menu.aboutToShow.connect(self.__showContextMenuChecks)
993 return menu 996 return menu
994 997
998 def __initContextMenuFormatting(self):
999 """
1000 Private method used to setup the Code Formatting context sub menu.
1001
1002 @return reference to the generated menu
1003 @rtype QMenu
1004 """
1005 menu = QMenu(self.tr("Code Formatting"))
1006
1007 menu.addAction(
1008 self.tr("Format Code"),
1009 lambda: self.__performFormatWithBlack(BlackFormattingAction.Format)
1010 )
1011 menu.addAction(
1012 self.tr("Check Formatting"),
1013 lambda: self.__performFormatWithBlack(BlackFormattingAction.Check)
1014 )
1015 menu.addAction(
1016 self.tr("Formatting Diff"),
1017 lambda: self.__performFormatWithBlack(BlackFormattingAction.Diff)
1018 )
1019
1020 menu.aboutToShow.connect(self.__showContextMenuFormatting)
1021
1022 return menu
1023
995 def __initContextMenuTools(self): 1024 def __initContextMenuTools(self):
996 """ 1025 """
997 Private method used to setup the Tools context sub menu. 1026 Private method used to setup the Tools context sub menu.
998 1027
999 @return reference to the generated menu 1028 @return reference to the generated menu
5797 """ 5826 """
5798 Private slot handling the aboutToShow signal of the tools context 5827 Private slot handling the aboutToShow signal of the tools context
5799 menu. 5828 menu.
5800 """ 5829 """
5801 self.showMenu.emit("Tools", self.toolsMenu, self) 5830 self.showMenu.emit("Tools", self.toolsMenu, self)
5802 5831
5832 def __showContextMenuFormatting(self):
5833 """
5834 Private slot handling the aboutToShow signal of the code formatting context
5835 menu.
5836 """
5837 self.showMenu.emit("Formatting", self.codeFormattingMenu, self)
5838
5803 def __reopenWithEncodingMenuTriggered(self, act): 5839 def __reopenWithEncodingMenuTriggered(self, act):
5804 """ 5840 """
5805 Private method to handle the rereading of the file with a selected 5841 Private method to handle the rereading of the file with a selected
5806 encoding. 5842 encoding.
5807 5843
8872 @param x x-value of mouse screen position 8908 @param x x-value of mouse screen position
8873 @type int 8909 @type int
8874 @param y y-value of mouse screen position 8910 @param y y-value of mouse screen position
8875 @type int 8911 @type int
8876 """ 8912 """
8877 if not self.isCallTipActive() and not self.isListActive(): 8913 if (
8914 not self.isCallTipActive()
8915 and not self.isListActive()
8916 and not self.menu.isVisible()
8917 and not self.spellingMenu.isVisible()
8918 ):
8878 if self.__mouseHoverHelp is not None and pos > 0 and y > 0: 8919 if self.__mouseHoverHelp is not None and pos > 0 and y > 0:
8879 line, index = self.lineIndexFromPosition(pos) 8920 line, index = self.lineIndexFromPosition(pos)
8880 if index > 0: 8921 if index > 0:
8881 self.__mouseHoverHelp(self, line, index) 8922 self.__mouseHoverHelp(self, line, index)
8882 else: 8923 else:
8932 self.SendScintilla(QsciScintilla.SCI_CALLTIPSHOW, 8973 self.SendScintilla(QsciScintilla.SCI_CALLTIPSHOW,
8933 pos, self._encodeString(data)) 8974 pos, self._encodeString(data))
8934 self.__showingMouseHoverHelp = True 8975 self.__showingMouseHoverHelp = True
8935 else: 8976 else:
8936 self.__cancelMouseHoverHelp() 8977 self.__cancelMouseHoverHelp()
8978
8979 #######################################################################
8980 ## Methods implementing the Black code formatting interface
8981 #######################################################################
8982
8983 def __performFormatWithBlack(self, action):
8984 """
8985 Private method to format the source code using the 'Black' tool.
8986
8987 Following actions are supported.
8988 <ul>
8989 <li>BlackFormattingAction.Format - the code reformatting is performed</li>
8990 <li>BlackFormattingAction.Check - a check is performed, if code formatting
8991 is necessary</li>
8992 <li>BlackFormattingAction.Diff - a unified diff of potential code formatting
8993 changes is generated</li>
8994 </ul>
8995
8996 @param action formatting operation to be performed
8997 @type BlackFormattingAction
8998 """
8999 from CodeFormatting.BlackConfigurationDialog import BlackConfigurationDialog
9000 from CodeFormatting.BlackFormattingDialog import BlackFormattingDialog
9001
9002 if not self.isModified() or self.saveFile():
9003 withProject = (
9004 self.fileName and
9005 self.project.isOpen() and
9006 self.project.isProjectSource(self.fileName)
9007 )
9008 dlg = BlackConfigurationDialog(withProject=withProject)
9009 if dlg.exec() == QDialog.DialogCode.Accepted:
9010 config = dlg.getConfiguration()
9011
9012 formattingDialog = BlackFormattingDialog(
9013 config,
9014 [self.fileName],
9015 project=self.project,
9016 action=action
9017 )
9018 formattingDialog.exec()

eric ide

mercurial