forked from mhammond/pywin32
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.py
More file actions
704 lines (630 loc) · 25.9 KB
/
formatter.py
File metadata and controls
704 lines (630 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# Does Python source formatting for Scintilla controls.
import array
import string
import win32api
import win32con
import win32ui
from . import scintillacon
WM_KICKIDLE = 0x036A
# Used to indicate that style should use default color
from win32con import CLR_INVALID
debugging = 0
if debugging:
# Output must go to another process else the result of
# the printing itself will trigger again trigger a trace.
import win32trace
import win32traceutil
def trace(*args):
win32trace.write(" ".join(map(str, args)) + "\n")
else:
trace = lambda *args: None
class Style:
"""Represents a single format"""
def __init__(self, name, format, background=CLR_INVALID):
self.name = name # Name the format representes eg, "String", "Class"
# Default background for each style is only used when there are no
# saved settings (generally on first startup)
self.background = self.default_background = background
if isinstance(format, str):
self.aliased = format
self.format = None
else:
self.format = format
self.aliased = None
self.stylenum = None # Not yet registered.
def IsBasedOnDefault(self):
return len(self.format) == 5
# If the currently extended font defintion matches the
# default format, restore the format to the "simple" format.
def NormalizeAgainstDefault(self, defaultFormat):
if self.IsBasedOnDefault():
return 0 # No more to do, and not changed.
bIsDefault = (
self.format[7] == defaultFormat[7] and self.format[2] == defaultFormat[2]
)
if bIsDefault:
self.ForceAgainstDefault()
return bIsDefault
def ForceAgainstDefault(self):
self.format = self.format[:5]
def GetCompleteFormat(self, defaultFormat):
# Get the complete style after applying any relevant defaults.
if len(self.format) == 5: # It is a default one
fmt = self.format + defaultFormat[5:]
else:
fmt = self.format
flags = (
win32con.CFM_BOLD
| win32con.CFM_CHARSET
| win32con.CFM_COLOR
| win32con.CFM_FACE
| win32con.CFM_ITALIC
| win32con.CFM_SIZE
)
return (flags,) + fmt[1:]
# The Formatter interface
# used primarily when the actual formatting is done by Scintilla!
class FormatterBase:
def __init__(self, scintilla):
self.scintilla = scintilla
self.baseFormatFixed = (-402653169, 0, 200, 0, 0, 0, 49, "Courier New")
self.baseFormatProp = (-402653169, 0, 200, 0, 0, 0, 49, "Arial")
self.bUseFixed = 1
self.styles = {} # Indexed by name
self.styles_by_id = {} # Indexed by allocated ID.
self.SetStyles()
def HookFormatter(self, parent=None):
raise NotImplementedError
# Used by the IDLE extensions to quickly determine if a character is a string.
def GetStringStyle(self, pos):
try:
style = self.styles_by_id[self.scintilla.SCIGetStyleAt(pos)]
except KeyError:
# A style we don't know about - probably not even a .py file - can't be a string
return None
if style.name in self.string_style_names:
return style
return None
def RegisterStyle(self, style, stylenum):
assert stylenum is not None, "We must have a style number"
assert style.stylenum is None, "Style has already been registered"
assert stylenum not in self.styles, "We are reusing a style number!"
style.stylenum = stylenum
self.styles[style.name] = style
self.styles_by_id[stylenum] = style
def SetStyles(self):
raise NotImplementedError
def GetSampleText(self):
return "Sample Text for the Format Dialog"
def GetDefaultFormat(self):
if self.bUseFixed:
return self.baseFormatFixed
return self.baseFormatProp
# Update the control with the new style format.
def _ReformatStyle(self, style):
## Selection (background only for now)
## Passing False for WPARAM to SCI_SETSELBACK is documented as resetting to scintilla default,
## but does not work - selection background is not visible at all.
## Default value in SPECIAL_STYLES taken from scintilla source.
if style.name == STYLE_SELECTION:
clr = style.background
self.scintilla.SendScintilla(scintillacon.SCI_SETSELBACK, True, clr)
## Can't change font for selection, but could set color
## However, the font color dropbox has no option for default, and thus would
## always override syntax coloring
## clr = style.format[4]
## self.scintilla.SendScintilla(scintillacon.SCI_SETSELFORE, clr != CLR_INVALID, clr)
return
assert style.stylenum is not None, "Unregistered style."
# print("Reformat style", style.name, style.stylenum)
scintilla = self.scintilla
stylenum = style.stylenum
# Now we have the style number, indirect for the actual style.
if style.aliased is not None:
style = self.styles[style.aliased]
f = style.format
if style.IsBasedOnDefault():
baseFormat = self.GetDefaultFormat()
else:
baseFormat = f
scintilla.SCIStyleSetFore(stylenum, f[4])
scintilla.SCIStyleSetFont(stylenum, baseFormat[7], baseFormat[5])
if f[1] & 1:
scintilla.SCIStyleSetBold(stylenum, 1)
else:
scintilla.SCIStyleSetBold(stylenum, 0)
if f[1] & 2:
scintilla.SCIStyleSetItalic(stylenum, 1)
else:
scintilla.SCIStyleSetItalic(stylenum, 0)
scintilla.SCIStyleSetSize(stylenum, int(baseFormat[2] / 20))
scintilla.SCIStyleSetEOLFilled(stylenum, 1) # Only needed for unclosed strings.
## Default style background to whitespace background if set,
## otherwise use system window color
bg = style.background
if bg == CLR_INVALID:
bg = self.styles[STYLE_DEFAULT].background
if bg == CLR_INVALID:
bg = win32api.GetSysColor(win32con.COLOR_WINDOW)
scintilla.SCIStyleSetBack(stylenum, bg)
def GetStyleByNum(self, stylenum):
return self.styles_by_id[stylenum]
def ApplyFormattingStyles(self, bReload=1):
if bReload:
self.LoadPreferences()
baseFormat = self.GetDefaultFormat()
defaultStyle = Style("default", baseFormat)
defaultStyle.stylenum = scintillacon.STYLE_DEFAULT
self._ReformatStyle(defaultStyle)
for style in self.styles.values():
if style.aliased is None:
style.NormalizeAgainstDefault(baseFormat)
self._ReformatStyle(style)
self.scintilla.InvalidateRect()
# Some functions for loading and saving preferences. By default
# an INI file (well, MFC maps this to the registry) is used.
def LoadPreferences(self):
self.baseFormatFixed = eval(
self.LoadPreference("Base Format Fixed", str(self.baseFormatFixed))
)
self.baseFormatProp = eval(
self.LoadPreference("Base Format Proportional", str(self.baseFormatProp))
)
self.bUseFixed = int(self.LoadPreference("Use Fixed", 1))
for style in self.styles.values():
new = self.LoadPreference(style.name, str(style.format))
try:
style.format = eval(new)
except:
print("Error loading style data for", style.name)
# Use "vanilla" background hardcoded in PYTHON_STYLES if no settings in registry
style.background = int(
self.LoadPreference(
style.name + " background", style.default_background
)
)
def LoadPreference(self, name, default):
return win32ui.GetProfileVal("Format", name, default)
def SavePreferences(self):
self.SavePreference("Base Format Fixed", str(self.baseFormatFixed))
self.SavePreference("Base Format Proportional", str(self.baseFormatProp))
self.SavePreference("Use Fixed", self.bUseFixed)
for style in self.styles.values():
if style.aliased is None:
self.SavePreference(style.name, str(style.format))
bg_name = style.name + " background"
self.SavePreference(bg_name, style.background)
def SavePreference(self, name, value):
win32ui.WriteProfileVal("Format", name, value)
# An abstract formatter
# For all formatters we actually implement here.
# (as opposed to those formatters built in to Scintilla)
class Formatter(FormatterBase):
def __init__(self, scintilla):
self.bCompleteWhileIdle = 0
self.bHaveIdleHandler = 0 # Don't currently have an idle handle
self.nextstylenum = 0
FormatterBase.__init__(self, scintilla)
def HookFormatter(self, parent=None):
if parent is None:
parent = self.scintilla.GetParent() # was GetParentFrame()!?
parent.HookNotify(self.OnStyleNeeded, scintillacon.SCN_STYLENEEDED)
def OnStyleNeeded(self, std, extra):
notify = self.scintilla.SCIUnpackNotifyMessage(extra)
endStyledChar = self.scintilla.SendScintilla(scintillacon.SCI_GETENDSTYLED)
lineEndStyled = self.scintilla.LineFromChar(endStyledChar)
endStyled = self.scintilla.LineIndex(lineEndStyled)
# print(
# "endPosPaint",
# endPosPaint,
# "endStyledChar",
# endStyledChar,
# "lineEndStyled",
# lineEndStyled,
# "endStyled",
# endStyled,
# )
self.Colorize(endStyled, notify.position)
def ColorSeg(self, start, end, styleName):
end += 1
# assert end-start>=0, "Can't have negative styling"
stylenum = self.styles[styleName].stylenum
while start < end:
self.style_buffer[start] = stylenum
start += 1
# self.scintilla.SCISetStyling(end - start + 1, stylenum)
def RegisterStyle(self, style, stylenum=None):
if stylenum is None:
stylenum = self.nextstylenum
self.nextstylenum += 1
FormatterBase.RegisterStyle(self, style, stylenum)
def ColorizeString(self, str, styleStart):
raise RuntimeError("You must override this method")
def Colorize(self, start=0, end=-1):
scintilla = self.scintilla
# scintilla's formatting is all done in terms of utf, so
# we work with utf8 bytes instead of unicode. This magically
# works as any extended chars found in the utf8 don't change
# the semantics.
stringVal = scintilla.GetTextRange(start, end, decode=False)
if start > 0:
stylenum = scintilla.SCIGetStyleAt(start - 1)
styleStart = self.GetStyleByNum(stylenum).name
else:
styleStart = None
# trace("Coloring", start, end, end-start, len(stringVal), styleStart, self.scintilla.SCIGetCharAt(start))
scintilla.SCIStartStyling(start, 31)
self.style_buffer = array.array("b", (0,) * len(stringVal))
self.ColorizeString(stringVal, styleStart)
scintilla.SCISetStylingEx(self.style_buffer)
self.style_buffer = None
# trace("After styling, end styled is", self.scintilla.SCIGetEndStyled())
if (
self.bCompleteWhileIdle
and not self.bHaveIdleHandler
and end != -1
and end < scintilla.GetTextLength()
):
self.bHaveIdleHandler = 1
win32ui.GetApp().AddIdleHandler(self.DoMoreColoring)
# Kicking idle makes the app seem slower when initially repainting!
# win32ui.GetMainFrame().PostMessage(WM_KICKIDLE, 0, 0)
def DoMoreColoring(self, handler, count):
try:
scintilla = self.scintilla
endStyled = scintilla.SCIGetEndStyled()
lineStartStyled = scintilla.LineFromChar(endStyled)
start = scintilla.LineIndex(lineStartStyled)
end = scintilla.LineIndex(lineStartStyled + 1)
textlen = scintilla.GetTextLength()
if end < 0:
end = textlen
finished = end >= textlen
self.Colorize(start, end)
except (win32ui.error, AttributeError):
# Window may have closed before we finished - no big deal!
finished = 1
if finished:
self.bHaveIdleHandler = 0
win32ui.GetApp().DeleteIdleHandler(handler)
return not finished
# A Formatter that knows how to format Python source
from keyword import iskeyword, kwlist
wordstarts = "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
wordchars = "._0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
operators = "%^&*()-+=|{}[]:;<>,/?!.~"
STYLE_DEFAULT = "Whitespace"
STYLE_COMMENT = "Comment"
STYLE_COMMENT_BLOCK = "Comment Blocks"
STYLE_NUMBER = "Number"
STYLE_STRING = "String"
STYLE_SQSTRING = "SQ String"
STYLE_TQSSTRING = "TQS String"
STYLE_TQDSTRING = "TQD String"
STYLE_KEYWORD = "Keyword"
STYLE_CLASS = "Class"
STYLE_METHOD = "Method"
STYLE_OPERATOR = "Operator"
STYLE_IDENTIFIER = "Identifier"
STYLE_BRACE = "Brace/Paren - matching"
STYLE_BRACEBAD = "Brace/Paren - unmatched"
STYLE_STRINGEOL = "String with no terminator"
STYLE_LINENUMBER = "Line numbers"
STYLE_INDENTGUIDE = "Indent guide"
STYLE_SELECTION = "Selection"
STRING_STYLES = [
STYLE_STRING,
STYLE_SQSTRING,
STYLE_TQSSTRING,
STYLE_TQDSTRING,
STYLE_STRINGEOL,
]
# These styles can have any ID - they are not special to scintilla itself.
# However, if we use the built-in lexer, then we must use its style numbers
# so in that case, they _are_ special.
# (name, format, background, scintilla id)
PYTHON_STYLES = [
(STYLE_DEFAULT, (0, 0, 200, 0, 0x808080), CLR_INVALID, scintillacon.SCE_P_DEFAULT),
(
STYLE_COMMENT,
(0, 2, 200, 0, 0x008000),
CLR_INVALID,
scintillacon.SCE_P_COMMENTLINE,
),
(
STYLE_COMMENT_BLOCK,
(0, 2, 200, 0, 0x808080),
CLR_INVALID,
scintillacon.SCE_P_COMMENTBLOCK,
),
(STYLE_NUMBER, (0, 0, 200, 0, 0x808000), CLR_INVALID, scintillacon.SCE_P_NUMBER),
(STYLE_STRING, (0, 0, 200, 0, 0x008080), CLR_INVALID, scintillacon.SCE_P_STRING),
(STYLE_SQSTRING, STYLE_STRING, CLR_INVALID, scintillacon.SCE_P_CHARACTER),
(STYLE_TQSSTRING, STYLE_STRING, CLR_INVALID, scintillacon.SCE_P_TRIPLE),
(STYLE_TQDSTRING, STYLE_STRING, CLR_INVALID, scintillacon.SCE_P_TRIPLEDOUBLE),
(STYLE_STRINGEOL, (0, 0, 200, 0, 0x000000), 0x008080, scintillacon.SCE_P_STRINGEOL),
(STYLE_KEYWORD, (0, 1, 200, 0, 0x800000), CLR_INVALID, scintillacon.SCE_P_WORD),
(STYLE_CLASS, (0, 1, 200, 0, 0xFF0000), CLR_INVALID, scintillacon.SCE_P_CLASSNAME),
(STYLE_METHOD, (0, 1, 200, 0, 0x808000), CLR_INVALID, scintillacon.SCE_P_DEFNAME),
(
STYLE_OPERATOR,
(0, 0, 200, 0, 0x000000),
CLR_INVALID,
scintillacon.SCE_P_OPERATOR,
),
(
STYLE_IDENTIFIER,
(0, 0, 200, 0, 0x000000),
CLR_INVALID,
scintillacon.SCE_P_IDENTIFIER,
),
]
# These styles _always_ have this specific style number, regardless of
# internal or external formatter.
SPECIAL_STYLES = [
(STYLE_BRACE, (0, 0, 200, 0, 0x000000), 0xFFFF80, scintillacon.STYLE_BRACELIGHT),
(STYLE_BRACEBAD, (0, 0, 200, 0, 0x000000), 0x8EA5F2, scintillacon.STYLE_BRACEBAD),
(
STYLE_LINENUMBER,
(0, 0, 200, 0, 0x000000),
win32api.GetSysColor(win32con.COLOR_3DFACE),
scintillacon.STYLE_LINENUMBER,
),
(
STYLE_INDENTGUIDE,
(0, 0, 200, 0, 0x000000),
CLR_INVALID,
scintillacon.STYLE_INDENTGUIDE,
),
## Not actually a style; requires special handling to send appropriate messages to scintilla
(
STYLE_SELECTION,
(0, 0, 200, 0, CLR_INVALID),
win32api.RGB(0xC0, 0xC0, 0xC0),
999999,
),
]
PythonSampleCode = """\
# Some Python
class Sample(Super):
def Fn(self):
\tself.v = 1024
dest = 'dest.html'
x = func(a + 1)|)
s = "I forget...
## A large
## comment block"""
class PythonSourceFormatter(Formatter):
string_style_names = STRING_STYLES
def GetSampleText(self):
return PythonSampleCode
def LoadStyles(self):
pass
def SetStyles(self):
for name, format, bg, ignore in PYTHON_STYLES:
self.RegisterStyle(Style(name, format, bg))
for name, format, bg, sc_id in SPECIAL_STYLES:
self.RegisterStyle(Style(name, format, bg), sc_id)
def ClassifyWord(self, cdoc, start, end, prevWord):
word = cdoc[start : end + 1].decode("latin-1")
attr = STYLE_IDENTIFIER
if prevWord == "class":
attr = STYLE_CLASS
elif prevWord == "def":
attr = STYLE_METHOD
elif word[0] in string.digits:
attr = STYLE_NUMBER
elif iskeyword(word):
attr = STYLE_KEYWORD
self.ColorSeg(start, end, attr)
return word
def ColorizeString(self, str, styleStart):
if styleStart is None:
styleStart = STYLE_DEFAULT
return self.ColorizePythonCode(str, 0, styleStart)
def ColorizePythonCode(self, cdoc, charStart, styleStart):
# Straight translation of C++, should do better
lengthDoc = len(cdoc)
if lengthDoc <= charStart:
return
prevWord = ""
state = styleStart
chPrev = chPrev2 = chPrev3 = " "
chNext2 = chNext = cdoc[charStart : charStart + 1].decode("latin-1")
startSeg = i = charStart
while i < lengthDoc:
ch = chNext
chNext = " "
if i + 1 < lengthDoc:
chNext = cdoc[i + 1 : i + 2].decode("latin-1")
chNext2 = " "
if i + 2 < lengthDoc:
chNext2 = cdoc[i + 2 : i + 3].decode("latin-1")
if state == STYLE_DEFAULT:
if ch in wordstarts:
self.ColorSeg(startSeg, i - 1, STYLE_DEFAULT)
state = STYLE_KEYWORD
startSeg = i
elif ch == "#":
self.ColorSeg(startSeg, i - 1, STYLE_DEFAULT)
if chNext == "#":
state = STYLE_COMMENT_BLOCK
else:
state = STYLE_COMMENT
startSeg = i
elif ch == '"':
self.ColorSeg(startSeg, i - 1, STYLE_DEFAULT)
startSeg = i
state = STYLE_COMMENT
if chNext == '"' and chNext2 == '"':
i += 2
state = STYLE_TQDSTRING
ch = " "
chPrev = " "
chNext = " "
if i + 1 < lengthDoc:
chNext = cdoc[i + 1]
else:
state = STYLE_STRING
elif ch == "'":
self.ColorSeg(startSeg, i - 1, STYLE_DEFAULT)
startSeg = i
state = STYLE_COMMENT
if chNext == "'" and chNext2 == "'":
i += 2
state = STYLE_TQSSTRING
ch = " "
chPrev = " "
chNext = " "
if i + 1 < lengthDoc:
chNext = cdoc[i + 1]
else:
state = STYLE_SQSTRING
elif ch in operators:
self.ColorSeg(startSeg, i - 1, STYLE_DEFAULT)
self.ColorSeg(i, i, STYLE_OPERATOR)
startSeg = i + 1
elif state == STYLE_KEYWORD:
if ch not in wordchars:
prevWord = self.ClassifyWord(cdoc, startSeg, i - 1, prevWord)
state = STYLE_DEFAULT
startSeg = i
if ch == "#":
if chNext == "#":
state = STYLE_COMMENT_BLOCK
else:
state = STYLE_COMMENT
elif ch == '"':
if chNext == '"' and chNext2 == '"':
i += 2
state = STYLE_TQDSTRING
ch = " "
chPrev = " "
chNext = " "
if i + 1 < lengthDoc:
chNext = cdoc[i + 1]
else:
state = STYLE_STRING
elif ch == "'":
if chNext == "'" and chNext2 == "'":
i += 2
state = STYLE_TQSSTRING
ch = " "
chPrev = " "
chNext = " "
if i + 1 < lengthDoc:
chNext = cdoc[i + 1]
else:
state = STYLE_SQSTRING
elif ch in operators:
self.ColorSeg(startSeg, i, STYLE_OPERATOR)
startSeg = i + 1
elif state == STYLE_COMMENT or state == STYLE_COMMENT_BLOCK:
if ch == "\r" or ch == "\n":
self.ColorSeg(startSeg, i - 1, state)
state = STYLE_DEFAULT
startSeg = i
elif state == STYLE_STRING:
if ch == "\\":
if chNext == '"' or chNext == "'" or chNext == "\\":
i += 1
ch = chNext
chNext = " "
if i + 1 < lengthDoc:
chNext = cdoc[i + 1]
elif ch == '"':
self.ColorSeg(startSeg, i, STYLE_STRING)
state = STYLE_DEFAULT
startSeg = i + 1
elif state == STYLE_SQSTRING:
if ch == "\\":
if chNext == '"' or chNext == "'" or chNext == "\\":
i += 1
ch = chNext
chNext = " "
if i + 1 < lengthDoc:
chNext = cdoc[i + 1]
elif ch == "'":
self.ColorSeg(startSeg, i, STYLE_SQSTRING)
state = STYLE_DEFAULT
startSeg = i + 1
elif state == STYLE_TQSSTRING:
if ch == "'" and chPrev == "'" and chPrev2 == "'" and chPrev3 != "\\":
self.ColorSeg(startSeg, i, STYLE_TQSSTRING)
state = STYLE_DEFAULT
startSeg = i + 1
elif (
state == STYLE_TQDSTRING
and ch == '"'
and chPrev == '"'
and chPrev2 == '"'
and chPrev3 != "\\"
):
self.ColorSeg(startSeg, i, STYLE_TQDSTRING)
state = STYLE_DEFAULT
startSeg = i + 1
chPrev3 = chPrev2
chPrev2 = chPrev
chPrev = ch
i += 1
if startSeg < lengthDoc:
if state == STYLE_KEYWORD:
self.ClassifyWord(cdoc, startSeg, lengthDoc - 1, prevWord)
else:
self.ColorSeg(startSeg, lengthDoc - 1, state)
# These taken from the SciTE properties file.
source_formatter_extensions = [
(".py .pys .pyw".split(), scintillacon.SCLEX_PYTHON),
(".html .htm .asp .shtml".split(), scintillacon.SCLEX_HTML),
(
"c .cc .cpp .cxx .h .hh .hpp .hxx .idl .odl .php3 .phtml .inc .js".split(),
scintillacon.SCLEX_CPP,
),
(".vbs .frm .ctl .cls".split(), scintillacon.SCLEX_VB),
(".pl .pm .cgi .pod".split(), scintillacon.SCLEX_PERL),
(".sql .spec .body .sps .spb .sf .sp".split(), scintillacon.SCLEX_SQL),
(".tex .sty".split(), scintillacon.SCLEX_LATEX),
(".xml .xul".split(), scintillacon.SCLEX_XML),
(".err".split(), scintillacon.SCLEX_ERRORLIST),
(".mak".split(), scintillacon.SCLEX_MAKEFILE),
(".bat .cmd".split(), scintillacon.SCLEX_BATCH),
]
class BuiltinSourceFormatter(FormatterBase):
# A class that represents a formatter built-in to Scintilla
def __init__(self, scintilla, ext):
self.ext = ext
FormatterBase.__init__(self, scintilla)
def Colorize(self, start=0, end=-1):
self.scintilla.SendScintilla(scintillacon.SCI_COLOURISE, start, end)
def RegisterStyle(self, style, stylenum=None):
assert style.stylenum is None, "Style has already been registered"
if stylenum is None:
stylenum = self.nextstylenum
self.nextstylenum += 1
assert self.styles.get(stylenum) is None, "We are reusing a style number!"
style.stylenum = stylenum
self.styles[style.name] = style
self.styles_by_id[stylenum] = style
def HookFormatter(self, parent=None):
sc = self.scintilla
for exts, formatter in source_formatter_extensions:
if self.ext in exts:
formatter_use = formatter
break
else:
formatter_use = scintillacon.SCLEX_PYTHON
sc.SendScintilla(scintillacon.SCI_SETLEXER, formatter_use)
keywords = " ".join(kwlist)
sc.SCISetKeywords(keywords)
class BuiltinPythonSourceFormatter(BuiltinSourceFormatter):
sci_lexer_name = scintillacon.SCLEX_PYTHON
string_style_names = STRING_STYLES
def __init__(self, sc, ext=".py"):
BuiltinSourceFormatter.__init__(self, sc, ext)
def SetStyles(self):
for name, format, bg, sc_id in PYTHON_STYLES:
self.RegisterStyle(Style(name, format, bg), sc_id)
for name, format, bg, sc_id in SPECIAL_STYLES:
self.RegisterStyle(Style(name, format, bg), sc_id)
def GetSampleText(self):
return PythonSampleCode