1fec987e pyros2097

13 years ago
lexer working
Files changed (7) hide show
  1. Widget/editor.py +4 -1
  2. Widget/editor.pyc +0 -0
  3. Widget/keylex.py +0 -287
  4. Widget/lexersquirrel.py +78 -34
  5. Widget/style.py +12 -1
  6. config.yaml +1 -3
  7. window.py +11 -32
Widget/editor.py CHANGED
@@ -45,7 +45,7 @@ class Editor(QsciScintilla):
45
45
  self.setFont(self.font)
46
46
  self.fontmetrics = QFontMetrics(self.font)
47
47
  self.setMarginsFont(self.font)
48
- self.setMarginWidth(0, self.fontmetrics.width("000") + 6)
48
+ self.setMarginWidth(0, self.fontmetrics.width("0000") + 6)
49
49
  # Margin 0 is used for line numbers
50
50
  self.setMarginLineNumbers(0, True)
51
51
  self.setCaretLineVisible(True)
@@ -68,6 +68,9 @@ class Editor(QsciScintilla):
68
68
  self.setCaretLineBackgroundColor(self.colorStyle.caret)
69
69
  self.setMarginsBackgroundColor(self.colorStyle.margin)
70
70
  self.setMarkerBackgroundColor(self.colorStyle.marker,self.ARROW_MARKER_NUM)
71
+ if self.lang == 2:
72
+ self.lexer.setColorStyle(self.colorStyle)
73
+
71
74
 
72
75
  def on_margin_clicked(self, nmargin, nline, modifiers):
73
76
  # Toggle marker for the line the margin was clicked on
Widget/editor.pyc CHANGED
Binary file
Widget/keylex.py DELETED
@@ -1,287 +0,0 @@
1
- import sys
2
- from PyQt4.QtCore import SIGNAL, SLOT, QString,QStringList
3
- from PyQt4.QtGui import QApplication, QMainWindow, QColor, QFont
4
- from PyQt4.Qsci import QsciScintilla, QsciLexerCustom, QsciStyle
5
-
6
- _sample = """
7
- This example shows how to highlight some specific lines or words.
8
-
9
- + A first level title bold and red
10
- + A secund level title bold and blue with a yellow background
11
- Some text with green in green but also bold and underlined.
12
- The digits are gray with an orange backround. You don't believe it, look at that : 1 , 2 , ... , 123456789...
13
-
14
- It's very uggly but it shows how to do more pretty "highlighters".
15
-
16
- /*
17
- * credit scene
18
- */
19
- class CreditScene {
20
- foreground = null;
21
- okButton = null;
22
- layer = null;
23
-
24
- fadingOut = false;
25
-
26
- function onLoad() {
27
- stage.bgcolor(0, 0, 0, 1);
28
-
29
- local stageCenterX = stage.getWindowWidth() * 0.5;
30
- local stageCenterY = stage.getWindowHeight() * 0.5;
31
-
32
- local bgWidth = 480;
33
- local bgHeight = 320;
34
-
35
- if (useHD) {
36
- bgWidth = 960;
37
- bgHeight = 640;
38
- }
39
-
40
- if (background != null) {
41
- background.remove();
42
- }
43
-
44
- background = emo.Sprite(getHdImageName("credit_background.png"));
45
- background.moveCenter(stageCenterX, stageCenterY);
46
- background.setZ(0);
47
- background.load();
48
-
49
- layer = emo.Rectangle();
50
- layer.setSize(stage.getWindowWidth(), stage.getWindowHeight());
51
- layer.color(0.5, 0.5, 0.5, 0.78);
52
- layer.setZ(1);
53
- layer.load();
54
-
55
- foreground = emo.SpriteSheet(getHdImageName("credit.png"), bgWidth, bgHeight);
56
- foreground.moveCenter(stageCenterX, stageCenterY);
57
- foreground.setZ(2);
58
- foreground.load();
59
-
60
- foreground.animate(0, 2, 200, -1);
61
-
62
- local btWidth = 159;
63
- local btHeight = 52;
64
-
65
- if (useHD) {
66
- btWidth = 318;
67
- btHeight = 104;
68
- }
69
-
70
- okButton = emo.SpriteSheet(getHdImageName("credit_button.png"), btWidth, btHeight);
71
- okButton.move(
72
- foreground.getX() + foreground.getWidth() - okButton.getWidth(),
73
- foreground.getY() + foreground.getHeight() - okButton.getHeight());
74
- okButton.setZ(3);
75
- okButton.load();
76
- okButton.setFrame(1);
77
- }
78
-
79
- /*
80
- * Called when the app has gained focus
81
- */
82
- function onGainedFocus() {
83
- audio.playBGM();
84
- }
85
-
86
- /*
87
- * Called when the app has lost focus
88
- */
89
- function onLostFocus() {
90
- audio.pauseBGM();
91
- }
92
-
93
- function onDispose() {
94
- okButton.remove();
95
- foreground.remove();
96
- layer.remove();
97
- background.remove();
98
-
99
- background = null;
100
- }
101
-
102
- /*
103
- * touch event
104
- */
105
- function onMotionEvent(mevent) {
106
- local x = mevent.getX();
107
- local y = mevent.getY();
108
- if (mevent.getAction() == MOTION_EVENT_ACTION_DOWN) {
109
- if (okButton.contains(x, y)) {
110
- okButton.setFrame(0);
111
- audio.playSE0();
112
- if (!fadingOut) {
113
- fadingOut = true;
114
- stage.load(TitleScene(),
115
- null, emo.AlphaModifier(0, 1, 500, emo.easing.CubicOut));
116
- }
117
- }
118
- }
119
- }
120
- }
121
-
122
- """
123
-
124
- class ConfigLexer(QsciLexerCustom):
125
- def __init__(self, parent):
126
- QsciLexerCustom.__init__(self, parent)
127
- self._styles = {
128
- 0: 'Default',
129
- 1: 'FirstLevelTitle',
130
- 2: 'SecundLevelTitle',
131
- 3: 'Green',
132
- 4: 'Digits',
133
- 5: 'KeyWord1',
134
- 6: 'KeyWord2',
135
- 7: 'KeyWord3',
136
- 8: 'KeyWord4',
137
- }
138
- for key,value in self._styles.iteritems():
139
- setattr(self, value, key)
140
-
141
- def language(self):
142
- return 'Squirrel'
143
-
144
- def foldCompact(self):
145
- return self._foldcompact
146
-
147
- def setFoldCompact(self, enable):
148
- self._foldcompact = bool(enable)
149
-
150
- def description(self, style):
151
- return self._styles.get(style, '')
152
-
153
- def defaultColor(self, style):
154
- if style == self.Default:
155
- return QColor('#000000')
156
- elif style == self.FirstLevelTitle:
157
- return QColor('#FF0000')
158
- elif style == self.SecundLevelTitle:
159
- return QColor('#0000FF')
160
- elif style == self.Green:
161
- return QColor('#00FF00')
162
- elif style == self.Digits:
163
- return QColor('#AAAAAA')
164
- elif style == self.KeyWord1:
165
- return QColor('#8000FF')
166
- elif style == self.KeyWord2:
167
- return QColor('#400080')
168
- elif style == self.KeyWord3:
169
- return QColor('#FF0000')
170
- elif style == self.KeyWord4:
171
- return QColor('#000000')
172
-
173
- return QsciLexerCustom.defaultColor(self, style)
174
-
175
- def defaultFont(self, style):
176
- font = QsciLexerCustom.defaultFont(self, style)
177
-
178
- if style == self.FirstLevelTitle or style == self.SecundLevelTitle:
179
- font.setBold(True)
180
- elif style == self.Green:
181
- font.setBold(True)
182
- font.setUnderline(True)
183
-
184
- return font
185
-
186
- def defaultPaper(self, style):
187
- # Here we change the color of the background.
188
- # We want to colorize all the background of the line.
189
- # This is done by using the following method defaultEolFill() .
190
- if style == self.SecundLevelTitle:
191
- return QColor('#FFFF99')
192
- elif style == self.Digits:
193
- return QColor('#FFCC66')
194
-
195
- return QsciLexerCustom.defaultPaper(self, style)
196
-
197
- def defaultEolFill(self, style):
198
- # This allowed to colorize all the background of a line.
199
- if style == self.SecundLevelTitle:
200
- return True
201
- return QsciLexerCustom.defaultEolFill(self, style)
202
-
203
- def styleText(self, start, end):
204
- editor = self.editor()
205
- if editor is None:
206
- return
207
-
208
- SCI = editor.SendScintilla
209
- set_style = self.setStyling
210
-
211
- source = ''
212
- if end > editor.length():
213
- end = editor.length()
214
- if end > start:
215
- data = bytearray(end - start + 1)
216
- source = QString(data)
217
- SCI(QsciScintilla.SCI_GETTEXTRANGE, start, end, source)
218
- if not source:
219
- return
220
- self.startStyling(start, 0x1f)
221
-
222
- index = SCI(QsciScintilla.SCI_LINEFROMPOSITION, start)
223
-
224
- for line in source.splitlines(True):
225
- # Try to uncomment the following line to see in the console
226
- # how Scintiallla works. You have to think in terms of isolated
227
- # lines rather than globally on the whole text.
228
- # print line
229
-
230
- length = len(line)
231
-
232
- if line.startswith('+'):
233
- newState = self.FirstLevelTitle
234
- elif line.startswith('\t+') or line.startswith(' +'):
235
- newState = self.SecundLevelTitle
236
- else:
237
- pos = SCI(QsciScintilla.SCI_GETLINEENDPOSITION, index) - length + 1
238
- i = 0
239
- while i < length:
240
- wordLength = 1
241
-
242
- self.startStyling(i + pos, 0x1f)
243
-
244
- if chr(line[i]) in '0123456789':
245
- newState = self.Digits
246
- else:
247
- newState = self.gett(line[i:])
248
- wordLength = 5
249
- if line[i:].startswith("class"):
250
- newState = self.KeyWord2
251
- wordLength = len('class')
252
- else:
253
- newState = self.Default
254
- i += wordLength
255
- set_style(wordLength, newState)
256
- newState = None
257
-
258
- if newState:
259
- set_style(length, newState)
260
-
261
- index += 1
262
-
263
-
264
- class MainWindow(QMainWindow):
265
- def __init__(self):
266
- QMainWindow.__init__(self)
267
- self.setWindowTitle('Custom Lexer For Config Files')
268
- self.setGeometry(50, 200, 400, 400)
269
- self.editor = QsciScintilla(self)
270
- self.editor.setUtf8(True)
271
-
272
- # LINES' NUMBER IN THE MARGIN
273
- self.editor.setMarginLineNumbers(1,True)
274
- self.editor.setMarginWidth(1, QString("-------"))
275
- # OK for 3 digits. This was found by direct tests...
276
- # WRAPING
277
- self.editor.setWrapMode(True)
278
- self.setCentralWidget(self.editor)
279
- self.lexer = ConfigLexer(self.editor)
280
- self.editor.setLexer(self.lexer)
281
- self.editor.setText(_sample)
282
-
283
- if __name__ == "__main__":
284
- app = QApplication(sys.argv)
285
- win = MainWindow()
286
- win.show()
287
- sys.exit(app.exec_())
Widget/lexersquirrel.py CHANGED
@@ -1,4 +1,4 @@
1
- from PyQt4.Qsci import QsciLexerCustom,QsciStyle
1
+ from PyQt4.Qsci import QsciLexerCustom,QsciStyle,QsciScintilla
2
2
  from PyQt4.QtCore import QString
3
3
  from PyQt4.QtGui import QFont, QColor
4
4
  from globals import fontName, fontSize
@@ -63,6 +63,8 @@ class LexerSquirrel(QsciLexerCustom):
63
63
 
64
64
  def setColorStyle(self,cs):
65
65
  self.colorStyle = cs
66
+ self.styles[0].setColor(self.colorStyle.color)
67
+ self.styles[0].setPaper(self.colorStyle.paper)
66
68
 
67
69
  def language(self):
68
70
  return 'Squirrel'
@@ -104,39 +106,81 @@ class LexerSquirrel(QsciLexerCustom):
104
106
  return QsciLexerCustom.defaultEolFill(self, ix)
105
107
 
106
108
  def styleText(self, start, end):
107
- #print("LexerErlang.styleText(%d,%d)" % (start, end))
108
- lines = self.getText(start, end)
109
+ editor = self.editor()
110
+ if editor is None:
111
+ return
112
+
113
+ SCI = editor.SendScintilla
114
+ set_style = self.setStyling
115
+
116
+ source = ''
117
+ if end > editor.length():
118
+ end = editor.length()
109
- offset = start
119
+ if end > start:
120
+ source = bytearray(end - start)
121
+ SCI(QsciScintilla.SCI_GETTEXTRANGE, start, end, source)
122
+ if not source:
123
+ return
124
+
110
- self.startStyling(offset, 0)
125
+ self.startStyling(start, 0x1f)
126
+
127
+ index = SCI(QsciScintilla.SCI_LINEFROMPOSITION, start)
128
+
111
- #print("startStyling()")
129
+ for line in source.splitlines(True):
130
+ # Try to uncomment the following line to see in the console
131
+ # how Scintiallla works. You have to think in terms of isolated
132
+ # lines rather than globally on the whole text.
112
- for i in lines:
133
+ # print line
134
+
113
- length = len(i)
135
+ length = len(line)
136
+
137
+ if line.startswith('#'):
138
+ newState = self.styles[3]
139
+ elif line.startswith('\t+') or line.startswith(' +'):
140
+ newState = self.styles[3]
141
+ else:
142
+ pos = SCI(QsciScintilla.SCI_GETLINEENDPOSITION, index) - length + 1
143
+ i = 0
144
+ while i < length:
145
+ wordLength = 1
146
+
147
+ self.startStyling(i + pos, 0x1f)
148
+ newState = self.styles[0]
114
- if i == "":
149
+ """
150
+ for word in self.words1:
151
+ if line[i:].startswith(word):
152
+ newState = self.styles[3]
153
+ wordLength = len(word)
154
+
155
+ """
156
+ if chr(line[i]) in '0123456789':
157
+ newState = self.styles[4]
158
+ else:
159
+ if line[i:].startswith("class"):
160
+ newState = self.styles[2]
161
+ wordLength = len('class')
162
+ elif line[i:].startswith('function'):
163
+ newState = self.styles[3]
164
+ wordLength = len('function')
165
+ elif line[i:].startswith('if'):
166
+ newState = self.styles[4]
167
+ wordLength = len('if')
168
+ elif line[i:].startswith('#'):
169
+ newState = self.styles[4]
170
+ wordLength = length
171
+ elif line[i:].startswith('//'):
172
+ newState = self.styles[4]
173
+ wordLength = length
174
+ else:
115
- self.setStyling(1, self.styles[0])
175
+ newState = self.styles[0]
176
+
177
+ i += wordLength
116
- #print("setStyling(1)")
178
+ set_style(wordLength, newState)
179
+ newState = None
180
+ if newState:
181
+ set_style(length, newState)
182
+
117
- offset += 1
183
+ index += 1
118
- continue
119
- if i == '#':
120
- self.setStyling(1, self.styles[2])
121
- offset += 1
122
- continue
123
- if i == 'g':
124
- self.setStyling(1, self.styles[3])
125
- offset += 1
126
- continue
127
- if i[0] == '%':
128
- self.setStyling(length+1, self.styles[1])
129
- #print("setStyling(%)")
130
- offset += length+1
131
- continue
132
- self.setStyling(length+1, self.styles[0])
133
- #print("setStyling(n)")
134
- offset += length+1
135
-
136
- def getText(self, start, end):
137
- data = self.sci.text()
138
- #print("LexerErlang.getText(): " + str(len(data)) + " chars")
139
- return data[start:end].split('\n')
140
184
 
141
185
 
142
186
  import sys
Widget/style.py CHANGED
@@ -70,4 +70,15 @@ class Style8:
70
70
  self.paper = QColor('#0f0f00')
71
71
  self.caret = QColor('#ffe4e4')
72
72
  self.marker = QColor('#ee1111')
73
- self.margin = QColor('#cccccc')
73
+ self.margin = QColor('#cccccc')
74
+
75
+ Styles = []
76
+ Styles.append(Style0())
77
+ Styles.append(Style1())
78
+ Styles.append(Style2())
79
+ Styles.append(Style3())
80
+ Styles.append(Style4())
81
+ Styles.append(Style5())
82
+ Styles.append(Style6())
83
+ Styles.append(Style7())
84
+ Styles.append(Style8())
config.yaml CHANGED
@@ -6,9 +6,7 @@ ADB:
6
6
  shell kill
7
7
  File:
8
8
  - C:/CODE/assets/creditscene.nut
9
- - C:/CODE/assets/common.nut
10
9
  - C:/CODE/assets/runtime.nut
11
- - C:/CODE/assets/Engine.h
12
10
  Project:
13
11
  - C:/CODE/data/
14
12
  - C:/CODE/assets/
@@ -18,7 +16,7 @@ Setting:
18
16
  fontname: Courier New
19
17
  fontsize: 10
20
18
  iconsize: 16
21
- styleindex: 5
19
+ styleindex: 0
22
20
  thresh: 1
23
21
  workspace: C:/CODE/
24
22
  Style: 0
window.py CHANGED
@@ -4,7 +4,7 @@ from PyQt4.QtGui import (QAction,QIcon,QMessageBox,QWidgetAction,QMenu,QWidget,
4
4
  QMainWindow,QPalette,QColor)
5
5
  from PyQt4.QtCore import QSize,Qt, QT_VERSION_STR,PYQT_VERSION_STR,QStringList
6
6
  from Widget import Tab,Tree
7
- from Widget.style import *
7
+ from Widget.style import Styles
8
8
 
9
9
  from globals import (ospathsep,ospathjoin,ospathbasename,workDir,
10
10
  OS_NAME,PY_VERSION,__version__,os_icon,config,workSpace,
@@ -202,16 +202,6 @@ class Window(QMainWindow):
202
202
  self.tab_8.show()
203
203
  else:
204
204
  self.tab_8.hide()
205
-
206
- def initColorStyle(self):
207
- self.colorStyle = self.checkColorStyle(self.styleIndex)
208
- pal = QPalette(self.tabWidget_2.palette())
209
- #print pal.color(QPalette.Base).name()
210
- #print pal.color(QPalette.Window).name()
211
- pal.setColor(QPalette.Base,self.colorStyle.paper)
212
- pal.setColor(QPalette.Text,self.colorStyle.color)
213
- self.tabWidget_2.setPalette(pal)
214
- self.tabWidget_3.setPalette(pal)
215
205
 
216
206
  def initToolBar(self):
217
207
  self.action_NewProject = QAction(os_icon('newprj_wiz'), 'Project', self)
@@ -403,27 +393,16 @@ class Window(QMainWindow):
403
393
  while(edt.findText(self.lineEdit.text(),self.regex.isChecked(),self.caseSensitive.isChecked(),self.wholeWord.isChecked(),self.backward.isChecked())):
404
394
  edt.replaceText(self.lineEdit_2.text())
405
395
 
406
- def checkColorStyle(self,ind):
396
+ def initColorStyle(self):
397
+ self.colorStyle = Styles[self.styleIndex]
398
+ pal = QPalette(self.tabWidget_2.palette())
399
+ #print pal.color(QPalette.Base).name()
407
- if ind == 0:
400
+ #print pal.color(QPalette.Window).name()
401
+ pal.setColor(QPalette.Base,self.colorStyle.paper)
402
+ pal.setColor(QPalette.Text,self.colorStyle.color)
408
- return Style0()
403
+ self.tabWidget_2.setPalette(pal)
409
- elif ind == 1:
410
- return Style1()
404
+ self.tabWidget_3.setPalette(pal)
411
- elif ind == 2:
405
+
412
- return Style2()
413
- elif ind == 3:
414
- return Style3()
415
- elif ind == 4:
416
- return Style4()
417
- elif ind == 5:
418
- return Style5()
419
- elif ind == 6:
420
- return Style6()
421
- elif ind == 7:
422
- return Style7()
423
- elif ind == 8:
424
- return Style8()
425
-
426
-
427
406
  def style_clicked(self,no):
428
407
  self.styleIndex = no -1
429
408
  #print self.styleIndex