diff --git a/.gitignore b/.gitignore index 48df0e669..830b2c936 100644 --- a/.gitignore +++ b/.gitignore @@ -83,3 +83,4 @@ venv/ # mypy .dmypy.json +.mypy_cache diff --git a/xonsh/ptk_shell/shell.py b/xonsh/ptk_shell/shell.py index 3ec9f3316..90409613c 100644 --- a/xonsh/ptk_shell/shell.py +++ b/xonsh/ptk_shell/shell.py @@ -40,6 +40,7 @@ from prompt_toolkit.styles.pygments import ( ANSI_OSC_PATTERN = re.compile("\x1b].*?\007") +CAPITAL_PATTERN = re.compile(r"([a-z])([A-Z])") Token = _TokenType() events.transmogrify("on_ptk_create", "LoadEvent") @@ -92,6 +93,18 @@ def remove_ansi_osc(prompt): return prompt, osc_tokens +def _extract_ptk_style(pygments_style): + """Extract PTK specific rules that are not handled by ``style_from_pygments_cls`` + """ + rules = {} + for rule, value in pygments_style.styles.items(): + if str(rule).startswith("Token.PTK"): + key = CAPITAL_PATTERN.sub(r"\1-\2", str(rule)[10:]).lower() + rules[key] = value + + return rules + + class PromptToolkitShell(BaseShell): """The xonsh shell for prompt_toolkit v2 and later.""" @@ -201,7 +214,14 @@ class PromptToolkitShell(BaseShell): if env.get("COLOR_INPUT"): if HAS_PYGMENTS: prompt_args["lexer"] = PygmentsLexer(pyghooks.XonshLexer) - style = style_from_pygments_cls(pyghooks.xonsh_style_proxy(self.styler)) + style = merge_styles( + [ + Style.from_dict(_extract_ptk_style(self.styler)), + style_from_pygments_cls( + pyghooks.xonsh_style_proxy(self.styler) + ), + ] + ) else: style = style_from_pygments_dict(DEFAULT_STYLE_DICT) diff --git a/xonsh/pyghooks.py b/xonsh/pyghooks.py index 630c10aa9..6adbe08d7 100644 --- a/xonsh/pyghooks.py +++ b/xonsh/pyghooks.py @@ -382,8 +382,8 @@ class XonshStyle(Style): self.background_color = style_obj.background_color except (ImportError, pygments.util.ClassNotFound): self._smap = XONSH_BASE_STYLE.copy() - compound = CompoundColorMap(ChainMap(self.trap, cmap, PTK_STYLE, self._smap)) - self.styles = ChainMap(self.trap, cmap, PTK_STYLE, self._smap, compound) + compound = CompoundColorMap(ChainMap(self.trap, cmap, self._smap, PTK_STYLE)) + self.styles = ChainMap(self.trap, cmap, self._smap, PTK_STYLE, compound) self._style_name = value for file_type, xonsh_color in builtins.__xonsh__.env.get(