annotate python/codeeditor.py @ 171:3eb9b9e2958d

Improved IR code
author Windel Bouwman
date Wed, 03 Apr 2013 22:20:20 +0200
parents d8c735dc31f9
children
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)
154
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
60 h = QFontMetrics(self.font()).height()
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
61 print(h)
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
62 self.errorPixmap = QPixmap('error.png').scaled(h, h)
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
63 self.errorList = []
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
64
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
65 self.blockCountChanged.connect(self.updateLineNumberAreaWidth)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
66 self.updateRequest.connect(self.updateLineNumberArea)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
67
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
68 # Syntax highlighter:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
69 self.highlighter = MySyntaxHighlighter(self.document())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
70
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
71 def setFileName(self, filename):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
72 self.filename = filename
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
73 self.isUntitled = False
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
74 self.setWindowTitle(filename)
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
75
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
76 def setSource(self, source):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
77 self.setPlainText(source)
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
78 def getSource(self):
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
79 return self.toPlainText()
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 154
diff changeset
80 Source = property(getSource, setSource)
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
81
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
82 def save(self):
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
83 self.saveFile()
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
84 def saveAs(self):
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
85 print('save as')
8
edd70006d3e4 Started with MDI functions
windel
parents: 1
diff changeset
86
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
87 def saveFile(self):
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
88 if self.isUntitled:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
89 self.saveAs()
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
90 else:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
91 source = str(self.toPlainText())
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
92 with open(self.filename, 'w') as f:
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
93 f.write(source)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
94
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
95 def highlightErrorLocation(self, row, col):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
96 tc = QTextCursor(self.document())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
97 tc.clearSelection()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
98 tc.movePosition(tc.Down, tc.MoveAnchor, row - 1)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
99 tc.movePosition(tc.Right, tc.MoveAnchor, col - 1)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
100 tc.movePosition(tc.NextCharacter, tc.KeepAnchor) # Select 1 character
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
101 selection = QTextEdit.ExtraSelection()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
102 lineColor = QColor(Qt.red).lighter(160)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
103 selection.format.setBackground(lineColor)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
104 #selection.format.setProperty(QTextFormat.FullWidthSelection, True)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
105 selection.cursor = tc
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
106 self.setExtraSelections( [ selection ] )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
107 def clearErrors(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
108 self.setExtraSelections( [ ] )
154
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
109 def setErrors(self, el):
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
110 self.errorList = el
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
111 self.lineNumberArea.update()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
112
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
113 def lineNumberAreaWidth(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
114 digits = 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
115 mx = max(1, self.blockCount())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
116 while mx >= 10:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
117 mx = mx / 10
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
118 digits += 1
154
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
119 space = 3 + self.fontMetrics().width('8') * digits + self.errorPixmap.width() + 2
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
120 return space
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
121 def lineNumberAreaPaintEvent(self, ev):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
122 painter = QPainter(self.lineNumberArea)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
123 painter.fillRect(ev.rect(), Qt.lightGray)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
124 block = self.firstVisibleBlock()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
125 blockNumber = block.blockNumber()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
126 top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
127 bottom = top + self.blockBoundingRect(block).height()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
128 while block.isValid() and top <= ev.rect().bottom():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
129 if block.isVisible() and bottom >= ev.rect().top():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
130 num = str(blockNumber + 1)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
131 painter.setPen(Qt.black)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
132 painter.drawText(0, top, self.lineNumberArea.width(), self.fontMetrics().height(), Qt.AlignRight, num)
154
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
133 for e in self.errorList:
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
134 if e.loc.row == blockNumber + 1:
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
135 painter.drawPixmap(0, top, self.errorPixmap)
81e08e2e7777 Used error icon
Windel Bouwman
parents: 101
diff changeset
136
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
137 block = block.next()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
138 top = bottom
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
139 bottom = top + self.blockBoundingRect(block).height()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
140 blockNumber += 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
141 def resizeEvent(self, ev):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
142 super(CodeEdit, self).resizeEvent(ev)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
143 cr = self.contentsRect()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
144 self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height() ))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
145 def updateLineNumberAreaWidth(self, newBlockCount):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
146 self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
147 def updateLineNumberArea(self, rect, dy):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
148 if dy > 0:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
149 self.lineNumberArea.scroll(0, dy)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
150 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
151 self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
152 if rect.contains(self.viewport().rect()):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
153 self.updateLineNumberAreaWidth(0)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
154