Skip to content

Commit e6ad917

Browse files
authored
Merge pull request #389 from ltrzesniewski/keywords-order
Prioritize longer keywords also when they're not alphanumeric
2 parents ddba666 + 9c5c737 commit e6ad917

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

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)