Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlsemantictokens_p.h
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QQMLSEMANTICTOKENS_P_H
6#define QQMLSEMANTICTOKENS_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtLanguageServer/private/qlanguageserverspec_p.h>
20#include <QtQmlDom/private/qqmldomitem_p.h>
21#include <QtCore/qlist.h>
22#include <QtCore/qmap.h>
23
25
27
28namespace QmlHighlighting {
30
31// Protocol agnostic highlighting kinds
32// Use this enum while visiting dom tree to define the highlighting kinds for the semantic tokens
33// Then map it to the protocol specific token types and modifiers
34// This can be as much as detailed as needed
35enum class QmlHighlightKind {
36 QmlKeyword, // Qml keyword
37 QmlType, // Qml type name
38 QmlImportId, // Qml import module name
39 QmlNamespace, // Qml module namespace, i.e import QtQuick as Namespace
40 QmlLocalId, // Object id within the same file
41 QmlExternalId, // Object id defined in another file. [UNUSED FOR NOW]
42 QmlProperty, // Qml property. For now used for all kind of properties
43 QmlScopeObjectProperty, // Qml property defined in the current scope
44 QmlRootObjectProperty, // Qml property defined in the parent scopes
45 QmlExternalObjectProperty, // Qml property defined in the root object of another file
50 QmlEnumName, // Enum type name
51 QmlEnumMember, // Enum field names
52 QmlPragmaName, // Qml pragma name
53 QmlPragmaValue, // Qml pragma value
54 QmlTypeModifier, // list<QtObject>, list is the modifier, QtObject is the type
55 JsImport, // Js imported name
56 JsGlobalVar, // Js global variable or objects
57 JsGlobalMethod, // Js global method
58 JsScopeVar, // Js variable defined in the current scope
59 JsLabel, // js label
64 Field, // Used for the field names in the property chains,
65 Unknown, // Used for the unknown tokens
66};
67
78
80
81// Protocol specific token types
82// The values in this enum are converted to relevant strings and sent to the client as server
83// capabilities The convention is that the first letter in the enum value is decapitalized and the
84// rest is unchanged i.e Namespace -> "namespace" This is handled in enumToByteArray() helper
85// function.
87 // Subset of the QLspSpefication::SemanticTokenTypes enum
88 // We register only the token types used in the qml semantic highlighting
104
105 // Additional token types for the extended semantic highlighting
106 QmlLocalId, // object id within the same file
107 QmlExternalId, // object id defined in another file
108 QmlRootObjectProperty, // qml property defined in the parent scopes
109 QmlScopeObjectProperty, // qml property defined in the current scope
110 QmlExternalObjectProperty, // qml property defined in the root object of another file
111 JsScopeVar, // js variable defined in the current file
112 JsImportVar, // js import name that is imported in the qml file
113 JsGlobalVar, // js global variables
114 QmlStateName, // name of a qml state
115 Field, // used for the field names in the property chains
116 Unknown, // used for the unknown contexts
117};
118Q_ENUM_NS(SemanticTokenProtocolTypes)
119
120// Represents a semantic highlighting token
121// startLine and startColumn are 0-based as in LSP spec.
123{
124 HighlightToken() = default;
125 HighlightToken(const QQmlJS::SourceLocation &loc, QmlHighlightKind,
126 QmlHighlightModifiers = QmlHighlightModifier::None);
127
128 inline friend bool operator==(const HighlightToken &lhs, const HighlightToken &rhs)
129 {
130 return lhs.loc == rhs.loc && lhs.kind == rhs.kind && lhs.modifiers == rhs.modifiers;
131 }
132
136};
137
140
141/*!
142\internal
143Offsets start from zero.
144*/
150
151namespace Utils
152{
153QList<int> encodeSemanticTokens(const HighlightsContainer &highlights, HighlightingMode mode = HighlightingMode::Default);
155sourceLocationsFromMultiLineToken(QStringView code,
156 const QQmlJS::SourceLocation &tokenLocation);
157void addModifier(QLspSpecification::SemanticTokenModifiers modifier, int *baseModifier);
158bool rangeOverlapsWithSourceLocation(const QQmlJS::SourceLocation &loc, const HighlightsRange &r);
159QList<QLspSpecification::SemanticTokensEdit> computeDiff(const QList<int> &, const QList<int> &);
160void updateResultID(QByteArray &resultID);
161QList<int> collectTokens(const QQmlJS::Dom::DomItem &item,
162 const std::optional<HighlightsRange> &range,
163 HighlightingMode mode = HighlightingMode::Default);
164HighlightsContainer visitTokens(const QQmlJS::Dom::DomItem &item,
165 const std::optional<HighlightsRange> &range);
166void addHighlight(HighlightsContainer &out, const QQmlJS::SourceLocation &loc, QmlHighlightKind,
167 QmlHighlightModifiers = QmlHighlightModifier::None);
168} // namespace Utils
169
171{
172public:
173 HighlightingVisitor(const QQmlJS::Dom::DomItem &item,
174 const std::optional<HighlightsRange> &range);
175 const HighlightsContainer &hightights() const { return m_highlights; }
176 HighlightsContainer &highlights() { return m_highlights; }
177
178private:
179 bool visitor(QQmlJS::Dom::Path, const QQmlJS::Dom::DomItem &item, bool);
180 void highlightComment(const QQmlJS::Dom::DomItem &item);
181 void highlightImport(const QQmlJS::Dom::DomItem &item);
182 void highlightBinding(const QQmlJS::Dom::DomItem &item);
183 void highlightPragma(const QQmlJS::Dom::DomItem &item);
184 void highlightEnumItem(const QQmlJS::Dom::DomItem &item);
185 void highlightEnumDecl(const QQmlJS::Dom::DomItem &item);
186 void highlightQmlObject(const QQmlJS::Dom::DomItem &item);
187 void highlightComponent(const QQmlJS::Dom::DomItem &item);
188 void highlightPropertyDefinition(const QQmlJS::Dom::DomItem &item);
189 void highlightMethod(const QQmlJS::Dom::DomItem &item);
190 void highlightScriptLiteral(const QQmlJS::Dom::DomItem &item);
191 void highlightIdentifier(const QQmlJS::Dom::DomItem &item);
192 void highlightBySemanticAnalysis(const QQmlJS::Dom::DomItem &item, QQmlJS::SourceLocation loc);
193 void highlightScriptExpressions(const QQmlJS::Dom::DomItem &item);
194 void highlightCallExpression(const QQmlJS::Dom::DomItem &item);
195 void highlightFieldMemberAccess(const QQmlJS::Dom::DomItem &item, QQmlJS::SourceLocation loc);
196 void addHighlight(const QQmlJS::SourceLocation &loc, QmlHighlightKind,
197 QmlHighlightModifiers = QmlHighlightModifier::None);
198private:
199 HighlightsContainer m_highlights;
200 std::optional<HighlightsRange> m_range;
201};
202
203} // namespace QmlHighlighting
204
205QT_END_NAMESPACE
206
207#endif // QQMLSEMANTICTOKENS_P_H
Implements a server for the language server protocol.
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
QString name() const override
QQmlHighlightSupport(QmlLsp::QQmlCodeModelManager *codeModel)
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
const HighlightsContainer & hightights() const
HIDE_UNUSED_OVERRIDES QmlHighlighting::HighlightingMode m_mode
void process(QQmlBaseModule< SemanticTokensDeltaRequest >::RequestPointerArgument req) override
SemanticTokenDeltaHandler(QmlLsp::QQmlCodeModelManager *codeModel)
void setHighlightingMode(QmlHighlighting::HighlightingMode mode)
void registerHandlers(QLanguageServer *, QLanguageServerProtocol *) override
void process(QQmlBaseModule< SemanticTokensRequest >::RequestPointerArgument req) override
void setHighlightingMode(QmlHighlighting::HighlightingMode mode)
SemanticTokenFullHandler(QmlLsp::QQmlCodeModelManager *codeModel)
void registerHandlers(QLanguageServer *, QLanguageServerProtocol *) override
HIDE_UNUSED_OVERRIDES QmlHighlighting::HighlightingMode m_mode
SemanticTokenRangeHandler(QmlLsp::QQmlCodeModelManager *codeModel)
void setHighlightingMode(QmlHighlighting::HighlightingMode mode)
void process(QQmlBaseModule< SemanticTokensRangeRequest >::RequestPointerArgument req) override
void registerHandlers(QLanguageServer *, QLanguageServerProtocol *) override
HIDE_UNUSED_OVERRIDES QmlHighlighting::HighlightingMode m_mode
Combined button and popup list for selecting options.
QList< int > encodeSemanticTokens(const HighlightsContainer &highlights, HighlightingMode mode=HighlightingMode::Default)
QList< QLspSpecification::SemanticTokensEdit > computeDiff(const QList< int > &, const QList< int > &)
void addHighlight(HighlightsContainer &out, const QQmlJS::SourceLocation &loc, QmlHighlightKind, QmlHighlightModifiers=QmlHighlightModifier::None)
void updateResultID(QByteArray &resultID)
QList< QQmlJS::SourceLocation > sourceLocationsFromMultiLineToken(QStringView code, const QQmlJS::SourceLocation &tokenLocation)
Returns multiple source locations for a given raw comment.
void addModifier(QLspSpecification::SemanticTokenModifiers modifier, int *baseModifier)
bool rangeOverlapsWithSourceLocation(const QQmlJS::SourceLocation &loc, const HighlightsRange &r)
int(*)(QmlHighlightKind) QmlHighlightKindToLspKind
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcher)
QList< QByteArray > extendedTokenTypesList()
QList< QByteArray > defaultTokenModifiersList()
static QList< QByteArray > enumToByteArray()
#define HIDE_UNUSED_OVERRIDES
HighlightToken(const QQmlJS::SourceLocation &loc, QmlHighlightKind, QmlHighlightModifiers=QmlHighlightModifier::None)
friend bool operator==(const HighlightToken &lhs, const HighlightToken &rhs)
This class sends a result or an error when going out of scope.