diff options
author | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2017-06-29 16:35:48 +0200 |
---|---|---|
committer | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2017-06-30 08:58:07 +0000 |
commit | 7a09bb441853667de80f911a36c75d8b49a38c18 (patch) | |
tree | e8429636d0249826eb52acbb08c95f8b7d0381ff /src/plugins/texteditor | |
parent | 0f7e69034e0b800b559d777085d58f6fb2006036 (diff) | |
download | qt-creator-7a09bb441853667de80f911a36c75d8b49a38c18.tar.gz |
TextEditor: Reduce BaseHoverHandler::identifyMatch() calls
...because they are potentially expensive.
Change-Id: Iaa235ea1fa864a0a67f3ed10b7f89d23179c642b
Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/texteditor')
-rw-r--r-- | src/plugins/texteditor/basehoverhandler.cpp | 5 | ||||
-rw-r--r-- | src/plugins/texteditor/basehoverhandler.h | 2 | ||||
-rw-r--r-- | src/plugins/texteditor/convenience.cpp | 26 | ||||
-rw-r--r-- | src/plugins/texteditor/convenience.h | 2 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.cpp | 34 |
5 files changed, 65 insertions, 4 deletions
diff --git a/src/plugins/texteditor/basehoverhandler.cpp b/src/plugins/texteditor/basehoverhandler.cpp index abbf2154ab..56ad0c0ddb 100644 --- a/src/plugins/texteditor/basehoverhandler.cpp +++ b/src/plugins/texteditor/basehoverhandler.cpp @@ -33,9 +33,10 @@ namespace TextEditor { BaseHoverHandler::~BaseHoverHandler() {} -void BaseHoverHandler::showToolTip(TextEditorWidget *widget, const QPoint &point) +void BaseHoverHandler::showToolTip(TextEditorWidget *widget, const QPoint &point, bool decorate) { - decorateToolTip(); + if (decorate) + decorateToolTip(); operateTooltip(widget, point); } diff --git a/src/plugins/texteditor/basehoverhandler.h b/src/plugins/texteditor/basehoverhandler.h index f5c8d757b7..44dfc61e92 100644 --- a/src/plugins/texteditor/basehoverhandler.h +++ b/src/plugins/texteditor/basehoverhandler.h @@ -44,7 +44,7 @@ public: QString contextHelpId(TextEditorWidget *widget, int pos); int checkToolTip(TextEditorWidget *widget, int pos); - void showToolTip(TextEditorWidget *widget, const QPoint &point); + void showToolTip(TextEditorWidget *widget, const QPoint &point, bool decorate = true); protected: enum { diff --git a/src/plugins/texteditor/convenience.cpp b/src/plugins/texteditor/convenience.cpp index 22dcb99e9b..8ee92147dd 100644 --- a/src/plugins/texteditor/convenience.cpp +++ b/src/plugins/texteditor/convenience.cpp @@ -86,5 +86,31 @@ QTextCursor flippedCursor(const QTextCursor &cursor) return flipped; } +static bool isValidIdentifierChar(const QChar &c) +{ + return c.isLetter() + || c.isNumber() + || c == QLatin1Char('_') + || c.isHighSurrogate() + || c.isLowSurrogate(); +} + +QTextCursor wordStartCursor(const QTextCursor &textCursor) +{ + const int originalPosition = textCursor.position(); + QTextCursor cursor(textCursor); + cursor.movePosition(QTextCursor::StartOfWord); + const int wordStartPosition = cursor.position(); + + if (originalPosition == wordStartPosition) { + // Cursor is not on an identifier, check whether we are right after one. + const QChar c = textCursor.document()->characterAt(originalPosition - 1); + if (isValidIdentifierChar(c)) + cursor.movePosition(QTextCursor::PreviousWord); + } + + return cursor; +} + } // Util } // TextEditor diff --git a/src/plugins/texteditor/convenience.h b/src/plugins/texteditor/convenience.h index 5cbff33bef..e4f5f0480b 100644 --- a/src/plugins/texteditor/convenience.h +++ b/src/plugins/texteditor/convenience.h @@ -47,5 +47,7 @@ TEXTEDITOR_EXPORT QTextCursor selectAt(QTextCursor textCursor, uint line, uint c TEXTEDITOR_EXPORT QTextCursor flippedCursor(const QTextCursor &cursor); +TEXTEDITOR_EXPORT QTextCursor wordStartCursor(const QTextCursor &cursor); + } // Util } // TextEditor diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 474e71fb50..aa794ad63c 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -468,6 +468,26 @@ public: CodeAssistant m_codeAssistant; bool m_assistRelevantContentAdded = false; + + struct LastHoverHandlerInfo { + LastHoverHandlerInfo() = default; + LastHoverHandlerInfo(BaseHoverHandler *handler, int documentRevision, int cursorPosition) + : handler(handler) + , documentRevision(documentRevision) + , cursorPosition(cursorPosition) + {} + + bool applies(int documentRevision, int cursorPosition) const + { + return handler + && documentRevision == this->documentRevision + && cursorPosition == this->cursorPosition; + } + + BaseHoverHandler *handler = nullptr; + int documentRevision = -1; + int cursorPosition = -1; + } m_lastHoverHandlerInfo; QList<BaseHoverHandler *> m_hoverHandlers; // Not owned QPointer<QSequentialAnimationGroup> m_navigationAnimation; @@ -3161,6 +3181,15 @@ void TextEditorWidgetPrivate::processTooltipRequest(const QTextCursor &c) return; } + // Does the last handler still applies? + const int documentRevision = m_document->document()->revision(); + const int cursorPosition = Convenience::wordStartCursor(c).position(); + if (m_lastHoverHandlerInfo.applies(documentRevision, cursorPosition)) { + m_lastHoverHandlerInfo.handler->showToolTip(q, toolTipPoint, /*decorate=*/ false); + return; + } + + // Determine best handler int highestPriority = -1; BaseHoverHandler *highest = 0; foreach (BaseHoverHandler *handler, m_hoverHandlers) { @@ -3171,8 +3200,11 @@ void TextEditorWidgetPrivate::processTooltipRequest(const QTextCursor &c) } } - if (highest) + // Let the best handler show the tooltip + if (highest) { + m_lastHoverHandlerInfo = LastHoverHandlerInfo{highest, documentRevision, cursorPosition}; highest->showToolTip(q, toolTipPoint); + } } bool TextEditorWidgetPrivate::processAnnotaionTooltipRequest(const QTextBlock &block, |