CppEditor: Do not start CppUseSelectionsUpdater for same identifier

Saves some cycles when navigating with the text cursor over the
characters.

Change-Id: Ie9a23d97ac09ca45a32178cae5b8945d0c623811
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Nikolai Kosjar
2017-06-09 13:36:35 +02:00
parent 38789eb0a6
commit 9de9da7423
2 changed files with 35 additions and 6 deletions

View File

@@ -28,6 +28,8 @@
#include "cppeditor.h" #include "cppeditor.h"
#include "cppeditordocument.h" #include "cppeditordocument.h"
#include <cpptools/cpptoolsreuse.h>
#include <QTextBlock> #include <QTextBlock>
#include <QTextCursor> #include <QTextCursor>
@@ -57,6 +59,23 @@ void CppUseSelectionsUpdater::abortSchedule()
m_timer.stop(); m_timer.stop();
} }
static QTextCursor cursorAtWordStart(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 (CppTools::isValidIdentifierChar(c))
cursor.movePosition(QTextCursor::PreviousWord);
}
return cursor;
}
void CppUseSelectionsUpdater::update(CallType callType) void CppUseSelectionsUpdater::update(CallType callType)
{ {
auto *cppEditorWidget = qobject_cast<CppEditorWidget *>(m_editorWidget); auto *cppEditorWidget = qobject_cast<CppEditorWidget *>(m_editorWidget);
@@ -67,9 +86,12 @@ void CppUseSelectionsUpdater::update(CallType callType)
CppTools::CursorInfoParams params; CppTools::CursorInfoParams params;
params.semanticInfo = cppEditorWidget->semanticInfo(); params.semanticInfo = cppEditorWidget->semanticInfo();
params.textCursor = cppEditorWidget->textCursor(); params.textCursor = cursorAtWordStart(cppEditorWidget->textCursor());
if (callType == Asynchronous) { if (callType == Asynchronous) {
if (isSameIdentifierAsBefore(params.textCursor))
return;
if (m_runnerWatcher) if (m_runnerWatcher)
m_runnerWatcher->cancel(); m_runnerWatcher->cancel();
@@ -78,7 +100,7 @@ void CppUseSelectionsUpdater::update(CallType callType)
this, &CppUseSelectionsUpdater::onFindUsesFinished); this, &CppUseSelectionsUpdater::onFindUsesFinished);
m_runnerRevision = m_editorWidget->document()->revision(); m_runnerRevision = m_editorWidget->document()->revision();
m_runnerCursorPosition = m_editorWidget->position(); m_runnerWordStartPosition = params.textCursor.position();
m_runnerWatcher->setFuture(cppEditorDocument->cursorInfo(params)); m_runnerWatcher->setFuture(cppEditorDocument->cursorInfo(params));
} else { // synchronous case } else { // synchronous case
@@ -89,6 +111,13 @@ void CppUseSelectionsUpdater::update(CallType callType)
} }
} }
bool CppUseSelectionsUpdater::isSameIdentifierAsBefore(const QTextCursor &cursorAtWordStart) const
{
return m_runnerRevision != -1
&& m_runnerRevision == m_editorWidget->document()->revision()
&& m_runnerWordStartPosition == cursorAtWordStart.position();
}
void CppUseSelectionsUpdater::processResults(const CursorInfo &result) void CppUseSelectionsUpdater::processResults(const CursorInfo &result)
{ {
ExtraSelections localVariableSelections; ExtraSelections localVariableSelections;
@@ -111,8 +140,7 @@ void CppUseSelectionsUpdater::onFindUsesFinished()
return; return;
if (m_runnerRevision != m_editorWidget->document()->revision()) if (m_runnerRevision != m_editorWidget->document()->revision())
return; return;
// Optimizable: If the cursor is still on the same identifier the results should be valid. if (m_runnerWordStartPosition != cursorAtWordStart(m_editorWidget->textCursor()).position())
if (m_runnerCursorPosition != m_editorWidget->position())
return; return;
processResults(m_runnerWatcher->result()); processResults(m_runnerWatcher->result());

View File

@@ -57,6 +57,7 @@ signals:
private: private:
CppUseSelectionsUpdater(); CppUseSelectionsUpdater();
bool isSameIdentifierAsBefore(const QTextCursor &cursorAtWordStart) const;
void processResults(const CppTools::CursorInfo &result); void processResults(const CppTools::CursorInfo &result);
void onFindUsesFinished(); void onFindUsesFinished();
@@ -75,8 +76,8 @@ private:
QTimer m_timer; QTimer m_timer;
QScopedPointer<QFutureWatcher<CppTools::CursorInfo>> m_runnerWatcher; QScopedPointer<QFutureWatcher<CppTools::CursorInfo>> m_runnerWatcher;
int m_runnerRevision; int m_runnerRevision = -1;
int m_runnerCursorPosition; int m_runnerWordStartPosition = -1;
}; };
} // namespace Internal } // namespace Internal