forked from qt-creator/qt-creator
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:
committed by
Tim Jenssen
parent
50d1690854
commit
b7e2f672cf
@@ -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;
|
||||
|
@@ -556,24 +556,28 @@ static bool isInNodeDefinition(int nodeTextOffset, int nodeTextLength, int curso
|
||||
return (nodeTextOffset <= cursorPosition) && (nodeTextOffset + nodeTextLength > cursorPosition);
|
||||
}
|
||||
|
||||
ModelNode RewriterView::nodeAtTextCursorPosition(int cursorPosition) const
|
||||
ModelNode RewriterView::nodeAtTextCursorPositionRekursive(const ModelNode &root, int cursorPosition) const
|
||||
{
|
||||
const QList<ModelNode> allNodes = allModelNodes();
|
||||
|
||||
ModelNode nearestNode;
|
||||
int nearestNodeTextOffset = -1;
|
||||
|
||||
foreach (const ModelNode ¤tNode, allNodes) {
|
||||
const int nodeTextOffset = nodeOffset(currentNode);
|
||||
const int nodeTextLength = nodeLength(currentNode);
|
||||
if (isInNodeDefinition(nodeTextOffset, nodeTextLength, cursorPosition)
|
||||
&& (nodeTextOffset > nearestNodeTextOffset)) {
|
||||
nearestNode = currentNode;
|
||||
nearestNodeTextOffset = nodeTextOffset;
|
||||
}
|
||||
ModelNode node = root;
|
||||
foreach (const ModelNode ¤tNode, node.directSubModelNodes()) {
|
||||
const int offset = nodeOffset(currentNode);
|
||||
if (offset < cursorPosition)
|
||||
node = nodeAtTextCursorPositionRekursive(currentNode, cursorPosition);
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
return nearestNode;
|
||||
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
|
||||
{
|
||||
return nodeAtTextCursorPositionRekursive(rootModelNode(), cursorPosition);
|
||||
}
|
||||
|
||||
bool RewriterView::renameId(const QString& oldId, const QString& newId)
|
||||
|
Reference in New Issue
Block a user