Short color token name support for register_custom_style() (#4339) (#4342)

This commit is contained in:
Gyuri Horak 2021-07-24 17:37:51 +02:00 committed by GitHub
parent 09fcab65d2
commit 98d4d2a184
Failed to generate hash of commit
3 changed files with 37 additions and 6 deletions

View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* Short color token names can be used in ``register_custom_style()`` (#4339)
**Security:**
* <news item>

View file

@ -365,6 +365,11 @@ def test_colorize_file_ca(xs_LS_COLORS, monkeypatch):
{"completion-menu.completion.current": "#00ff00"}, {"completion-menu.completion.current": "#00ff00"},
{Token.PTK.CompletionMenu.Completion.Current: "#00ff00"}, {Token.PTK.CompletionMenu.Completion.Current: "#00ff00"},
), # ptk style ), # ptk style
(
"test6",
{"RED": "#ff0000"},
{Token.Color.RED: "#ff0000"},
), # short color name
], ],
) )
def test_register_custom_pygments_style(name, styles, refrules): def test_register_custom_pygments_style(name, styles, refrules):

View file

@ -245,18 +245,21 @@ def color_token_by_name(xc: tuple, styles=None) -> _TokenType:
try: try:
styles = XSH.shell.shell.styler.styles # type:ignore styles = XSH.shell.shell.styler.styles # type:ignore
except AttributeError: except AttributeError:
return Color pass
tokName = xc[0] tokName = xc[0]
pc = color_name_to_pygments_code(xc[0], styles) if styles:
pc = color_name_to_pygments_code(xc[0], styles)
if len(xc) > 1: if len(xc) > 1:
pc += " " + color_name_to_pygments_code(xc[1], styles) pc += " " + color_name_to_pygments_code(xc[1], styles)
tokName += "__" + xc[1] tokName += "__" + xc[1]
token = getattr(Color, norm_name(tokName)) token = getattr(Color, norm_name(tokName))
if token not in styles or not styles[token]:
if styles and (token not in styles or not styles[token]):
styles[token] = pc styles[token] = pc
return token return token