diff --git a/CSharpMath.Editor.Tests/ClosestPointTests.cs b/CSharpMath.Editor.Tests/ClosestPointTests.cs index 313f03883..6146cf749 100644 --- a/CSharpMath.Editor.Tests/ClosestPointTests.cs +++ b/CSharpMath.Editor.Tests/ClosestPointTests.cs @@ -203,6 +203,29 @@ void Test(ListDisplay displayList, float x, float y, MathListIndex expected) { [Theory, MemberData(nameof(ExponentData))] public void ExponentTest(float x, float y, MathListIndex expected) => Test(Exponent, x, y, expected); + // https://github.com/verybadcat/CSharpMath/issues/49 + public static TestData Exponent2Data => + new TestData { + { 55, 0, Level0Index(1) }, + { 55, 20, Level0Index(1) }, + { 55, 40, Level0Index(1) }, + }; + static readonly ListDisplay Exponent2 = CreateDisplay("2^{x+y-4}"); + [Theory, MemberData(nameof(Exponent2Data))] + public void Exponent2Test(float x, float y, MathListIndex expected) => Test(Exponent2, x, y, expected); + + // https://github.com/verybadcat/CSharpMath/issues/46 + public static TestData Issue46Data => + new TestData { + { 50, 10, Level0Index(4) }, + { 90, 0, Level0Index(5) }, + { 90, 20, Level0Index(5) }, + { 90, 40, Level0Index(5) }, + }; + static readonly ListDisplay Issue46 = CreateDisplay("2+x+x^y"); + [Theory, MemberData(nameof(Issue46Data))] + public void Issue46Test(float x, float y, MathListIndex expected) => Test(Issue46, x, y, expected); + public static TestData ComplexData => new TestData { { -10, -20, Level0Index(0) }, diff --git a/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs b/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs index 7640f0688..71e029ff7 100644 --- a/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs +++ b/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs @@ -33,11 +33,19 @@ public static MathListIndex IndexForPoint(this ListDisplay= self.Width + PixelDelta) { - // All the way to the right - if (closest != null) - return MathListIndex.Level0Index(self.Range.End); + // if closest is a script + if (closest != null && closest is ListDisplay ld + && ld.LinePosition != Enumerations.LinePosition.Regular) { + // then we try to find its parent + var parent = self.Displays.FirstOrDefault(d => d.HasScript && d.Range.Contains(ld.IndexInParent)); + + if (parent != null) { + return MathListIndex.Level0Index(parent.Range.End); + } + + } // All the way to the right return self.Range.End < 0 ? null : MathListIndex.Level0Index(self.Range.End); } else diff --git a/CSharpMath/Atoms/Factories/MathLists.cs b/CSharpMath/Atoms/Factories/MathLists.cs index 9396d976d..8d06e6721 100644 --- a/CSharpMath/Atoms/Factories/MathLists.cs +++ b/CSharpMath/Atoms/Factories/MathLists.cs @@ -1,4 +1,4 @@ -using CSharpMath.Interfaces; +using CSharpMath.Interfaces; using System; using System.Collections.Generic; using System.Text;