Skip to content

Commit

Permalink
Make FontStyle a flags enum instead of int constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Khitiara committed Nov 25, 2023
1 parent 84a4383 commit b6d9724
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/TextMateSharp.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static void Main(string[] args)

int foreground = -1;
int background = -1;
int fontStyle = -1;
FontStyle fontStyle = FontStyle.NotSet;

foreach (var themeRule in theme.Match(token.Scopes))
{
Expand All @@ -82,7 +82,7 @@ static void Main(string[] args)
if (background == -1 && themeRule.background > 0)
background = themeRule.background;

if (fontStyle == -1 && themeRule.fontStyle > 0)
if (fontStyle == FontStyle.NotSet && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}

Expand All @@ -103,7 +103,7 @@ static void Main(string[] args)
Console.WriteLine("ERROR: " + ex.Message);
}
}
static void WriteToken(string text, int foreground, int background, int fontStyle, Theme theme)
static void WriteToken(string text, int foreground, int background, FontStyle fontStyle, Theme theme)
{
if (foreground == -1)
{
Expand All @@ -130,7 +130,7 @@ static Color GetColor(int colorId, Theme theme)
return HexToColor(theme.GetColor(colorId));
}

static Decoration GetDecoration(int fontStyle)
static Decoration GetDecoration(FontStyle fontStyle)
{
Decoration result = Decoration.None;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void StackElementMetadata_Should_Work_At_Max_Values()
int maxLangId = 255;
int maxTokenType = StandardTokenType.Comment | StandardTokenType.Other | StandardTokenType.RegEx
| StandardTokenType.String;
int maxFontStyle = FontStyle.Bold | FontStyle.Italic | FontStyle.Underline;
FontStyle maxFontStyle = FontStyle.Bold | FontStyle.Italic | FontStyle.Underline;
int maxForeground = 511;
int maxBackground = 254;

Expand All @@ -109,7 +109,7 @@ static void AssertMetadataHasProperties(
int languageId,
/*StandardTokenType*/ int tokenType,
bool containsBalancedBrackets,
int fontStyle,
FontStyle fontStyle,
int foreground,
int background)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static int MergeAttributes(
return existingTokenAttributes;
}

int fontStyle = FontStyle.NotSet;
FontStyle fontStyle = FontStyle.NotSet;
int foreground = 0;
int background = 0;

Expand Down
8 changes: 4 additions & 4 deletions src/TextMateSharp/Internal/Grammars/EncodedTokenAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public static bool ContainsBalancedBrackets(int metadata)
return (uintValue & MetadataConsts.BALANCED_BRACKETS_MASK) != 0;
}

public static int GetFontStyle(int metadata)
public static FontStyle GetFontStyle(int metadata)
{
uint uintValue = (uint)metadata;
return (int)((uintValue & MetadataConsts.FONT_STYLE_MASK) >> MetadataConsts.FONT_STYLE_OFFSET);
return (FontStyle)((uintValue & MetadataConsts.FONT_STYLE_MASK) >> MetadataConsts.FONT_STYLE_OFFSET);
}

public static int GetForeground(int metadata)
Expand All @@ -59,7 +59,7 @@ public static int Set(
int languageId,
/*OptionalStandardTokenType*/ int tokenType,
bool? containsBalancedBrackets,
int fontStyle,
FontStyle fontStyle,
int foreground,
int background)
{
Expand All @@ -74,7 +74,7 @@ public static int Set(
return ((languageId << MetadataConsts.LANGUAGEID_OFFSET)
| (tokenType << MetadataConsts.TOKEN_TYPE_OFFSET)
| (containsBalancedBracketsBit << MetadataConsts.BALANCED_BRACKETS_OFFSET)
| (fontStyle << MetadataConsts.FONT_STYLE_OFFSET)
| ((int)fontStyle << MetadataConsts.FONT_STYLE_OFFSET)
| (foreground << MetadataConsts.FOREGROUND_OFFSET)
| (background << MetadataConsts.BACKGROUND_OFFSET)) >> 0;
}
Expand Down
17 changes: 10 additions & 7 deletions src/TextMateSharp/Themes/FontStyle.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;

namespace TextMateSharp.Themes
{
public class FontStyle
[Flags]
public enum FontStyle
{
public const int NotSet = -1;
NotSet = -1,

// This can are bit-flags, so it can be `Italic | Bold`
public const int None = 0;
public const int Italic = 1;
public const int Bold = 2;
public const int Underline = 4;
public const int Strikethrough = 8;
None = 0,
Italic = 1,
Bold = 2,
Underline = 4,
Strikethrough = 8
}
}
6 changes: 3 additions & 3 deletions src/TextMateSharp/Themes/ParsedThemeRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public class ParsedThemeRule
public int index;

// -1 if not set.An or mask of `FontStyle` otherwise.
public int fontStyle;
public FontStyle fontStyle;
public string foreground;
public string background;

public ParsedThemeRule(string name, string scope, List<string> parentScopes, int index, int fontStyle, string foreground, string background)
public ParsedThemeRule(string name, string scope, List<string> parentScopes, int index, FontStyle fontStyle, string foreground, string background)
{
this.name = name;
this.scope = scope;
Expand All @@ -31,7 +31,7 @@ public override int GetHashCode()
int prime = 31;
int result = 1;
result = prime * result + ((background == null) ? 0 : background.GetHashCode());
result = prime * result + fontStyle;
result = prime * result + (int)fontStyle;
result = prime * result + ((foreground == null) ? 0 : foreground.GetHashCode());
result = prime * result + index;
result = prime * result + ((parentScopes == null) ? 0 : parentScopes.GetHashCode());
Expand Down
12 changes: 6 additions & 6 deletions src/TextMateSharp/Themes/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static void LookupThemeRules(
scopes.Add("");
}

int fontStyle = FontStyle.NotSet;
FontStyle fontStyle = FontStyle.NotSet;
object settingsFontStyle = entry.GetSetting().GetFontStyle();
if (settingsFontStyle is string)
{
Expand All @@ -159,16 +159,16 @@ static void LookupThemeRules(
switch (segment)
{
case "italic":
fontStyle = fontStyle | FontStyle.Italic;
fontStyle |= FontStyle.Italic;
break;
case "bold":
fontStyle = fontStyle | FontStyle.Bold;
fontStyle |= FontStyle.Bold;
break;
case "underline":
fontStyle = fontStyle | FontStyle.Underline;
fontStyle |= FontStyle.Underline;
break;
case "strikethrough":
fontStyle = fontStyle | FontStyle.Strikethrough;
fontStyle |= FontStyle.Strikethrough;
break;
}
}
Expand Down Expand Up @@ -240,7 +240,7 @@ static ParsedTheme ResolveParsedThemeRules(
});

// Determine defaults
int defaultFontStyle = FontStyle.None;
FontStyle defaultFontStyle = FontStyle.None;
string defaultForeground = "#000000";
string defaultBackground = "#ffffff";
while (parsedThemeRules.Count >= 1 && "".Equals(parsedThemeRules[0].scope))
Expand Down
4 changes: 2 additions & 2 deletions src/TextMateSharp/Themes/ThemeTrieElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public List<ThemeTrieElementRule> Match(string scope)
return ThemeTrieElement.SortBySpecificity(arr);
}

public void Insert(string name, int scopeDepth, string scope, List<string> parentScopes, int fontStyle, int foreground,
public void Insert(string name, int scopeDepth, string scope, List<string> parentScopes, FontStyle fontStyle, int foreground,
int background)
{
if ("".Equals(scope))
Expand Down Expand Up @@ -144,7 +144,7 @@ public void Insert(string name, int scopeDepth, string scope, List<string> paren
child.Insert(name, scopeDepth + 1, tail, parentScopes, fontStyle, foreground, background);
}

private void DoInsertHere(string name, int scopeDepth, List<string> parentScopes, int fontStyle, int foreground,
private void DoInsertHere(string name, int scopeDepth, List<string> parentScopes, FontStyle fontStyle, int foreground,
int background)
{

Expand Down
6 changes: 3 additions & 3 deletions src/TextMateSharp/Themes/ThemeTrieElementRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public class ThemeTrieElementRule

public int scopeDepth;
public List<string> parentScopes;
public int fontStyle;
public FontStyle fontStyle;
public int foreground;
public int background;
public string name;

public ThemeTrieElementRule(string name, int scopeDepth, List<string> parentScopes, int fontStyle, int foreground,
public ThemeTrieElementRule(string name, int scopeDepth, List<string> parentScopes, FontStyle fontStyle, int foreground,
int background)
{
this.name = name;
Expand All @@ -41,7 +41,7 @@ public static List<ThemeTrieElementRule> cloneArr(List<ThemeTrieElementRule> arr
return r;
}

public void AcceptOverwrite(string name, int scopeDepth, int fontStyle, int foreground, int background)
public void AcceptOverwrite(string name, int scopeDepth, FontStyle fontStyle, int foreground, int background)
{
if (this.scopeDepth > scopeDepth)
{
Expand Down

0 comments on commit b6d9724

Please sign in to comment.