From 7c274409163045040d6a1fa6a56995c10a2f43fb Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Wed, 19 Aug 2020 10:21:56 +0200 Subject: [PATCH] [poincare/layout] Tracking issue in addSibling The addSibling method sometimes did not take into account the offset introduced by adding parentheses, leading to some issues : - Type 2 SQUARE RIGHTPARENTHESIS LEFT SQUARE. Parentheses are added but the new square should be outside, between the two right parentheses. Change-Id: Ifbc49cee2cd03c4511fc5a678d6d5d42243f4a22 --- poincare/src/layout.cpp | 8 +++++++- poincare/src/vertical_offset_layout.cpp | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/poincare/src/layout.cpp b/poincare/src/layout.cpp index 1caa20ce6..bc81e3ec1 100644 --- a/poincare/src/layout.cpp +++ b/poincare/src/layout.cpp @@ -176,7 +176,13 @@ void Layout::addSibling(LayoutCursor * cursor, Layout sibling, bool moveCursor) assert(!p.isUninitialized()); if (p.type() == LayoutNode::Type::HorizontalLayout) { int indexInParent = p.indexOfChild(*this); - int siblingIndex = cursor->position() == LayoutCursor::Position::Left ? indexInParent : indexInParent + 1; + int indexOfCursor = p.indexOfChild(cursor->layout()); + /* indexOfCursor == -1 if cursor->layout() is not a child of p. This should + * never happen, as addSibling is only called from inside + * LayoutField::handleEventWithText, and LayoutField is supposed to keep + * its cursor up to date.*/ + assert(indexOfCursor >= 0); + int siblingIndex = cursor->position() == LayoutCursor::Position::Left ? indexOfCursor : indexOfCursor + 1; /* Special case: If the neighbour sibling is a VerticalOffsetLayout, let it * handle the insertion of the new sibling. Do not enter the special case if diff --git a/poincare/src/vertical_offset_layout.cpp b/poincare/src/vertical_offset_layout.cpp index 40d6571ff..1ab101e27 100644 --- a/poincare/src/vertical_offset_layout.cpp +++ b/poincare/src/vertical_offset_layout.cpp @@ -236,7 +236,7 @@ bool VerticalOffsetLayoutNode::willAddSibling(LayoutCursor * cursor, LayoutNode // Add the Right parenthesis RightParenthesisLayout rightParenthesis = RightParenthesisLayout::Builder(); if (cursor->position() == LayoutCursor::Position::Right) { - parentRef.addChildAtIndex(rightParenthesis, idxInParent + 1, parentRef.numberOfChildren(), nullptr); + parentRef.addChildAtIndex(rightParenthesis, idxInParent + 1, parentRef.numberOfChildren(), nullptr); } else { assert(cursor->position() == LayoutCursor::Position::Left); parentRef.addChildAtIndex(rightParenthesis, idxInParent, parentRef.numberOfChildren(), nullptr);