54 |
54 |
55 from eric7.UI import PythonDisViewer |
55 from eric7.UI import PythonDisViewer |
56 |
56 |
57 from eric7.CodeFormatting.BlackFormattingAction import BlackFormattingAction |
57 from eric7.CodeFormatting.BlackFormattingAction import BlackFormattingAction |
58 from eric7.CodeFormatting.BlackUtilities import aboutBlack |
58 from eric7.CodeFormatting.BlackUtilities import aboutBlack |
|
59 |
|
60 from eric7.CodeFormatting.IsortFormattingAction import IsortFormattingAction |
|
61 from eric7.CodeFormatting.IsortUtilities import aboutIsort |
59 |
62 |
60 EditorAutoCompletionListID = 1 |
63 EditorAutoCompletionListID = 1 |
61 TemplateCompletionListID = 2 |
64 TemplateCompletionListID = 2 |
62 ReferencesListID = 3 |
65 ReferencesListID = 3 |
63 |
66 |
1044 @return reference to the generated menu |
1047 @return reference to the generated menu |
1045 @rtype QMenu |
1048 @rtype QMenu |
1046 """ |
1049 """ |
1047 menu = QMenu(self.tr("Code Formatting")) |
1050 menu = QMenu(self.tr("Code Formatting")) |
1048 |
1051 |
|
1052 ####################################################################### |
|
1053 ## Black related entries |
|
1054 ####################################################################### |
|
1055 |
1049 act = menu.addAction(self.tr("Black"), aboutBlack) |
1056 act = menu.addAction(self.tr("Black"), aboutBlack) |
1050 font = act.font() |
1057 font = act.font() |
1051 font.setBold(True) |
1058 font.setBold(True) |
1052 act.setFont(font) |
1059 act.setFont(font) |
1053 menu.addAction( |
1060 menu.addAction( |
1060 ) |
1067 ) |
1061 menu.addAction( |
1068 menu.addAction( |
1062 self.tr("Formatting Diff"), |
1069 self.tr("Formatting Diff"), |
1063 lambda: self.__performFormatWithBlack(BlackFormattingAction.Diff), |
1070 lambda: self.__performFormatWithBlack(BlackFormattingAction.Diff), |
1064 ) |
1071 ) |
|
1072 menu.addSeparator() |
|
1073 |
|
1074 ####################################################################### |
|
1075 ## isort related entries |
|
1076 ####################################################################### |
|
1077 |
|
1078 act = menu.addAction(self.tr("isort"), aboutIsort) |
|
1079 font = act.font() |
|
1080 font.setBold(True) |
|
1081 act.setFont(font) |
|
1082 menu.addAction( |
|
1083 self.tr("Sort Imports"), |
|
1084 lambda: self.__performImportSortingWithIsort(IsortFormattingAction.Sort), |
|
1085 ) |
|
1086 menu.addAction( |
|
1087 self.tr("Imports Sorting Diff"), |
|
1088 lambda: self.__performImportSortingWithIsort(IsortFormattingAction.Diff), |
|
1089 ) |
|
1090 menu.addSeparator() |
1065 |
1091 |
1066 menu.aboutToShow.connect(self.__showContextMenuFormatting) |
1092 menu.aboutToShow.connect(self.__showContextMenuFormatting) |
1067 |
1093 |
1068 return menu |
1094 return menu |
1069 |
1095 |
9059 self.__showingMouseHoverHelp = True |
9085 self.__showingMouseHoverHelp = True |
9060 else: |
9086 else: |
9061 self.__cancelMouseHoverHelp() |
9087 self.__cancelMouseHoverHelp() |
9062 |
9088 |
9063 ####################################################################### |
9089 ####################################################################### |
9064 ## Methods implementing the Black code formatting interface |
9090 ## Methods implementing the code formatting interface |
9065 ####################################################################### |
9091 ####################################################################### |
9066 |
9092 |
9067 def __performFormatWithBlack(self, action): |
9093 def __performFormatWithBlack(self, action): |
9068 """ |
9094 """ |
9069 Private method to format the source code using the 'Black' tool. |
9095 Private method to format the source code using the 'Black' tool. |
9097 |
9123 |
9098 formattingDialog = BlackFormattingDialog( |
9124 formattingDialog = BlackFormattingDialog( |
9099 config, [self.fileName], project=self.project, action=action |
9125 config, [self.fileName], project=self.project, action=action |
9100 ) |
9126 ) |
9101 formattingDialog.exec() |
9127 formattingDialog.exec() |
|
9128 |
|
9129 def __performImportSortingWithIsort(self, action): |
|
9130 """ |
|
9131 Private method to sort the import statements using the 'isort' tool. |
|
9132 |
|
9133 Following actions are supported. |
|
9134 <ul> |
|
9135 <li>IsortFormattingAction.Sort - the import statement sorting is performed</li> |
|
9136 <li>IsortFormattingAction.Check - a check is performed, if import statement |
|
9137 resorting is necessary</li> |
|
9138 <li>IsortFormattingAction.Diff - a unified diff of potential import statement |
|
9139 changes is generated</li> |
|
9140 </ul> |
|
9141 |
|
9142 @param action sorting operation to be performed |
|
9143 @type IsortFormattingAction |
|
9144 """ |
|
9145 from eric7.CodeFormatting.IsortConfigurationDialog import ( |
|
9146 IsortConfigurationDialog, |
|
9147 ) |
|
9148 from eric7.CodeFormatting.IsortFormattingDialog import IsortFormattingDialog |
|
9149 |
|
9150 if not self.isModified() or self.saveFile(): |
|
9151 withProject = ( |
|
9152 self.fileName |
|
9153 and self.project.isOpen() |
|
9154 and self.project.isProjectSource(self.fileName) |
|
9155 ) |
|
9156 dlg = IsortConfigurationDialog(withProject=withProject) |
|
9157 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
9158 config = dlg.getConfiguration() |
|
9159 |
|
9160 formattingDialog = IsortFormattingDialog( |
|
9161 config, [self.fileName], project=self.project, action=action |
|
9162 ) |
|
9163 formattingDialog.exec() |