From 1a5ddad787d6298c1d2fbe540641a7b6a22ddfcd Mon Sep 17 00:00:00 2001 From: "E. Paine" <63801254+E-Paine@users.noreply.github.com> Date: Tue, 20 Apr 2021 10:43:31 +0100 Subject: [PATCH 1/3] Reformat idlelib colorizer --- Lib/idlelib/colorizer.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Lib/idlelib/colorizer.py b/Lib/idlelib/colorizer.py index db1266fed3b691..17044feb66691e 100644 --- a/Lib/idlelib/colorizer.py +++ b/Lib/idlelib/colorizer.py @@ -8,15 +8,17 @@ DEBUG = False + def any(name, alternates): "Return a named group pattern matching list of alternates." return "(?P<%s>" % name + "|".join(alternates) + ")" + def make_pat(): kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b" builtinlist = [str(name) for name in dir(builtins) - if not name.startswith('_') and \ - name not in keyword.kwlist] + if not name.startswith('_') and \ + name not in keyword.kwlist] builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b" comment = any("COMMENT", [r"#[^\n]*"]) stringprefix = r"(?i:r|u|f|fr|rf|b|br|rb)?" @@ -25,12 +27,14 @@ def make_pat(): sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?" dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?' string = any("STRING", [sq3string, dq3string, sqstring, dqstring]) - return kw + "|" + builtin + "|" + comment + "|" + string +\ + return kw + "|" + builtin + "|" + comment + "|" + string + \ "|" + any("SYNC", [r"\n"]) + prog = re.compile(make_pat(), re.S) idprog = re.compile(r"\s+(\w+)", re.S) + def color_config(text): """Set color options of Text widget. @@ -120,14 +124,17 @@ def LoadTagDefs(self): "BUILTIN": idleConf.GetHighlight(theme, "builtin"), "STRING": idleConf.GetHighlight(theme, "string"), "DEFINITION": idleConf.GetHighlight(theme, "definition"), - "SYNC": {'background':None,'foreground':None}, - "TODO": {'background':None,'foreground':None}, + "SYNC": {'background': None, 'foreground': None}, + "TODO": {'background': None, 'foreground': None}, "ERROR": idleConf.GetHighlight(theme, "error"), - # The following is used by ReplaceDialog: + # "hit" is used by ReplaceDialog to mark matches. It shouldn't be changed by Colorizer, but + # that currently isn't technically possible. This should be moved elsewhere in the future + # when fixing the "hit" tag's visibility, or when the replace dialog is replaced with a + # non-modal alternative. "hit": idleConf.GetHighlight(theme, "hit"), - } + } - if DEBUG: print('tagdefs',self.tagdefs) + if DEBUG: print('tagdefs', self.tagdefs) def insert(self, index, chars, tags=None): "Insert chars into widget at index and mark for colorizing." @@ -184,7 +191,7 @@ def toggle_colorize_event(self, event=None): if self.allow_colorizing and not self.colorizing: self.after_id = self.after(1, self.recolorize) if DEBUG: - print("auto colorizing turned",\ + print("auto colorizing turned", \ self.allow_colorizing and "on" or "off") return "break" @@ -215,7 +222,7 @@ def recolorize(self): t0 = time.perf_counter() self.recolorize_main() t1 = time.perf_counter() - if DEBUG: print("%.3f seconds" % (t1-t0)) + if DEBUG: print("%.3f seconds" % (t1 - t0)) finally: self.colorizing = False if self.allow_colorizing and self.tag_nextrange("TODO", "1.0"): @@ -316,7 +323,7 @@ def _color_delegator(parent): # htest # "b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x', rB'x',Rb'x',RB'x'\n" "# Invalid combinations of legal characters should be half colored.\n" "ur'x', ru'x', uf'x', fu'x', UR'x', ufr'x', rfu'x', xf'x', fx'x'\n" - ) + ) text = Text(top, background="white") text.pack(expand=1, fill="both") text.insert("insert", source) From 7cba82fe8dc1be902977b4fa712573bd9b0127ba Mon Sep 17 00:00:00 2001 From: "E. Paine" <63801254+E-Paine@users.noreply.github.com> Date: Tue, 20 Apr 2021 17:23:21 +0100 Subject: [PATCH 2/3] Remove redundant backslash --- Lib/idlelib/colorizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/idlelib/colorizer.py b/Lib/idlelib/colorizer.py index 17044feb66691e..7ac0451e1952ef 100644 --- a/Lib/idlelib/colorizer.py +++ b/Lib/idlelib/colorizer.py @@ -191,7 +191,7 @@ def toggle_colorize_event(self, event=None): if self.allow_colorizing and not self.colorizing: self.after_id = self.after(1, self.recolorize) if DEBUG: - print("auto colorizing turned", \ + print("auto colorizing turned", self.allow_colorizing and "on" or "off") return "break" From 39c2f0c46d3f53b67addee75cf6ae54ed8fee266 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Thu, 22 Apr 2021 18:52:17 -0400 Subject: [PATCH 3/3] Tweak format, add 2 conditional expressions. --- Lib/idlelib/colorizer.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Lib/idlelib/colorizer.py b/Lib/idlelib/colorizer.py index 7ac0451e1952ef..0aae1778a580c0 100644 --- a/Lib/idlelib/colorizer.py +++ b/Lib/idlelib/colorizer.py @@ -17,7 +17,7 @@ def any(name, alternates): def make_pat(): kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b" builtinlist = [str(name) for name in dir(builtins) - if not name.startswith('_') and \ + if not name.startswith('_') and name not in keyword.kwlist] builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b" comment = any("COMMENT", [r"#[^\n]*"]) @@ -27,8 +27,8 @@ def make_pat(): sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?" dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?' string = any("STRING", [sq3string, dq3string, sqstring, dqstring]) - return kw + "|" + builtin + "|" + comment + "|" + string + \ - "|" + any("SYNC", [r"\n"]) + return (kw + "|" + builtin + "|" + comment + "|" + string + + "|" + any("SYNC", [r"\n"])) prog = re.compile(make_pat(), re.S) @@ -53,7 +53,7 @@ def color_config(text): selectforeground=select_colors['foreground'], selectbackground=select_colors['background'], inactiveselectbackground=select_colors['background'], # new in 8.5 - ) + ) class ColorDelegator(Delegator): @@ -132,7 +132,7 @@ def LoadTagDefs(self): # when fixing the "hit" tag's visibility, or when the replace dialog is replaced with a # non-modal alternative. "hit": idleConf.GetHighlight(theme, "hit"), - } + } if DEBUG: print('tagdefs', self.tagdefs) @@ -192,7 +192,7 @@ def toggle_colorize_event(self, event=None): self.after_id = self.after(1, self.recolorize) if DEBUG: print("auto colorizing turned", - self.allow_colorizing and "on" or "off") + "on" if self.allow_colorizing else "off") return "break" def recolorize(self): @@ -222,7 +222,7 @@ def recolorize(self): t0 = time.perf_counter() self.recolorize_main() t1 = time.perf_counter() - if DEBUG: print("%.3f seconds" % (t1 - t0)) + if DEBUG: print("%.3f seconds" % (t1-t0)) finally: self.colorizing = False if self.allow_colorizing and self.tag_nextrange("TODO", "1.0"): @@ -239,10 +239,7 @@ def recolorize_main(self): head, tail = item self.tag_remove("SYNC", head, tail) item = self.tag_prevrange("SYNC", head) - if item: - head = item[1] - else: - head = "1.0" + head = item[1] if item else "1.0" chars = "" next = head @@ -314,7 +311,7 @@ def _color_delegator(parent): # htest # "elif False: print(0)\n" "else: float(None)\n" "if iF + If + IF: 'keyword matching must respect case'\n" - "if'': x or'' # valid string-keyword no-space combinations\n" + "if'': x or'' # valid keyword-string no-space combinations\n" "async def f(): await g()\n" "# All valid prefixes for unicode and byte strings should be colored.\n" "'x', '''x''', \"x\", \"\"\"x\"\"\"\n" @@ -323,7 +320,7 @@ def _color_delegator(parent): # htest # "b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x', rB'x',Rb'x',RB'x'\n" "# Invalid combinations of legal characters should be half colored.\n" "ur'x', ru'x', uf'x', fu'x', UR'x', ufr'x', rfu'x', xf'x', fx'x'\n" - ) + ) text = Text(top, background="white") text.pack(expand=1, fill="both") text.insert("insert", source)