Skip to content

Commit

Permalink
Get rid of the old autoCloseBracket setting
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Jan 10, 2023
1 parent eade013 commit f98266b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 135 deletions.
38 changes: 3 additions & 35 deletions python/console/console_editor.py
Expand Up @@ -471,44 +471,12 @@ def syntaxCheck(self):

def keyPressEvent(self, e):
t = e.text()
startLine, _, endLine, endPos = self.getSelection()
line, pos = self.getCursorPosition()
self.autoCloseBracket = self.settings.value("pythonConsole/autoCloseBracket", False, type=bool)
self.autoImport = self.settings.value("pythonConsole/autoInsertionImport", True, type=bool)
txt = self.text(line)[:pos]
# Close bracket automatically
if t in self.opening and self.autoCloseBracket:
self.beginUndoAction()
i = self.opening.index(t)
if self.hasSelectedText():
selText = self.selectedText()
self.removeSelectedText()
if startLine == endLine:
self.insert(self.opening[i] + selText + self.closing[i])
self.setCursorPosition(endLine, endPos + 2)
self.endUndoAction()
return
elif startLine < endLine and self.opening[i] in ("'", '"'):
self.insert("'''" + selText + "'''")
self.setCursorPosition(endLine, endPos + 3)
self.endUndoAction()
return
elif t == '(' and (re.match(r'^[ \t]*def \w+$', txt) or re.match(r'^[ \t]*class \w+$', txt)):
self.insert('):')
else:
self.insert(self.closing[i])
self.endUndoAction()
# FIXES #8392 (automatically removes the redundant char
# when autoclosing brackets option is enabled)
elif t in [')', ']', '}'] and self.autoCloseBracket:
txt = self.text(line)
try:
if txt[pos - 1] in self.opening and t == txt[pos]:
self.setCursorPosition(line, pos + 1)
self.SendScintilla(QsciScintilla.SCI_DELETEBACK)
except IndexError:
pass
elif t == ' ' and self.autoImport:

# Automatically insert "import" after "from xxx "
if t == ' ' and self.autoImport:
ptrn = r'^[ \t]*from [\w.]+$'
if re.match(ptrn, txt):
self.insert(' import')
Expand Down
103 changes: 14 additions & 89 deletions python/console/console_sci.py
Expand Up @@ -199,98 +199,23 @@ def keyPressEvent(self, e):
# update the live history
self.updateSoftHistory()

startLine, startPos, endLine, endPos = self.getSelection()

# handle invalid cursor position and multiline selections
if startLine < endLine:
# allow copying and selecting
if e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier):
if e.key() == Qt.Key_C:
# only catch and return from Ctrl-C here if there's a selection
if self.hasSelectedText():
QsciScintilla.keyPressEvent(self, e)
return
elif e.key() == Qt.Key_A:
QsciScintilla.keyPressEvent(self, e)
return
else:
return
# allow selection
if e.modifiers() & Qt.ShiftModifier:
if e.key() in (Qt.Key_Left, Qt.Key_Right, Qt.Key_Home, Qt.Key_End):
QsciScintilla.keyPressEvent(self, e)
return
# all other keystrokes get sent to the input line
self.moveCursorToEnd()

# keyboard interrupt
if e.modifiers() & (
Qt.ControlModifier | Qt.MetaModifier) and e.key() == Qt.Key_C and not self.hasSelectedText():
# keyboard interrupt
sys.stdout.fire_keyboard_interrupt = True
return

line, index = self.getCursorPosition()
cmd = self.text(line)
hasSelectedText = self.hasSelectedText()

if e.key() in (Qt.Key_Return, Qt.Key_Enter) and not self.isListActive():
self.entered()

elif e.key() in (Qt.Key_Left, Qt.Key_Home):
QsciScintilla.keyPressEvent(self, e)

elif e.key() in (Qt.Key_Backspace, Qt.Key_Delete):
QsciScintilla.keyPressEvent(self, e)
self.recolor()

elif (e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier) and e.key() == Qt.Key_V) or \
(e.modifiers() & Qt.ShiftModifier and e.key() == Qt.Key_Insert):
self.paste()
e.accept()

elif e.key() == Qt.Key_Down and not self.isListActive():
self.showPreviousCommand()

elif e.key() == Qt.Key_Up and not self.isListActive():
self.showNextCommand()

# TODO: press event for auto-completion file directory
else:
t = e.text()
self.autoCloseBracket = self.settings.value("pythonConsole/autoCloseBracket", False,
type=bool)
self.autoImport = self.settings.value("pythonConsole/autoInsertionImport", True,
type=bool)
# Close bracket automatically
if t in self.opening and self.autoCloseBracket:
i = self.opening.index(t)
if self.hasSelectedText() and startPos != 0:
selText = self.selectedText()
self.removeSelectedText()
self.insert(self.opening[i] + selText + self.closing[i])
self.setCursorPosition(endLine, endPos + 2)
return
elif t == '(' and (re.match(r'^[ \t]*def \w+$', cmd)
or re.match(r'^[ \t]*class \w+$', cmd)):
self.insert('):')
else:
self.insert(self.closing[i])
# FIXES #8392 (automatically removes the redundant char
# when autoclosing brackets option is enabled)
elif t in [')', ']', '}'] and self.autoCloseBracket:
try:
if cmd[index - 1] in self.opening and t == cmd[index]:
self.setCursorPosition(line, index + 1)
self.SendScintilla(QsciScintilla.SCI_DELETEBACK)
except IndexError:
pass
elif t == ' ' and self.autoImport:
ptrn = r'^[ \t]*from [\w.]+$'
if re.match(ptrn, cmd):
self.insert(' import')
self.setCursorPosition(line, index + 7)
QsciScintilla.keyPressEvent(self, e)

autoImport = self.settings.value("pythonConsole/autoInsertionImport", True, type=bool)
# Automatically insert "import" after "from xxx "
if e.text() == ' ' and autoImport:
line, index = self.getCursorPosition()
cmd = self.text(line)
ptrn = r'^[ \t]*from [\w.]+$'
if re.match(ptrn, cmd):
self.insert(' import')
self.setCursorPosition(line, index + 7)

QgsCodeEditorPython.keyPressEvent(self, e)
self.updatePrompt()

def populateContextMenu(self, menu):
Expand All @@ -314,7 +239,7 @@ def mousePressEvent(self, e):
self.insertFromDropPaste(stringSel)
e.accept()
else:
QsciScintilla.mousePressEvent(self, e)
QgsCodeEditorPython.mousePressEvent(self, e)

def paste(self):
"""
Expand All @@ -340,7 +265,7 @@ def dropEvent(self, e):
e.setDropAction(Qt.CopyAction)
e.accept()
else:
QsciScintilla.dropEvent(self, e)
QgsCodeEditorPython.dropEvent(self, e)

def insertFromDropPaste(self, textDP):
pasteList = textDP.splitlines()
Expand Down
2 changes: 0 additions & 2 deletions python/console/console_settings.py
Expand Up @@ -202,7 +202,6 @@ def saveSettings(self):
settings.setValue("pythonConsole/autoCompleteSource", 'fromDocAPI')

settings.setValue("pythonConsole/enableObjectInsp", self.enableObjectInspector.isChecked())
settings.setValue("pythonConsole/autoCloseBracket", self.autoCloseBracket.isChecked())
settings.setValue("pythonConsole/autoInsertionImport", self.autoInsertionImport.isChecked())

def restoreSettings(self):
Expand All @@ -226,7 +225,6 @@ def restoreSettings(self):
self.groupBoxAutoCompletion.setChecked(settings.value("pythonConsole/autoCompleteEnabled", True, type=bool))

self.enableObjectInspector.setChecked(settings.value("pythonConsole/enableObjectInsp", False, type=bool))
self.autoCloseBracket.setChecked(settings.value("pythonConsole/autoCloseBracket", False, type=bool))
self.autoInsertionImport.setChecked(settings.value("pythonConsole/autoInsertionImport", True, type=bool))

if settings.value("pythonConsole/autoCompleteSource") == 'fromDoc':
Expand Down
10 changes: 1 addition & 9 deletions python/console/console_settings.ui
Expand Up @@ -83,20 +83,13 @@
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_11">
<item row="1" column="0">
<item row="0" column="0">
<widget class="QCheckBox" name="autoInsertionImport">
<property name="text">
<string>Automatic insertion of the 'import' string on 'from xxx'</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="autoCloseBracket">
<property name="text">
<string>Automatic parentheses insertion</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -463,7 +456,6 @@
<tabstop>autoCompFromDoc</tabstop>
<tabstop>autoCompFromAPI</tabstop>
<tabstop>autoCompFromDocAPI</tabstop>
<tabstop>autoCloseBracket</tabstop>
<tabstop>autoInsertionImport</tabstop>
<tabstop>enableObjectInspector</tabstop>
<tabstop>autoSaveScript</tabstop>
Expand Down

0 comments on commit f98266b

Please sign in to comment.