1 | n/a | import builtins |
---|
2 | n/a | import keyword |
---|
3 | n/a | import re |
---|
4 | n/a | import time |
---|
5 | n/a | |
---|
6 | n/a | from idlelib.config import idleConf |
---|
7 | n/a | from idlelib.delegator import Delegator |
---|
8 | n/a | |
---|
9 | n/a | DEBUG = False |
---|
10 | n/a | |
---|
11 | n/a | def any(name, alternates): |
---|
12 | n/a | "Return a named group pattern matching list of alternates." |
---|
13 | n/a | return "(?P<%s>" % name + "|".join(alternates) + ")" |
---|
14 | n/a | |
---|
15 | n/a | def make_pat(): |
---|
16 | n/a | kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b" |
---|
17 | n/a | builtinlist = [str(name) for name in dir(builtins) |
---|
18 | n/a | if not name.startswith('_') and \ |
---|
19 | n/a | name not in keyword.kwlist] |
---|
20 | n/a | # self.file = open("file") : |
---|
21 | n/a | # 1st 'file' colorized normal, 2nd as builtin, 3rd as string |
---|
22 | n/a | builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b" |
---|
23 | n/a | comment = any("COMMENT", [r"#[^\n]*"]) |
---|
24 | n/a | stringprefix = r"(?i:\br|u|f|fr|rf|b|br|rb)?" |
---|
25 | n/a | sqstring = stringprefix + r"'[^'\\\n]*(\\.[^'\\\n]*)*'?" |
---|
26 | n/a | dqstring = stringprefix + r'"[^"\\\n]*(\\.[^"\\\n]*)*"?' |
---|
27 | n/a | sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?" |
---|
28 | n/a | dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?' |
---|
29 | n/a | string = any("STRING", [sq3string, dq3string, sqstring, dqstring]) |
---|
30 | n/a | return kw + "|" + builtin + "|" + comment + "|" + string +\ |
---|
31 | n/a | "|" + any("SYNC", [r"\n"]) |
---|
32 | n/a | |
---|
33 | n/a | prog = re.compile(make_pat(), re.S) |
---|
34 | n/a | idprog = re.compile(r"\s+(\w+)", re.S) |
---|
35 | n/a | |
---|
36 | n/a | def color_config(text): # Called from htest, Editor, and Turtle Demo. |
---|
37 | n/a | '''Set color opitons of Text widget. |
---|
38 | n/a | |
---|
39 | n/a | Should be called whenever ColorDelegator is called. |
---|
40 | n/a | ''' |
---|
41 | n/a | # Not automatic because ColorDelegator does not know 'text'. |
---|
42 | n/a | theme = idleConf.CurrentTheme() |
---|
43 | n/a | normal_colors = idleConf.GetHighlight(theme, 'normal') |
---|
44 | n/a | cursor_color = idleConf.GetHighlight(theme, 'cursor', fgBg='fg') |
---|
45 | n/a | select_colors = idleConf.GetHighlight(theme, 'hilite') |
---|
46 | n/a | text.config( |
---|
47 | n/a | foreground=normal_colors['foreground'], |
---|
48 | n/a | background=normal_colors['background'], |
---|
49 | n/a | insertbackground=cursor_color, |
---|
50 | n/a | selectforeground=select_colors['foreground'], |
---|
51 | n/a | selectbackground=select_colors['background'], |
---|
52 | n/a | inactiveselectbackground=select_colors['background'], # new in 8.5 |
---|
53 | n/a | ) |
---|
54 | n/a | |
---|
55 | n/a | class ColorDelegator(Delegator): |
---|
56 | n/a | |
---|
57 | n/a | def __init__(self): |
---|
58 | n/a | Delegator.__init__(self) |
---|
59 | n/a | self.prog = prog |
---|
60 | n/a | self.idprog = idprog |
---|
61 | n/a | self.LoadTagDefs() |
---|
62 | n/a | |
---|
63 | n/a | def setdelegate(self, delegate): |
---|
64 | n/a | if self.delegate is not None: |
---|
65 | n/a | self.unbind("<<toggle-auto-coloring>>") |
---|
66 | n/a | Delegator.setdelegate(self, delegate) |
---|
67 | n/a | if delegate is not None: |
---|
68 | n/a | self.config_colors() |
---|
69 | n/a | self.bind("<<toggle-auto-coloring>>", self.toggle_colorize_event) |
---|
70 | n/a | self.notify_range("1.0", "end") |
---|
71 | n/a | else: |
---|
72 | n/a | # No delegate - stop any colorizing |
---|
73 | n/a | self.stop_colorizing = True |
---|
74 | n/a | self.allow_colorizing = False |
---|
75 | n/a | |
---|
76 | n/a | def config_colors(self): |
---|
77 | n/a | for tag, cnf in self.tagdefs.items(): |
---|
78 | n/a | if cnf: |
---|
79 | n/a | self.tag_configure(tag, **cnf) |
---|
80 | n/a | self.tag_raise('sel') |
---|
81 | n/a | |
---|
82 | n/a | def LoadTagDefs(self): |
---|
83 | n/a | theme = idleConf.CurrentTheme() |
---|
84 | n/a | self.tagdefs = { |
---|
85 | n/a | "COMMENT": idleConf.GetHighlight(theme, "comment"), |
---|
86 | n/a | "KEYWORD": idleConf.GetHighlight(theme, "keyword"), |
---|
87 | n/a | "BUILTIN": idleConf.GetHighlight(theme, "builtin"), |
---|
88 | n/a | "STRING": idleConf.GetHighlight(theme, "string"), |
---|
89 | n/a | "DEFINITION": idleConf.GetHighlight(theme, "definition"), |
---|
90 | n/a | "SYNC": {'background':None,'foreground':None}, |
---|
91 | n/a | "TODO": {'background':None,'foreground':None}, |
---|
92 | n/a | "ERROR": idleConf.GetHighlight(theme, "error"), |
---|
93 | n/a | # The following is used by ReplaceDialog: |
---|
94 | n/a | "hit": idleConf.GetHighlight(theme, "hit"), |
---|
95 | n/a | } |
---|
96 | n/a | |
---|
97 | n/a | if DEBUG: print('tagdefs',self.tagdefs) |
---|
98 | n/a | |
---|
99 | n/a | def insert(self, index, chars, tags=None): |
---|
100 | n/a | index = self.index(index) |
---|
101 | n/a | self.delegate.insert(index, chars, tags) |
---|
102 | n/a | self.notify_range(index, index + "+%dc" % len(chars)) |
---|
103 | n/a | |
---|
104 | n/a | def delete(self, index1, index2=None): |
---|
105 | n/a | index1 = self.index(index1) |
---|
106 | n/a | self.delegate.delete(index1, index2) |
---|
107 | n/a | self.notify_range(index1) |
---|
108 | n/a | |
---|
109 | n/a | after_id = None |
---|
110 | n/a | allow_colorizing = True |
---|
111 | n/a | colorizing = False |
---|
112 | n/a | |
---|
113 | n/a | def notify_range(self, index1, index2=None): |
---|
114 | n/a | self.tag_add("TODO", index1, index2) |
---|
115 | n/a | if self.after_id: |
---|
116 | n/a | if DEBUG: print("colorizing already scheduled") |
---|
117 | n/a | return |
---|
118 | n/a | if self.colorizing: |
---|
119 | n/a | self.stop_colorizing = True |
---|
120 | n/a | if DEBUG: print("stop colorizing") |
---|
121 | n/a | if self.allow_colorizing: |
---|
122 | n/a | if DEBUG: print("schedule colorizing") |
---|
123 | n/a | self.after_id = self.after(1, self.recolorize) |
---|
124 | n/a | |
---|
125 | n/a | close_when_done = None # Window to be closed when done colorizing |
---|
126 | n/a | |
---|
127 | n/a | def close(self, close_when_done=None): |
---|
128 | n/a | if self.after_id: |
---|
129 | n/a | after_id = self.after_id |
---|
130 | n/a | self.after_id = None |
---|
131 | n/a | if DEBUG: print("cancel scheduled recolorizer") |
---|
132 | n/a | self.after_cancel(after_id) |
---|
133 | n/a | self.allow_colorizing = False |
---|
134 | n/a | self.stop_colorizing = True |
---|
135 | n/a | if close_when_done: |
---|
136 | n/a | if not self.colorizing: |
---|
137 | n/a | close_when_done.destroy() |
---|
138 | n/a | else: |
---|
139 | n/a | self.close_when_done = close_when_done |
---|
140 | n/a | |
---|
141 | n/a | def toggle_colorize_event(self, event): |
---|
142 | n/a | if self.after_id: |
---|
143 | n/a | after_id = self.after_id |
---|
144 | n/a | self.after_id = None |
---|
145 | n/a | if DEBUG: print("cancel scheduled recolorizer") |
---|
146 | n/a | self.after_cancel(after_id) |
---|
147 | n/a | if self.allow_colorizing and self.colorizing: |
---|
148 | n/a | if DEBUG: print("stop colorizing") |
---|
149 | n/a | self.stop_colorizing = True |
---|
150 | n/a | self.allow_colorizing = not self.allow_colorizing |
---|
151 | n/a | if self.allow_colorizing and not self.colorizing: |
---|
152 | n/a | self.after_id = self.after(1, self.recolorize) |
---|
153 | n/a | if DEBUG: |
---|
154 | n/a | print("auto colorizing turned",\ |
---|
155 | n/a | self.allow_colorizing and "on" or "off") |
---|
156 | n/a | return "break" |
---|
157 | n/a | |
---|
158 | n/a | def recolorize(self): |
---|
159 | n/a | self.after_id = None |
---|
160 | n/a | if not self.delegate: |
---|
161 | n/a | if DEBUG: print("no delegate") |
---|
162 | n/a | return |
---|
163 | n/a | if not self.allow_colorizing: |
---|
164 | n/a | if DEBUG: print("auto colorizing is off") |
---|
165 | n/a | return |
---|
166 | n/a | if self.colorizing: |
---|
167 | n/a | if DEBUG: print("already colorizing") |
---|
168 | n/a | return |
---|
169 | n/a | try: |
---|
170 | n/a | self.stop_colorizing = False |
---|
171 | n/a | self.colorizing = True |
---|
172 | n/a | if DEBUG: print("colorizing...") |
---|
173 | n/a | t0 = time.perf_counter() |
---|
174 | n/a | self.recolorize_main() |
---|
175 | n/a | t1 = time.perf_counter() |
---|
176 | n/a | if DEBUG: print("%.3f seconds" % (t1-t0)) |
---|
177 | n/a | finally: |
---|
178 | n/a | self.colorizing = False |
---|
179 | n/a | if self.allow_colorizing and self.tag_nextrange("TODO", "1.0"): |
---|
180 | n/a | if DEBUG: print("reschedule colorizing") |
---|
181 | n/a | self.after_id = self.after(1, self.recolorize) |
---|
182 | n/a | if self.close_when_done: |
---|
183 | n/a | top = self.close_when_done |
---|
184 | n/a | self.close_when_done = None |
---|
185 | n/a | top.destroy() |
---|
186 | n/a | |
---|
187 | n/a | def recolorize_main(self): |
---|
188 | n/a | next = "1.0" |
---|
189 | n/a | while True: |
---|
190 | n/a | item = self.tag_nextrange("TODO", next) |
---|
191 | n/a | if not item: |
---|
192 | n/a | break |
---|
193 | n/a | head, tail = item |
---|
194 | n/a | self.tag_remove("SYNC", head, tail) |
---|
195 | n/a | item = self.tag_prevrange("SYNC", head) |
---|
196 | n/a | if item: |
---|
197 | n/a | head = item[1] |
---|
198 | n/a | else: |
---|
199 | n/a | head = "1.0" |
---|
200 | n/a | |
---|
201 | n/a | chars = "" |
---|
202 | n/a | next = head |
---|
203 | n/a | lines_to_get = 1 |
---|
204 | n/a | ok = False |
---|
205 | n/a | while not ok: |
---|
206 | n/a | mark = next |
---|
207 | n/a | next = self.index(mark + "+%d lines linestart" % |
---|
208 | n/a | lines_to_get) |
---|
209 | n/a | lines_to_get = min(lines_to_get * 2, 100) |
---|
210 | n/a | ok = "SYNC" in self.tag_names(next + "-1c") |
---|
211 | n/a | line = self.get(mark, next) |
---|
212 | n/a | ##print head, "get", mark, next, "->", repr(line) |
---|
213 | n/a | if not line: |
---|
214 | n/a | return |
---|
215 | n/a | for tag in self.tagdefs: |
---|
216 | n/a | self.tag_remove(tag, mark, next) |
---|
217 | n/a | chars = chars + line |
---|
218 | n/a | m = self.prog.search(chars) |
---|
219 | n/a | while m: |
---|
220 | n/a | for key, value in m.groupdict().items(): |
---|
221 | n/a | if value: |
---|
222 | n/a | a, b = m.span(key) |
---|
223 | n/a | self.tag_add(key, |
---|
224 | n/a | head + "+%dc" % a, |
---|
225 | n/a | head + "+%dc" % b) |
---|
226 | n/a | if value in ("def", "class"): |
---|
227 | n/a | m1 = self.idprog.match(chars, b) |
---|
228 | n/a | if m1: |
---|
229 | n/a | a, b = m1.span(1) |
---|
230 | n/a | self.tag_add("DEFINITION", |
---|
231 | n/a | head + "+%dc" % a, |
---|
232 | n/a | head + "+%dc" % b) |
---|
233 | n/a | m = self.prog.search(chars, m.end()) |
---|
234 | n/a | if "SYNC" in self.tag_names(next + "-1c"): |
---|
235 | n/a | head = next |
---|
236 | n/a | chars = "" |
---|
237 | n/a | else: |
---|
238 | n/a | ok = False |
---|
239 | n/a | if not ok: |
---|
240 | n/a | # We're in an inconsistent state, and the call to |
---|
241 | n/a | # update may tell us to stop. It may also change |
---|
242 | n/a | # the correct value for "next" (since this is a |
---|
243 | n/a | # line.col string, not a true mark). So leave a |
---|
244 | n/a | # crumb telling the next invocation to resume here |
---|
245 | n/a | # in case update tells us to leave. |
---|
246 | n/a | self.tag_add("TODO", next) |
---|
247 | n/a | self.update() |
---|
248 | n/a | if self.stop_colorizing: |
---|
249 | n/a | if DEBUG: print("colorizing stopped") |
---|
250 | n/a | return |
---|
251 | n/a | |
---|
252 | n/a | def removecolors(self): |
---|
253 | n/a | for tag in self.tagdefs: |
---|
254 | n/a | self.tag_remove(tag, "1.0", "end") |
---|
255 | n/a | |
---|
256 | n/a | |
---|
257 | n/a | def _color_delegator(parent): # htest # |
---|
258 | n/a | from tkinter import Toplevel, Text |
---|
259 | n/a | from idlelib.percolator import Percolator |
---|
260 | n/a | |
---|
261 | n/a | top = Toplevel(parent) |
---|
262 | n/a | top.title("Test ColorDelegator") |
---|
263 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
---|
264 | n/a | top.geometry("700x250+%d+%d" % (x + 20, y + 175)) |
---|
265 | n/a | source = ("# Following has syntax errors\n" |
---|
266 | n/a | "if True: then int 1\nelif False: print 0\nelse: float(None)\n" |
---|
267 | n/a | "if iF + If + IF: 'keywork matching must respect case'\n" |
---|
268 | n/a | "# All valid prefixes for unicode and byte strings should be colored\n" |
---|
269 | n/a | "'x', '''x''', \"x\", \"\"\"x\"\"\"\n" |
---|
270 | n/a | "r'x', u'x', R'x', U'x', f'x', F'x', ur'is invalid'\n" |
---|
271 | n/a | "fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x'\n" |
---|
272 | n/a | "b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x'.rB'x',Rb'x',RB'x'\n") |
---|
273 | n/a | text = Text(top, background="white") |
---|
274 | n/a | text.pack(expand=1, fill="both") |
---|
275 | n/a | text.insert("insert", source) |
---|
276 | n/a | text.focus_set() |
---|
277 | n/a | |
---|
278 | n/a | color_config(text) |
---|
279 | n/a | p = Percolator(text) |
---|
280 | n/a | d = ColorDelegator() |
---|
281 | n/a | p.insertfilter(d) |
---|
282 | n/a | |
---|
283 | n/a | if __name__ == "__main__": |
---|
284 | n/a | import unittest |
---|
285 | n/a | unittest.main('idlelib.idle_test.test_colorizer', |
---|
286 | n/a | verbosity=2, exit=False) |
---|
287 | n/a | |
---|
288 | n/a | from idlelib.idle_test.htest import run |
---|
289 | n/a | run(_color_delegator) |
---|