From 1bfa97091c430d201d63255ff807b16dede67e53 Mon Sep 17 00:00:00 2001 From: Salvatore Larosa Date: Tue, 11 Jun 2013 19:07:16 +0200 Subject: [PATCH] [pyqgis-console] improving to auto closing bracket --- python/console/console_editor.py | 21 +++++++++++++++++++-- python/console/console_sci.py | 10 ++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/python/console/console_editor.py b/python/console/console_editor.py index 1fb9b1b7c59..0adba46bbfe 100644 --- a/python/console/console_editor.py +++ b/python/console/console_editor.py @@ -643,12 +643,29 @@ class Editor(QsciScintilla): def keyPressEvent(self, e): if self.settings.value("pythonConsole/autoCloseBracketEditor", True, type=bool): + startLine, _, endLine, _ = self.getSelection() t = unicode(e.text()) ## Close bracket automatically if t in self.opening: i = self.opening.index(t) - self.insert(self.closing[i]) - QsciScintilla.keyPressEvent(self, e) + if self.hasSelectedText(): + self.beginUndoAction() + selText = self.selectedText() + self.removeSelectedText() + if startLine == endLine: + self.insert(self.opening[i] + selText + self.closing[i]) + return + elif startLine < endLine and self.opening[i] in ("'", '"'): + self.insert("'''" + selText + "'''") + return + else: + self.insert(self.closing[i]) + self.endUndoAction() + else: + self.insert(self.closing[i]) + QsciScintilla.keyPressEvent(self, e) + else: + QsciScintilla.keyPressEvent(self, e) def focusInEvent(self, e): pathfile = self.parent.path diff --git a/python/console/console_sci.py b/python/console/console_sci.py index 445465c0a27..070247e9481 100644 --- a/python/console/console_sci.py +++ b/python/console/console_sci.py @@ -349,7 +349,7 @@ class ShellScintilla(QsciScintilla, code.InteractiveInterpreter): #self.SendScintilla(QsciScintilla.SCI_DELETEBACK) def keyPressEvent(self, e): - startLine, _, endLine, _ = self.getSelection() + startLine, startPos, endLine, _ = self.getSelection() # handle invalid cursor position and multiline selections if not self.is_cursor_on_edition_zone() or startLine < endLine: @@ -411,7 +411,13 @@ class ShellScintilla(QsciScintilla, code.InteractiveInterpreter): ## Close bracket automatically if t in self.opening: i = self.opening.index(t) - self.insert(self.closing[i]) + if self.hasSelectedText() and startPos != 0: + selText = self.selectedText() + self.removeSelectedText() + self.insert(self.opening[i] + selText + self.closing[i]) + return + else: + self.insert(self.closing[i]) QsciScintilla.keyPressEvent(self, e) def contextMenuEvent(self, e):