Commit e25a08de 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 <waqar.17a@gmail.com>
(cherry picked from commit 1c5dc7c8)
parent 80e1d658
......@@ -16,6 +16,11 @@ SemanticHighlighter::SemanticHighlighter(QSharedPointer<LSPClientServerManager>
: QObject(parent)
, m_serverManager(std::move(serverManager))
{
m_requestTimer.setInterval(2000);
m_requestTimer.setSingleShot(true);
m_requestTimer.connect(&m_requestTimer, &QTimer::timeout, this, [this]() {
doSemanticHighlighting_impl(m_currentView);
});
}
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;
m_requestTimer.start();
}
void SemanticHighlighter::doSemanticHighlighting_impl(KTextEditor::View *view)
{
if (!view) {
return;
......
......@@ -7,13 +7,14 @@
#define LSP_SEMANTIC_HIGHLIGHTING_H
#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);
private:
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;
};
#endif
Markdown is supported
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