Skip to content

Commit

Permalink
Support configurations with depth > 2 and parsing fix (#57, #62)
Browse files Browse the repository at this point in the history
  • Loading branch information
theron-wang committed Aug 9, 2024
1 parent 116e8b3 commit 7aeaeb6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/Completions/CompletionUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ internal string GetDescriptionFromClass(string text, bool shouldFormat = true)
string color;
if (segments.Length >= 3)
{
color = $"{segments[segments.Length - 2]}-{segments[segments.Length - 1]}";
color = string.Join("-", segments.Skip(1));
}
else
{
Expand Down
40 changes: 25 additions & 15 deletions src/Configuration/ConfigFileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function main(setup, configuration = {{}}) {{
try {{
configuration = require('./{Path.GetFileName(path)}');
}} catch {{
configuration = await import('./{Path.GetFileName(path)}');
configuration = await import('././{Path.GetFileName(path)}');
}}
if (configuration.default) {{
Expand Down Expand Up @@ -147,6 +147,28 @@ function getValueByKeyBracket(object, key) {{
configuration.plugins = newPlugins;
}}
function theme(key, defaultValue) {{
const defaultTheme = require('tailwindcss/defaultTheme');
const colors = require('tailwindcss/colors');
const custom = configuration;
let defaultThemeValue = getValueByKeyBracket(defaultTheme, key);
// Default theme may contain functions, so we should provide that
if (typeof defaultThemeValue === 'function') {{
console.log(theme);
defaultThemeValue = defaultThemeValue({{ theme: theme, colors: colors }});
}}
const output = {{
...defaultThemeValue,
...getValueByKeyBracket(custom.theme, key),
...getValueByKeyBracket(custom.theme.extend, key)
}};
return (!output || Object.keys(output).length === 0) ? defaultValue : output;
}}
const defaultLog = console.log;
console.log = function () {{ }}
Expand All @@ -157,13 +179,7 @@ function getValueByKeyBracket(object, key) {{
var modifiers = [];
value.forEach(function (p) {{
p({{
theme: (key, defaultValue) => {{
var defaultTheme = require('tailwindcss/defaultTheme');
var custom = configuration;
var output = {{ ...getValueByKeyBracket(defaultTheme, key), ...getValueByKeyBracket(custom.theme, key), ...getValueByKeyBracket(custom.theme.extend, key) }};
return (!output || Object.keys(output).length === 0) ? defaultValue : output;
}},
theme: theme,
config: (key, defaultValue) => {{
return getValueByKeyBracket(configuration, key) || defaultValue;
}},
Expand Down Expand Up @@ -269,13 +285,7 @@ function getValueByKeyBracket(object, key) {{
}};
}} else {{
return typeof value === 'function' ? value({{
theme: (key, defaultValue) => {{
var defaultTheme = require('tailwindcss/defaultTheme');
var custom = configuration;
var output = {{ ...getValueByKeyBracket(defaultTheme, key), ...getValueByKeyBracket(custom.theme, key), ...getValueByKeyBracket(custom.theme.extend, key) }};
return (!output || Object.keys(output).length === 0) ? defaultValue : output;
}}
theme: theme
}}) : value;
}}
}}
Expand Down
87 changes: 19 additions & 68 deletions src/Configuration/ConfigurationClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,20 +446,32 @@ private void LoadPlugins(TailwindConfiguration config)
_completionBase.PluginModifiers = config.PluginModifiers;
}

private Dictionary<string, string> GetColorMapper(Dictionary<string, object> colors)
private Dictionary<string, string> GetColorMapper(Dictionary<string, object> colors, string prev = "")
{
var newColorToRgbMapper = new Dictionary<string, string>();

foreach (var key in colors.Keys)
{
var value = colors[key];

var actual = prev;

// when the root key is DEFAULT, it takes no effect on class names
if (prev == "" || key != "DEFAULT")
{
if (actual != "")
{
actual += "-";
}
actual += key;
}

if (value is string s)
{
if (IsHex(s, out string hex))
{
var color = ColorTranslator.FromHtml($"#{hex}");
newColorToRgbMapper[key] = $"{color.R},{color.G},{color.B}";
newColorToRgbMapper[actual] = $"{color.R},{color.G},{color.B}";
}
else if (s.StartsWith("rgb"))
{
Expand All @@ -472,85 +484,24 @@ private Dictionary<string, string> GetColorMapper(Dictionary<string, object> col

if (values.Length >= 3 && values.Take(3).All(v => float.TryParse(v, out _)))
{
newColorToRgbMapper[key] = $"{float.Parse(values[0]):0},{float.Parse(values[1]):1},{float.Parse(values[2]):2}";
newColorToRgbMapper[actual] = $"{float.Parse(values[0]):0},{float.Parse(values[1]):1},{float.Parse(values[2]):2}";
}
else
{
newColorToRgbMapper[key] = "{noparse}" + s;
newColorToRgbMapper[actual] = "{noparse}" + s;
}
}
}
else
{
newColorToRgbMapper[key] = "{noparse}" + s;
newColorToRgbMapper[actual] = "{noparse}" + s;
}
}
else if (value is Dictionary<string, object> colorVariants)
{
foreach (var colorVariant in colorVariants.Keys)
foreach (var pair in GetColorMapper(colorVariants, actual))
{
if (colorVariant == "DEFAULT")
{
if (IsHex(colorVariants[colorVariant], out string hex))
{
var color = ColorTranslator.FromHtml($"#{hex}");
newColorToRgbMapper[key] = $"{color.R},{color.G},{color.B}";
}
else if (colorVariants[colorVariant].ToString().StartsWith("rgb"))
{
var openParen = colorVariants[colorVariant].ToString().IndexOf('(');
var closeParen = colorVariants[colorVariant].ToString().IndexOf(')', openParen);
if (openParen != -1 && closeParen != -1 && closeParen - openParen > 1)
{
var text = colorVariants[colorVariant].ToString().Substring(openParen + 1, closeParen - openParen - 1);
var values = text.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);

if (values.Length >= 3 && values.Take(3).All(v => float.TryParse(v, out _)))
{
newColorToRgbMapper[key] = $"{float.Parse(values[0]):0},{float.Parse(values[1]):1},{float.Parse(values[2]):2}";
}
else
{
newColorToRgbMapper[key] = "{noparse}" + colorVariants[colorVariant];
}
}
}
else
{
newColorToRgbMapper[key] = "{noparse}" + colorVariants[colorVariant];
}
}
else
{
if (IsHex(colorVariants[colorVariant], out string hex))
{
var color = ColorTranslator.FromHtml($"#{hex}");
newColorToRgbMapper[key + "-" + colorVariant] = $"{color.R},{color.G},{color.B}";
}
else if (colorVariants[colorVariant].ToString().StartsWith("rgb"))
{
var openParen = colorVariants[colorVariant].ToString().IndexOf('(');
var closeParen = colorVariants[colorVariant].ToString().IndexOf(')', openParen);
if (openParen != -1 && closeParen != -1 && closeParen - openParen > 1)
{
var text = colorVariants[colorVariant].ToString().Substring(openParen + 1, closeParen - openParen - 1);
var values = text.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);

if (values.Length >= 3 && values.Take(3).All(v => float.TryParse(v, out _)))
{
newColorToRgbMapper[key + "-" + colorVariant] = $"{float.Parse(values[0]):0},{float.Parse(values[1]):1},{float.Parse(values[2]):2}";
}
else
{
newColorToRgbMapper[key + "-" + colorVariant] = "{noparse}" + colorVariants[colorVariant];
}
}
}
else
{
newColorToRgbMapper[key + "-" + colorVariant] = "{noparse}" + colorVariants[colorVariant];
}
}
newColorToRgbMapper[pair.Key] = pair.Value;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/QuickInfo/RazorQuickInfoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ protected override bool IsInClassScope(IAsyncQuickInfoSession session, out Snaps
numberOfQuotes++;
}

if (depth == 0 && numberOfQuotes % 2 == 0 && character == ' ')
if (depth == 0 && numberOfQuotes % 2 == 0 && char.IsWhiteSpace(character))
{
isInRazor = false;
}
}
else if (character == ' ' && searchPos <= end)
else if (char.IsWhiteSpace(character) && searchPos <= end)
{
break;
}
Expand Down

0 comments on commit 7aeaeb6

Please sign in to comment.