def test_theme_default_settings_from_no_scope(): theme = Theme.from_dct({ 'tokenColors': [ {'settings': {'foreground': '#cccccc', 'background': '#333333'}}, ], }) assert theme.default.fg == Color.parse('#cccccc') assert theme.default.bg == Color.parse('#333333')
def test_theme_scope_comma_at_beginning_and_end(): theme = Theme.from_dct({ 'colors': {'foreground': '#cccccc', 'background': '#333333'}, 'tokenColors': [ {'scope': '\n,a,b,\n', 'settings': {'fontStyle': 'italic'}}, ], }) assert theme.select(('d',)).i is False assert theme.select(('a',)).i is True assert theme.select(('b',)).i is True
def test_theme_scope_as_A_list(): theme = Theme.from_dct({ 'colors': {'foreground': '#cccccc', 'background': '#333333'}, 'tokenColors': [ {'scope': ['a', 'b', 'c'], 'settings': {'fontStyle': 'underline'}}, ], }) assert theme.select(('d',)).u is False assert theme.select(('a',)).u is True assert theme.select(('b',)).u is True assert theme.select(('c',)).u is True
def test_theme_scope_internal_newline_commas(): # this is arguably malformed, but `cobalt2` in the wild has this issue theme = Theme.from_dct({ 'colors': {'foreground': '#cccccc', 'background': '#333333'}, 'tokenColors': [ {'scope': '\n,a,\n,b,\n', 'settings': {'fontStyle': 'italic'}}, ], }) assert theme.select(('d',)).i is False assert theme.select(('a',)).i is True assert theme.select(('b',)).i is True
def test_theme_scope_split_by_commas(): theme = Theme.from_dct({ 'colors': {'foreground': '#cccccc', 'background': '#333333'}, 'tokenColors': [ {'scope': 'a, b, c', 'settings': {'fontStyle': 'italic'}}, ], }) assert theme.select(('d',)).i is False assert theme.select(('a',)).i is True assert theme.select(('b',)).i is True assert theme.select(('c',)).i is True
def _highlight_output(theme: Theme, compiler: Compiler, filename: str) -> int: state = compiler.root_state if theme.default.bg is not None: print('\x1b[48;2;{r};{g};{b}m'.format(**theme.default.bg._asdict())) with open(filename) as f: for line_idx, line in enumerate(f): first_line = line_idx == 0 state, regions = highlight_line(compiler, state, line, first_line) for start, end, scope in regions: print_styled(line[start:end], theme.select(scope)) print('\x1b[m', end='') return 0
def main() -> int: parser = argparse.ArgumentParser() parser.add_argument('--theme', default=DEFAULT_THEME) parser.add_argument('--syntax-dir', default=DEFAULT_SYNTAX_DIR) parser.add_argument('filename') args = parser.parse_args() with open(args.filename) as f: first_line = next(f, '') theme = Theme.from_filename(args.theme) grammars = Grammars.from_syntax_dir(args.syntax_dir) compiler = grammars.compiler_for_file(args.filename, first_line) return _highlight_output(theme, compiler, args.filename)
import pytest from highlight_demo.color import Color from highlight_demo.theme import Theme THEME = Theme.from_dct({ 'colors': {'foreground': '#100000', 'background': '#aaaaaa'}, 'tokenColors': [ {'scope': 'foo.bar', 'settings': {'foreground': '#200000'}}, {'scope': 'foo', 'settings': {'foreground': '#300000'}}, {'scope': 'parent foo.bar', 'settings': {'foreground': '#400000'}}, ], }) def unhex(color): return f'#{hex(color.r << 16 | color.g << 8 | color.b)[2:]}' @pytest.mark.parametrize( ('scope', 'expected'), ( pytest.param(('',), '#100000', id='trivial'), pytest.param(('unknown',), '#100000', id='unknown'), pytest.param(('foo.bar',), '#200000', id='exact match'), pytest.param(('foo.baz',), '#300000', id='prefix match'), pytest.param(('src.diff', 'foo.bar'), '#200000', id='nested scope'), pytest.param( ('foo.bar', 'unrelated'), '#200000', id='nested scope not last one', ),