eric6/QScintilla/Editor.py

changeset 7969
62eff8b34a8d
parent 7960
e8fc383322f7
child 7998
cd41c844862f
--- a/eric6/QScintilla/Editor.py	Mon Jan 11 19:31:21 2021 +0100
+++ b/eric6/QScintilla/Editor.py	Tue Jan 12 17:19:02 2021 +0100
@@ -157,6 +157,19 @@
         r"""^>>>>>>> .*?$""",
     )
     
+    EncloseChars = {
+        '"': '"',
+        "'": "'",
+        "(": "()",
+        ")": "()",
+        "{": "{}",          # __IGNORE_WARNING_M613__
+        "}": "{}",          # __IGNORE_WARNING_M613__
+        "[": "[]",
+        "]": "[]",
+        "<": "<>",
+        ">": "<>",
+    }
+    
     def __init__(self, dbs, fn="", vm=None,
                  filetype="", editor=None, tv=None):
         """
@@ -6938,19 +6951,37 @@
         @param ev key event
         @type QKeyEvent
         """
+        def encloseSelectedText(encString):
+            """
+            Local function to enclose the current selection with some
+            characters.
+            
+            @param encString string to use to enclose the selection
+                (one or two characters)
+            @type str
+            """
+            startChar = encString[0]
+            if len(encString) == 2:
+                endChar = encString[1]
+            else:
+                endChar = startChar
+            
+            sline, sindex, eline, eindex = self.getSelection()
+            replaceText = startChar + self.selectedText() + endChar
+            self.beginUndoAction()
+            self.replaceSelectedText(replaceText)
+            self.endUndoAction()
+            self.setSelection(sline, sindex + 1, eline, eindex + 1)
+        
         txt = ev.text()
         
         # See it is text to insert.
         if len(txt) and txt >= " ":
-            if txt in ('"', "'") and self.hasSelectedText():
-                sline, sindex, eline, eindex = self.getSelection()
-                replaceText = txt + self.selectedText() + txt
-                self.beginUndoAction()
-                self.replaceSelectedText(replaceText)
-                self.endUndoAction()
-                self.setSelection(sline, sindex + 1, eline, eindex + 1)
-                ev.accept()
-                return
+            if self.hasSelectedText():
+                if txt in Editor.EncloseChars:
+                    encloseSelectedText(Editor.EncloseChars[txt])
+                    ev.accept()
+                    return
             
             super(Editor, self).keyPressEvent(ev)
         else:

eric ide

mercurial