Commit 1c5dc7c8 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

semantic highlighter: Use a timer to reduce amount of requests being sent

Currently we are sending requests on every typed character and everytime
we change view. This can be really heavy on the server, especially with
large documents. It results in problems such as "incorrect highlighting"
because server isn't in sync with the latest changes in the document.

This change introduces a simple mechanism to fix this: Use a QTimer to
reduce the amount of requests. Every time we type or change the current
KTE::View this timer gets started/restarted and when it times out, it
just refreshes the highlighting.

This means no more highlight-as-you-type, which is both good and bad but
since we can't really control the servers, I think this is better.
Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <>
parent 5476fe10
Pipeline #71846 passed with stage
in 6 minutes and 3 seconds
......@@ -16,6 +16,11 @@ SemanticHighlighter::SemanticHighlighter(QSharedPointer<LSPClientServerManager>
: QObject(parent)
, m_serverManager(std::move(serverManager))
m_requestTimer.connect(&m_requestTimer, &QTimer::timeout, this, [this]() {
static KTextEditor::Range getCurrentViewLinesRange(KTextEditor::View *view)
......@@ -30,6 +35,18 @@ static KTextEditor::Range getCurrentViewLinesRange(KTextEditor::View *view)
void SemanticHighlighter::doSemanticHighlighting(KTextEditor::View *view)
// start the timer
// We dont send the request directly because then there can be too many requests
// which leads to too much load on the server and client/server getting out of sync.
// This strategy can be problematic if the user keeps typing, but in reality that is
// unlikely to happen I think
m_currentView = view;
void SemanticHighlighter::doSemanticHighlighting_impl(KTextEditor::View *view)
if (!view) {
......@@ -7,13 +7,14 @@
#include <QObject>
#include <QPointer>
#include <QString>
#include <QTimer>
#include <KTextEditor/MovingRange>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace KTextEditor
......@@ -35,6 +36,8 @@ public:
void doSemanticHighlighting(KTextEditor::View *v);
void doSemanticHighlighting_impl(KTextEditor::View *v);
void semanticHighlightRange(KTextEditor::View *view, const KTextEditor::Cursor &);
QString previousResultIdForDoc(KTextEditor::Document *doc) const;
......@@ -86,5 +89,8 @@ private:
std::unordered_map<KTextEditor::Document *, TokensData> m_docSemanticInfo;
QSharedPointer<LSPClientServerManager> m_serverManager;
QTimer m_requestTimer;
QPointer<KTextEditor::View> m_currentView;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment