QmlDesigner: Fix selection from code editor

Calculate the correct node length if nodes are enclosed.
We cannot just look for the next closing brace, we have to
ignore opened and closed braces.

Task-number: QDS-12543
Change-Id: Ieee232777637f4bc022e65af5d8cb437e025a146
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Thomas Hartmann
2024-06-27 16:49:14 +02:00
parent d5ea5cabe4
commit 548cd083b4

View File

@@ -844,6 +844,26 @@ static bool isInNodeDefinition(int nodeTextOffset, int nodeTextLength, int curso
return (nodeTextOffset <= cursorPosition) && (nodeTextOffset + nodeTextLength > cursorPosition);
}
int findEvenClosingBrace(const QStringView &string)
{
int count = 0;
int index = 0;
for (auto currentChar : string) {
if (currentChar == '{')
count++;
if (currentChar == '}') {
if (count == 1)
return index;
else
count--;
}
index++;
}
return index;
}
ModelNode RewriterView::nodeAtTextCursorPositionHelper(const ModelNode &root, int cursorPosition) const
{
QTC_ASSERT(m_textModifier, return {});
@@ -865,8 +885,9 @@ ModelNode RewriterView::nodeAtTextCursorPositionHelper(const ModelNode &root, in
ModelNode node = pair.first;
const int nodeTextOffset = nodeOffset(node);
const int nodeTextLength = m_textModifier->text().indexOf("}", nodeTextOffset)
- nodeTextOffset - 1;
const int nodeTextLength = findEvenClosingBrace(m_textModifier->text().sliced(nodeTextOffset))
- 1;
if (isInNodeDefinition(nodeTextOffset, nodeTextLength, cursorPosition))
lastNode = node;