Skip to content
Merged
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
23 changes: 16 additions & 7 deletions AnyText/AnyText.Core/ChangeTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public List<RuleApplicationListMigrationEntry> CalculateListMigrations(List<Rule
continue;
}
var migrateIndex = -1;
if (!IsInsertion(target, context, edit) && !IsObsoleted(current, context, edit))
if (!IsInsertion(target.Essential(), context, edit) && !IsObsoleted(current.Essential(), context, edit))
{
migrateIndex = result.Count;
result.Add(new RuleApplicationListMigrationEntry(index, RuleApplicationListMigrationType.Migrate));
Expand Down Expand Up @@ -374,7 +374,7 @@ private void InsertInsertions(List<RuleApplication> migrateTo, ParseContext cont
{
if (edit.NewText.Length > 1 || (edit.NewText.Length == 1 && edit.NewText[0].Length > 0))
{
while (index < mLen && IsInsertion(migrateTo[index], context, edit))
while (index < mLen && IsInsertion(migrateTo[index].Essential(), context, edit))
{
result.Add(new RuleApplicationListMigrationEntry(index, RuleApplicationListMigrationType.Insert));
index++;
Expand All @@ -387,7 +387,7 @@ private void RemoveObsoleted(List<RuleApplication> old, ParseContext context, Li
{
if (edit.End > edit.Start)
{
while (oldIndex + indexOffset < len && IsObsoleted(old[oldIndex + indexOffset], context, edit))
while (oldIndex + indexOffset < len && IsObsoleted(old[oldIndex + indexOffset].Essential(), context, edit))
{
result.Add(new RuleApplicationListMigrationEntry(oldIndex, RuleApplicationListMigrationType.Remove));
indexOffset++;
Expand Down Expand Up @@ -435,12 +435,21 @@ private bool IsInsertion(RuleApplication ruleApplication, ParseContext context,

private bool IsObsoleted(RuleApplication ruleApplication, ParseContext context, TextEdit byEdit)
{
if (context.Matcher.IsObsoleted(ruleApplication))
if (ruleApplication.CurrentPosition >= byEdit.Start)
{
return true;
var afterEdit = byEdit.EndAfterEdit;
var isObsoletedWithinAfterEdit = ruleApplication.CurrentPosition + ruleApplication.Length <= afterEdit;

if (isObsoletedWithinAfterEdit)
{
return true;
}
if (byEdit.End > afterEdit)
{
return context.Matcher.IsObsoleted(ruleApplication, afterEdit);
}
}
return ruleApplication.CurrentPosition >= byEdit.Start &&
ruleApplication.CurrentPosition + ruleApplication.Length <= byEdit.EndAfterEdit;
return false;
}

public void Reset()
Expand Down
15 changes: 13 additions & 2 deletions AnyText/AnyText.Core/Matcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public bool IsObsoleted(RuleApplication ruleApplication)
{
var column = ruleApplication.Column;
var line = column.Line;
if (!IsObsoleted(line) && line.HasColumn(column))
if (!IsObsoleted(line))
{
return false;
}
Expand All @@ -653,6 +653,17 @@ public bool IsObsoleted(RuleApplication ruleApplication)
return ruleApplication.IterateLiterals(IsObsoleted);
}

/// <summary>
/// Determines whether the given rule application is obsoleted
/// </summary>
/// <param name="ruleApplication">the rule application to check</param>
/// <param name="fromPosition">position from where to start</param>
/// <returns>true, if the rule application is on a line that has been obsoleted</returns>
public bool IsObsoleted(RuleApplication ruleApplication, ParsePosition fromPosition)
{
return ruleApplication.IterateLiterals(IsObsoleted, fromPosition);
}

/// <summary>
/// Deteremines whether the given rule application has an up to date position
/// </summary>
Expand All @@ -661,7 +672,7 @@ public bool IsObsoleted(RuleApplication ruleApplication)
public bool IsFaithfulPosition(RuleApplication ruleApplication)
{
var column = ruleApplication.Column;
return !IsObsoleted(column.Line) && column.Line.HasColumn(column);
return !IsObsoleted(column.Line);
}

private bool IsObsoleted(MemoLine line)
Expand Down
11 changes: 11 additions & 0 deletions AnyText/AnyText.Core/Rules/ChoiceRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ protected internal override bool IsEpsilonAllowed(List<Rule> trace)
return Array.Exists(Alternatives, r => r.Rule.IsEpsilonAllowed(trace));
}


/// <inheritdoc />
protected internal override RuleApplication GetEssentialInnerRuleApplication(RuleApplication ruleApplication)
{
if (ruleApplication is SingleRuleApplication single)
{
return single.Inner.Rule.GetEssentialInnerRuleApplication(single.Inner);
}
return ruleApplication;
}

/// <inheritdoc />
public override RuleApplication Match(ParseContext context, RecursionContext recursionContext, ref ParsePosition position)
{
Expand Down
10 changes: 10 additions & 0 deletions AnyText/AnyText.Core/Rules/FailedChoiceRuleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action,
return true;
}

public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action, ParsePosition from, bool includeFailures)
{
if (includeFailures)
{
var farest = GetRuleApplicationWithFarestExaminationLength();
if (farest != null) return farest.IterateLiterals(action, from, true);
}
return true;
}

public override void Write(PrettyPrintWriter writer, ParseContext context)
{
}
Expand Down
6 changes: 6 additions & 0 deletions AnyText/AnyText.Core/Rules/FailedRuleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action,
return true;
}

/// <inheritdoc />
public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action, ParsePosition from, bool includeFailures)
{
return true;
}

/// <inheritdoc />
public override void Write(PrettyPrintWriter writer, ParseContext context)
{
Expand Down
9 changes: 9 additions & 0 deletions AnyText/AnyText.Core/Rules/InheritedFailRuleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action,
return true;
}

public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action, ParsePosition from, bool includeFailures)
{
if (includeFailures)
{
return _innerFail.IterateLiterals(action, from, true);
}
return true;
}

public override void Write(PrettyPrintWriter writer, ParseContext context)
{
}
Expand Down
16 changes: 16 additions & 0 deletions AnyText/AnyText.Core/Rules/LiteralRuleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action,
return action(this);
}

/// <inheritdoc />
public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action, ParsePosition from, bool includeFailures)
{
if (Comments != null)
{
foreach (var comment in Comments)
{
if (comment.CurrentPosition >= from && !comment.IterateLiterals(action, true))
{
return false;
}
}
}
return action(this);
Comment thread
georghinkel marked this conversation as resolved.
}

/// <inheritdoc />
public override void Write(PrettyPrintWriter writer, ParseContext context)
{
Expand Down
18 changes: 18 additions & 0 deletions AnyText/AnyText.Core/Rules/MultiRuleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,24 @@ public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action,
return true;
}

/// <inheritdoc />
public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action, ParsePosition from, bool includeFailures)
{
var index = FindLargestIndexBefore(from);
if (index < 0 || Inner[index].CurrentPosition + Inner[index].Length < from)
{
index++;
}
for (int i = index; i < Inner.Count; i++)
{
if (!Inner[i].IterateLiterals(action, includeFailures && i == Inner.Count - 1))
Comment thread
georghinkel marked this conversation as resolved.
{
return false;
}
}
return true;
}

private static bool StopsBefore(RuleApplication ruleApplication, ParsePosition to)
{
return ruleApplication.CurrentPosition + ruleApplication.Length < to;
Expand Down
10 changes: 10 additions & 0 deletions AnyText/AnyText.Core/Rules/QuoteRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public override RuleApplication Match(ParseContext context, RecursionContext rec
return new InheritedFailRuleApplication(this, app, app.ExaminedTo);
}

/// <inheritdoc />
protected internal override RuleApplication GetEssentialInnerRuleApplication(RuleApplication ruleApplication)
{
if (ruleApplication is SingleRuleApplication single)
{
return single.Inner.Rule.GetEssentialInnerRuleApplication(single.Inner);
}
return ruleApplication;
}

/// <inheritdoc />
protected internal override RuleApplication Recover(RuleApplication ruleApplication, RuleApplication failedRuleApplication, RuleApplication currentRoot, ParseContext context, out ParsePosition position)
{
Expand Down
12 changes: 12 additions & 0 deletions AnyText/AnyText.Core/Rules/RecoveredSequenceRuleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action,
return action.Invoke(_stopper);
}

public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action, ParsePosition from, bool includeFailures)
{
for (int i = 0; i < _successfulApplications.Count; i++)
{
if (_successfulApplications[i].CurrentPosition >= from && !_successfulApplications[i].IterateLiterals(action, includeFailures))
Comment thread
georghinkel marked this conversation as resolved.
{
return false;
}
}
return action.Invoke(_stopper);
}

public override void Write(PrettyPrintWriter writer, ParseContext context)
{
var lastPos = CurrentPosition;
Expand Down
2 changes: 2 additions & 0 deletions AnyText/AnyText.Core/Rules/Rule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@
return false;
}

protected internal virtual RuleApplication GetEssentialInnerRuleApplication(RuleApplication ruleApplication) => ruleApplication;

Check warning on line 189 in AnyText/AnyText.Core/Rules/Rule.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'Rule.GetEssentialInnerRuleApplication(RuleApplication)'

Check warning on line 189 in AnyText/AnyText.Core/Rules/Rule.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'Rule.GetEssentialInnerRuleApplication(RuleApplication)'

/// <summary>
/// Indicates whether the rule is recursive
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions AnyText/AnyText.Core/Rules/RuleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ internal void ChangeParent(RuleApplication newParent, ParseContext context)
}
}

internal RuleApplication Essential() => Rule.GetEssentialInnerRuleApplication(this);

/// <summary>
/// Gets a collection of parse errors represented by this rule application
/// </summary>
Expand Down Expand Up @@ -519,6 +521,21 @@ protected internal virtual void OnContextChange(object oldContext, object newCon
/// <param name="includeFailures">true, if failed rule applications should be considered, otherwise false</param>
public abstract bool IterateLiterals(Func<LiteralRuleApplication, bool> action, bool includeFailures);

/// <summary>
/// Iterate over all literals
/// </summary>
/// <param name="action">the action that should be performed for all literals</param>
/// <param name="from">the inclusive position from which the literals should be iterated</param>
public bool IterateLiterals(Func<LiteralRuleApplication, bool> action, ParsePosition from) => IterateLiterals(action, from, true);

/// <summary>
/// Iterate over all literals
/// </summary>
/// <param name="action">the action that should be performed for all literals</param>
/// <param name="from">the inclusive position from which the literals should be iterated</param>
/// <param name="includeFailures">true, if failed rule applications should be considered, otherwise false</param>
public abstract bool IterateLiterals(Func<LiteralRuleApplication, bool> action, ParsePosition from, bool includeFailures);

/// <summary>
/// Iterate over all literals
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions AnyText/AnyText.Core/Rules/SequenceRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ protected bool Accept(ref RuleApplication ruleApplication, List<RuleApplication>
return context.AcceptSequenceAdd(this, ref ruleApplication, ruleApplications, ref examined);
}

/// <inheritdoc />
protected internal override RuleApplication GetEssentialInnerRuleApplication(RuleApplication ruleApplication)
{
if (ruleApplication is MultiRuleApplication multiRuleApplication)
{
for (int i = 0; i < Rules.Length; i++)
{
if (!Rules[i].Rule.IsEpsilonAllowed())
{
return multiRuleApplication.Inner[i];
}
}
}
return base.GetEssentialInnerRuleApplication(ruleApplication);
}

/// <summary>
/// The rules that should occur in sequence
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions AnyText/AnyText.Core/Rules/SingleRuleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action,
return Inner.IterateLiterals(action, includeFailures);
}

/// <inheritdoc />
public override bool IterateLiterals(Func<LiteralRuleApplication, bool> action, ParsePosition from, bool includeFailures)
{
return Inner.IterateLiterals(action, from, includeFailures);
}

/// <inheritdoc />
public override void Write(PrettyPrintWriter writer, ParseContext context)
{
Expand Down
3 changes: 2 additions & 1 deletion AnyText/AnyText.history
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ patch: unification fix
patch: carry text edits until successful migration
patch: if a rule application is found, do not increase the deletion index
patch: improve code lenses for synchronizations
major: changed extension API for indentation-aware, create API to propagate context changes
major: changed extension API for indentation-aware, create API to propagate context changes
patch: do not check whether rule application is memoized if it is only interesting to see whether the line is obsolete
Loading
Loading