source: trunk/tools/writing_editor/scripts/syntaxHighlight.py @ 282

Revision 282, 3.4 KB checked in by bretzel_parpg, 10 years ago (diff)

Patch by Bretzel

  • Update the syntax highlighting to match the yaml syntax
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18from PyQt4 import QtCore, QtGui
19
20class SyntaxHighlighter(QtGui.QSyntaxHighlighter):
21    """
22    A class to highlight the syntax keywords
23    """
24
25    def highlightBlock(self, text):
26        """
27        Highlight the syntax in the text of self.widget
28        @type text: QtCore.QString
29        @param text: the text to highlight syntax in
30        @return: None
31        """
32        boldCmdFormat = QtGui.QTextCharFormat()
33        boldCmdFormat.setFontWeight(QtGui.QFont.Bold)
34        boldCmdFormat.setForeground(QtGui.QColor("blue"))
35
36        cmdFormat = QtGui.QTextCharFormat()
37        cmdFormat.setForeground(QtGui.QColor("blue"))
38       
39        quoteFormat = QtGui.QTextCharFormat()
40        quoteFormat.setForeground(QtGui.QColor("green"))
41
42        commentFormat = QtGui.QTextCharFormat()
43        commentFormat.setForeground(QtGui.QColor("red"))
44           
45        bold_cmds = ["NPC", "AVATAR", "START", "SECTIONS"]
46        cmds = ["say", "responses", "pc", "dialogue", "end", "back", "\"", "'"]
47        comments = ["#"]
48       
49        all_cmds = cmds + bold_cmds + comments
50        for cmd in all_cmds:
51            # if its a comment
52            if (cmd == "#"):
53                startExp = QtCore.QRegExp(cmd)
54                startIndex = text.indexOf(startExp)
55                self.setFormat(startIndex, text.length(), commentFormat)
56
57            # if its a quote
58            elif (cmd == "\"" or cmd == "'"):
59                startExp = QtCore.QRegExp(cmd)
60                startIndex = text.indexOf(startExp)
61                if (self.format(startIndex) == commentFormat):
62                    return
63                else:
64                    endIndex = text.indexOf(startExp, startIndex+1)
65                    quoteLength = endIndex - startIndex + 1
66                    self.setFormat(startIndex, quoteLength, quoteFormat)
67
68            # if its just a regular command
69            elif cmd in cmds:
70                expression = QtCore.QRegExp(cmd)
71                index = int(text.indexOf(expression))
72                while (index >= 0):
73                    length = int(expression.matchedLength())
74                    self.setFormat(index, length, cmdFormat)
75                    index = text.indexOf(expression, index + length)               
76
77            # if its a command that needs to be bold
78            elif cmd in bold_cmds:       
79                expression = QtCore.QRegExp(cmd)
80                index = int(text.indexOf(expression))
81                while (index >= 0):
82                    length = int(expression.matchedLength())
83                    self.setFormat(index, length, boldCmdFormat)
84                    index = text.indexOf(expression, index + length)
85
86            else:
87                raise Exception("SyntaxHighlighter: Cannot find command %s" % cmd)
Note: See TracBrowser for help on using the repository browser.