Commit 5c5ef2cc authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Move everything to SemanticHighlighter


Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent 2704109e
......@@ -627,33 +627,6 @@ public:
}
}
void doSemanticHighlighting(KTextEditor::View *view)
{
if (!view) {
return;
}
auto server = m_serverManager->findServer(view);
if (!server) {
return;
}
// m_semHighlightingManager.setTypes(server->capabilities().semanticTokenProvider.types);
QPointer<KTextEditor::View> v = view;
auto h = [this, v, server](const LSPSemanticTokensDelta &st) {
if (v && server) {
const auto legend = &server->capabilities().semanticTokenProvider.legend;
m_semHighlightingManager.processTokens(st, v, legend);
}
};
auto prevResultId = m_semHighlightingManager.previousResultIdForDoc(view->document());
if (!server->capabilities().semanticTokenProvider.fullDelta) {
server->documentSemanticTokensFull(view->document()->url(), QString(), this, h);
} else {
server->documentSemanticTokensFullDelta(view->document()->url(), prevResultId, this, h);
}
}
// This is taken from KDevelop :)
KTextEditor::View *viewFromWidget(QWidget *widget)
{
......@@ -2190,7 +2163,7 @@ public:
}
if (m_plugin->m_semanticHighlighting) {
doSemanticHighlighting(activeView);
m_semHighlightingManager.doSemanticHighlighting(activeView, m_serverManager);
}
if (m_onTypeFormattingTriggers.empty()) {
......@@ -2252,21 +2225,8 @@ public:
isClangd = lspServer == QStringLiteral("clangd");
const bool semHighlightingEnabled = m_plugin->m_semanticHighlighting;
const bool serverSupportsSemHighlighting = caps.semanticTokenProvider.full || caps.semanticTokenProvider.fullDelta;
if (semHighlightingEnabled && serverSupportsSemHighlighting) {
if (doc) {
connect(doc,
SIGNAL(aboutToInvalidateMovingInterfaceContent(KTextEditor::Document *)),
this,
SLOT(clearSemanticTokensHighlighting(KTextEditor::Document *)),
Qt::UniqueConnection);
connect(doc,
SIGNAL(aboutToDeleteMovingInterfaceContent(KTextEditor::Document *)),
this,
SLOT(clearSemanticTokensHighlighting(KTextEditor::Document *)),
Qt::UniqueConnection);
doSemanticHighlighting(activeView);
}
if (semHighlightingEnabled) {
m_semHighlightingManager.doSemanticHighlighting(activeView, m_serverManager);
}
}
......@@ -2340,13 +2300,6 @@ public:
}
}
Q_SLOT void clearSemanticTokensHighlighting(KTextEditor::Document *doc)
{
if (doc) {
m_semHighlightingManager.remove(doc);
}
}
void viewDestroyed(QObject *view)
{
m_completionViews.remove(static_cast<KTextEditor::View *>(view));
......
......@@ -6,12 +6,72 @@
*/
#include "lspsemantichighlighting.h"
#include "lspclientprotocol.h"
#include "lspclientservermanager.h"
#include "semantic_tokens_legend.h"
#include <KTextEditor/MovingInterface>
#include <KTextEditor/MovingRange>
#include <KTextEditor/View>
SemanticHighlighter::SemanticHighlighter(QObject *parent)
: QObject(parent)
{
}
void SemanticHighlighter::doSemanticHighlighting(KTextEditor::View *view, QSharedPointer<LSPClientServerManager> serverManager)
{
if (!view) {
return;
}
auto server = serverManager->findServer(view);
if (!server) {
return;
}
const auto &caps = server->capabilities();
const bool serverSupportsSemHighlighting = caps.semanticTokenProvider.full || caps.semanticTokenProvider.fullDelta;
if (!serverSupportsSemHighlighting) {
return;
}
auto doc = view->document();
if (m_docResultId.count(doc) == 0) {
connect(doc,
SIGNAL(aboutToInvalidateMovingInterfaceContent(KTextEditor::Document *)),
this,
SLOT(remove(KTextEditor::Document *)),
Qt::UniqueConnection);
connect(doc, SIGNAL(aboutToDeleteMovingInterfaceContent(KTextEditor::Document *)), this, SLOT(remove(KTextEditor::Document *)), Qt::UniqueConnection);
}
// m_semHighlightingManager.setTypes(server->capabilities().semanticTokenProvider.types);
QPointer<KTextEditor::View> v = view;
auto h = [this, v, server](const LSPSemanticTokensDelta &st) {
if (v && server) {
const auto legend = &server->capabilities().semanticTokenProvider.legend;
processTokens(st, v, legend);
}
};
if (!server->capabilities().semanticTokenProvider.fullDelta) {
server->documentSemanticTokensFull(doc->url(), QString(), this, h);
} else {
auto prevResultId = previousResultIdForDoc(doc);
server->documentSemanticTokensFullDelta(doc->url(), prevResultId, this, h);
}
}
QString SemanticHighlighter::previousResultIdForDoc(KTextEditor::Document *doc) const
{
auto it = m_docResultId.find(doc);
if (it != m_docResultId.end()) {
return it->second;
}
return QString();
}
void SemanticHighlighter::processTokens(const LSPSemanticTokensDelta &tokens, KTextEditor::View *view, const SemanticTokensLegend *legend)
{
Q_ASSERT(view);
......
......@@ -7,6 +7,7 @@
#ifndef LSP_SEMANTIC_HIGHLIGHTING_H
#define LSP_SEMANTIC_HIGHLIGHTING_H
#include <QObject>
#include <QString>
#include <KTextEditor/MovingRange>
......@@ -22,28 +23,27 @@ class Document;
}
class SemanticTokensLegend;
class LSPClientServerManager;
struct LSPSemanticTokensDelta;
class SemanticHighlighter
class SemanticHighlighter : public QObject
{
Q_OBJECT
public:
QString previousResultIdForDoc(KTextEditor::Document *doc) const
{
auto it = m_docResultId.find(doc);
if (it != m_docResultId.end()) {
return it->second;
}
return QString();
}
SemanticHighlighter(QObject *parent = nullptr);
void doSemanticHighlighting(KTextEditor::View *v, QSharedPointer<LSPClientServerManager> serverManager);
private:
QString previousResultIdForDoc(KTextEditor::Document *doc) const;
/**
* Unregister a doc from highlighter and remove all its associated moving ranges and tokens
*/
void remove(KTextEditor::Document *doc);
Q_SLOT void remove(KTextEditor::Document *doc);
void processTokens(const LSPSemanticTokensDelta &tokens, KTextEditor::View *view, const SemanticTokensLegend *legend);
private:
/**
* Does the actual highlighting
*/
......
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