QmlDesigner: Improve performance of nodeAtTextCursorPosition

We find candidates by a linear search using just the offset.
This way we have to calculate the length (which is slow) only for
a very few selected nodes.
The number of nodes we have to calulate the length for is bounded by
the maximum nesting, which is for any sane QML file smaller then 10.
Before we had to calculate the length for every node.

Change-Id: I53a03818e9986db4a224aef876b333444aa1c15b
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Thomas Hartmann
2017-02-22 18:27:35 +01:00
committed by Tim Jenssen
parent 50d1690854
commit b7e2f672cf
2 changed files with 21 additions and 15 deletions

View File

@@ -176,6 +176,8 @@ protected: // functions
void notifyErrorsAndWarnings(const QList<DocumentMessage> &errors);
private: //variables
ModelNode nodeAtTextCursorPositionRekursive(const ModelNode &root, int cursorPosition) const;
TextModifier *m_textModifier = nullptr;
int transactionLevel = 0;
bool m_modificationGroupActive = false;

View File

@@ -556,24 +556,28 @@ static bool isInNodeDefinition(int nodeTextOffset, int nodeTextLength, int curso
return (nodeTextOffset <= cursorPosition) && (nodeTextOffset + nodeTextLength > cursorPosition);
}
ModelNode RewriterView::nodeAtTextCursorPositionRekursive(const ModelNode &root, int cursorPosition) const
{
ModelNode node = root;
foreach (const ModelNode &currentNode, node.directSubModelNodes()) {
const int offset = nodeOffset(currentNode);
if (offset < cursorPosition)
node = nodeAtTextCursorPositionRekursive(currentNode, cursorPosition);
else
break;
}
const int nodeTextLength = nodeLength(node);
const int nodeTextOffset = nodeOffset(node);
if (isInNodeDefinition(nodeTextOffset, nodeTextLength, cursorPosition))
return node;
return root;
}
ModelNode RewriterView::nodeAtTextCursorPosition(int cursorPosition) const
{
const QList<ModelNode> allNodes = allModelNodes();
ModelNode nearestNode;
int nearestNodeTextOffset = -1;
foreach (const ModelNode &currentNode, allNodes) {
const int nodeTextOffset = nodeOffset(currentNode);
const int nodeTextLength = nodeLength(currentNode);
if (isInNodeDefinition(nodeTextOffset, nodeTextLength, cursorPosition)
&& (nodeTextOffset > nearestNodeTextOffset)) {
nearestNode = currentNode;
nearestNodeTextOffset = nodeTextOffset;
}
}
return nearestNode;
return nodeAtTextCursorPositionRekursive(rootModelNode(), cursorPosition);
}
bool RewriterView::renameId(const QString& oldId, const QString& newId)