Skip to content

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
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6a3d01a
Implemented for fractions, radicals and superscripts
Happypig375 May 24, 2020
9e8e435
Added tests
Happypig375 May 24, 2020
98aa35d
More tests
Happypig375 May 24, 2020
eccb0a8
Tests for Inner
Happypig375 May 24, 2020
ab4a4a7
Delete trash
Happypig375 May 24, 2020
638bd1e
Tests for delete at beginnning of script when both scripts are present
Happypig375 May 24, 2020
f6db396
Also add power counterpart for this test
Happypig375 May 24, 2020
8e2d2ca
And placeholder counterparts
Happypig375 May 24, 2020
fc58258
Deleting placeholders at the start should not eliminate scripts
Happypig375 May 25, 2020
4ec984c
More range checking
Happypig375 May 27, 2020
5616369
Merge branch 'master' into issue108/DeleteInPlaceholder in order to make
Happypig375 May 27, 2020
f018231
Remove refs on MathListIndex inputs
charlesroddie Jun 17, 2020
85427c6
Move IsBeforeSubList and PreviousOrBeforeWholeList to prevent usage i…
charlesroddie Jun 17, 2020
4858a68
Rename SetTo to ReplaceWith
charlesroddie Jun 17, 2020
624ce67
Accepted simplification recommendations
charlesroddie Jun 25, 2020
bf87ef0
Merge branch 'master' into issue108/DeleteInPlaceholder
Happypig375 Aug 5, 2020
da9c95b
Merge branch 'issue108/DeleteInPlaceholder' of https://github.com/ver…
charlesroddie Aug 10, 2020
4d2b72a
tweak wording
charlesroddie Aug 10, 2020
c8675a1
remove unnecessary null checks
charlesroddie Aug 11, 2020
ac01721
add documentation TODOs
charlesroddie Aug 11, 2020
5b1ad8e
Add TODOs
charlesroddie Aug 11, 2020
9e91a94
failing test is unnecessary
charlesroddie Aug 11, 2020
5a764bb
add TODO
charlesroddie Aug 11, 2020
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
45 changes: 24 additions & 21 deletions CSharpMath.Editor/Extensions/MathList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ public static void InsertAndAdvance(this MathList self, ref MathListIndex index,
}
}

public static void RemoveAt(this MathList self, ref MathListIndex index) {
void RemoveAtInnerList<TAtom>(ref MathListIndex index, TAtom atom, int innerListIndex) where TAtom : MathAtom, IMathListContainer {
public static void RemoveAt(this MathList self, MathListIndex index) {
void RemoveAtInnerList<TAtom>(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);
index.SetTo(
index.LevelDown()
?? throw new InvalidCodePathException($"{nameof(index.SubIndex)} is not null but {nameof(index.LevelDown)} is null"));
self.RemoveAt(index);
MathListIndex tempIndex = index;
int i = 0;
foreach (var innerList in atom.InnerLists)
Expand All @@ -106,31 +107,32 @@ void RemoveAtInnerList<TAtom>(ref MathListIndex index, TAtom atom, int innerList
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);
} else atom.InnerLists.ElementAt(innerListIndex).RemoveAt(index.SubIndex);
}
void RemoveAtInnerScript(ref MathListIndex index, MathAtom atom, bool superscript) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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");
index.SetTo(
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;
else index.SetTo(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);
} else script.RemoveAt(index.SubIndex);
}

if (index.AtomIndex < -1 || index.AtomIndex >= self.Atoms.Count)
throw new IndexOutOfRangeException($"Deletion index {index.AtomIndex} is out of bounds for list of size {self.Atoms.Count}");
switch (index.SubIndexType) {
case MathListSubIndexType.None:
if (index.AtomIndex == -1) {
index = index.Next;
index.SetTo(index.Next);
if (self.Atoms[index.AtomIndex] is Atoms.Placeholder { Superscript: var super, Subscript: var sub }) {
self.RemoveAt(index.AtomIndex);
var tempIndex = index;
Expand Down Expand Up @@ -169,37 +171,38 @@ void RemoveAtInnerScript(ref MathListIndex index, MathAtom atom, bool superscrip
previous.Subscript.Append(currentAtom.Subscript);
self.RemoveAt(index.AtomIndex);
// it was in the nucleus and we removed it, get out of the nucleus and get in the nucleus of the previous one.
index = downIndex.Previous is MathListIndex downPrev
index.SetTo(
downIndex.Previous is MathListIndex downPrev
? downPrev.LevelUpWithSubIndex(MathListSubIndexType.BetweenBaseAndScripts, MathListIndex.Level0Index(1))
: downIndex;
: downIndex);
break;
}
// insert placeholder since we couldn't place the scripts in previous atom
var insertionAtom = LaTeXSettings.Placeholder;
insertionAtom.Subscript.Append(currentAtom.Subscript);
insertionAtom.Superscript.Append(currentAtom.Superscript);
self.RemoveAt(index.AtomIndex);
index = downIndex;
index.SetTo(downIndex);
self.InsertAndAdvance(ref index, insertionAtom, MathListSubIndexType.None);
index = index.Previous ?? throw new InvalidCodePathException("Cannot go back after insertion?");
index.SetTo(index.Previous ?? throw new InvalidCodePathException("Cannot go back after insertion?"));
return;
case MathListSubIndexType.Radicand:
case MathListSubIndexType.Degree:
if (!(self.Atoms[index.AtomIndex] is Atoms.Radical radical))
throw new SubIndexTypeMismatchException(typeof(Atoms.Radical), index);
if (index.SubIndexType == MathListSubIndexType.Degree)
RemoveAtInnerList(ref index, radical, 0);
RemoveAtInnerList(radical, 0);
else
RemoveAtInnerList(ref index, radical, 1);
RemoveAtInnerList(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)
RemoveAtInnerList(ref index, frac, 0);
RemoveAtInnerList(frac, 0);
else
RemoveAtInnerList(ref index, frac, 1);
RemoveAtInnerList(frac, 1);
break;
case MathListSubIndexType.Subscript:
var current = self.Atoms[index.AtomIndex];
Expand All @@ -216,7 +219,7 @@ void RemoveAtInnerScript(ref MathListIndex index, MathAtom atom, bool superscrip
case MathListSubIndexType.Inner:
if (!(self.Atoms[index.AtomIndex] is Atoms.Inner inner))
throw new SubIndexTypeMismatchException(typeof(Atoms.Inner), index);
RemoveAtInnerList(ref index, inner, 0);
RemoveAtInnerList(inner, 0);
break;
default:
throw new SubIndexTypeMismatchException(index);
Expand All @@ -225,7 +228,7 @@ void RemoveAtInnerScript(ref MathListIndex index, MathAtom atom, bool superscrip
// 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.SetTo(index.Previous ?? throw new InvalidCodePathException("Cannot go back after insertion?"));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CSharpMath.Editor/MathKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void DeleteBackwards() {
// delete the last atom from the list
if (HasText && _insertionIndex.PreviousOrBeforeWholeList is MathListIndex previous) {
_insertionIndex = previous;
MathList.RemoveAt(ref _insertionIndex);
MathList.RemoveAt(_insertionIndex);
}
}

Expand Down
6 changes: 6 additions & 0 deletions CSharpMath.Editor/MathListIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ private MathListIndex() { }
public int AtomIndex { get; set; }
///<summary>The type of subindex, e.g. superscript, numerator etc.</summary>
public MathListSubIndexType SubIndexType { get; set; }

///<summary>The index into the sublist.</summary>
public MathListIndex? SubIndex;
public void SetTo(MathListIndex replacement) {
AtomIndex = replacement.AtomIndex;
SubIndexType = replacement.SubIndexType;
SubIndex = replacement.SubIndex;
}

/** <summary>Factory function to create a `MathListIndex` with no subindexes.</summary>
<param name="index">The index of the atom that the `MathListIndex` points at.</param>
Expand Down