From 9c5c737cef67658def5512bf2f36771723efd50a Mon Sep 17 00:00:00 2001 From: Lucas Trzesniewski Date: Sat, 25 Feb 2023 18:57:20 +0100 Subject: [PATCH] Prioritize longer keywords also when they're not alphanumeric --- .gitignore | 3 ++ .../XmlHighlightingDefinitionTests.cs | 39 +++++++++++++++++++ .../Xshd/XmlHighlightingDefinition.cs | 4 +- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 ICSharpCode.AvalonEdit.Tests/Highlighting/XmlHighlightingDefinitionTests.cs diff --git a/.gitignore b/.gitignore index 86b4b6c0..c9f9de7e 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,9 @@ bld/ # Visual Studio 2017 auto generated files Generated\ Files/ +# JetBrains Rider +.idea/ + # MSTest test Results [Tt]est[Rr]esult*/ [Bb]uild[Ll]og.* diff --git a/ICSharpCode.AvalonEdit.Tests/Highlighting/XmlHighlightingDefinitionTests.cs b/ICSharpCode.AvalonEdit.Tests/Highlighting/XmlHighlightingDefinitionTests.cs new file mode 100644 index 00000000..0bb05ef1 --- /dev/null +++ b/ICSharpCode.AvalonEdit.Tests/Highlighting/XmlHighlightingDefinitionTests.cs @@ -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(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.")); + } + } +} diff --git a/ICSharpCode.AvalonEdit/Highlighting/Xshd/XmlHighlightingDefinition.cs b/ICSharpCode.AvalonEdit/Highlighting/Xshd/XmlHighlightingDefinition.cs index 1fd58063..5a493456 100644 --- a/ICSharpCode.AvalonEdit/Highlighting/Xshd/XmlHighlightingDefinition.cs +++ b/ICSharpCode.AvalonEdit/Highlighting/Xshd/XmlHighlightingDefinition.cs @@ -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]))