source: trunk/PARPG/tools/Writing Editor/scripts/syntaxHighlight.py @ 233

Revision 233, 4.1 KB checked in by bretzel_parpg, 10 years ago (diff)

Added an example to the docs. Added a SCRIPTNAME function

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        cmdFormat = QtGui.QTextCharFormat()
33        cmdFormat.setFontWeight(QtGui.QFont.Bold)
34        cmdFormat.setForeground(QtGui.QColor("blue"))
35       
36        quoteFormat = QtGui.QTextCharFormat()
37        quoteFormat.setForeground(QtGui.QColor("green"))
38
39        singleLineCommentFormat = QtGui.QTextCharFormat()
40        singleLineCommentFormat.setForeground(QtGui.QColor("red"))
41
42        multiLineCommentFormat = QtGui.QTextCharFormat()
43        multiLineCommentFormat.setForeground(QtGui.QColor("magenta"))
44           
45        cmds = ["npc", "NPC", "pc", "PC", "section", "SECTION", "SCRIPTNAME",
46                "scriptname", "endsection", "ENDSECTION", "callsection", 
47                "CALLSECTION", "option", "OPTION", "endoption", "ENDOPTION", 
48                "playsound", "PLAYSOUND", "say", "SAY", "attack", "ATTACK", 
49                "return", "RETURN","IF", "if", "ELIF", "elif", "ELSE", "else",
50                "#", "/\*", "\""]
51
52        for cmd in cmds:
53            # if its a quote
54            if (cmd == "\""):
55                startExp = QtCore.QRegExp(cmd)
56                startIndex = text.indexOf(startExp)
57                if (self.format(startIndex) == singleLineCommentFormat or
58                    self.format(startIndex) == multiLineCommentFormat):
59                    return
60                else:
61                    endIndex = text.indexOf(startExp, startIndex+1)
62                    quoteLength = endIndex - startIndex + 1
63                    self.setFormat(startIndex, quoteLength, quoteFormat)
64               
65            # if its a singeline comment
66            elif (cmd == "#"):
67                startExp = QtCore.QRegExp(cmd)
68                startIndex = text.indexOf(startExp)
69                self.setFormat(startIndex, text.length(), singleLineCommentFormat)               
70
71            # if its a multiline comment
72            elif (cmd == "/\*"):
73                startExp = QtCore.QRegExp(cmd)
74                endExp = QtCore.QRegExp("\*/")
75                self.setCurrentBlockState(0)
76               
77                startIndex = 0
78                if (self.previousBlockState() != 1):
79                    startIndex = text.indexOf(startExp)
80
81                while (startIndex >= 0):
82                    endIndex = text.indexOf(endExp, startIndex)
83                    if (endIndex == -1):
84                        self.setCurrentBlockState(1)
85                        commentLength = text.length() - startIndex
86                    else:
87                        commentLength = endIndex - startIndex + endExp.matchedLength()
88
89                    self.setFormat(startIndex, commentLength, multiLineCommentFormat)
90                    startIndex = text.indexOf(startExp, startIndex + commentLength)
91
92            # otherwise its just a command
93            else:       
94                expression = QtCore.QRegExp(cmd)
95                index = int(text.indexOf(expression))
96                while (index >= 0):
97                    length = int(expression.matchedLength())
98                    self.setFormat(index, length, cmdFormat)
99                    index = text.indexOf(expression, index + length)
Note: See TracBrowser for help on using the repository browser.