Skip to content
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
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
Copy Markdown
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) {
Comment thread
charlesroddie marked this conversation as resolved.
Outdated
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