Skip to content

Commit

Permalink
Merge pull request #64 from Natestah/master
Browse files Browse the repository at this point in the history
Expose "colors" dictionary
  • Loading branch information
danipen authored Jun 28, 2024
2 parents 3a00ae0 + 91cc5e6 commit c4720a8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/TextMateSharp.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/TextMateSharp.Grammars/Resources/Themes/hc_light.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"$schema": "vscode://schemas/color-theme",
"name": "Light High Contrast",
"include": "./light_vs.json",
"tokenColors": [
{
"scope": ["meta.embedded", "source.groovy.embedded"],
Expand Down
2 changes: 1 addition & 1 deletion src/TextMateSharp.Grammars/Resources/Themes/light_vs.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"string meta.image.inline.markdown"
],
"settings": {
"foreground": "#000000ff"
"foreground": "#000000"
}
},
{
Expand Down
11 changes: 11 additions & 0 deletions src/TextMateSharp/Internal/Themes/ThemeRaw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ThemeRaw : Dictionary<string, object>, 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";
Expand Down Expand Up @@ -47,6 +48,16 @@ public ICollection<IRawThemeSetting> GetTokenColors()
return result.Cast<IRawThemeSetting>().ToList();
}

public ICollection<KeyValuePair<string,object>> GetGuiColors()
{
ICollection result = TryGetObject<ICollection>(COLORS);

if (result == null)
return null;

return result.Cast<KeyValuePair<string,object>>().ToList();
}

public object GetScope()
{
return TryGetObject<object>(SCOPE);
Expand Down
1 change: 1 addition & 0 deletions src/TextMateSharp/Themes/IRawTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface IRawTheme
string GetInclude();
ICollection<IRawThemeSetting> GetSettings();
ICollection<IRawThemeSetting> GetTokenColors();
ICollection<KeyValuePair<string,object>> GetGuiColors();
}
}
51 changes: 44 additions & 7 deletions src/TextMateSharp/Themes/Theme.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using TextMateSharp.Internal.Utils;
using TextMateSharp.Registry;
Expand All @@ -11,29 +12,43 @@ public class Theme
private ParsedTheme _theme;
private ParsedTheme _include;
private ColorMap _colorMap;
private Dictionary<string, string> _guiColorDictionary;

public static Theme CreateFromRawTheme(
IRawTheme source,
IRegistryOptions registryOptions)
{
ColorMap colorMap = new ColorMap();
var guiColorsDictionary = new Dictionary<string, string>();

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.
if (themeInclude != null)
{
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<string,string> guiColorDictionary)
{
_colorMap = colorMap;
_theme = theme;
_include = include;
_guiColorDictionary = guiColorDictionary;
}

public List<ThemeTrieElementRule> Match(IList<string> scopeNames)
Expand All @@ -49,6 +64,11 @@ public List<ThemeTrieElementRule> Match(IList<string> scopeNames)
return result;
}

public ReadOnlyDictionary<string, string> GetGuiColorDictionary()
{
return new ReadOnlyDictionary<string, string>(this._guiColorDictionary);
}

public ICollection<string> GetColorMap()
{
return this._colorMap.GetColorMap();
Expand Down Expand Up @@ -92,19 +112,37 @@ internal static List<ParsedThemeRule> ParseTheme(IRawTheme source, int priority)
return result;
}

internal static void ParsedGuiColors(IRawTheme source, Dictionary<string,string> colorDictionary)
{
var colors = source.GetGuiColors();
if (colors == null)
{
return;
}
foreach (var kvp in colors)
{
colorDictionary[kvp.Key] = (string)kvp.Value;
}
}


internal static List<ParsedThemeRule> ParseInclude(
IRawTheme source,
IRegistryOptions registryOptions,
int priority)
int priority,
out IRawTheme themeInclude)
{
List<ParsedThemeRule> result = new List<ParsedThemeRule>();

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;
Expand Down Expand Up @@ -270,7 +308,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);
}

Expand Down

0 comments on commit c4720a8

Please sign in to comment.