8000 Reformat idlelib colorizer (GH-25479) · python/cpython@702a088 · GitHub
[go: up one dir, main page]

Skip to content

Commit 702a088

Browse files
E-Paineterryjreedy
andauthored
Reformat idlelib colorizer (GH-25479)
Also replace if-then and and-or with conditional expressions. Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
1 parent 14092b5 commit 702a088

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

Lib/idlelib/colorizer.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88

99
DEBUG = False
1010

11+
1112
def any(name, alternates):
1213
"Return a named group pattern matching list of alternates."
1314
return "(?P<%s>" % name + "|".join(alternates) + ")"
1415

16+
1517
def make_pat():
1618
kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b"
1719
builtinlist = [str(name) for name in dir(builtins)
18-
if not name.startswith('_') and \
19-
name not in keyword.kwlist]
20+
if not name.startswith('_') and
21+
name not in keyword.kwlist]
2022
builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b"
2123
comment = any("COMMENT", [r"#[^\n]*"])
2224
stringprefix = r"(?i:r|u|f|fr|rf|b|br|rb)?"
@@ -25,12 +27,14 @@ def make_pat():
2527
sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
2628
dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
2729
string = any("STRING", [sq3string, dq3string, sqstring, dqstring])
28-
return kw + "|" + builtin + "|" + comment + "|" + string +\
29-
"|" + any("SYNC", [r"\n"])
30+
return (kw + "|" + builtin + "|" + comment + "|" + string +
31+
"|" + any("SYNC", [r"\n"]))
32+
3033

3134
prog = re.compile(make_pat(), re.S)
3235
idprog = re.compile(r"\s+(\w+)", re.S)
3336

37+
3438
def color_config(text):
3539
"""Set color options of Text widget.
3640
@@ -49,7 +53,7 @@ def color_config(text):
4953
selectforeground=select_colors['foreground'],
5054
selectbackground=select_colors['background'],
5155
inactiveselectbackground=select_colors['background'], # new in 8.5
52-
)
56+
)
5357

5458

5559
class ColorDelegator(Delegator):
@@ -120,14 +124,17 @@ def LoadTagDefs(self):
120124
"BUILTIN": idleConf.GetHighlight(theme, "builtin"),
121125
"STRING": idleConf.GetHighlight(theme, "string"),
122126
"DEFINITION": idleConf.GetHighlight(theme, "definition"),
123-
"SYNC": {'background':None,'foreground':None},
124-
"TODO": {'background':None,'foreground':None},
127+
"SYNC": {'background': None, 'foreground': None},
128+
"TODO": {'background': None, 'foreground': None},
125129
"ERROR": idleConf.GetHighlight(theme, "error"),
126-
# The following is used by ReplaceDialog:
130+
# "hit" is used by ReplaceDialog to mark matches. It shouldn't be changed by Colorizer, but
131+
# that currently isn't technically possible. This should be moved elsewhere in the future
132+
# when fixing the "hit" tag's visibility, or when the replace dialog is replaced with a
133+
# non-modal alternative.
127134
"hit": idleConf.GetHighlight(theme, "hit"),
128135
}
129136

130-
if DEBUG: print('tagdefs',self.tagdefs)
137+
if DEBUG: print('tagdefs', self.tagdefs)
131138

132139
def insert(self, index, chars, tags=None):
133140
"Insert chars into widget at index and mark for colorizing."
@@ -184,8 +191,8 @@ def toggle_colorize_event(self, event=None):
184191
if self.allow_colorizing and not self.colorizing:
185192
self.after_id = self.after(1, self.recolorize)
186193
if DEBUG:
187-
print("auto colorizing turned",\
188-
self.allow_colorizing and "on" or "off")
194+
print("auto colorizing turned",
195+
"on" if self.allow_colorizing else "off")
189196
return "break"
190197

191198
def recolorize(self):
@@ -232,10 +239,7 @@ def recolorize_main(self):
232239
head, tail = item
233240
self.tag_remove("SYNC", head, tail)
234241
item = self.tag_prevrange("SYNC", head)
235-
if item:
236-
head = item[1]
237-
else:
238-
head = "1.0"
242+
head = item[1] if item else "1.0"
239243

240244
chars = ""
241245
next = head
@@ -307,7 +311,7 @@ def _color_delegator(parent): # htest #
307311
"elif False: print(0)\n"
308312
"else: float(None)\n"
309313
"if iF + If + IF: 'keyword matching must respect case'\n"
310-
"if'': x or'' # valid string-keyword no-space combinations\n"
314+
"if'': x or'' # valid keyword-string no-space combinations\n"
311315
"async def f(): await g()\n"
312316
"# All valid prefixes for unicode and byte strings should be colored.\n"
313317
"'x', '''x''', \"x\", \"\"\"x\"\"\"\n"

0 commit comments

Comments
 (0)
0