Skip to content

Commit 9c5c737

Browse files
committed
Prioritize longer keywords also when they're not alphanumeric
1 parent d12a782 commit 9c5c737

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ bld/
4545
# Visual Studio 2017 auto generated files
4646
Generated\ Files/
4747

48+
# JetBrains Rider
49+
.idea/
50+
4851
# MSTest test Results
4952
[Tt]est[Rr]esult*/
5053
[Bb]uild[Ll]og.*
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Linq;
2+
3+
using ICSharpCode.AvalonEdit.Document;
4+
using ICSharpCode.AvalonEdit.Highlighting;
5+
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
6+
7+
using NUnit.Framework;
8+
9+
namespace ICSharpCode.AvalonEdit.Tests.Highlighting
10+
{
11+
[TestFixture]
12+
public class XmlHighlightingDefinitionTests
13+
{
14+
[Test]
15+
public void LongerKeywordsArePreferred()
16+
{
17+
var color = new XshdColor { Name = "Result" };
18+
var syntaxDefinition = new XshdSyntaxDefinition {
19+
Elements = {
20+
color,
21+
new XshdRuleSet {
22+
Elements = { new XshdKeywords {
23+
ColorReference = new XshdReference<XshdColor>(null, color.Name),
24+
Words = { "foo", "foo.bar." }
25+
}
26+
}
27+
}
28+
}
29+
};
30+
31+
var document = new TextDocument("This is a foo.bar. keyword");
32+
var highlighter = new DocumentHighlighter(document, new XmlHighlightingDefinition(syntaxDefinition, HighlightingManager.Instance));
33+
var result = highlighter.HighlightLine(1);
34+
35+
var highlightedText = document.GetText(result.Sections.Single());
36+
Assert.That(highlightedText, Is.EqualTo("foo.bar."));
37+
}
38+
}
39+
}

ICSharpCode.AvalonEdit/Highlighting/Xshd/XmlHighlightingDefinition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ public object VisitKeywords(XshdKeywords keywords)
240240
}
241241
keyWordRegex.Append(@")\b");
242242
} else {
243-
keyWordRegex.Append('(');
243+
keyWordRegex.Append("(?>");
244244
int i = 0;
245-
foreach (string keyword in keywords.Words) {
245+
foreach (string keyword in keywords.Words.OrderByDescending(w => w.Length)) {
246246
if (i++ > 0)
247247
keyWordRegex.Append('|');
248248
if (char.IsLetterOrDigit(keyword[0]))

0 commit comments

Comments
 (0)