annotate python/codeeditor.py @ 120:bb08633510bf

Added repostat script
author Windel Bouwman
date Sat, 12 Jan 2013 10:56:12 +0100
parents af0d7913677a
children 81e08e2e7777
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 from PyQt4.QtCore import *
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2 from PyQt4.QtGui import *
100
fe145e42259d Fixes after movage
windel
parents: 97
diff changeset
3 #import compiler.lexer
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
4 import os.path
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
5
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
6 class MySyntaxHighlighter(QSyntaxHighlighter):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
7 def __init__(self, parent=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
8 super(MySyntaxHighlighter, self).__init__(parent)
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
9 # Syntax highlighting:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10 self.rules = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11 fmt = QTextCharFormat()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
12 fmt.setForeground(Qt.darkBlue)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13 fmt.setFontWeight(QFont.Bold)
100
fe145e42259d Fixes after movage
windel
parents: 97
diff changeset
14 #for kw in compiler.lexer.keywords:
fe145e42259d Fixes after movage
windel
parents: 97
diff changeset
15 # pattern = '\\b'+kw+'\\b'
fe145e42259d Fixes after movage
windel
parents: 97
diff changeset
16 # self.rules.append( (pattern, fmt) )
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
18 # Comments:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19 fmt = QTextCharFormat()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
20 fmt.setForeground(Qt.gray)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 fmt.setFontItalic(True)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22 pattern = '\{.*\}'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
23 self.rules.append( (pattern, fmt) )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 # Procedure:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 fmt = QTextCharFormat()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27 fmt.setForeground(Qt.blue)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 fmt.setFontItalic(True)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 #pattern = '(?<=procedure )[A-Za-z]'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30 # TODO lookbehind does not work, think something else
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31 #self.rules.append( (pattern, fmt) )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 def highlightBlock(self, text):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 for pattern, fmt in self.rules:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 expression = QRegExp(pattern)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 index = expression.indexIn(text)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 while index >= 0:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 length = expression.matchedLength()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39 self.setFormat(index, length, fmt)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 index = expression.indexIn(text, index + length)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42 class LineNumberArea(QWidget):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 def __init__(self, codeedit):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
44 super(LineNumberArea, self).__init__(codeedit)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45 self.codeedit = codeedit
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
46 # TODO: display error in this: self.setToolTip('hello world')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
47 def sizeHint(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
48 return QSize(self.codeedit.lineNumberAreaWidth(), 0)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
49 def paintEvent(self, ev):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
50 self.codeedit.lineNumberAreaPaintEvent(ev)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
51
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
52 class CodeEdit(QPlainTextEdit):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
53 def __init__(self, parent=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
54 super(CodeEdit, self).__init__(parent)
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
55 # members:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
56 self.isUntitled = True
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
57 self.filename = None
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
58 self.setFont(QFont('Courier'))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
59 self.lineNumberArea = LineNumberArea(self)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
60
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
61 self.blockCountChanged.connect(self.updateLineNumberAreaWidth)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
62 self.updateRequest.connect(self.updateLineNumberArea)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
63
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
64 # Syntax highlighter:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
65 self.highlighter = MySyntaxHighlighter(self.document())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
66
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
67 def setFileName(self, filename):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
68 self.filename = filename
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
69 self.isUntitled = False
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
70 self.setWindowTitle(filename)
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
71
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
72 def setSource(self, source):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
73 self.setPlainText(source)
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
74 def getSource(self):
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
75 return self.toPlainText()
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
76 source = property(getSource, setSource)
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
77
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
78 def save(self):
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
79 self.saveFile()
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
80 def saveAs(self):
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
81 print('save as')
8
edd70006d3e4 Started with MDI functions
windel
parents: 1
diff changeset
82
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
83 def saveFile(self):
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
84 if self.isUntitled:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
85 self.saveAs()
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
86 else:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
87 source = str(self.toPlainText())
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
88 with open(self.filename, 'w') as f:
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
89 f.write(source)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
90
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
91 def highlightErrorLocation(self, row, col):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
92 tc = QTextCursor(self.document())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
93 tc.clearSelection()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
94 tc.movePosition(tc.Down, tc.MoveAnchor, row - 1)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
95 tc.movePosition(tc.Right, tc.MoveAnchor, col - 1)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
96 tc.movePosition(tc.NextCharacter, tc.KeepAnchor) # Select 1 character
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
97 selection = QTextEdit.ExtraSelection()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
98 lineColor = QColor(Qt.red).lighter(160)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
99 selection.format.setBackground(lineColor)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
100 #selection.format.setProperty(QTextFormat.FullWidthSelection, True)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
101 selection.cursor = tc
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
102 self.setExtraSelections( [ selection ] )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
103 def clearErrors(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
104 self.setExtraSelections( [ ] )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
105
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
106 def lineNumberAreaWidth(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
107 digits = 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
108 mx = max(1, self.blockCount())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
109 while mx >= 10:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
110 mx = mx / 10
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
111 digits += 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
112 space = 3 + self.fontMetrics().width('8') * digits
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
113 return space
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
114 def lineNumberAreaPaintEvent(self, ev):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
115 painter = QPainter(self.lineNumberArea)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
116 painter.fillRect(ev.rect(), Qt.lightGray)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
117 block = self.firstVisibleBlock()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
118 blockNumber = block.blockNumber()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
119 top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
120 bottom = top + self.blockBoundingRect(block).height()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
121 while block.isValid() and top <= ev.rect().bottom():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
122 if block.isVisible() and bottom >= ev.rect().top():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
123 num = str(blockNumber + 1)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
124 painter.setPen(Qt.black)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
125 painter.drawText(0, top, self.lineNumberArea.width(), self.fontMetrics().height(), Qt.AlignRight, num)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
126 block = block.next()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
127 top = bottom
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
128 bottom = top + self.blockBoundingRect(block).height()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
129 blockNumber += 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
130 def resizeEvent(self, ev):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
131 super(CodeEdit, self).resizeEvent(ev)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
132 cr = self.contentsRect()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
133 self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height() ))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
134 def updateLineNumberAreaWidth(self, newBlockCount):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
135 self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
136 def updateLineNumberArea(self, rect, dy):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
137 if dy > 0:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
138 self.lineNumberArea.scroll(0, dy)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
139 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
140 self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
141 if rect.contains(self.viewport().rect()):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
142 self.updateLineNumberAreaWidth(0)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
143