PTK specific rules should not break pygments, more _graceful_ error handling

This commit is contained in:
Gyuri Horak 2020-10-27 15:02:06 +01:00
parent 68504b6ec5
commit 3395ba7334
Failed to generate hash of commit
2 changed files with 22 additions and 9 deletions

View file

@ -251,23 +251,22 @@ class PromptToolkitShell(BaseShell):
)
self._overrides_deprecation_warning_shown = True
style_overrides_env.update(env.get("XONSH_STYLE_OVERRIDES", {}))
if HAS_PYGMENTS:
prompt_args["lexer"] = PygmentsLexer(pyghooks.XonshLexer)
self.styler.override(style_overrides_env)
style = _style_from_pygments_cls(
pyghooks.xonsh_style_proxy(self.styler)
)
else:
style = _style_from_pygments_dict(DEFAULT_STYLE_DICT)
if style_overrides_env:
try:
style = merge_styles(
[
_style_from_pygments_dict(DEFAULT_STYLE_DICT),
_style_from_pygments_dict(style_overrides_env),
]
[style, _style_from_pygments_dict(style_overrides_env),]
)
except (AttributeError, TypeError, ValueError):
print_exception()
style = _style_from_pygments_dict(DEFAULT_STYLE_DICT)
except (AttributeError, TypeError, ValueError) as e:
print_warning(f"Error applying style override!\n{e}\n")
prompt_args["style"] = style
events.on_timingprobe.fire(name="on_post_prompt_style")

View file

@ -71,6 +71,7 @@ from xonsh.events import events
#
Color = Token.Color # alias to new color token namespace
PTK_SPECIFIC_VALUES = ["reverse", "noreverse", "hidden", "nohidden", "blink", "noblink"]
def color_by_name(name, fg=None, bg=None):
@ -446,6 +447,15 @@ def xonsh_style_proxy(styler):
return XonshStyleProxy
def _ptk_specific_style_value(style_value):
"""Checks if the given value is PTK style specific"""
for ptk_spec in PTK_SPECIFIC_VALUES:
if ptk_spec in style_value:
return True
return False
def _format_ptk_style_name(name):
"""Format PTK style name to be able to include it in a pygments style"""
parts = name.split("-")
@ -480,7 +490,11 @@ def _get_token_by_name(name):
def _tokenize_style_dict(styles):
"""Converts possible string keys in style dicts to Tokens"""
return {_get_token_by_name(token): value for token, value in styles.items()}
return {
_get_token_by_name(token): value
for token, value in styles.items()
if not _ptk_specific_style_value(value)
}
def register_custom_pygments_style(