Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[pyqgis-console] added some typing setting:
- auto insertion "import" string
- auto insertion "):" for classes and methods
  • Loading branch information
slarosa committed Sep 15, 2013
1 parent cb1090f commit dc1824f
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 236 deletions.
1 change: 1 addition & 0 deletions python/console/console.py
Expand Up @@ -684,6 +684,7 @@ def openHelp(self):
QgsContextHelp.run( "PythonConsole" )

def openSettings(self):
self.options.restoreSettings()
self.options.exec_()

def prefChanged(self):
Expand Down
85 changes: 46 additions & 39 deletions python/console/console_editor.py
Expand Up @@ -36,6 +36,7 @@
from operator import itemgetter
import traceback
import codecs
import re

class KeyFilter(QObject):
SHORTCUTS = {
Expand Down Expand Up @@ -654,45 +655,51 @@ def syntaxCheck(self, filename=None, fromContextMenu=True):
return True

def keyPressEvent(self, e):
if self.settings.value("pythonConsole/autoCloseBracketEditor", False, type=bool):
startLine, _, endLine, endPos = self.getSelection()
t = unicode(e.text())
## Close bracket automatically
if t in self.opening:
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
else:
self.insert(self.closing[i])
else:
self.insert(self.closing[i])
self.endUndoAction()
## FIXES #8392 (automatically removes the redundant char
## when autoclosing brackets option is enabled)
if t in [')', ']', '}']:
l, pos = self.getCursorPosition()
txt = self.text(l)
try:
if txt[pos-1] in self.opening:
self.setCursorPosition(l, pos+1)
self.SendScintilla(QsciScintilla.SCI_DELETEBACK)
except IndexError:
pass
QsciScintilla.keyPressEvent(self, e)
else:
QsciScintilla.keyPressEvent(self, e)
t = unicode(e.text())
startLine, _, endLine, endPos = self.getSelection()
line, pos = self.getCursorPosition()
self.autoCloseBracket = self.settings.value("pythonConsole/autoCloseBracketEditor", False, type=bool)
self.autoImport = self.settings.value("pythonConsole/autoInsertionImportEditor", 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:
ptrn = r'^[ \t]*from [\w.]+$'
if re.match(ptrn, txt):
self.insert(' import')
self.setCursorPosition(line, pos + 7)
QsciScintilla.keyPressEvent(self, e)

def focusInEvent(self, e):
pathfile = self.parent.path
Expand Down
58 changes: 34 additions & 24 deletions python/console/console_sci.py
Expand Up @@ -30,6 +30,7 @@
import os
import code
import codecs
import re

from qgis.core import QgsApplication
from ui_console_history_dlg import Ui_HistoryDialogPythonConsole
Expand Down Expand Up @@ -414,30 +415,39 @@ def keyPressEvent(self, e):
self.showNext()
## TODO: press event for auto-completion file directory
else:
if self.settings.value("pythonConsole/autoCloseBracket", False, type=bool):
t = unicode(e.text())
## Close bracket automatically
if t in self.opening:
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
else:
self.insert(self.closing[i])
## FIXES #8392 (automatically removes the redundant char
## when autoclosing brackets option is enabled)
if t in [')', ']', '}']:
l, pos = self.getCursorPosition()
txt = self.text(l)
try:
if txt[pos-1] in self.opening:
self.setCursorPosition(l, pos+1)
self.SendScintilla(QsciScintilla.SCI_DELETEBACK)
except IndexError:
pass
t = unicode(e.text())
self.autoCloseBracket = self.settings.value("pythonConsole/autoCloseBracket", False, type=bool)
self.autoImport = self.settings.value("pythonConsole/autoInsertionImport", True, type=bool)
txt = cmd[:index].replace('>>> ', '').replace('... ', '')
## 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+$', txt) \
or re.match(r'^[ \t]*class \w+$', txt)):
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:
txt = self.text(line)
try:
if txt[index-1] in self.opening and t == txt[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, txt):
self.insert(' import')
self.setCursorPosition(line, index + 7)
QsciScintilla.keyPressEvent(self, e)

def contextMenuEvent(self, e):
Expand Down
6 changes: 5 additions & 1 deletion python/console/console_settings.py
Expand Up @@ -35,7 +35,7 @@ def __init__(self, parent):
self.listPath = []
self.lineEdit.setReadOnly(True)

self.restoreSettings()
#self.restoreSettings()
self.initialCheck()

self.addAPIpath.setIcon(QIcon(":/images/themes/default/symbologyAdd.png"))
Expand Down Expand Up @@ -180,6 +180,8 @@ def saveSettings(self):
settings.setValue("pythonConsole/enableObjectInsp", self.enableObjectInspector.isChecked())
settings.setValue("pythonConsole/autoCloseBracket", self.autoCloseBracket.isChecked())
settings.setValue("pythonConsole/autoCloseBracketEditor", self.autoCloseBracketEditor.isChecked())
settings.setValue("pythonConsole/autoInsertionImport", self.autoInsertionImport.isChecked())
settings.setValue("pythonConsole/autoInsertionImportEditor", self.autoInsertionImportEditor.isChecked())

settings.setValue("pythonConsole/defaultFontColor", self.defaultFontColor.color())
settings.setValue("pythonConsole/keywordFontColor", self.keywordFontColor.color())
Expand Down Expand Up @@ -223,6 +225,8 @@ def restoreSettings(self):
self.enableObjectInspector.setChecked(settings.value("pythonConsole/enableObjectInsp", False, type=bool))
self.autoCloseBracketEditor.setChecked(settings.value("pythonConsole/autoCloseBracketEditor", False, type=bool))
self.autoCloseBracket.setChecked(settings.value("pythonConsole/autoCloseBracket", False, type=bool))
self.autoInsertionImportEditor.setChecked(settings.value("pythonConsole/autoInsertionImportEditor", True, type=bool))
self.autoInsertionImport.setChecked(settings.value("pythonConsole/autoInsertionImport", True, type=bool))

if settings.value("pythonConsole/autoCompleteSource") == 'fromDoc':
self.autoCompFromDoc.setChecked(True)
Expand Down

0 comments on commit dc1824f

Please sign in to comment.