Skip to content

Commit 4f1af4a

Browse files
committed
Finalize
1 parent b35873a commit 4f1af4a

File tree

15 files changed

+106
-138
lines changed

15 files changed

+106
-138
lines changed

CSharpMath.Core.Tests/Editor/IndexForPointTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public void Add((double x, double y) point,
1919
case var _:
2020
mathListIndex = new(subIndexRecursive[^1].subIndex);
2121
for (var i = subIndexRecursive.Length - 2; i >= 0; i--)
22-
mathListIndex = mathListIndex.WrapInIndex(subIndexRecursive[i].subIndex, subIndexRecursive[i + 1].subType);
23-
mathListIndex = mathListIndex.WrapInIndex(index, subIndexRecursive[0].subType);
22+
mathListIndex = mathListIndex.Wrap(subIndexRecursive[i].subIndex, subIndexRecursive[i + 1].subType);
23+
mathListIndex = mathListIndex.Wrap(index, subIndexRecursive[0].subType);
2424
goto default;
2525
default:
2626
Add(new PointF((float)point.x, (float)point.y), mathListIndex);

CSharpMath.Core.Tests/Editor/PointForIndexTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using Xunit;
33

44
namespace CSharpMath.Core.EditorTests {
5-
using DisplayTests;
65
using BackEnd;
6+
using DisplayTests;
77
using Editor;
88
using static IndexForPointTests;
99
// Use CSharpMath.Core.Example to visualize the test cases

CSharpMath.Rendering.Tests/TestRendering.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ protected void Run<TContent>(
163163
return;
164164
}
165165

166-
DisplayTests.Approximately.Equal(expectedStream.Length, actualStream.Length, expectedStream.Length * FileSizeTolerance);
166+
Core.DisplayTests.Approximately.Equal(expectedStream.Length, actualStream.Length, expectedStream.Length * FileSizeTolerance);
167167
if (FileSizeTolerance == 0)
168168
Assert.True(contentsMatch, "The images differ.");
169169
}

CSharpMath/CSharpMath.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1515
</PackageReference>
16+
<PackageReference Include="IsExternalInit" Version="1.0.0" PrivateAssets="all" />
1617
<PackageReference Include="System.Memory" Version="4.6.3" />
1718
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.2" />
1819
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="all" />

CSharpMath/Editor/Extensions/FractionDisplay.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ partial class Extensions {
1717
// We are after the fraction
1818
? new(self.Range.End)
1919
: point.Y > self.LinePosition + PixelDelta
20-
? self.Numerator.IndexForPoint(context, point)?.WrapInIndex(self.Range.Location, MathListSubIndexType.Numerator)
20+
? self.Numerator.IndexForPoint(context, point)?.Wrap(self.Range.Location, MathListSubIndexType.Numerator)
2121
: point.Y < self.LinePosition - PixelDelta
22-
? self.Denominator.IndexForPoint(context, point)?.WrapInIndex(self.Range.Location, MathListSubIndexType.Denominator)
22+
? self.Denominator.IndexForPoint(context, point)?.Wrap(self.Range.Location, MathListSubIndexType.Denominator)
2323
: point.X > self.Position.X + self.Width / 2
2424
? new(self.Range.End)
2525
: new(self.Range.Location);

CSharpMath/Editor/Extensions/InnerDisplay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ partial class Extensions {
1616
: point.X > self.Position.X + self.Width - (self.Right?.Width / 2 ?? 0)
1717
//We are after the inner
1818
? new(self.Range.End)
19-
: self.Inner.IndexForPoint(context, point)?.WrapInIndex(self.Range.Location, MathListSubIndexType.Inner);
19+
: self.Inner.IndexForPoint(context, point)?.Wrap(self.Range.Location, MathListSubIndexType.Inner);
2020

2121
public static PointF? PointForIndex<TFont, TGlyph>(
2222
this InnerDisplay<TFont, TGlyph> self,

CSharpMath/Editor/Extensions/LargeOpLimitsDisplay.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ partial class Extensions {
1818
// We are after the large operator
1919
? new(self.Range.End)
2020
: self.UpperLimit is { } u && point.Y > self.Position.Y + u.Position.Y - PixelDelta
21-
? u.IndexForPoint(context, point)?.WrapInIndex(self.Range.Location, MathListSubIndexType.Superscript)
21+
? u.IndexForPoint(context, point)?.Wrap(self.Range.Location, MathListSubIndexType.Superscript)
2222
: self.LowerLimit is { } l && point.Y < self.Position.Y + l.Position.Y + l.DisplayBounds().Height + PixelDelta
23-
? l.IndexForPoint(context, point)?.WrapInIndex(self.Range.Location, MathListSubIndexType.Subscript)
23+
? l.IndexForPoint(context, point)?.Wrap(self.Range.Location, MathListSubIndexType.Subscript)
2424
: point.X > self.Position.X + self.Width * 3 / 4
2525
? new(self.Range.End)
2626
: point.X > self.Position.X + self.Width / 2

CSharpMath/Editor/Extensions/ListDisplay.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ closestLine.LinePosition is LinePosition.Subscript
8484
if (closestLine.IndexInParent is int.MinValue)
8585
throw new ArgumentException
8686
($"Index was not set for a {indexType} in the {nameof(ListDisplay<TFont, TGlyph>)}.", nameof(self));
87-
return index?.WrapInIndex(closestLine.IndexInParent, indexType);
87+
return index?.Wrap(closestLine.IndexInParent, indexType);
8888
} else if (displayWithPoint.HasScript)
8989
// The display list has a subscript or a superscript.
9090
// If the index is at the end of the atom,
@@ -108,7 +108,8 @@ closestLine.LinePosition is LinePosition.Subscript
108108
(MathListSubIndexType.BetweenBaseAndScripts, var subIndex) => display.PointForIndex(context, new(index.AtomIndex + subIndex.AtomIndex)),
109109
null => display.PointForIndex(context, index),
110110
(_, var subIndex) => display.PointForIndex(context, subIndex) // Recurse
111-
}; else
111+
};
112+
else
112113
// Outside the range
113114
return null;
114115
if (position is PointF found) {
@@ -166,7 +167,7 @@ when largeOps.Range.Contains(index.AtomIndex):
166167
((MathListSubIndexType.Inner, _), InnerDisplay<TFont, TGlyph> inner) => inner.Inner,
167168
((MathListSubIndexType.Superscript or MathListSubIndexType.Subscript, _), _) =>
168169
throw new Atom.InvalidCodePathException("Superscripts and subscripts should have been handled in a separate case above."),
169-
_ => throw new SubIndexTypeMismatchException(index),
170+
((var type, _), _) => throw new ArgumentOutOfRangeException(nameof(index), type, "Index type out of valid range."),
170171
};
171172
return null;
172173
}

CSharpMath/Editor/Extensions/MathList.cs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public static MathListIndex InsertAndAdvance(this MathList self, MathListIndex i
2525
throw new IndexOutOfRangeException($"Index {index.AtomIndex} is out of bounds for list of size {self.Atoms.Count}");
2626
switch (index.SubIndexInfo) {
2727
case null:
28-
return self.InsertAtAtomIndexAndAdvance(index.AtomIndex, atom, index, null);
28+
return self.InsertAtAtomIndexAndAdvance(index.AtomIndex, atom, index, advanceType);
2929
case (MathListSubIndexType.BetweenBaseAndScripts, _):
3030
var currentAtom = self.Atoms[index.AtomIndex];
3131
if (currentAtom.Subscript.IsEmpty() && currentAtom.Superscript.IsEmpty())
32-
throw new SubIndexTypeMismatchException(index);
32+
throw new SubIndexTypeMismatchException(nameof(MathListSubIndexType.BetweenBaseAndScripts), index.AtomIndex);
3333
if (atom.Subscript.IsNonEmpty() || atom.Superscript.IsNonEmpty())
3434
throw new ArgumentException("Cannot fuse with an atom that already has a subscript or a superscript");
3535
atom.Subscript.Append(currentAtom.Subscript);
@@ -42,30 +42,30 @@ public static MathListIndex InsertAndAdvance(this MathList self, MathListIndex i
4242
return self.InsertAtAtomIndexAndAdvance(atomIndex + 1, atom, index, advanceType);
4343
case (MathListSubIndexType.Degree, var subIndex)
4444
when self.Atoms[index.AtomIndex] is Atoms.Radical radical ? true
45-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Radical), index):
46-
return radical.Degree.InsertAndAdvance(subIndex, atom, advanceType).WrapInIndex(index.AtomIndex, MathListSubIndexType.Degree);
45+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Radical), index.AtomIndex):
46+
return radical.Degree.InsertAndAdvance(subIndex, atom, advanceType).Wrap(index.AtomIndex, MathListSubIndexType.Degree);
4747
case (MathListSubIndexType.Radicand, var subIndex)
4848
when self.Atoms[index.AtomIndex] is Atoms.Radical radical ? true
49-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Radical), index):
50-
return radical.Degree.InsertAndAdvance(subIndex, atom, advanceType).WrapInIndex(index.AtomIndex, MathListSubIndexType.Radicand);
49+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Radical), index.AtomIndex):
50+
return radical.Radicand.InsertAndAdvance(subIndex, atom, advanceType).Wrap(index.AtomIndex, MathListSubIndexType.Radicand);
5151
case (MathListSubIndexType.Numerator, var subIndex)
5252
when self.Atoms[index.AtomIndex] is Atoms.Fraction frac ? true
53-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Fraction), index):
54-
return frac.Numerator.InsertAndAdvance(subIndex, atom, advanceType).WrapInIndex(index.AtomIndex, MathListSubIndexType.Numerator);
53+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Fraction), index.AtomIndex):
54+
return frac.Numerator.InsertAndAdvance(subIndex, atom, advanceType).Wrap(index.AtomIndex, MathListSubIndexType.Numerator);
5555
case (MathListSubIndexType.Denominator, var subIndex)
5656
when self.Atoms[index.AtomIndex] is Atoms.Fraction frac ? true
57-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Fraction), index):
58-
return frac.Denominator.InsertAndAdvance(subIndex, atom, advanceType).WrapInIndex(index.AtomIndex, MathListSubIndexType.Denominator);
57+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Fraction), index.AtomIndex):
58+
return frac.Denominator.InsertAndAdvance(subIndex, atom, advanceType).Wrap(index.AtomIndex, MathListSubIndexType.Denominator);
5959
case (MathListSubIndexType.Subscript, var subIndex):
60-
return self.Atoms[index.AtomIndex].Subscript.InsertAndAdvance(subIndex, atom, advanceType).WrapInIndex(index.AtomIndex, MathListSubIndexType.Subscript);
60+
return self.Atoms[index.AtomIndex].Subscript.InsertAndAdvance(subIndex, atom, advanceType).Wrap(index.AtomIndex, MathListSubIndexType.Subscript);
6161
case (MathListSubIndexType.Superscript, var subIndex):
62-
return self.Atoms[index.AtomIndex].Superscript.InsertAndAdvance(subIndex, atom, advanceType).WrapInIndex(index.AtomIndex, MathListSubIndexType.Superscript);
62+
return self.Atoms[index.AtomIndex].Superscript.InsertAndAdvance(subIndex, atom, advanceType).Wrap(index.AtomIndex, MathListSubIndexType.Superscript);
6363
case (MathListSubIndexType.Inner, var subIndex)
6464
when self.Atoms[index.AtomIndex] is Atoms.Inner inner ? true
65-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Fraction), index):
66-
return inner.InnerList.InsertAndAdvance(subIndex, atom, advanceType).WrapInIndex(index.AtomIndex, MathListSubIndexType.Inner);
67-
default:
68-
throw new SubIndexTypeMismatchException(index);
65+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Inner), index.AtomIndex):
66+
return inner.InnerList.InsertAndAdvance(subIndex, atom, advanceType).Wrap(index.AtomIndex, MathListSubIndexType.Inner);
67+
case (var type, _):
68+
throw new ArgumentOutOfRangeException(nameof(index), type, "Index type out of valid range.");
6969
}
7070
}
7171
/// <summary>Removes the atom at <paramref name="index"/>, placing a new placeholder if all atoms removed, and returns a new <see cref="MathListIndex"/> with <paramref name="index"/> advanced to the next position indicated by <paramref name="advanceType"/>.</summary>
@@ -79,7 +79,7 @@ public static MathListIndex RemoveAt(this MathList self, MathListIndex index) {
7979
case (MathListSubIndexType.BetweenBaseAndScripts, _):
8080
var currentAtom = self.Atoms[index.AtomIndex];
8181
if (currentAtom.Subscript.IsEmpty() && currentAtom.Superscript.IsEmpty())
82-
throw new SubIndexTypeMismatchException(index);
82+
throw new SubIndexTypeMismatchException(nameof(MathListSubIndexType.BetweenBaseAndScripts), index.AtomIndex);
8383
var downIndex = index.LevelDown() ?? throw new InvalidCodePathException("downIndex is null");
8484
if (index.AtomIndex > 0 &&
8585
self.Atoms[index.AtomIndex - 1] is MathAtom previous &&
@@ -112,43 +112,43 @@ public static MathListIndex RemoveAt(this MathList self, MathListIndex index) {
112112
return index.Previous ?? throw new InvalidCodePathException("Cannot go back after insertion?");
113113
case (MathListSubIndexType.Degree, var subIndex)
114114
when self.Atoms[index.AtomIndex] is Atoms.Radical radical ? true
115-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Radical), index):
116-
index = radical.Degree.RemoveAt(subIndex).WrapInIndex(index.AtomIndex, MathListSubIndexType.Degree);
115+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Radical), index.AtomIndex):
116+
index = radical.Degree.RemoveAt(subIndex).Wrap(index.AtomIndex, MathListSubIndexType.Degree);
117117
break;
118118
case (MathListSubIndexType.Radicand, var subIndex)
119119
when self.Atoms[index.AtomIndex] is Atoms.Radical radical ? true
120-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Radical), index):
121-
index = radical.Radicand.RemoveAt(subIndex).WrapInIndex(index.AtomIndex, MathListSubIndexType.Radicand);
120+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Radical), index.AtomIndex):
121+
index = radical.Radicand.RemoveAt(subIndex).Wrap(index.AtomIndex, MathListSubIndexType.Radicand);
122122
break;
123123
case (MathListSubIndexType.Numerator, var subIndex)
124124
when self.Atoms[index.AtomIndex] is Atoms.Fraction frac ? true
125-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Fraction), index):
126-
index = frac.Numerator.RemoveAt(subIndex).WrapInIndex(index.AtomIndex, MathListSubIndexType.Numerator);
125+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Fraction), index.AtomIndex):
126+
index = frac.Numerator.RemoveAt(subIndex).Wrap(index.AtomIndex, MathListSubIndexType.Numerator);
127127
break;
128128
case (MathListSubIndexType.Denominator, var subIndex)
129129
when self.Atoms[index.AtomIndex] is Atoms.Fraction frac ? true
130-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Fraction), index):
131-
index = frac.Denominator.RemoveAt(subIndex).WrapInIndex(index.AtomIndex, MathListSubIndexType.Denominator);
130+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Fraction), index.AtomIndex):
131+
index = frac.Denominator.RemoveAt(subIndex).Wrap(index.AtomIndex, MathListSubIndexType.Denominator);
132132
break;
133133
case (MathListSubIndexType.Subscript, var subIndex):
134134
var current = self.Atoms[index.AtomIndex];
135135
if (current.Subscript.IsEmpty())
136-
throw new SubIndexTypeMismatchException(index);
137-
index = current.Subscript.RemoveAt(subIndex).WrapInIndex(index.AtomIndex, MathListSubIndexType.Subscript);
136+
throw new SubIndexTypeMismatchException(nameof(MathListSubIndexType.Subscript), index.AtomIndex);
137+
index = current.Subscript.RemoveAt(subIndex).Wrap(index.AtomIndex, MathListSubIndexType.Subscript);
138138
break;
139139
case (MathListSubIndexType.Superscript, var subIndex):
140140
current = self.Atoms[index.AtomIndex];
141141
if (current.Superscript.IsEmpty())
142-
throw new SubIndexTypeMismatchException(index);
143-
index = current.Superscript.RemoveAt(subIndex).WrapInIndex(index.AtomIndex, MathListSubIndexType.Superscript);
142+
throw new SubIndexTypeMismatchException(nameof(MathListSubIndexType.Superscript), index.AtomIndex);
143+
index = current.Superscript.RemoveAt(subIndex).Wrap(index.AtomIndex, MathListSubIndexType.Superscript);
144144
break;
145145
case (MathListSubIndexType.Inner, var subIndex)
146146
when self.Atoms[index.AtomIndex] is Atoms.Inner inner ? true
147-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Inner), index):
148-
index = inner.InnerList.RemoveAt(subIndex).WrapInIndex(index.AtomIndex, MathListSubIndexType.Inner);
147+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Inner), index.AtomIndex):
148+
index = inner.InnerList.RemoveAt(subIndex).Wrap(index.AtomIndex, MathListSubIndexType.Inner);
149149
break;
150-
default:
151-
throw new SubIndexTypeMismatchException(index);
150+
case (var type, _):
151+
throw new ArgumentOutOfRangeException(nameof(index), type, "Index type out of valid range.");
152152
}
153153
if (index.Previous is null && index.SubIndexInfo is { })
154154
// We have deleted to the beginning of the line and it is not the outermost line
@@ -168,37 +168,37 @@ public static void RemoveAtoms(this MathList self, MathListRange? nullableRange)
168168
throw new NotSupportedException("Nuclear fission is not supported");
169169
case (MathListSubIndexType.Degree, _)
170170
when self.Atoms[start.AtomIndex] is Atoms.Radical radical ? true
171-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Radical), start):
171+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Radical), start.AtomIndex):
172172
radical.Degree.RemoveAtoms(range.SubIndexRange);
173173
break;
174174
case (MathListSubIndexType.Radicand, _)
175175
when self.Atoms[start.AtomIndex] is Atoms.Radical radical ? true
176-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Radical), start):
176+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Radical), start.AtomIndex):
177177
radical.Radicand.RemoveAtoms(range.SubIndexRange);
178178
break;
179179
case (MathListSubIndexType.Numerator, _)
180180
when self.Atoms[start.AtomIndex] is Atoms.Fraction frac ? true
181-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Fraction), start):
181+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Fraction), start.AtomIndex):
182182
frac.Numerator.RemoveAtoms(range.SubIndexRange);
183183
break;
184-
case (MathListSubIndexType.Radicand, _)
184+
case (MathListSubIndexType.Denominator, _)
185185
when self.Atoms[start.AtomIndex] is Atoms.Fraction frac ? true
186-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Fraction), start):
186+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Fraction), start.AtomIndex):
187187
frac.Denominator.RemoveAtoms(range.SubIndexRange);
188188
break;
189189
case (MathListSubIndexType.Subscript, _):
190190
var current = self.Atoms[start.AtomIndex];
191-
if (current.Subscript.IsEmpty()) throw new SubIndexTypeMismatchException(start);
191+
if (current.Subscript.IsEmpty()) throw new SubIndexTypeMismatchException(nameof(MathListSubIndexType.Subscript), start.AtomIndex);
192192
current.Subscript.RemoveAtoms(range.SubIndexRange);
193193
break;
194194
case (MathListSubIndexType.Superscript, _):
195195
current = self.Atoms[start.AtomIndex];
196-
if (current.Superscript.IsEmpty()) throw new SubIndexTypeMismatchException(start);
196+
if (current.Superscript.IsEmpty()) throw new SubIndexTypeMismatchException(nameof(MathListSubIndexType.Superscript), start.AtomIndex);
197197
current.Superscript.RemoveAtoms(range.SubIndexRange);
198198
break;
199199
case (MathListSubIndexType.Inner, _)
200200
when self.Atoms[start.AtomIndex] is Atoms.Inner inner ? true
201-
: throw new SubIndexTypeMismatchException(typeof(Atoms.Fraction), start):
201+
: throw new SubIndexTypeMismatchException(nameof(Atoms.Inner), start.AtomIndex):
202202
inner.InnerList.RemoveAtoms(range.SubIndexRange);
203203
break;
204204
}
@@ -217,7 +217,7 @@ when self.Atoms[start.AtomIndex] is Atoms.Inner inner ? true
217217
(MathListSubIndexType.Numerator, var subIndex) => atom is Atoms.Fraction frac ? frac.Numerator.AtomAt(subIndex) : null,
218218
(MathListSubIndexType.Denominator, var subIndex) => atom is Atoms.Fraction frac ? frac.Denominator.AtomAt(subIndex) : null,
219219
(MathListSubIndexType.Inner, var subIndex) => atom is Atoms.Inner inner ? inner.InnerList.AtomAt(subIndex) : null,
220-
_ => throw new SubIndexTypeMismatchException(index),
220+
(var type, _) => throw new ArgumentOutOfRangeException(nameof(index), type, "Index type out of valid range."),
221221
};
222222
}
223223
}

0 commit comments

Comments
 (0)