diff --git a/plugins/clang/codecompletion/model.cpp b/plugins/clang/codecompletion/model.cpp --- a/plugins/clang/codecompletion/model.cpp +++ b/plugins/clang/codecompletion/model.cpp @@ -37,6 +37,8 @@ #include #include +#include + using namespace KDevelop; namespace { @@ -78,6 +80,25 @@ public Q_SLOTS: void completionRequested(const QUrl &url, const KTextEditor::Cursor& position, const QString& text, const QString& followingText) + { + // group requests and only handle the latest one + m_url = url; + m_position = position; + m_text = text; + m_followingText = followingText; + + if (!m_timer) { + // lazy-load the timer to initialize it in the background thread + m_timer = new QTimer(this); + m_timer->setInterval(0); + m_timer->setSingleShot(true); + connect(m_timer, &QTimer::timeout, this, &ClangCodeCompletionWorker::run); + } + m_timer->start(); + } + +private: + void run() { aborting() = false; @@ -87,17 +108,17 @@ return; } - auto top = DUChainUtils::standardContextForUrl(url); + auto top = DUChainUtils::standardContextForUrl(m_url); if (!top) { - qCWarning(KDEV_CLANG) << "No context found for" << url; + qCWarning(KDEV_CLANG) << "No context found for" << m_url; return; } ParseSessionData::Ptr sessionData(ClangIntegration::DUChainUtils::findParseSessionData(top->url(), m_index->translationUnitForUrl(top->url()))); if (!sessionData) { // TODO: trigger reparse and re-request code completion - qCWarning(KDEV_CLANG) << "No parse session / AST attached to context for url" << url; + qCWarning(KDEV_CLANG) << "No parse session / AST attached to context for url" << m_url; return; } @@ -109,7 +130,8 @@ // We hold DUChain lock, and ask for ParseSession, but TUDUChain indirectly holds ParseSession lock. lock.unlock(); - auto completionContext = ::createCompletionContext(DUContextPointer(top), sessionData, url, position, text, followingText); + auto completionContext = ::createCompletionContext(DUContextPointer(top), sessionData, m_url, + m_position, m_text, m_followingText); lock.lock(); if (aborting()) { @@ -141,6 +163,11 @@ } private: ClangIndex* m_index; + QTimer* m_timer = nullptr; + QUrl m_url; + KTextEditor::Cursor m_position; + QString m_text; + QString m_followingText; }; }