src/eric7/QScintilla/Editor.py

branch
eric7-maintenance
changeset 9654
7328efba128b
parent 9549
67295777d9fe
parent 9653
e67609152c5e
child 9725
b9a29a7aa820
--- a/src/eric7/QScintilla/Editor.py	Thu Dec 01 10:18:07 2022 +0100
+++ b/src/eric7/QScintilla/Editor.py	Mon Jan 02 11:16:03 2023 +0100
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-# Copyright (c) 2002 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
+# Copyright (c) 2002 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>
 #
 
 """
@@ -40,7 +40,7 @@
 )
 from PyQt6.QtWidgets import QApplication, QDialog, QInputDialog, QLineEdit, QMenu
 
-from eric7 import Preferences, Utilities
+from eric7 import Globals, Preferences, Utilities
 from eric7.CodeFormatting.BlackFormattingAction import BlackFormattingAction
 from eric7.CodeFormatting.BlackUtilities import aboutBlack
 from eric7.CodeFormatting.IsortFormattingAction import IsortFormattingAction
@@ -51,6 +51,7 @@
 from eric7.EricWidgets import EricFileDialog, EricMessageBox
 from eric7.EricWidgets.EricApplication import ericApp
 from eric7.Globals import recentNameBreakpointConditions
+from eric7.SystemUtilities import OSUtilities, PythonUtilities
 from eric7.UI import PythonDisViewer
 from eric7.Utilities import MouseUtilities
 
@@ -401,14 +402,6 @@
         if editor is None:
             if self.fileName:
                 if not Utilities.MimeTypes.isTextFile(self.fileName):
-                    EricMessageBox.warning(
-                        None,
-                        self.tr("Open File"),
-                        self.tr(
-                            "<p>The file <b>{0}</b> is not a text file. It will not be"
-                            " opened!</p>"
-                        ).format(self.fileName),
-                    )
                     raise OSError()
 
                 fileSizeKB = pathlib.Path(self.fileName).stat().st_size // 1024
@@ -560,6 +553,8 @@
 
         self.__initContextMenu()
         self.__initContextMenuMargins()
+        self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
+        self.customContextMenuRequested.connect(self.__showContextMenu)
 
         self.__checkEol()
         if editor is None:
@@ -989,7 +984,7 @@
             self.__contextSaveCopy,
         )
 
-        self.menu.aboutToShow.connect(self.__showContextMenu)
+        self.menu.aboutToShow.connect(self.__aboutToShowContextMenu)
 
         self.spellingMenu = QMenu()
         self.__menus["Spelling"] = self.spellingMenu
@@ -1866,7 +1861,9 @@
                     language = Preferences.getEditorLexerAssoc(bindName)
             if language == "Python":
                 # correction for Python
-                pyVer = Utilities.determinePythonVersion(filename, self.text(0), self)
+                pyVer = PythonUtilities.determinePythonVersion(
+                    filename, self.text(0), self
+                )
                 language = "Python{0}".format(pyVer)
             if language in [
                 "Python3",
@@ -1900,16 +1897,13 @@
             else:
                 self.apiLanguage = "Pygments|{0}".format(pyname)
         else:
-            if language == "Protocol":
-                self.apiLanguage = language
-            else:
-                # Change API language for lexer where QScintilla reports
-                # an abbreviated name.
-                self.apiLanguage = self.lexer_.language()
-                if self.apiLanguage == "POV":
-                    self.apiLanguage = "Povray"
-                elif self.apiLanguage == "PO":
-                    self.apiLanguage = "Gettext"
+            # Change API language for lexer where QScintilla reports
+            # an abbreviated name.
+            self.apiLanguage = self.lexer_.language()
+            if self.apiLanguage == "POV":
+                self.apiLanguage = "Povray"
+            elif self.apiLanguage == "PO":
+                self.apiLanguage = "Gettext"
         self.setLexer(self.lexer_)
         self.__setMarginsDisplay()
         if self.lexer_.lexer() == "container" or self.lexer_.lexer() is None:
@@ -1981,21 +1975,17 @@
         """
         if self.apiLanguage == "Guessed" or self.apiLanguage.startswith("Pygments|"):
             lang = self.lexer_.name()
-            if normalized:
+            if normalized:  # __IGNORE_WARNING_Y102__
                 # adjust some Pygments lexer names
                 if lang in ("Python 2.x", "Python"):
                     lang = "Python3"
-                elif lang == "Protocol Buffer":
-                    lang = "Protocol"
 
         else:
             lang = self.apiLanguage
-            if forPygments:
+            if forPygments:  # __IGNORE_WARNING_Y102__
                 # adjust some names to Pygments lexer names
                 if lang == "Python3":
                     lang = "Python"
-                elif lang == "Protocol":
-                    lang = "Protocol Buffer"
         return lang
 
     def getApiLanguage(self):
@@ -2179,7 +2169,7 @@
 
         @return Python version or 0 if it's not a Python file (int)
         """
-        return Utilities.determinePythonVersion(self.fileName, self.text(0), self)
+        return PythonUtilities.determinePythonVersion(self.fileName, self.text(0), self)
 
     def isPyFile(self):
         """
@@ -3409,7 +3399,7 @@
                 self,
                 self.tr("Save File"),
                 self.tr(
-                    "<p>The file <b>{0}</b> could not be saved.<br/>" "Reason: {1}</p>"
+                    "<p>The file <b>{0}</b> could not be saved.<br/>Reason: {1}</p>"
                 ).format(fn, str(why)),
             )
             return False
@@ -3431,7 +3421,7 @@
         if not path and self.fileName:
             path = os.path.dirname(self.fileName)
         if not path:
-            path = Preferences.getMultiProject("Workspace") or Utilities.getHomeDir()
+            path = Preferences.getMultiProject("Workspace") or OSUtilities.getHomeDir()
 
         if self.fileName:
             filterPattern = "(*{0})".format(os.path.splitext(self.fileName)[1])
@@ -3466,7 +3456,7 @@
                     self,
                     self.tr("Save File"),
                     self.tr(
-                        "<p>The file <b>{0}</b> already exists." " Overwrite it?</p>"
+                        "<p>The file <b>{0}</b> already exists. Overwrite it?</p>"
                     ).format(fpath),
                     icon=EricMessageBox.Warning,
                 )
@@ -5605,35 +5595,36 @@
                 return margin
         return -1
 
-    def contextMenuEvent(self, evt):
-        """
-        Protected method implementing the context menu event.
-
-        @param evt the context menu event (QContextMenuEvent)
-        """
-        evt.accept()
-        if self.__marginNumber(evt.x()) == -1:
-            self.spellingMenuPos = self.positionFromPoint(evt.pos())
+    @pyqtSlot(QPoint)
+    def __showContextMenu(self, pos):
+        """
+        Private slot to show a context menu.
+
+        @param pos position for the context menu
+        @type QPoint
+        """
+        if self.__marginNumber(pos.x()) == -1:
+            self.spellingMenuPos = self.positionFromPoint(pos)
             if (
                 self.spellingMenuPos >= 0
                 and self.spell is not None
                 and self.hasIndicator(self.spellingIndicator, self.spellingMenuPos)
             ):
-                self.spellingMenu.popup(evt.globalPos())
+                self.spellingMenu.popup(self.mapToGlobal(pos))
             else:
-                self.menu.popup(evt.globalPos())
-        else:
-            self.line = self.lineAt(evt.pos())
-            if self.__marginNumber(evt.x()) in [self.__bmMargin, self.__linenoMargin]:
-                self.bmMarginMenu.popup(evt.globalPos())
-            elif self.__marginNumber(evt.x()) == self.__bpMargin:
-                self.bpMarginMenu.popup(evt.globalPos())
-            elif self.__marginNumber(evt.x()) == self.__indicMargin:
-                self.indicMarginMenu.popup(evt.globalPos())
-            elif self.__marginNumber(evt.x()) == self.__foldMargin:
-                self.foldMarginMenu.popup(evt.globalPos())
-
-    def __showContextMenu(self):
+                self.menu.popup(self.mapToGlobal(pos))
+        else:
+            self.line = self.lineAt(pos)
+            if self.__marginNumber(pos.x()) in [self.__bmMargin, self.__linenoMargin]:
+                self.bmMarginMenu.popup(self.mapToGlobal(pos))
+            elif self.__marginNumber(pos.x()) == self.__bpMargin:
+                self.bpMarginMenu.popup(self.mapToGlobal(pos))
+            elif self.__marginNumber(pos.x()) == self.__indicMargin:
+                self.indicMarginMenu.popup(self.mapToGlobal(pos))
+            elif self.__marginNumber(pos.x()) == self.__foldMargin:
+                self.foldMarginMenu.popup(self.mapToGlobal(pos))
+
+    def __aboutToShowContextMenu(self):
         """
         Private slot handling the aboutToShow signal of the context menu.
         """
@@ -6926,7 +6917,7 @@
         """
         Public method to load a macro from a file.
         """
-        configDir = Utilities.getConfigDir()
+        configDir = Globals.getConfigDir()
         fname = EricFileDialog.getOpenFileName(
             self,
             self.tr("Load macro file"),
@@ -6965,7 +6956,7 @@
         """
         Public method to save a macro to a file.
         """
-        configDir = Utilities.getConfigDir()
+        configDir = Globals.getConfigDir()
 
         name, ok = self.__getMacroName()
         if not ok or not name:
@@ -6993,7 +6984,7 @@
                 self,
                 self.tr("Save macro"),
                 self.tr(
-                    "<p>The macro file <b>{0}</b> already exists." " Overwrite it?</p>"
+                    "<p>The macro file <b>{0}</b> already exists. Overwrite it?</p>"
                 ).format(fpath),
                 icon=EricMessageBox.Warning,
             )
@@ -7850,7 +7841,7 @@
             cmd in (QsciScintilla.SCI_LOWERCASE, QsciScintilla.SCI_UPPERCASE)
             and self.hasSelectedText()
         ):
-            startLine, startIndex, endLine, _ = self.getSelection()
+            startLine, startIndex, endLine, endIndex = self.getSelection()
             selectedText = self.selectedText()
             replacementText = (
                 selectedText.upper()
@@ -7858,11 +7849,6 @@
                 else selectedText.lower()
             )
             self.replaceSelectedText(replacementText)
-            endIndex = (
-                startIndex + len(replacementText)
-                if len(replacementText.splitlines()) <= 1
-                else len(replacementText.splitlines()[-1])
-            )
             self.setSelection(startLine, startIndex, endLine, endIndex)
             return
 
@@ -8863,9 +8849,9 @@
             elif option == "DefaultEncoding":
                 value = config["charset"]
             elif option == "InsertFinalNewline":
-                value = Utilities.toBool(config["insert_final_newline"])
+                value = Globals.toBool(config["insert_final_newline"])
             elif option == "StripTrailingWhitespace":
-                value = Utilities.toBool(config["trim_trailing_whitespace"])
+                value = Globals.toBool(config["trim_trailing_whitespace"])
             elif option == "TabWidth":
                 value = int(config["tab_width"])
             elif option == "IndentWidth":

eric ide

mercurial