aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSemih Yavuz <semih.yavuz@qt.io>2025-06-18 14:01:58 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2025-07-03 08:33:09 +0000
commit012a3dafbbde35cd82274075754214337e375ff4 (patch)
treedd949329536f196c40047d7aa0e2f2aaa15f69a7 /src
parent5f6ba877209b732d36c3fe4c545fc2d2c8b1fc73 (diff)
revisit semantic highligher: Restructure the entry point
Let visitor own the highlights data and invoke visitTree in the constructor. This makes it more structural and easier to test. Rename highlights() into tokens() to prevent confusion, as the owner class' name is also Highlights. Pick-to: 6.8 Task-number: QTBUG-137116 Change-Id: Ifed9a40c04fa1a86c3314bd172995246208a4756 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> Reviewed-by: Sami Shalayel <sami.shalayel@qt.io> (cherry picked from commit 5ce826b34762c8f56e9294d7e0526e3ea5c14743) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit 004e94ab51d50903b135cdf109ae58bbe225e9dd)
Diffstat (limited to 'src')
-rw-r--r--src/qmlls/qqmlsemantictokens.cpp35
-rw-r--r--src/qmlls/qqmlsemantictokens_p.h18
2 files changed, 33 insertions, 20 deletions
diff --git a/src/qmlls/qqmlsemantictokens.cpp b/src/qmlls/qqmlsemantictokens.cpp
index 6bf26c9311..456d5149bd 100644
--- a/src/qmlls/qqmlsemantictokens.cpp
+++ b/src/qmlls/qqmlsemantictokens.cpp
@@ -215,13 +215,17 @@ static FieldFilter highlightingFilter()
return FieldFilter{ fieldFilterAdd, fieldFilterRemove };
}
-HighlightingVisitor::HighlightingVisitor(Highlights &highlights,
- const std::optional<HighlightsRange> &range)
- : m_highlights(highlights), m_range(range)
+HighlightingVisitor::HighlightingVisitor(const QQmlJS::Dom::DomItem &item,
+ const std::optional<HighlightsRange> &range,
+ HighlightingMode mode)
+ : m_highlights(mode), m_range(range)
{
+ item.visitTree(Path(),
+ [this](Path path, const DomItem &item, bool b) { return this->visitor(path, item, b); },
+ VisitOption::Default, emptyChildrenVisitor, emptyChildrenVisitor, highlightingFilter());
}
-bool HighlightingVisitor::operator()(Path, const DomItem &item, bool)
+bool HighlightingVisitor::visitor(Path, const DomItem &item, bool)
{
if (m_range.has_value()) {
const auto fLocs = FileLocations::treeOf(item);
@@ -519,7 +523,7 @@ void HighlightingVisitor::highlightIdentifier(const DomItem &item)
// Many of the scriptIdentifiers expressions are already handled by
// other cases. In those cases, if the location offset is already in the list
// we don't need to perform expensive resolveExpressionType operation.
- if (m_highlights.highlights().contains(loc.offset))
+ if (m_highlights.tokens().contains(loc.offset))
return;
highlightBySemanticAnalysis(item, loc);
@@ -717,7 +721,7 @@ void HighlightingVisitor::highlightScriptExpressions(const DomItem &item)
return;
case DomType::ScriptTemplateExpressionPart:
m_highlights.addHighlight(regions[DollarLeftBraceTokenRegion], QmlHighlightKind::Operator);
- operator()(Path(), item.field(Fields::expression), false);
+ visitor(Path(), item.field(Fields::expression), false);
m_highlights.addHighlight(regions[RightBraceRegion], QmlHighlightKind::Operator);
return;
case DomType::ScriptTemplateLiteral:
@@ -800,7 +804,7 @@ HighlightingUtils::sourceLocationsFromMultiLineToken(QStringView stringLiteral,
QList<int> HighlightingUtils::encodeSemanticTokens(Highlights &highlights)
{
QList<int> result;
- const auto highlightingTokens = highlights.highlights();
+ const auto highlightingTokens = highlights.tokens();
constexpr auto tokenEncodingLength = 5;
result.reserve(tokenEncodingLength * highlightingTokens.size());
@@ -931,19 +935,20 @@ void Highlights::addHighlightImpl(const QQmlJS::SourceLocation &loc, int tokenTy
m_highlights.insert(loc.offset, QT_PREPEND_NAMESPACE(Token)(loc, tokenType, tokenModifier));
}
-QList<int> HighlightingUtils::collectTokens(const QQmlJS::Dom::DomItem &item,
+Highlights HighlightingUtils::visitTokens(const QQmlJS::Dom::DomItem &item,
const std::optional<HighlightsRange> &range,
HighlightingMode mode)
{
using namespace QQmlJS::Dom;
- Highlights highlights(mode);
- HighlightingVisitor highlightDomElements(highlights, range);
- // In QmlFile level, visitTree visits even FileLocations tree which takes quite a time to
- // finish. HighlightingFilter is added to prevent unnecessary visits.
- item.visitTree(Path(), highlightDomElements, VisitOption::Default, emptyChildrenVisitor,
- emptyChildrenVisitor, highlightingFilter());
+ HighlightingVisitor highlightDomElements(item, range, mode);
+ return highlightDomElements.highlights();
+}
+QList<int> HighlightingUtils::collectTokens(const QQmlJS::Dom::DomItem &item,
+ const std::optional<HighlightsRange> &range,
+ HighlightingMode mode)
+{
+ Highlights highlights = visitTokens(item, range, mode);
return HighlightingUtils::encodeSemanticTokens(highlights);
}
-
QT_END_NAMESPACE
diff --git a/src/qmlls/qqmlsemantictokens_p.h b/src/qmlls/qqmlsemantictokens_p.h
index 5f5e5e8e0c..6ce273ac31 100644
--- a/src/qmlls/qqmlsemantictokens_p.h
+++ b/src/qmlls/qqmlsemantictokens_p.h
@@ -169,8 +169,8 @@ public:
void addHighlight(const QQmlJS::SourceLocation &loc, HighlightingUtils::QmlHighlightKind,
HighlightingUtils::QmlHighlightModifiers =
HighlightingUtils::QmlHighlightModifier::None);
- HighlightsContainer &highlights() { return m_highlights; }
- const HighlightsContainer &highlights() const { return m_highlights; }
+ HighlightsContainer &tokens() { return m_highlights; }
+ const HighlightsContainer &tokens() const { return m_highlights; }
private:
void addHighlightImpl(const QQmlJS::SourceLocation &loc, int tokenType, int tokenModifier = 0);
@@ -191,15 +191,23 @@ namespace HighlightingUtils
QList<int> collectTokens(const QQmlJS::Dom::DomItem &item,
const std::optional<HighlightsRange> &range,
HighlightingMode mode = HighlightingMode::Default);
+ Highlights visitTokens(const QQmlJS::Dom::DomItem &item,
+ const std::optional<HighlightsRange> &range,
+ HighlightingMode mode = HighlightingMode::Default);
} // namespace HighlightingUtils
class HighlightingVisitor
{
public:
- HighlightingVisitor(Highlights &highlights, const std::optional<HighlightsRange> &range);
- bool operator()(QQmlJS::Dom::Path, const QQmlJS::Dom::DomItem &item, bool);
+ HighlightingVisitor(const QQmlJS::Dom::DomItem &item,
+ const std::optional<HighlightsRange> &range,
+ HighlightingUtils::HighlightingMode mode =
+ HighlightingUtils::HighlightingMode::Default);
+ const Highlights &hightights() const { return m_highlights; }
+ Highlights &highlights() { return m_highlights; }
private:
+ bool visitor(QQmlJS::Dom::Path, const QQmlJS::Dom::DomItem &item, bool);
void highlightComment(const QQmlJS::Dom::DomItem &item);
void highlightImport(const QQmlJS::Dom::DomItem &item);
void highlightBinding(const QQmlJS::Dom::DomItem &item);
@@ -216,7 +224,7 @@ private:
void highlightScriptExpressions(const QQmlJS::Dom::DomItem &item);
private:
- Highlights &m_highlights;
+ Highlights m_highlights;
std::optional<HighlightsRange> m_range;
};