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
20 changes: 17 additions & 3 deletions CSharpMath.Editor/Extensions/MathList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,25 @@ public static void InsertAndAdvance(this MathList self, ref MathListIndex index,
throw new SubIndexTypeMismatchException(index);
}
}

public static MathListIndex? PreviousOrBeforeWholeList(MathListIndex index) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an extension method?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps. I tried to move it inside the Delete method because the xml comment said "only use from Delete..." but it also needed to be used in DeleteBackwards in MathKeyboard.

return
index.SubIndexType switch
{
MathListSubIndexType.None => index.AtomIndex > -1 ? MathListIndex.Level0Index(index.AtomIndex - 1) : null,
_ =>
(index.SubIndex == null) ? null :
PreviousOrBeforeWholeList(index.SubIndex) is MathListIndex prevSubIndex
? MathListIndex.IndexAtLocation(index.AtomIndex, index.SubIndexType, prevSubIndex) : null,
};
}
public static void RemoveAt(this MathList self, MathListIndex index) {
static bool IsBeforeSubList(MathListIndex index) {
return (index.SubIndex != null && index.SubIndex.AtomIndex == -1) && index.SubIndexType == MathListSubIndexType.None;
}

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) {
if (IsBeforeSubList(index)) {
index.SetTo(
index.LevelDown()
?? throw new InvalidCodePathException($"{nameof(index.SubIndex)} is not null but {nameof(index.LevelDown)} is null"));
Expand All @@ -112,7 +126,7 @@ void RemoveAtInnerList<TAtom>(TAtom atom, int innerListIndex) where TAtom : Math
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) {
if (IsBeforeSubList(index)) {
index.SetTo(
index.LevelDown()
?? throw new InvalidCodePathException($"{nameof(index.SubIndex)} is not null but {nameof(index.LevelDown)} is null"));
Expand Down
2 changes: 1 addition & 1 deletion CSharpMath.Editor/MathKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void MoveCursorRight() {

void DeleteBackwards() {
// delete the last atom from the list
if (HasText && _insertionIndex.PreviousOrBeforeWholeList is MathListIndex previous) {
if (HasText && (Extensions.PreviousOrBeforeWholeList(_insertionIndex)) is MathListIndex previous) {
_insertionIndex = previous;
MathList.RemoveAt(_insertionIndex);
}
Expand Down
8 changes: 0 additions & 8 deletions CSharpMath.Editor/MathListIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ public MathListIndex LevelUpWithSubIndex(MathListSubIndexType type, MathListInde
MathListSubIndexType.None => AtomIndex > 0 ? Level0Index(AtomIndex - 1) : null,
_ => SubIndex?.Previous is MathListIndex prevSubIndex ? IndexAtLocation(AtomIndex, SubIndexType, prevSubIndex) : null,
};
/// <summary>Should be used inside <see cref="Extensions.RemoveAt(Atom.MathList, ref MathListIndex)"/> only!</summary>
internal bool IsBeforeSubList => SubIndex is { AtomIndex: -1, SubIndexType: MathListSubIndexType.None };
/// <summary>Valid for <see cref="Extensions.RemoveAt(Atom.MathList, ref MathListIndex)"/> only!</summary>
internal MathListIndex? PreviousOrBeforeWholeList => SubIndexType switch
{
MathListSubIndexType.None => AtomIndex > -1 ? Level0Index(AtomIndex - 1) : null,
_ => SubIndex?.PreviousOrBeforeWholeList is MathListIndex prevSubIndex ? IndexAtLocation(AtomIndex, SubIndexType, prevSubIndex) : null,
};

///<summary>Returns the next index.</summary>
public MathListIndex Next => SubIndexType switch
Expand Down