Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Lib/idlelib/colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def make_pat():
]) +
r"))"
)
type_softkw = (
r"^[ \t]*" + # at beginning of line + possible indentation
r"(?P<TYPE_SOFTKW>type)" +
r"(?=[ \t]+(?!(?:" + "|".join(keyword.kwlist) + r")\b)[^\W\d])"
)
builtinlist = [str(name) for name in dir(builtins)
if not name.startswith('_') and
name not in keyword.kwlist]
Expand All @@ -54,7 +59,7 @@ def make_pat():
dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
string = any("STRING", [sq3string, dq3string, sqstring, dqstring])
prog = re.compile("|".join([
builtin, comment, string, kw,
type_softkw, builtin, comment, string, kw,
match_softkw, case_default,
case_softkw_and_pattern,
any("SYNC", [r"\n"]),
Expand All @@ -70,6 +75,7 @@ def make_pat():
"CASE_SOFTKW": "KEYWORD",
"CASE_DEFAULT_UNDERSCORE": "KEYWORD",
"CASE_SOFTKW2": "KEYWORD",
"TYPE_SOFTKW": "KEYWORD",
}


Expand Down
15 changes: 15 additions & 0 deletions Lib/idlelib/idle_test/test_colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,21 @@ def test_def_statement(self):
# def followed by non-keyword
self._assert_highlighting('def ++', {'KEYWORD': [('1.0', '1.3')]})

def test_type_soft_keyword(self):
# type as a soft keyword
self._assert_highlighting('type X = int',
{'KEYWORD': [('1.0', '1.4')],
'BUILTIN': [('1.9', '1.12')]})
# type as a name
self._assert_highlighting('type = type(1)',
{'BUILTIN': [('1.0', '1.4'), ('1.7', '1.11')]})
self._assert_highlighting('type(x)',
{'BUILTIN': [('1.0', '1.4')]})
self._assert_highlighting('type in (int, str)',
{'KEYWORD': [('1.5', '1.7')],
'BUILTIN': [('1.0', '1.4'), ('1.9', '1.12'),
('1.14', '1.17')]})

def test_match_soft_keyword(self):
# empty match
self._assert_highlighting('match', {'KEYWORD': [('1.0', '1.5')]})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Colorize the soft keyword ``type`` in IDLE.
Loading