File indexing completed on 2024-04-28 05:49:01
0001 /* 0002 SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #pragma once 0007 0008 #include <QObject> 0009 #include <QPointer> 0010 #include <QString> 0011 #include <QTimer> 0012 0013 #include <KTextEditor/MovingRange> 0014 0015 #include <memory> 0016 #include <unordered_map> 0017 #include <vector> 0018 0019 namespace KTextEditor 0020 { 0021 class View; 0022 class Document; 0023 } 0024 0025 class SemanticTokensLegend; 0026 class LSPClientServerManager; 0027 struct LSPSemanticTokensDelta; 0028 0029 class SemanticHighlighter : public QObject 0030 { 0031 Q_OBJECT 0032 public: 0033 explicit SemanticHighlighter(std::shared_ptr<LSPClientServerManager> serverManager, QObject *parent = nullptr); 0034 0035 void doSemanticHighlighting(KTextEditor::View *v, bool textChanged); 0036 0037 private: 0038 void doSemanticHighlighting_impl(KTextEditor::View *v); 0039 0040 void semanticHighlightRange(KTextEditor::View *view, const KTextEditor::Cursor &); 0041 0042 QString previousResultIdForDoc(KTextEditor::Document *doc) const; 0043 0044 /** 0045 * Unregister a doc from highlighter and remove all its associated moving ranges and tokens 0046 */ 0047 Q_SLOT void remove(KTextEditor::Document *doc); 0048 0049 void processTokens(const LSPSemanticTokensDelta &tokens, KTextEditor::View *view, const SemanticTokensLegend *legend); 0050 0051 /** 0052 * Does the actual highlighting 0053 */ 0054 void highlight(KTextEditor::View *view, const SemanticTokensLegend *legend); 0055 0056 /** 0057 * Insert new incoming tokens @p data for doc with @p url 0058 */ 0059 void insert(KTextEditor::Document *doc, const QString &resultId, const std::vector<uint32_t> &data); 0060 0061 /** 0062 * Handle SemanticTokensEdits 0063 */ 0064 void update(KTextEditor::Document *doc, const QString &resultId, uint32_t start, uint32_t deleteCount, const std::vector<uint32_t> &data); 0065 0066 /** 0067 * The current visible range for which we requested highlights 0068 */ 0069 KTextEditor::Range m_currentHighlightedRange; 0070 0071 /** 0072 * A simple struct which holds the tokens recieved from server + 0073 * moving ranges that were created to highlight those tokens 0074 */ 0075 struct TokensData { 0076 std::vector<uint32_t> tokens; 0077 std::vector<std::unique_ptr<KTextEditor::MovingRange>> movingRanges; 0078 }; 0079 0080 /** 0081 * token types specified in server caps. Uncomment for debugging 0082 */ 0083 // QList<QString> m_types; 0084 0085 /** 0086 * Doc => result-id mapping 0087 */ 0088 std::unordered_map<KTextEditor::Document *, QString> m_docResultId; 0089 0090 /** 0091 * semantic token and moving range mapping for doc 0092 */ 0093 std::unordered_map<KTextEditor::Document *, TokensData> m_docSemanticInfo; 0094 0095 std::shared_ptr<LSPClientServerManager> m_serverManager; 0096 0097 QTimer m_requestTimer; 0098 QPointer<KTextEditor::View> m_currentView; 0099 };