From 43421b25b67d3ae02769742cf39aa03fe87100fc Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 23 Jan 2024 15:09:16 -0800 Subject: [PATCH] Update to use the bat theme for PowerShell syntax highlighting --- .../ColorCode.VT/VTSyntaxHighlighter.cs | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/shell/Markdown.VT/ColorCode.VT/VTSyntaxHighlighter.cs b/shell/Markdown.VT/ColorCode.VT/VTSyntaxHighlighter.cs index 4b1c504f..74450d8c 100644 --- a/shell/Markdown.VT/ColorCode.VT/VTSyntaxHighlighter.cs +++ b/shell/Markdown.VT/ColorCode.VT/VTSyntaxHighlighter.cs @@ -31,7 +31,7 @@ public class VTSyntaxHighlighter : CodeColorizerBase /// The Custom styles to Apply to the formatted Code. /// The language parser that the instance will use for its lifetime. public VTSyntaxHighlighter(StyleDictionary styles = null, ILanguageParser languageParser = null) - : base(styles, languageParser) + : base(styles.UseBatStyle(), languageParser) { _buffer = new StringBuilder(capacity: 512); @@ -200,6 +200,49 @@ private void WriteCapturedStyle(Scope scope) public static class ColorExtensionMethods { + /// + /// Use the style from 'bat' for PowerShell syntax highlighting. + /// + internal static StyleDictionary UseBatStyle(this StyleDictionary styles) + { + if (styles is null) + { + return null; + } + + styles.Remove(ScopeName.String); + styles.Remove(ScopeName.Comment); + styles.Remove(ScopeName.PowerShellOperator); + styles.Remove(ScopeName.PowerShellVariable); + + styles.Add( + new Style(ScopeName.String) + { + Foreground = "\x1b[38;5;186m", + ReferenceName = "string" + }); + styles.Add( + new Style(ScopeName.Comment) + { + Foreground = "\x1b[38;5;242m", + ReferenceName = "comment" + }); + styles.Add( + new Style(ScopeName.PowerShellOperator) + { + Foreground = "\x1b[38;5;203m", + ReferenceName = "powershellOperator" + }); + styles.Add( + new Style(ScopeName.PowerShellVariable) + { + Foreground = "\x1b[92m", + ReferenceName = "powershellVariable" + }); + + return styles; + } + public static string ToVTColor(this string color, bool isForeground = true) { if (color == null)