Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 38a99f9

Browse files
committed
Better class-like matches
1 parent c39a63a commit 38a99f9

File tree

3 files changed

+43
-53
lines changed

3 files changed

+43
-53
lines changed

Interop/TranslationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private void FillToEnglishDefaults()
260260
language.Add("RestartEdiFullEff", "Restart editor to take full effect...");
261261
language.Add("RestartEdiEff", "Restart editor to take effect...");
262262
language.Add("Program", "Program");
263-
language.Add("HardwareAcc", "Use hardware acceleration (if availablessss)");
263+
language.Add("HardwareAcc", "Use hardware acceleration (if available)");
264264
language.Add("UIAnim", "UI animations");
265265
language.Add("OpenInc", "Auto open includes");
266266
language.Add("OpenIncRec", "Open Includes Recursively");

SourcepawnCondenser/SourcepawnCondenser/SMDefinition/SMDefinition.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ public void ProduceStringArrays()
123123
constantNames.Sort((a, b) => string.Compare(a, b));
124124
ConstantsStrings = constantNames.ToArray();
125125
var typeNames = new List<string>();
126-
typeNames.Capacity = Enums.Count + Structs.Count + Methodmaps.Count;
126+
typeNames.Capacity = Enums.Count + Structs.Count + Methodmaps.Count + EnumStructs.Count;
127127
typeNames.AddRange(Enums.Select(i => i.Name));
128128
typeNames.AddRange(Structs.Select(i => i.Name));
129129
typeNames.AddRange(Methodmaps.Select(i => i.Name));
130130
typeNames.AddRange(Typedefs.Select(i => i.Name));
131+
typeNames.AddRange(EnumStructs.Select(i => i.Name));
131132
typeNames.Sort((a, b) => string.Compare(a, b));
132133
TypeStrings = typeNames.ToArray();
133134
}

UI/Components/EditorElementHighlighter.cs

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ namespace Spedit.UI.Components
1414
{
1515
public class AeonEditorHighlighting : IHighlightingDefinition
1616
{
17+
public AeonEditorHighlighting(IEnumerable<HighlightingColor> namedHighlightingColors)
18+
{
19+
NamedHighlightingColors = namedHighlightingColors;
20+
}
21+
1722
public string Name => "SM";
1823

1924
public HighlightingRuleSet MainRuleSet
2025
{
2126
get
2227
{
23-
var commentMarkerSet = new HighlightingRuleSet();
24-
commentMarkerSet.Name = "CommentMarkerSet";
28+
var commentMarkerSet = new HighlightingRuleSet {Name = "CommentMarkerSet"};
2529
commentMarkerSet.Rules.Add(new HighlightingRule
2630
{
2731
Regex = RegexKeywordsHelper.GetRegexFromKeywords(new[]
@@ -197,41 +201,28 @@ public HighlightingRuleSet MainRuleSet
197201
if (def.MethodsStrings.Length > 0)
198202
rs.Rules.Add(new HighlightingRule //Methods
199203
{
200-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.MethodsStrings, true),
204+
Regex = RegexKeywordsHelper.GetRegexFromKeywords2(def.MethodsStrings),
201205
Color = new HighlightingColor
202206
{Foreground = new SimpleHighlightingBrush(Program.OptionsObject.SH_Methods)}
203207
});
204208

205-
// TODO: Methodmaps are not implements in this way, but I couldnt find where they are defined
206-
if (def.EnumStructStrings.Length > 0)
207-
{
208-
rs.Rules.Add(new HighlightingRule //Methods
209-
{
210-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.EnumStructStrings, true),
211-
Color = new HighlightingColor
212-
{Foreground = new SimpleHighlightingBrush(Program.OptionsObject.SH_Types)}
213-
});
214-
}
215-
216209
if (def.StructFieldStrings.Length > 0)
217-
{
218210
rs.Rules.Add(new HighlightingRule //Methods
219211
{
220-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.StructFieldStrings, true),
212+
Regex = RegexKeywordsHelper.GetRegexFromKeywords2(
213+
def.StructFieldStrings.Select(e => e).ToArray()),
221214
Color = new HighlightingColor
222215
{Foreground = new SimpleHighlightingBrush(Program.OptionsObject.SH_Methods)}
223216
});
224-
}
225-
217+
226218
if (def.StructMethodStrings.Length > 0)
227-
{
228219
rs.Rules.Add(new HighlightingRule //Methods
229220
{
230-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.StructMethodStrings, true),
221+
Regex = RegexKeywordsHelper.GetRegexFromKeywords2(
222+
def.StructMethodStrings.Select(e => e).ToArray()),
231223
Color = new HighlightingColor
232224
{Foreground = new SimpleHighlightingBrush(Program.OptionsObject.SH_Methods)}
233225
});
234-
}
235226

236227
rs.Rules.Add(new HighlightingRule //unknown function calls
237228
{
@@ -256,14 +247,13 @@ public HighlightingColor GetNamedColor(string name)
256247
return null;
257248
}
258249

259-
public IEnumerable<HighlightingColor> NamedHighlightingColors { get; set; }
250+
public IEnumerable<HighlightingColor> NamedHighlightingColors { get; }
260251

261252
public IDictionary<string, string> Properties
262253
{
263254
get
264255
{
265-
var propertiesDictionary = new Dictionary<string, string>();
266-
propertiesDictionary.Add("DocCommentMarker", "///");
256+
var propertiesDictionary = new Dictionary<string, string> {{"DocCommentMarker", "///"}};
267257
return propertiesDictionary;
268258
}
269259
}
@@ -286,6 +276,7 @@ public SimpleHighlightingBrush(Color color) : this(new SolidColorBrush(color))
286276

287277
private SimpleHighlightingBrush(SerializationInfo info, StreamingContext context)
288278
{
279+
// ReSharper disable once PossibleNullReferenceException
289280
brush = new SolidColorBrush((Color) ColorConverter.ConvertFromString(info.GetString("color")));
290281
brush.Freeze();
291282
}
@@ -307,8 +298,7 @@ public override string ToString()
307298

308299
public override bool Equals(object obj)
309300
{
310-
var other = obj as SimpleHighlightingBrush;
311-
if (other == null)
301+
if (!(obj is SimpleHighlightingBrush other))
312302
return false;
313303
return brush.Color.Equals(other.brush.Color);
314304
}
@@ -327,20 +317,10 @@ public static Regex GetRegexFromKeywords(string[] keywords, bool ForceAtomicRege
327317

328318
if (keywords.Length == 0) return new Regex("SPEdit_Error"); //hehe
329319

330-
var UseAtomicRegex = true;
331-
for (var j = 0; j < keywords.Length; ++j)
332-
if (!char.IsLetterOrDigit(keywords[j][0]) ||
333-
!char.IsLetterOrDigit(keywords[j][keywords[j].Length - 1]))
334-
{
335-
UseAtomicRegex = false;
336-
break;
337-
}
320+
var UseAtomicRegex = keywords.All(t => char.IsLetterOrDigit(t[0]) && char.IsLetterOrDigit(t[t.Length - 1]));
338321

339322
var regexBuilder = new StringBuilder();
340-
if (UseAtomicRegex)
341-
regexBuilder.Append(@"\b(?>");
342-
else
343-
regexBuilder.Append(@"(");
323+
regexBuilder.Append(UseAtomicRegex ? @"\b(?>" : @"(");
344324

345325
var orderedKeyWords = new List<string>(keywords);
346326
var i = 0;
@@ -361,24 +341,33 @@ public static Regex GetRegexFromKeywords(string[] keywords, bool ForceAtomicRege
361341
}
362342
}
363343

364-
if (UseAtomicRegex)
365-
regexBuilder.Append(@")\b");
366-
else
367-
regexBuilder.Append(@")");
344+
regexBuilder.Append(UseAtomicRegex ? @")\b" : @")");
368345

369346
return new Regex(regexBuilder.ToString(), RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
370347
}
371348

372-
public static string[] ConvertToAtomicRegexAbleStringArray(string[] keywords)
349+
private static string[] ConvertToAtomicRegexAbleStringArray(IReadOnlyList<string> keywords)
350+
{
351+
return keywords.Where(t => t.Length > 0)
352+
.Where(t => char.IsLetterOrDigit(t[0]) && char.IsLetterOrDigit(t[t.Length - 1]))
353+
.ToArray();
354+
}
355+
356+
/// Use this for class like matches (methodmaps and enumstructs atm)
357+
public static Regex GetRegexFromKeywords2(IEnumerable<string> keywords)
373358
{
374-
var atomicRegexAbleList = new List<string>();
375-
for (var j = 0; j < keywords.Length; ++j)
376-
if (keywords[j].Length > 0)
377-
if (char.IsLetterOrDigit(keywords[j][0]) &&
378-
char.IsLetterOrDigit(keywords[j][keywords[j].Length - 1]))
379-
atomicRegexAbleList.Add(keywords[j]);
380-
381-
return atomicRegexAbleList.ToArray();
359+
var regexBuilder = new StringBuilder(@"(?<=\b[^\s]+\.)(");
360+
var i = 0;
361+
foreach (var keyword in keywords)
362+
{
363+
if (i++ > 0)
364+
regexBuilder.Append("|");
365+
366+
regexBuilder.Append(keyword);
367+
}
368+
369+
regexBuilder.Append(@")\b");
370+
return new Regex(regexBuilder.ToString(), RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
382371
}
383372
}
384373
}

0 commit comments

Comments
 (0)