Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HighlightingRule extensible so rules aren't required to use Regex #401

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions ICSharpCode.AvalonEdit/Highlighting/HighlightingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ void HighlightLineInternal()
position = 0;
ResetColorStack();
HighlightingRuleSet currentRuleSet = this.CurrentRuleSet;
Stack<Match[]> storedMatchArrays = new Stack<Match[]>();
Match[] matches = AllocateMatchArray(currentRuleSet.Spans.Count);
Match endSpanMatch = null;
Stack<RuleMatch[]> storedMatchArrays = new Stack<RuleMatch[]>();
RuleMatch[] matches = AllocateMatchArray(currentRuleSet.Spans.Count);
RuleMatch endSpanMatch = null;

while (true) {
for (int i = 0; i < matches.Length; i++) {
if (matches[i] == null || (matches[i].Success && matches[i].Index < position))
matches[i] = currentRuleSet.Spans[i].StartExpression.Match(lineText, position);
matches[i] = currentRuleSet.Spans[i].GetStartMatch(lineText, position);
}
if (endSpanMatch == null && !spanStack.IsEmpty)
endSpanMatch = spanStack.Peek().EndExpression.Match(lineText, position);
endSpanMatch = spanStack.Peek().GetEndMatch(lineText, position);

Match firstMatch = Minimum(matches, endSpanMatch);
RuleMatch firstMatch = Minimum(matches, endSpanMatch);
if (firstMatch == null)
break;

Expand Down Expand Up @@ -191,14 +191,14 @@ void HighlightNonSpans(int until)
if (position == until)
return;
if (highlightedLine != null) {
IList<HighlightingRule> rules = CurrentRuleSet.Rules;
Match[] matches = AllocateMatchArray(rules.Count);
IList<IHighlightingRule> rules = CurrentRuleSet.Rules;
RuleMatch[] matches = AllocateMatchArray(rules.Count);
while (true) {
for (int i = 0; i < matches.Length; i++) {
if (matches[i] == null || (matches[i].Success && matches[i].Index < position))
matches[i] = rules[i].Regex.Match(lineText, position, until - position);
matches[i] = rules[i].GetMatch(lineText, position, until - position, highlightedLine.DocumentLine.LineNumber);
}
Match firstMatch = Minimum(matches, null);
RuleMatch firstMatch = Minimum(matches, null);
if (firstMatch == null)
break;

Expand All @@ -208,7 +208,7 @@ void HighlightNonSpans(int until)
throw new InvalidOperationException(
"A highlighting rule matched 0 characters, which would cause an endless loop.\n" +
"Change the highlighting definition so that the rule matches at least one character.\n" +
"Regex: " + rules[ruleIndex].Regex);
rules[ruleIndex].RuleInfo);
}
PushColor(rules[ruleIndex].Color);
position = firstMatch.Index + firstMatch.Length;
Expand Down Expand Up @@ -297,10 +297,10 @@ void PopAllColors()
/// <summary>
/// Returns the first match from the array or endSpanMatch.
/// </summary>
static Match Minimum(Match[] arr, Match endSpanMatch)
static RuleMatch Minimum(RuleMatch[] arr, RuleMatch endSpanMatch)
{
Match min = null;
foreach (Match v in arr) {
RuleMatch min = null;
foreach (RuleMatch v in arr) {
if (v.Success && (min == null || v.Index < min.Index))
min = v;
}
Expand All @@ -310,12 +310,12 @@ static Match Minimum(Match[] arr, Match endSpanMatch)
return min;
}

static Match[] AllocateMatchArray(int count)
static RuleMatch[] AllocateMatchArray(int count)
{
if (count == 0)
return Empty<Match>.Array;
return Empty<RuleMatch>.Array;
else
return new Match[count];
return new RuleMatch[count];
}
#endregion
}
Expand Down
11 changes: 10 additions & 1 deletion ICSharpCode.AvalonEdit/Highlighting/HighlightingRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// A highlighting rule.
/// </summary>
[Serializable]
public class HighlightingRule
public class HighlightingRule : IHighlightingRule
{
/// <summary>
/// Gets/Sets the regular expression for the rule.
Expand All @@ -37,6 +37,15 @@ public class HighlightingRule
/// </summary>
public HighlightingColor Color { get; set; }

/// <inheritdoc/>
public string RuleInfo => $"Regex: {Regex}";

/// <inheritdoc/>
public RuleMatch GetMatch(string input, int beginning, int length, int lineNumber)
{
return RuleMatch.FromRegexMatch(Regex.Match(input, beginning, length));
}

/// <inheritdoc/>
public override string ToString()
{
Expand Down
4 changes: 2 additions & 2 deletions ICSharpCode.AvalonEdit/Highlighting/HighlightingRuleSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class HighlightingRuleSet
public HighlightingRuleSet()
{
this.Spans = new NullSafeCollection<HighlightingSpan>();
this.Rules = new NullSafeCollection<HighlightingRule>();
this.Rules = new NullSafeCollection<IHighlightingRule>();
}

/// <summary>
Expand All @@ -51,7 +51,7 @@ public HighlightingRuleSet()
/// <summary>
/// Gets the list of rules.
/// </summary>
public IList<HighlightingRule> Rules { get; private set; }
public IList<IHighlightingRule> Rules { get; private set; }

/// <inheritdoc/>
public override string ToString()
Expand Down
22 changes: 22 additions & 0 deletions ICSharpCode.AvalonEdit/Highlighting/HighlightingSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ public class HighlightingSpan
/// </summary>
public bool SpanColorIncludesEnd { get; set; }

/// <summary>
/// Gets the match from the specified position using the <see cref="StartExpression"/> regex.
/// </summary>
/// <param name="text">The string to search for a match</param>
/// <param name="position">The zero-based character position at which to start the search</param>
/// <returns>And object that contains information about the match</returns>
public RuleMatch GetStartMatch(string text, int position)
{
return RuleMatch.FromRegexMatch(StartExpression.Match(text, position));
}

/// <summary>
/// Gets the match from the specified position using the <see cref="EndExpression"/> regex.
/// </summary>
/// <param name="text">The string to search for a match</param>
/// <param name="position">The zero-based character position at which to start the search</param>
/// <returns>And object that contains information about the match</returns>
public RuleMatch GetEndMatch(string text, int position)
{
return RuleMatch.FromRegexMatch(EndExpression.Match(text, position));
}

/// <inheritdoc/>
public override string ToString()
{
Expand Down
47 changes: 47 additions & 0 deletions ICSharpCode.AvalonEdit/Highlighting/IHighlightingRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

namespace ICSharpCode.AvalonEdit.Highlighting
{
/// <summary>
/// Interface of a highlighting rule
/// </summary>
public interface IHighlightingRule
{
/// <summary>
/// Gets the first match for the rule
/// </summary>
/// <param name="input">The string to search for a match.</param>
/// <param name="beginning">The zero-based character position in the input string that defines the leftmost
/// position to be searched.</param>
/// <param name="length">The number of characters in the substring to include in the search.</param>
/// <param name="lineNumber">The line number of the <paramref name="input"/> string.</param>
/// <returns>An object that contains information about the match.</returns>
RuleMatch GetMatch(string input, int beginning, int length, int lineNumber);

/// <summary>
/// Gets the highlighting color.
/// </summary>
HighlightingColor Color { get; }

/// <summary>
/// Info about rule. Used to help figure out why rule failed
/// </summary>
string RuleInfo { get; }
}
}
62 changes: 62 additions & 0 deletions ICSharpCode.AvalonEdit/Highlighting/RuleMatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System.Text.RegularExpressions;

namespace ICSharpCode.AvalonEdit.Highlighting
{
/// <summary>
/// And object that contains information about a rule's match
/// </summary>
public class RuleMatch
{
/// <summary>
/// Creates a new RuleMatch instance.
/// </summary>
public RuleMatch() { }

/// <summary>
/// Gets a value indicating whether the match was successful.
/// </summary>
public bool Success { get; set; }

/// <summary>
/// The position in the original string where the first character of captured substring was found.
/// </summary>
public int Index { get; set; }

/// <summary>
/// The length of the captured substring.
/// </summary>
public int Length { get; set; }

/// <summary>
/// Creates a new RuleMatch instance from a <see cref="Match"/> instance.
/// </summary>
/// <param name="match">Match to use</param>
/// <returns>RuleMatch instance built from match parameter</returns>
public static RuleMatch FromRegexMatch(Match match)
{
return new RuleMatch() {
Success = match.Success,
Index = match.Index,
Length = match.Length,
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public object VisitRuleSet(XshdRuleSet ruleSet)
if (span != null) {
rs.Spans.Add(span);
} else {
HighlightingRule elementRule = o as HighlightingRule;
IHighlightingRule elementRule = o as IHighlightingRule;
if (elementRule != null) {
rs.Rules.Add(elementRule);
}
Expand Down