Skip to content

Commit

Permalink
Merge pull request #389 from ltrzesniewski/keywords-order
Browse files Browse the repository at this point in the history
Prioritize longer keywords also when they're not alphanumeric
  • Loading branch information
dgrunwald authored Mar 18, 2023
2 parents ddba666 + 9c5c737 commit e6ad917
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;

using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;

using NUnit.Framework;

namespace ICSharpCode.AvalonEdit.Tests.Highlighting
{
[TestFixture]
public class XmlHighlightingDefinitionTests
{
[Test]
public void LongerKeywordsArePreferred()
{
var color = new XshdColor { Name = "Result" };
var syntaxDefinition = new XshdSyntaxDefinition {
Elements = {
color,
new XshdRuleSet {
Elements = { new XshdKeywords {
ColorReference = new XshdReference<XshdColor>(null, color.Name),
Words = { "foo", "foo.bar." }
}
}
}
}
};

var document = new TextDocument("This is a foo.bar. keyword");
var highlighter = new DocumentHighlighter(document, new XmlHighlightingDefinition(syntaxDefinition, HighlightingManager.Instance));
var result = highlighter.HighlightLine(1);

var highlightedText = document.GetText(result.Sections.Single());
Assert.That(highlightedText, Is.EqualTo("foo.bar."));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ public object VisitKeywords(XshdKeywords keywords)
}
keyWordRegex.Append(@")\b");
} else {
keyWordRegex.Append('(');
keyWordRegex.Append("(?>");
int i = 0;
foreach (string keyword in keywords.Words) {
foreach (string keyword in keywords.Words.OrderByDescending(w => w.Length)) {
if (i++ > 0)
keyWordRegex.Append('|');
if (char.IsLetterOrDigit(keyword[0]))
Expand Down

0 comments on commit e6ad917

Please sign in to comment.