-
Notifications
You must be signed in to change notification settings - Fork 68
Delete when in a placeholder should simplify containing expression #117
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
Draft
Happypig375
wants to merge
23
commits into
master
Choose a base branch
from
issue108/DeleteInPlaceholder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6a3d01a
Implemented for fractions, radicals and superscripts
Happypig375 9e8e435
Added tests
Happypig375 98aa35d
More tests
Happypig375 eccb0a8
Tests for Inner
Happypig375 ab4a4a7
Delete trash
Happypig375 638bd1e
Tests for delete at beginnning of script when both scripts are present
Happypig375 f6db396
Also add power counterpart for this test
Happypig375 8e2d2ca
And placeholder counterparts
Happypig375 fc58258
Deleting placeholders at the start should not eliminate scripts
Happypig375 4ec984c
More range checking
Happypig375 5616369
Merge branch 'master' into issue108/DeleteInPlaceholder in order to make
Happypig375 f018231
Remove refs on MathListIndex inputs
charlesroddie 85427c6
Move IsBeforeSubList and PreviousOrBeforeWholeList to prevent usage i…
charlesroddie 4858a68
Rename SetTo to ReplaceWith
charlesroddie 624ce67
Accepted simplification recommendations
charlesroddie bf87ef0
Merge branch 'master' into issue108/DeleteInPlaceholder
Happypig375 da9c95b
Merge branch 'issue108/DeleteInPlaceholder' of https://github.com/ver…
charlesroddie 4d2b72a
tweak wording
charlesroddie c8675a1
remove unnecessary null checks
charlesroddie ac01721
add documentation TODOs
charlesroddie 5b1ad8e
Add TODOs
charlesroddie 9e91a94
failing test is unnecessary
charlesroddie 5a764bb
add TODO
charlesroddie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ namespace CSharpMath.Editor { | |
using Atom; | ||
using Atoms = Atom.Atoms; | ||
using Structures; | ||
using System.Linq; | ||
|
||
partial class Extensions { | ||
static void InsertAtAtomIndexAndAdvance(this MathList self, int atomIndex, MathAtom atom, ref MathListIndex advance, MathListSubIndexType advanceType) { | ||
if (atomIndex < 0 || atomIndex > self.Count) | ||
|
@@ -81,12 +83,66 @@ public static void InsertAndAdvance(this MathList self, ref MathListIndex index, | |
} | ||
|
||
public static void RemoveAt(this MathList self, ref MathListIndex index) { | ||
index ??= MathListIndex.Level0Index(0); | ||
void RemoveAtInnerList<TAtom>(ref MathListIndex index, TAtom atom, int innerListIndex) where TAtom : MathAtom, IMathListContainer { | ||
if (index.SubIndex is null) throw new InvalidCodePathException($"{nameof(index.SubIndex)} should exist"); | ||
if (index.IsBeforeSubList) { | ||
index = index.LevelDown() | ||
?? throw new InvalidCodePathException($"{nameof(index.SubIndex)} is not null but {nameof(index.LevelDown)} is null"); | ||
self.RemoveAt(ref index); | ||
MathListIndex tempIndex = index; | ||
int i = 0; | ||
foreach (var innerList in atom.InnerLists) | ||
if (!(innerList.Count == 1 && innerList[0] is Atoms.Placeholder)) | ||
if (i++ < innerListIndex) { | ||
foreach (var inner in innerList) | ||
self.InsertAndAdvance(ref index, inner, MathListSubIndexType.None); | ||
tempIndex = index; | ||
} | ||
else | ||
foreach (var inner in innerList) | ||
self.InsertAndAdvance(ref tempIndex, inner, MathListSubIndexType.None); | ||
if(index.SubIndexType != MathListSubIndexType.None && tempIndex.AtomIndex == 0 // We deleted an atom only consisting of placeholders | ||
|| atom.Superscript.Count > 0 || atom.Subscript.Count > 0) | ||
self.InsertAndAdvance(ref tempIndex, LaTeXSettings.Placeholder, MathListSubIndexType.None); | ||
if(atom.Superscript.Count > 0) self[tempIndex.AtomIndex - 1].Superscript.Append(atom.Superscript); | ||
if(atom.Subscript.Count > 0) self[tempIndex.AtomIndex - 1].Subscript.Append(atom.Subscript); | ||
} else atom.InnerLists.ElementAt(innerListIndex).RemoveAt(ref index.SubIndex); | ||
} | ||
void RemoveAtInnerScript(ref MathListIndex index, MathAtom atom, bool superscript) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for this function |
||
if (index.SubIndex is null) throw new InvalidCodePathException($"{nameof(index.SubIndex)} should exist"); | ||
var script = superscript ? atom.Superscript : atom.Subscript; | ||
if (index.IsBeforeSubList) { | ||
index = index.LevelDown() | ||
?? throw new InvalidCodePathException($"{nameof(index.SubIndex)} is not null but {nameof(index.LevelDown)} is null"); | ||
if (atom is Atoms.Placeholder && (superscript ? atom.Subscript : atom.Superscript).Count == 0) | ||
self.RemoveAt(index.AtomIndex); | ||
else index = index.Next; | ||
var tempIndex = index; | ||
if (!(script.Count == 1 && script[0] is Atoms.Placeholder)) | ||
foreach (var inner in script) | ||
self.InsertAndAdvance(ref tempIndex, inner, MathListSubIndexType.None); | ||
script.Clear(); | ||
} else script.RemoveAt(ref index.SubIndex); | ||
} | ||
|
||
if (index.AtomIndex > self.Atoms.Count) | ||
throw new IndexOutOfRangeException($"Index {index.AtomIndex} is out of bounds for list of size {self.Atoms.Count}"); | ||
switch (index.SubIndexType) { | ||
case MathListSubIndexType.None: | ||
self.RemoveAt(index.AtomIndex); | ||
if (index.AtomIndex == -1) { | ||
index = index.Next; | ||
if (self.Atoms[index.AtomIndex] is Atoms.Placeholder { Superscript: var super, Subscript: var sub }) { | ||
self.RemoveAt(index.AtomIndex); | ||
var tempIndex = index; | ||
if (!(sub.Count == 1 && sub[0] is Atoms.Placeholder)) | ||
foreach (var s in sub) | ||
self.InsertAndAdvance(ref tempIndex, s, MathListSubIndexType.None); | ||
if (!(super.Count == 1 && super[0] is Atoms.Placeholder)) | ||
foreach (var s in super) | ||
self.InsertAndAdvance(ref tempIndex, s, MathListSubIndexType.None); | ||
} | ||
} else | ||
self.RemoveAt(index.AtomIndex); | ||
break; | ||
case var _ when index.SubIndex is null: | ||
throw new InvalidCodePathException("index.SubIndex is null despite non-None subindex type"); | ||
|
@@ -132,33 +188,35 @@ public static void RemoveAt(this MathList self, ref MathListIndex index) { | |
if (!(self.Atoms[index.AtomIndex] is Atoms.Radical radical)) | ||
throw new SubIndexTypeMismatchException(typeof(Atoms.Radical), index); | ||
if (index.SubIndexType == MathListSubIndexType.Degree) | ||
radical.Degree.RemoveAt(ref index.SubIndex); | ||
else radical.Radicand.RemoveAt(ref index.SubIndex); | ||
RemoveAtInnerList(ref index, radical, 0); | ||
else | ||
RemoveAtInnerList(ref index, radical, 1); | ||
break; | ||
case MathListSubIndexType.Numerator: | ||
case MathListSubIndexType.Denominator: | ||
if (!(self.Atoms[index.AtomIndex] is Atoms.Fraction frac)) | ||
throw new SubIndexTypeMismatchException(typeof(Atoms.Fraction), index); | ||
if (index.SubIndexType == MathListSubIndexType.Numerator) | ||
frac.Numerator.RemoveAt(ref index.SubIndex); | ||
else frac.Denominator.RemoveAt(ref index.SubIndex); | ||
RemoveAtInnerList(ref index, frac, 0); | ||
else | ||
RemoveAtInnerList(ref index, frac, 1); | ||
break; | ||
case MathListSubIndexType.Subscript: | ||
var current = self.Atoms[index.AtomIndex]; | ||
if (current.Subscript.IsEmpty()) | ||
throw new SubIndexTypeMismatchException(index); | ||
current.Subscript.RemoveAt(ref index.SubIndex); | ||
RemoveAtInnerScript(ref index, current, false); | ||
break; | ||
case MathListSubIndexType.Superscript: | ||
current = self.Atoms[index.AtomIndex]; | ||
if (current.Superscript.IsEmpty()) | ||
throw new SubIndexTypeMismatchException(index); | ||
current.Superscript.RemoveAt(ref index.SubIndex); | ||
RemoveAtInnerScript(ref index, current, true); | ||
break; | ||
case MathListSubIndexType.Inner: | ||
if (!(self.Atoms[index.AtomIndex] is Atoms.Inner inner)) | ||
throw new SubIndexTypeMismatchException(typeof(Atoms.Inner), index); | ||
inner.InnerList.RemoveAt(ref index.SubIndex); | ||
RemoveAtInnerList(ref index, inner, 0); | ||
break; | ||
default: | ||
throw new SubIndexTypeMismatchException(index); | ||
|
@@ -167,7 +225,7 @@ public static void RemoveAt(this MathList self, ref MathListIndex index) { | |
// We have deleted to the beginning of the line and it is not the outermost line | ||
if (self.AtomAt(index) is null) { | ||
self.InsertAndAdvance(ref index, LaTeXSettings.Placeholder, MathListSubIndexType.None); | ||
index = index.Previous ?? throw new InvalidCodePathException("Cannot go back after insertion?"); ; | ||
index = index.Previous ?? throw new InvalidCodePathException("Cannot go back after insertion?"); | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you document this a bit? It's not clear from the name of the function and inputs:
atom
an Atom that resides atindex
?atom
the thing to be removed, or is instead a subatom ofatom
residing atinnerListIndex
withinatom
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
atom
is aMathAtom
directly contained inself
that contains(index.SubIndex.AtomIndex > -1
)/is(index.SubIndex.AtomIndex = -1
) the atom to remove.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK that takes us closer. What would be the right type for AtomIndex to have?
Nullable<int>
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An AtomIndex of
-1
indicates deletion at the beginning of line (not pointing to any atom). It is not a valid value for other methods acceptingMathListIndex
, which is whyPreviousOrBeforeWholeList
isinternal
.