From 69b92f8c3c0a2687abc708de9f597c8e8f338aaa Mon Sep 17 00:00:00 2001 From: Natestah Date: Sat, 22 Jun 2024 08:11:25 -0700 Subject: [PATCH 1/2] Expose "colors" dictionary My ultimate plan with this in Blitz Search is to provide "easy" theme selection. But I do think that AvaloniaEdit.TextMate should use the editor background to fix what I see as a bug in the AvaloniaEdit demonstration, where some themes don't look like they were meant for a dark background. Having things like selection highlights and all that come from a place that's already authored will ease up a lot of UI work for me ( color pickers and what-not ). --- src/TextMateSharp.Demo/Program.cs | 10 ++++ src/TextMateSharp/Internal/Themes/ThemeRaw.cs | 11 +++++ src/TextMateSharp/Themes/IRawTheme.cs | 1 + src/TextMateSharp/Themes/Theme.cs | 48 ++++++++++++++++--- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/TextMateSharp.Demo/Program.cs b/src/TextMateSharp.Demo/Program.cs index 8967132..eed263a 100644 --- a/src/TextMateSharp.Demo/Program.cs +++ b/src/TextMateSharp.Demo/Program.cs @@ -94,6 +94,16 @@ static void Main(string[] args) } } + var colorDictionary = theme.GetGuiColorDictionary(); + if (colorDictionary is { Count: > 0 }) + { + Console.WriteLine("Gui Control Colors"); + foreach (var kvp in colorDictionary) + { + Console.WriteLine( $" {kvp.Key}, {kvp.Value}"); + } + } + Console.WriteLine("File {0} tokenized in {1}ms.", Path.GetFileName(fileToParse), Environment.TickCount - tokenizeIni); diff --git a/src/TextMateSharp/Internal/Themes/ThemeRaw.cs b/src/TextMateSharp/Internal/Themes/ThemeRaw.cs index 4903012..d819914 100644 --- a/src/TextMateSharp/Internal/Themes/ThemeRaw.cs +++ b/src/TextMateSharp/Internal/Themes/ThemeRaw.cs @@ -11,6 +11,7 @@ public class ThemeRaw : Dictionary, IRawTheme, IRawThemeSetting, private static string NAME = "name"; private static string INCLUDE = "include"; private static string SETTINGS = "settings"; + private static string COLORS = "colors"; private static string TOKEN_COLORS = "tokenColors"; private static string SCOPE = "scope"; private static string FONT_STYLE = "fontStyle"; @@ -47,6 +48,16 @@ public ICollection GetTokenColors() return result.Cast().ToList(); } + public ICollection> GetGuiColors() + { + ICollection result = TryGetObject(COLORS); + + if (result == null) + return null; + + return result.Cast>().ToList(); + } + public object GetScope() { return TryGetObject(SCOPE); diff --git a/src/TextMateSharp/Themes/IRawTheme.cs b/src/TextMateSharp/Themes/IRawTheme.cs index bc0b8bf..5f2a4ce 100644 --- a/src/TextMateSharp/Themes/IRawTheme.cs +++ b/src/TextMateSharp/Themes/IRawTheme.cs @@ -8,5 +8,6 @@ public interface IRawTheme string GetInclude(); ICollection GetSettings(); ICollection GetTokenColors(); + ICollection> GetGuiColors(); } } \ No newline at end of file diff --git a/src/TextMateSharp/Themes/Theme.cs b/src/TextMateSharp/Themes/Theme.cs index 4b6eba7..1fbaee9 100644 --- a/src/TextMateSharp/Themes/Theme.cs +++ b/src/TextMateSharp/Themes/Theme.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using TextMateSharp.Internal.Utils; using TextMateSharp.Registry; @@ -11,29 +12,40 @@ public class Theme private ParsedTheme _theme; private ParsedTheme _include; private ColorMap _colorMap; + private Dictionary _guiColorDictionary; public static Theme CreateFromRawTheme( IRawTheme source, IRegistryOptions registryOptions) { ColorMap colorMap = new ColorMap(); + var guiColorsDictionary = new Dictionary(); + var themeRuleList = ParsedTheme.ParseTheme(source,0); + ParsedTheme theme = ParsedTheme.CreateFromParsedTheme( - ParsedTheme.ParseTheme(source, 0), + themeRuleList, colorMap); + IRawTheme themeInclude; ParsedTheme include = ParsedTheme.CreateFromParsedTheme( - ParsedTheme.ParseInclude(source, registryOptions, 0), + ParsedTheme.ParseInclude(source, registryOptions, 0, out themeInclude), colorMap); - return new Theme(colorMap, theme, include); + // First get colors from include, then try and overwrite with local colors.. + // I don't see this happening currently, but here just in case that ever happens. + ParsedTheme.ParsedGuiColors(themeInclude, guiColorsDictionary); + ParsedTheme.ParsedGuiColors(source, guiColorsDictionary); + + return new Theme(colorMap, theme, include, guiColorsDictionary); } - Theme(ColorMap colorMap, ParsedTheme theme, ParsedTheme include) + Theme(ColorMap colorMap, ParsedTheme theme, ParsedTheme include, Dictionary guiColorDictionary) { _colorMap = colorMap; _theme = theme; _include = include; + _guiColorDictionary = guiColorDictionary; } public List Match(IList scopeNames) @@ -49,6 +61,11 @@ public List Match(IList scopeNames) return result; } + public ReadOnlyDictionary GetGuiColorDictionary() + { + return new ReadOnlyDictionary(this._guiColorDictionary); + } + public ICollection GetColorMap() { return this._colorMap.GetColorMap(); @@ -92,19 +109,37 @@ internal static List ParseTheme(IRawTheme source, int priority) return result; } + internal static void ParsedGuiColors(IRawTheme source, Dictionary colorDictionary) + { + var colors = source.GetGuiColors(); + if (colors == null) + { + return; + } + foreach (var kvp in colors) + { + colorDictionary[kvp.Key] = (string)kvp.Value; + } + } + + internal static List ParseInclude( IRawTheme source, IRegistryOptions registryOptions, - int priority) + int priority, + out IRawTheme themeInclude) { List result = new List(); string include = source.GetInclude(); if (string.IsNullOrEmpty(include)) + { + themeInclude = null; return result; + } - IRawTheme themeInclude = registryOptions.GetTheme(include); + themeInclude = registryOptions.GetTheme(include); if (themeInclude == null) return result; @@ -270,7 +305,6 @@ static ParsedTheme ResolveParsedThemeRules( root.Insert(rule.name, 0, rule.scope, rule.parentScopes, rule.fontStyle, colorMap.GetId(rule.foreground), colorMap.GetId(rule.background)); } - return new ParsedTheme(defaults, root); } From 91cc5e6c9c13f71815c40b87d6c3b4e7ee9fd9c5 Mon Sep 17 00:00:00 2001 From: Natestah Date: Sat, 22 Jun 2024 09:26:25 -0700 Subject: [PATCH 2/2] some minor updates to theme -Light High Contrast - bases with VS.json -Update to handle null includes --- src/TextMateSharp.Grammars/Resources/Themes/hc_light.json | 1 + src/TextMateSharp.Grammars/Resources/Themes/light_vs.json | 2 +- src/TextMateSharp/Themes/Theme.cs | 5 ++++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/TextMateSharp.Grammars/Resources/Themes/hc_light.json b/src/TextMateSharp.Grammars/Resources/Themes/hc_light.json index 83a4083..8704efb 100644 --- a/src/TextMateSharp.Grammars/Resources/Themes/hc_light.json +++ b/src/TextMateSharp.Grammars/Resources/Themes/hc_light.json @@ -1,6 +1,7 @@ { "$schema": "vscode://schemas/color-theme", "name": "Light High Contrast", + "include": "./light_vs.json", "tokenColors": [ { "scope": ["meta.embedded", "source.groovy.embedded"], diff --git a/src/TextMateSharp.Grammars/Resources/Themes/light_vs.json b/src/TextMateSharp.Grammars/Resources/Themes/light_vs.json index d88e6ff..6d1cc42 100644 --- a/src/TextMateSharp.Grammars/Resources/Themes/light_vs.json +++ b/src/TextMateSharp.Grammars/Resources/Themes/light_vs.json @@ -36,7 +36,7 @@ "string meta.image.inline.markdown" ], "settings": { - "foreground": "#000000ff" + "foreground": "#000000" } }, { diff --git a/src/TextMateSharp/Themes/Theme.cs b/src/TextMateSharp/Themes/Theme.cs index 1fbaee9..395f939 100644 --- a/src/TextMateSharp/Themes/Theme.cs +++ b/src/TextMateSharp/Themes/Theme.cs @@ -34,7 +34,10 @@ public static Theme CreateFromRawTheme( // First get colors from include, then try and overwrite with local colors.. // I don't see this happening currently, but here just in case that ever happens. - ParsedTheme.ParsedGuiColors(themeInclude, guiColorsDictionary); + if (themeInclude != null) + { + ParsedTheme.ParsedGuiColors(themeInclude, guiColorsDictionary); + } ParsedTheme.ParsedGuiColors(source, guiColorsDictionary); return new Theme(colorMap, theme, include, guiColorsDictionary);