logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

colorizer.py (14773B)


  1. import builtins
  2. import keyword
  3. import re
  4. import time
  5. from idlelib.config import idleConf
  6. from idlelib.delegator import Delegator
  7. DEBUG = False
  8. def any(name, alternates):
  9. "Return a named group pattern matching list of alternates."
  10. return "(?P<%s>" % name + "|".join(alternates) + ")"
  11. def make_pat():
  12. kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b"
  13. match_softkw = (
  14. r"^[ \t]*" + # at beginning of line + possible indentation
  15. r"(?P<MATCH_SOFTKW>match)\b" +
  16. r"(?![ \t]*(?:" + "|".join([ # not followed by ...
  17. r"[:,;=^&|@~)\]}]", # a character which means it can't be a
  18. # pattern-matching statement
  19. r"\b(?:" + r"|".join(keyword.kwlist) + r")\b", # a keyword
  20. ]) +
  21. r"))"
  22. )
  23. case_default = (
  24. r"^[ \t]*" + # at beginning of line + possible indentation
  25. r"(?P<CASE_SOFTKW>case)" +
  26. r"[ \t]+(?P<CASE_DEFAULT_UNDERSCORE>_\b)"
  27. )
  28. case_softkw_and_pattern = (
  29. r"^[ \t]*" + # at beginning of line + possible indentation
  30. r"(?P<CASE_SOFTKW2>case)\b" +
  31. r"(?![ \t]*(?:" + "|".join([ # not followed by ...
  32. r"_\b", # a lone underscore
  33. r"[:,;=^&|@~)\]}]", # a character which means it can't be a
  34. # pattern-matching case
  35. r"\b(?:" + r"|".join(keyword.kwlist) + r")\b", # a keyword
  36. ]) +
  37. r"))"
  38. )
  39. builtinlist = [str(name) for name in dir(builtins)
  40. if not name.startswith('_') and
  41. name not in keyword.kwlist]
  42. builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b"
  43. comment = any("COMMENT", [r"#[^\n]*"])
  44. stringprefix = r"(?i:r|u|f|fr|rf|b|br|rb)?"
  45. sqstring = stringprefix + r"'[^'\\\n]*(\\.[^'\\\n]*)*'?"
  46. dqstring = stringprefix + r'"[^"\\\n]*(\\.[^"\\\n]*)*"?'
  47. sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
  48. dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
  49. string = any("STRING", [sq3string, dq3string, sqstring, dqstring])
  50. prog = re.compile("|".join([
  51. builtin, comment, string, kw,
  52. match_softkw, case_default,
  53. case_softkw_and_pattern,
  54. any("SYNC", [r"\n"]),
  55. ]),
  56. re.DOTALL | re.MULTILINE)
  57. return prog
  58. prog = make_pat()
  59. idprog = re.compile(r"\s+(\w+)")
  60. prog_group_name_to_tag = {
  61. "MATCH_SOFTKW": "KEYWORD",
  62. "CASE_SOFTKW": "KEYWORD",
  63. "CASE_DEFAULT_UNDERSCORE": "KEYWORD",
  64. "CASE_SOFTKW2": "KEYWORD",
  65. }
  66. def matched_named_groups(re_match):
  67. "Get only the non-empty named groups from an re.Match object."
  68. return ((k, v) for (k, v) in re_match.groupdict().items() if v)
  69. def color_config(text):
  70. """Set color options of Text widget.
  71. If ColorDelegator is used, this should be called first.
  72. """
  73. # Called from htest, TextFrame, Editor, and Turtledemo.
  74. # Not automatic because ColorDelegator does not know 'text'.
  75. theme = idleConf.CurrentTheme()
  76. normal_colors = idleConf.GetHighlight(theme, 'normal')
  77. cursor_color = idleConf.GetHighlight(theme, 'cursor')['foreground']
  78. select_colors = idleConf.GetHighlight(theme, 'hilite')
  79. text.config(
  80. foreground=normal_colors['foreground'],
  81. background=normal_colors['background'],
  82. insertbackground=cursor_color,
  83. selectforeground=select_colors['foreground'],
  84. selectbackground=select_colors['background'],
  85. inactiveselectbackground=select_colors['background'], # new in 8.5
  86. )
  87. class ColorDelegator(Delegator):
  88. """Delegator for syntax highlighting (text coloring).
  89. Instance variables:
  90. delegate: Delegator below this one in the stack, meaning the
  91. one this one delegates to.
  92. Used to track state:
  93. after_id: Identifier for scheduled after event, which is a
  94. timer for colorizing the text.
  95. allow_colorizing: Boolean toggle for applying colorizing.
  96. colorizing: Boolean flag when colorizing is in process.
  97. stop_colorizing: Boolean flag to end an active colorizing
  98. process.
  99. """
  100. def __init__(self):
  101. Delegator.__init__(self)
  102. self.init_state()
  103. self.prog = prog
  104. self.idprog = idprog
  105. self.LoadTagDefs()
  106. def init_state(self):
  107. "Initialize variables that track colorizing state."
  108. self.after_id = None
  109. self.allow_colorizing = True
  110. self.stop_colorizing = False
  111. self.colorizing = False
  112. def setdelegate(self, delegate):
  113. """Set the delegate for this instance.
  114. A delegate is an instance of a Delegator class and each
  115. delegate points to the next delegator in the stack. This
  116. allows multiple delegators to be chained together for a
  117. widget. The bottom delegate for a colorizer is a Text
  118. widget.
  119. If there is a delegate, also start the colorizing process.
  120. """
  121. if self.delegate is not None:
  122. self.unbind("<<toggle-auto-coloring>>")
  123. Delegator.setdelegate(self, delegate)
  124. if delegate is not None:
  125. self.config_colors()
  126. self.bind("<<toggle-auto-coloring>>", self.toggle_colorize_event)
  127. self.notify_range("1.0", "end")
  128. else:
  129. # No delegate - stop any colorizing.
  130. self.stop_colorizing = True
  131. self.allow_colorizing = False
  132. def config_colors(self):
  133. "Configure text widget tags with colors from tagdefs."
  134. for tag, cnf in self.tagdefs.items():
  135. self.tag_configure(tag, **cnf)
  136. self.tag_raise('sel')
  137. def LoadTagDefs(self):
  138. "Create dictionary of tag names to text colors."
  139. theme = idleConf.CurrentTheme()
  140. self.tagdefs = {
  141. "COMMENT": idleConf.GetHighlight(theme, "comment"),
  142. "KEYWORD": idleConf.GetHighlight(theme, "keyword"),
  143. "BUILTIN": idleConf.GetHighlight(theme, "builtin"),
  144. "STRING": idleConf.GetHighlight(theme, "string"),
  145. "DEFINITION": idleConf.GetHighlight(theme, "definition"),
  146. "SYNC": {'background': None, 'foreground': None},
  147. "TODO": {'background': None, 'foreground': None},
  148. "ERROR": idleConf.GetHighlight(theme, "error"),
  149. # "hit" is used by ReplaceDialog to mark matches. It shouldn't be changed by Colorizer, but
  150. # that currently isn't technically possible. This should be moved elsewhere in the future
  151. # when fixing the "hit" tag's visibility, or when the replace dialog is replaced with a
  152. # non-modal alternative.
  153. "hit": idleConf.GetHighlight(theme, "hit"),
  154. }
  155. if DEBUG: print('tagdefs', self.tagdefs)
  156. def insert(self, index, chars, tags=None):
  157. "Insert chars into widget at index and mark for colorizing."
  158. index = self.index(index)
  159. self.delegate.insert(index, chars, tags)
  160. self.notify_range(index, index + "+%dc" % len(chars))
  161. def delete(self, index1, index2=None):
  162. "Delete chars between indexes and mark for colorizing."
  163. index1 = self.index(index1)
  164. self.delegate.delete(index1, index2)
  165. self.notify_range(index1)
  166. def notify_range(self, index1, index2=None):
  167. "Mark text changes for processing and restart colorizing, if active."
  168. self.tag_add("TODO", index1, index2)
  169. if self.after_id:
  170. if DEBUG: print("colorizing already scheduled")
  171. return
  172. if self.colorizing:
  173. self.stop_colorizing = True
  174. if DEBUG: print("stop colorizing")
  175. if self.allow_colorizing:
  176. if DEBUG: print("schedule colorizing")
  177. self.after_id = self.after(1, self.recolorize)
  178. return
  179. def close(self):
  180. if self.after_id:
  181. after_id = self.after_id
  182. self.after_id = None
  183. if DEBUG: print("cancel scheduled recolorizer")
  184. self.after_cancel(after_id)
  185. self.allow_colorizing = False
  186. self.stop_colorizing = True
  187. def toggle_colorize_event(self, event=None):
  188. """Toggle colorizing on and off.
  189. When toggling off, if colorizing is scheduled or is in
  190. process, it will be cancelled and/or stopped.
  191. When toggling on, colorizing will be scheduled.
  192. """
  193. if self.after_id:
  194. after_id = self.after_id
  195. self.after_id = None
  196. if DEBUG: print("cancel scheduled recolorizer")
  197. self.after_cancel(after_id)
  198. if self.allow_colorizing and self.colorizing:
  199. if DEBUG: print("stop colorizing")
  200. self.stop_colorizing = True
  201. self.allow_colorizing = not self.allow_colorizing
  202. if self.allow_colorizing and not self.colorizing:
  203. self.after_id = self.after(1, self.recolorize)
  204. if DEBUG:
  205. print("auto colorizing turned",
  206. "on" if self.allow_colorizing else "off")
  207. return "break"
  208. def recolorize(self):
  209. """Timer event (every 1ms) to colorize text.
  210. Colorizing is only attempted when the text widget exists,
  211. when colorizing is toggled on, and when the colorizing
  212. process is not already running.
  213. After colorizing is complete, some cleanup is done to
  214. make sure that all the text has been colorized.
  215. """
  216. self.after_id = None
  217. if not self.delegate:
  218. if DEBUG: print("no delegate")
  219. return
  220. if not self.allow_colorizing:
  221. if DEBUG: print("auto colorizing is off")
  222. return
  223. if self.colorizing:
  224. if DEBUG: print("already colorizing")
  225. return
  226. try:
  227. self.stop_colorizing = False
  228. self.colorizing = True
  229. if DEBUG: print("colorizing...")
  230. t0 = time.perf_counter()
  231. self.recolorize_main()
  232. t1 = time.perf_counter()
  233. if DEBUG: print("%.3f seconds" % (t1-t0))
  234. finally:
  235. self.colorizing = False
  236. if self.allow_colorizing and self.tag_nextrange("TODO", "1.0"):
  237. if DEBUG: print("reschedule colorizing")
  238. self.after_id = self.after(1, self.recolorize)
  239. def recolorize_main(self):
  240. "Evaluate text and apply colorizing tags."
  241. next = "1.0"
  242. while todo_tag_range := self.tag_nextrange("TODO", next):
  243. self.tag_remove("SYNC", todo_tag_range[0], todo_tag_range[1])
  244. sync_tag_range = self.tag_prevrange("SYNC", todo_tag_range[0])
  245. head = sync_tag_range[1] if sync_tag_range else "1.0"
  246. chars = ""
  247. next = head
  248. lines_to_get = 1
  249. ok = False
  250. while not ok:
  251. mark = next
  252. next = self.index(mark + "+%d lines linestart" %
  253. lines_to_get)
  254. lines_to_get = min(lines_to_get * 2, 100)
  255. ok = "SYNC" in self.tag_names(next + "-1c")
  256. line = self.get(mark, next)
  257. ##print head, "get", mark, next, "->", repr(line)
  258. if not line:
  259. return
  260. for tag in self.tagdefs:
  261. self.tag_remove(tag, mark, next)
  262. chars += line
  263. self._add_tags_in_section(chars, head)
  264. if "SYNC" in self.tag_names(next + "-1c"):
  265. head = next
  266. chars = ""
  267. else:
  268. ok = False
  269. if not ok:
  270. # We're in an inconsistent state, and the call to
  271. # update may tell us to stop. It may also change
  272. # the correct value for "next" (since this is a
  273. # line.col string, not a true mark). So leave a
  274. # crumb telling the next invocation to resume here
  275. # in case update tells us to leave.
  276. self.tag_add("TODO", next)
  277. self.update()
  278. if self.stop_colorizing:
  279. if DEBUG: print("colorizing stopped")
  280. return
  281. def _add_tag(self, start, end, head, matched_group_name):
  282. """Add a tag to a given range in the text widget.
  283. This is a utility function, receiving the range as `start` and
  284. `end` positions, each of which is a number of characters
  285. relative to the given `head` index in the text widget.
  286. The tag to add is determined by `matched_group_name`, which is
  287. the name of a regular expression "named group" as matched by
  288. by the relevant highlighting regexps.
  289. """
  290. tag = prog_group_name_to_tag.get(matched_group_name,
  291. matched_group_name)
  292. self.tag_add(tag,
  293. f"{head}+{start:d}c",
  294. f"{head}+{end:d}c")
  295. def _add_tags_in_section(self, chars, head):
  296. """Parse and add highlighting tags to a given part of the text.
  297. `chars` is a string with the text to parse and to which
  298. highlighting is to be applied.
  299. `head` is the index in the text widget where the text is found.
  300. """
  301. for m in self.prog.finditer(chars):
  302. for name, matched_text in matched_named_groups(m):
  303. a, b = m.span(name)
  304. self._add_tag(a, b, head, name)
  305. if matched_text in ("def", "class"):
  306. if m1 := self.idprog.match(chars, b):
  307. a, b = m1.span(1)
  308. self._add_tag(a, b, head, "DEFINITION")
  309. def removecolors(self):
  310. "Remove all colorizing tags."
  311. for tag in self.tagdefs:
  312. self.tag_remove(tag, "1.0", "end")
  313. def _color_delegator(parent): # htest #
  314. from tkinter import Toplevel, Text
  315. from idlelib.idle_test.test_colorizer import source
  316. from idlelib.percolator import Percolator
  317. top = Toplevel(parent)
  318. top.title("Test ColorDelegator")
  319. x, y = map(int, parent.geometry().split('+')[1:])
  320. top.geometry("700x550+%d+%d" % (x + 20, y + 175))
  321. text = Text(top, background="white")
  322. text.pack(expand=1, fill="both")
  323. text.insert("insert", source)
  324. text.focus_set()
  325. color_config(text)
  326. p = Percolator(text)
  327. d = ColorDelegator()
  328. p.insertfilter(d)
  329. if __name__ == "__main__":
  330. from unittest import main
  331. main('idlelib.idle_test.test_colorizer', verbosity=2, exit=False)
  332. from idlelib.idle_test.htest import run
  333. run(_color_delegator)