Skip to content

Commit 69616c1

Browse files
authored
Merge pull request #59 from Khitiara/master
Make FontStyle a flags enum instead of int constants (fixes #58)
2 parents 84a4383 + b6d9724 commit 69616c1

File tree

9 files changed

+35
-32
lines changed

9 files changed

+35
-32
lines changed

src/TextMateSharp.Demo/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static void Main(string[] args)
7272

7373
int foreground = -1;
7474
int background = -1;
75-
int fontStyle = -1;
75+
FontStyle fontStyle = FontStyle.NotSet;
7676

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

85-
if (fontStyle == -1 && themeRule.fontStyle > 0)
85+
if (fontStyle == FontStyle.NotSet && themeRule.fontStyle > 0)
8686
fontStyle = themeRule.fontStyle;
8787
}
8888

@@ -103,7 +103,7 @@ static void Main(string[] args)
103103
Console.WriteLine("ERROR: " + ex.Message);
104104
}
105105
}
106-
static void WriteToken(string text, int foreground, int background, int fontStyle, Theme theme)
106+
static void WriteToken(string text, int foreground, int background, FontStyle fontStyle, Theme theme)
107107
{
108108
if (foreground == -1)
109109
{
@@ -130,7 +130,7 @@ static Color GetColor(int colorId, Theme theme)
130130
return HexToColor(theme.GetColor(colorId));
131131
}
132132

133-
static Decoration GetDecoration(int fontStyle)
133+
static Decoration GetDecoration(FontStyle fontStyle)
134134
{
135135
Decoration result = Decoration.None;
136136

src/TextMateSharp.Tests/Internal/Grammars/EncodedTokenAttributesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void StackElementMetadata_Should_Work_At_Max_Values()
8686
int maxLangId = 255;
8787
int maxTokenType = StandardTokenType.Comment | StandardTokenType.Other | StandardTokenType.RegEx
8888
| StandardTokenType.String;
89-
int maxFontStyle = FontStyle.Bold | FontStyle.Italic | FontStyle.Underline;
89+
FontStyle maxFontStyle = FontStyle.Bold | FontStyle.Italic | FontStyle.Underline;
9090
int maxForeground = 511;
9191
int maxBackground = 254;
9292

@@ -109,7 +109,7 @@ static void AssertMetadataHasProperties(
109109
int languageId,
110110
/*StandardTokenType*/ int tokenType,
111111
bool containsBalancedBrackets,
112-
int fontStyle,
112+
FontStyle fontStyle,
113113
int foreground,
114114
int background)
115115
{

src/TextMateSharp/Internal/Grammars/AttributedScopeStack.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static int MergeAttributes(
124124
return existingTokenAttributes;
125125
}
126126

127-
int fontStyle = FontStyle.NotSet;
127+
FontStyle fontStyle = FontStyle.NotSet;
128128
int foreground = 0;
129129
int background = 0;
130130

src/TextMateSharp/Internal/Grammars/EncodedTokenAttributes.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public static bool ContainsBalancedBrackets(int metadata)
3636
return (uintValue & MetadataConsts.BALANCED_BRACKETS_MASK) != 0;
3737
}
3838

39-
public static int GetFontStyle(int metadata)
39+
public static FontStyle GetFontStyle(int metadata)
4040
{
4141
uint uintValue = (uint)metadata;
42-
return (int)((uintValue & MetadataConsts.FONT_STYLE_MASK) >> MetadataConsts.FONT_STYLE_OFFSET);
42+
return (FontStyle)((uintValue & MetadataConsts.FONT_STYLE_MASK) >> MetadataConsts.FONT_STYLE_OFFSET);
4343
}
4444

4545
public static int GetForeground(int metadata)
@@ -59,7 +59,7 @@ public static int Set(
5959
int languageId,
6060
/*OptionalStandardTokenType*/ int tokenType,
6161
bool? containsBalancedBrackets,
62-
int fontStyle,
62+
FontStyle fontStyle,
6363
int foreground,
6464
int background)
6565
{
@@ -74,7 +74,7 @@ public static int Set(
7474
return ((languageId << MetadataConsts.LANGUAGEID_OFFSET)
7575
| (tokenType << MetadataConsts.TOKEN_TYPE_OFFSET)
7676
| (containsBalancedBracketsBit << MetadataConsts.BALANCED_BRACKETS_OFFSET)
77-
| (fontStyle << MetadataConsts.FONT_STYLE_OFFSET)
77+
| ((int)fontStyle << MetadataConsts.FONT_STYLE_OFFSET)
7878
| (foreground << MetadataConsts.FOREGROUND_OFFSET)
7979
| (background << MetadataConsts.BACKGROUND_OFFSET)) >> 0;
8080
}

src/TextMateSharp/Themes/FontStyle.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
using System;
2+
13
namespace TextMateSharp.Themes
24
{
3-
public class FontStyle
5+
[Flags]
6+
public enum FontStyle
47
{
5-
public const int NotSet = -1;
8+
NotSet = -1,
69

710
// This can are bit-flags, so it can be `Italic | Bold`
8-
public const int None = 0;
9-
public const int Italic = 1;
10-
public const int Bold = 2;
11-
public const int Underline = 4;
12-
public const int Strikethrough = 8;
11+
None = 0,
12+
Italic = 1,
13+
Bold = 2,
14+
Underline = 4,
15+
Strikethrough = 8
1316
}
1417
}

src/TextMateSharp/Themes/ParsedThemeRule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public class ParsedThemeRule
1111
public int index;
1212

1313
// -1 if not set.An or mask of `FontStyle` otherwise.
14-
public int fontStyle;
14+
public FontStyle fontStyle;
1515
public string foreground;
1616
public string background;
1717

18-
public ParsedThemeRule(string name, string scope, List<string> parentScopes, int index, int fontStyle, string foreground, string background)
18+
public ParsedThemeRule(string name, string scope, List<string> parentScopes, int index, FontStyle fontStyle, string foreground, string background)
1919
{
2020
this.name = name;
2121
this.scope = scope;
@@ -31,7 +31,7 @@ public override int GetHashCode()
3131
int prime = 31;
3232
int result = 1;
3333
result = prime * result + ((background == null) ? 0 : background.GetHashCode());
34-
result = prime * result + fontStyle;
34+
result = prime * result + (int)fontStyle;
3535
result = prime * result + ((foreground == null) ? 0 : foreground.GetHashCode());
3636
result = prime * result + index;
3737
result = prime * result + ((parentScopes == null) ? 0 : parentScopes.GetHashCode());

src/TextMateSharp/Themes/Theme.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static void LookupThemeRules(
147147
scopes.Add("");
148148
}
149149

150-
int fontStyle = FontStyle.NotSet;
150+
FontStyle fontStyle = FontStyle.NotSet;
151151
object settingsFontStyle = entry.GetSetting().GetFontStyle();
152152
if (settingsFontStyle is string)
153153
{
@@ -159,16 +159,16 @@ static void LookupThemeRules(
159159
switch (segment)
160160
{
161161
case "italic":
162-
fontStyle = fontStyle | FontStyle.Italic;
162+
fontStyle |= FontStyle.Italic;
163163
break;
164164
case "bold":
165-
fontStyle = fontStyle | FontStyle.Bold;
165+
fontStyle |= FontStyle.Bold;
166166
break;
167167
case "underline":
168-
fontStyle = fontStyle | FontStyle.Underline;
168+
fontStyle |= FontStyle.Underline;
169169
break;
170170
case "strikethrough":
171-
fontStyle = fontStyle | FontStyle.Strikethrough;
171+
fontStyle |= FontStyle.Strikethrough;
172172
break;
173173
}
174174
}
@@ -240,7 +240,7 @@ static ParsedTheme ResolveParsedThemeRules(
240240
});
241241

242242
// Determine defaults
243-
int defaultFontStyle = FontStyle.None;
243+
FontStyle defaultFontStyle = FontStyle.None;
244244
string defaultForeground = "#000000";
245245
string defaultBackground = "#ffffff";
246246
while (parsedThemeRules.Count >= 1 && "".Equals(parsedThemeRules[0].scope))

src/TextMateSharp/Themes/ThemeTrieElement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public List<ThemeTrieElementRule> Match(string scope)
106106
return ThemeTrieElement.SortBySpecificity(arr);
107107
}
108108

109-
public void Insert(string name, int scopeDepth, string scope, List<string> parentScopes, int fontStyle, int foreground,
109+
public void Insert(string name, int scopeDepth, string scope, List<string> parentScopes, FontStyle fontStyle, int foreground,
110110
int background)
111111
{
112112
if ("".Equals(scope))
@@ -144,7 +144,7 @@ public void Insert(string name, int scopeDepth, string scope, List<string> paren
144144
child.Insert(name, scopeDepth + 1, tail, parentScopes, fontStyle, foreground, background);
145145
}
146146

147-
private void DoInsertHere(string name, int scopeDepth, List<string> parentScopes, int fontStyle, int foreground,
147+
private void DoInsertHere(string name, int scopeDepth, List<string> parentScopes, FontStyle fontStyle, int foreground,
148148
int background)
149149
{
150150

src/TextMateSharp/Themes/ThemeTrieElementRule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public class ThemeTrieElementRule
99

1010
public int scopeDepth;
1111
public List<string> parentScopes;
12-
public int fontStyle;
12+
public FontStyle fontStyle;
1313
public int foreground;
1414
public int background;
1515
public string name;
1616

17-
public ThemeTrieElementRule(string name, int scopeDepth, List<string> parentScopes, int fontStyle, int foreground,
17+
public ThemeTrieElementRule(string name, int scopeDepth, List<string> parentScopes, FontStyle fontStyle, int foreground,
1818
int background)
1919
{
2020
this.name = name;
@@ -41,7 +41,7 @@ public static List<ThemeTrieElementRule> cloneArr(List<ThemeTrieElementRule> arr
4141
return r;
4242
}
4343

44-
public void AcceptOverwrite(string name, int scopeDepth, int fontStyle, int foreground, int background)
44+
public void AcceptOverwrite(string name, int scopeDepth, FontStyle fontStyle, int foreground, int background)
4545
{
4646
if (this.scopeDepth > scopeDepth)
4747
{

0 commit comments

Comments
 (0)