From 0230a53fee6e319dceb8d8e8515bfe5fbc2a8aa6 Mon Sep 17 00:00:00 2001 From: FoggyFinder Date: Sun, 25 Aug 2019 21:21:12 +0300 Subject: [PATCH 1/3] init --- .../DisplayEditingExtensions.List.cs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs b/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs index 0f980b02e..442bb7c69 100644 --- a/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs +++ b/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs @@ -35,9 +35,25 @@ public static MathListIndex IndexForPoint(this ListDisplay= self.Width + PixelDelta) { - // All the way to the right - if (closest != null) - return MathListIndex.Level0Index(self.Range.End); + // Check if self contains a script + var hasScript = self.Displays.Any(d => d.HasScript); + // if so try to find a parent + if (hasScript) { + var parent = + self.Displays + .Where(d => d.HasScript) + .OrderBy(d => d.Position.X) + .FirstOrDefault(); + + // if we really near to a script + if (closest is ListDisplay ld + && ld.LinePosition != Enumerations.LinePosition.Regular + && 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 From 7f9982b862ec93afd11e9c00bb38fb9abec777c2 Mon Sep 17 00:00:00 2001 From: FoggyFinder Date: Wed, 28 Aug 2019 15:16:24 +0300 Subject: [PATCH 2/3] add tests --- CSharpMath.Editor.Tests/ClosestPointTests.cs | 24 ++++++++++++++++++++ CSharpMath/Atoms/Factories/MathLists.cs | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CSharpMath.Editor.Tests/ClosestPointTests.cs b/CSharpMath.Editor.Tests/ClosestPointTests.cs index 5517705ff..126643c6b 100644 --- a/CSharpMath.Editor.Tests/ClosestPointTests.cs +++ b/CSharpMath.Editor.Tests/ClosestPointTests.cs @@ -200,6 +200,30 @@ void Test(ListDisplay displayList, PointF point, MathListIndex expected) { [Theory, MemberData(nameof(ExponentData))] public void ExponentTest(PointF point, MathListIndex expected) => Test(Exponent, point, expected); + // https://github.com/verybadcat/CSharpMath/issues/49 + public static TestData Exponent2Data => + new TestData { + { new PointF(55, 0), MathListIndex.Level0Index(1) }, + { new PointF(55, 20), MathListIndex.Level0Index(1) }, + { new PointF(55, 40), MathListIndex.Level0Index(1) }, + }; + static readonly ListDisplay Exponent2 = CreateDisplay("2^{x+y-4}"); + [Theory, MemberData(nameof(Exponent2Data))] + public void Exponent2Test(PointF point, MathListIndex expected) => Test(Exponent2, point, expected); + + // https://github.com/verybadcat/CSharpMath/issues/46 + public static TestData Issue46Data => + new TestData { + { new PointF(50, 10), MathListIndex.Level0Index(4) }, + { new PointF(90, 0), MathListIndex.Level0Index(5) }, + { new PointF(90, 20), MathListIndex.Level0Index(5) }, + { new PointF(90, 40), MathListIndex.Level0Index(5) }, + }; + static readonly ListDisplay Issue46 = CreateDisplay("2+x+x^y"); + [Theory, MemberData(nameof(Issue46Data))] + public void ComplexTest(PointF point, MathListIndex expected) => Test(Issue46, point, expected); + + // \frac a\frac bc\frac\frac123\sqrt d^e\sqrt[5]6\sqrt[6f]7_8\overline9\underline0 } } \ No newline at end of file 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; From 01a0d50eae942608ec7939f0a0d5777e80968366 Mon Sep 17 00:00:00 2001 From: FoggyFinder Date: Wed, 28 Aug 2019 15:53:26 +0300 Subject: [PATCH 3/3] fix --- .../DisplayEditingExtensions.List.cs | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs b/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs index 442bb7c69..6e0ad6ece 100644 --- a/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs +++ b/CSharpMath.Editor/Extensions/DisplayEditingExtensions.List.cs @@ -33,23 +33,15 @@ public static MathListIndex IndexForPoint(this ListDisplay= self.Width + PixelDelta) { - // Check if self contains a script - var hasScript = self.Displays.Any(d => d.HasScript); - // if so try to find a parent - if (hasScript) { - var parent = - self.Displays - .Where(d => d.HasScript) - .OrderBy(d => d.Position.X) - .FirstOrDefault(); + // 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 we really near to a script - if (closest is ListDisplay ld - && ld.LinePosition != Enumerations.LinePosition.Regular - && parent != null - ) { + if (parent != null) { return MathListIndex.Level0Index(parent.Range.End); }