Removed dead code from the syntax highlighter
[ugit.git] / ugitlibs / syntax.py
blob311aaf8cdf7944296e49fc93ad596e1d17ad6a8b
1 #!/usr/bin/python
2 import re
3 from PyQt4.QtCore import Qt
4 from PyQt4.QtGui import QFont
5 from PyQt4.QtGui import QSyntaxHighlighter
6 from PyQt4.QtGui import QTextCharFormat
8 class GitSyntaxHighlighter(QSyntaxHighlighter):
10 def __init__(self, doc):
11 QSyntaxHighlighter.__init__(self, doc)
13 begin = self.__mkformat(QFont.Bold, Qt.cyan)
14 addition = self.__mkformat(QFont.Bold, Qt.green)
15 removal = self.__mkformat(QFont.Bold, Qt.red)
16 message = self.__mkformat(QFont.Bold, Qt.yellow, Qt.black)
18 # Catch trailing whitespace
19 bad_ws_format = self.__mkformat(QFont.Bold, Qt.black, Qt.red)
20 self._bad_ws_regex = re.compile('(.*?)(\s+)$')
21 self._bad_ws_format = bad_ws_format
23 self._rules =(
24 ( re.compile('^(@@|\+\+\+|---)'), begin ),
25 ( re.compile('^\+'), addition ),
26 ( re.compile('^-'), removal ),
27 ( re.compile('^:'), message ),
30 def getFormat(self, line):
31 for regex, rule in self._rules:
32 if regex.match(line):
33 return rule
34 return None
36 def highlightBlock(self, qstr):
37 ascii = qstr.toAscii().data()
38 if not ascii: return
39 fmt = self.getFormat(ascii)
40 if fmt:
41 match = self._bad_ws_regex.match(ascii)
42 if match and match.group(2):
43 start = len(match.group(1))
44 self.setFormat(0, start, fmt)
45 self.setFormat(start, len(ascii),
46 self._bad_ws_format)
47 else:
48 self.setFormat(0, len(ascii), fmt)
50 def __mkformat(self, weight, color, bgcolor=None):
51 format = QTextCharFormat()
52 format.setFontWeight(weight)
53 format.setForeground(color)
54 if bgcolor: format.setBackground(bgcolor)
55 return format
58 if __name__ == '__main__':
59 import sys
60 from PyQt4 import QtCore, QtGui
62 class SyntaxTestDialog(QtGui.QDialog):
63 def __init__(self, parent):
64 QtGui.QDialog.__init__(self, parent)
65 self.setupUi(self)
67 def setupUi(self, CommandDialog):
68 CommandDialog.resize(QtCore.QSize(QtCore.QRect(0,0,720,512).size()).expandedTo(CommandDialog.minimumSizeHint()))
70 self.vboxlayout = QtGui.QVBoxLayout(CommandDialog)
71 self.vboxlayout.setObjectName("vboxlayout")
73 self.commandText = QtGui.QTextEdit(CommandDialog)
75 font = QtGui.QFont()
76 font.setFamily("Monospace")
77 font.setPointSize(13)
78 self.commandText.setFont(font)
79 self.commandText.setAcceptDrops(False)
80 #self.commandText.setReadOnly(True)
81 self.vboxlayout.addWidget(self.commandText)
83 GitSyntaxHighlighter(self.commandText.document())
86 app = QtGui.QApplication(sys.argv)
87 dialog = SyntaxTestDialog(app.activeWindow())
88 dialog.show()
89 dialog.exec_()