Skip to content

Commit 82b9f00

Browse files
author
wonder
committedFeb 10, 2010
Python console: basic syntax highlighting (prompt, errors)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12920 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed
 

‎python/console.py

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
1919
TODO:
2020
- configuration - init commands, font, ...
21-
- syntax highlighting
21+
- python code highlighting
2222
2323
"""
2424

@@ -92,6 +92,29 @@ def closeEvent(self, event):
9292
QWidget.closeEvent(self, event)
9393

9494

95+
class ConsoleHighlighter(QSyntaxHighlighter):
96+
EDIT_LINE, ERROR, OUTPUT, INIT = range(4)
97+
def __init__(self, doc):
98+
QSyntaxHighlighter.__init__(self,doc)
99+
formats = { self.OUTPUT : Qt.black,
100+
self.ERROR : Qt.red,
101+
self.EDIT_LINE : Qt.darkGreen,
102+
self.INIT : Qt.gray }
103+
self.f = {}
104+
for tag, color in formats.iteritems():
105+
self.f[tag] = QTextCharFormat()
106+
self.f[tag].setForeground(color)
107+
108+
def highlightBlock(self, txt):
109+
size = txt.length()
110+
state = self.currentBlockState()
111+
if state == self.OUTPUT or state == self.ERROR or state == self.INIT:
112+
self.setFormat(0,size, self.f[state])
113+
# highlight prompt only
114+
if state == self.EDIT_LINE:
115+
self.setFormat(0,3, self.f[self.EDIT_LINE])
116+
117+
95118
class PythonEdit(QTextEdit, code.InteractiveInterpreter):
96119

97120
def __init__(self,parent=None):
@@ -108,9 +131,9 @@ def __init__(self,parent=None):
108131

109132
self.buffer = []
110133

111-
self.insertPlainText("To access Quantum GIS environment from this console\n"
112-
"use qgis.utils.iface object (instance of QgisInterface class).\n"
113-
"\n")
134+
self.insertTaggedText("To access Quantum GIS environment from this console\n"
135+
"use qgis.utils.iface object (instance of QgisInterface class).\n"
136+
"\n", ConsoleHighlighter.INIT)
114137

115138
for line in _init_commands:
116139
self.runsource(line)
@@ -120,13 +143,12 @@ def __init__(self,parent=None):
120143
self.history = QStringList()
121144
self.historyIndex = 0
122145

123-
#from pythonhigh import PythonHighlighter
124-
#self.high = PythonHighlighter(self)
146+
self.high = ConsoleHighlighter(self)
125147

126148
def displayPrompt(self, more=False):
127149
self.currentPrompt = "... " if more else ">>> "
128150
self.currentPromptLength = len(self.currentPrompt)
129-
self.insertPlainText(self.currentPrompt)
151+
self.insertTaggedLine(self.currentPrompt, ConsoleHighlighter.EDIT_LINE)
130152
self.moveCursor(QTextCursor.End, QTextCursor.MoveAnchor)
131153

132154
def isCursorInEditionZone(self):
@@ -240,6 +262,24 @@ def entered(self):
240262
self.setTextCursor(self.cursor)
241263
self.runCommand( unicode(self.currentCommand()) )
242264

265+
def insertTaggedText(self, txt, tag):
266+
267+
if len(txt) > 0 and txt[-1] == '\n': # remove trailing newline to avoid one more empty line
268+
txt = txt[0:-1]
269+
270+
c = self.textCursor()
271+
for line in txt.split('\n'):
272+
b = c.block()
273+
b.setUserState(tag)
274+
c.insertText(line)
275+
c.insertBlock()
276+
277+
def insertTaggedLine(self, txt, tag):
278+
c = self.textCursor()
279+
b = c.block()
280+
b.setUserState(tag)
281+
c.insertText(txt)
282+
243283
def runCommand(self, cmd):
244284

245285
self.updateHistory(cmd)
@@ -254,13 +294,12 @@ def runCommand(self, cmd):
254294

255295
output = sys.stdout.get_and_clean_data()
256296
if output:
257-
self.insertPlainText(output)
297+
self.insertTaggedText(output, ConsoleHighlighter.OUTPUT)
258298
self.displayPrompt(more)
259299

260300
def write(self, txt):
261301
""" reimplementation from code.InteractiveInterpreter """
262-
self.insertPlainText(txt)
263-
302+
self.insertTaggedText(txt, ConsoleHighlighter.ERROR)
264303

265304
if __name__ == '__main__':
266305
a = QApplication(sys.argv)

0 commit comments

Comments
 (0)
Please sign in to comment.