1+ from __future__ import annotations
12import builtins
23import functools
34import keyword
45import re
56import token as T
67import tokenize
78import unicodedata
9+ import _colorize
810
911from collections import deque
1012from io import StringIO
1113from tokenize import TokenInfo as TI
12- from typing import Iterable , Iterator , Literal , Match , NamedTuple , Self
13- from _colorize import ANSIColors
14+ from typing import Iterable , Iterator , Match , NamedTuple , Self
1415
1516from .types import CharBuffer , CharWidths
1617from .trace import trace
2122IDENTIFIERS_AFTER = {"def" , "class" }
2223BUILTINS = {str (name ) for name in dir (builtins ) if not name .startswith ('_' )}
2324
24- type ColorTag = (
25- Literal ["KEYWORD" ]
26- | Literal ["BUILTIN" ]
27- | Literal ["COMMENT" ]
28- | Literal ["STRING" ]
29- | Literal ["NUMBER" ]
30- | Literal ["OP" ]
31- | Literal ["DEFINITION" ]
32- | Literal ["SOFT_KEYWORD" ]
33- | Literal ["SYNC" ]
34- )
35-
3625
3726class Span (NamedTuple ):
3827 """Span indexing that's inclusive on both ends."""
@@ -55,20 +44,7 @@ def from_token(cls, token: TI, line_len: list[int]) -> Self:
5544
5645class ColorSpan (NamedTuple ):
5746 span : Span
58- tag : ColorTag
59-
60-
61- TAG_TO_ANSI : dict [ColorTag , str ] = {
62- "KEYWORD" : ANSIColors .BOLD_BLUE ,
63- "BUILTIN" : ANSIColors .CYAN ,
64- "COMMENT" : ANSIColors .RED ,
65- "STRING" : ANSIColors .GREEN ,
66- "NUMBER" : ANSIColors .YELLOW ,
67- "OP" : ANSIColors .RESET ,
68- "DEFINITION" : ANSIColors .BOLD_WHITE ,
69- "SOFT_KEYWORD" : ANSIColors .BOLD_BLUE ,
70- "SYNC" : ANSIColors .RESET ,
71- }
47+ tag : _colorize .ColorTag
7248
7349
7450@functools .cache
@@ -305,11 +281,11 @@ def disp_str(
305281 post_color = ""
306282 if colors and colors [0 ].span .start < start_index :
307283 # looks like we're continuing a previous color (e.g. a multiline str)
308- pre_color = TAG_TO_ANSI [colors [0 ].tag ]
284+ pre_color = _colorize . theme [colors [0 ].tag ]
309285
310286 for i , c in enumerate (buffer , start_index ):
311287 if colors and colors [0 ].span .start == i : # new color starts now
312- pre_color = TAG_TO_ANSI [colors [0 ].tag ]
288+ pre_color = _colorize . theme [colors [0 ].tag ]
313289
314290 if c == "\x1a " : # CTRL-Z on Windows
315291 chars .append (c )
@@ -326,7 +302,7 @@ def disp_str(
326302 char_widths .append (str_width (c ))
327303
328304 if colors and colors [0 ].span .end == i : # current color ends now
329- post_color = TAG_TO_ANSI [ "SYNC " ]
305+ post_color = _colorize . theme [ "RESET " ]
330306 colors .pop (0 )
331307
332308 chars [- 1 ] = pre_color + chars [- 1 ] + post_color
@@ -336,7 +312,7 @@ def disp_str(
336312 if colors and colors [0 ].span .start < i and colors [0 ].span .end > i :
337313 # even though the current color should be continued, reset it for now.
338314 # the next call to `disp_str()` will revive it.
339- chars [- 1 ] += TAG_TO_ANSI [ "SYNC " ]
315+ chars [- 1 ] += _colorize . theme [ "RESET " ]
340316
341317 # trace("disp_str({buffer}) = {s}, {b}", buffer=repr(buffer), s=chars, b=char_widths)
342318 return chars , char_widths
0 commit comments