Skip to content

Commit

Permalink
[pyqgis-console] improving to auto closing bracket
Browse files Browse the repository at this point in the history
  • Loading branch information
slarosa committed Jun 11, 2013
1 parent 12fcea1 commit 1bfa970
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
21 changes: 19 additions & 2 deletions python/console/console_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,29 @@ def syntaxCheck(self, filename=None, fromContextMenu=True):

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
Expand Down
10 changes: 8 additions & 2 deletions python/console/console_sci.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def showNext(self):
#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:
Expand Down Expand Up @@ -411,7 +411,13 @@ def keyPressEvent(self, e):
## 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):
Expand Down

0 comments on commit 1bfa970

Please sign in to comment.