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
signaturespan.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <QJsonArray>
7#include <QStringList>
8
10
11using namespace Qt::Literals::StringLiterals;
12
13namespace IR {
14
15/*!
16 \enum IR::SpanRole
17 \internal
18 \brief Discriminator for semantic roles within a signature span sequence.
19
20 SpanRole classifies each span in a signature representation by its
21 semantic purpose. Renderers use the role to apply appropriate formatting
22 (e.g., linking type names, styling qualifiers as badges).
23
24 \value Text Plain text such as punctuation, whitespace, or keywords like "const".
25 \value Type A type name, potentially linkable to its documentation page.
26 \value Name The member or function name, potentially linkable.
27 \value Parameter A parameter name within a function signature.
28 \value Operator A delimiter operator such as "::" or ".".
29 \value Extra An extra qualifier badge such as "[static]" or "[virtual]".
30 \value TemplateDecl A template declaration wrapper containing child type spans.
31 \value Link An explicit link to a documented node.
32 \value ExternalRef An external reference such as a cppreference.com link.
33*/
34
35/*!
36 \struct IR::SignatureSpan
37 \internal
38 \brief Represents a single span within a structured signature.
39
40 SignatureSpan is a format-agnostic representation of one semantic
41 element within a function or member signature. Spans carry a semantic
42 role, display text, an optional link target, and optional children
43 for nested structures such as template declarations.
44
45 Unlike CodeMarker's tagged strings, SignatureSpan captures semantic
46 knowledge directly in the IR. Any generator consuming the IR can
47 render signatures correctly without calling back into QDoc internals.
48
49 This is a pure value type with no dependencies on QDoc's core
50 infrastructure. It belongs in QDocLib.
51
52 \sa SpanRole
53*/
54
56{
57 switch (role) {
58 case SpanRole::Text: return u"text"_s;
59 case SpanRole::Type: return u"type"_s;
60 case SpanRole::Name: return u"name"_s;
61 case SpanRole::Parameter: return u"parameter"_s;
62 case SpanRole::Operator: return u"operator"_s;
63 case SpanRole::Extra: return u"extra"_s;
64 case SpanRole::TemplateDecl: return u"template-decl"_s;
65 case SpanRole::Link: return u"link"_s;
66 case SpanRole::ExternalRef: return u"external-ref"_s;
67 }
68 Q_UNREACHABLE();
69}
70
71/*!
72 Converts the SignatureSpan to a QJsonObject for template rendering.
73
74 The JSON includes a \c role key with the kebab-case role identifier
75 and a \c text key with the span's display text. The \c href key is
76 omitted when empty. The \c children array is omitted when empty.
77*/
79{
80 QJsonObject json;
81 json["role"_L1] = spanRoleId(role);
82 json["text"_L1] = text;
83
84 if (!href.isEmpty())
85 json["href"_L1] = href;
86
87 if (!children.isEmpty()) {
88 QJsonArray childArr;
89 for (const auto &child : children)
90 childArr.append(child.toJson());
91 json["children"_L1] = childArr;
92 }
93
94 return json;
95}
96
97/*!
98 Returns the concatenated plain text of this span and all its
99 children, recursively.
100
101 The span's own text is emitted first, followed by the concatenated
102 plain text of all children. No separators are inserted — spans
103 carry their own whitespace as Text spans.
104*/
106{
107 if (children.isEmpty())
108 return text;
109
110 QStringList parts;
111 parts.reserve(children.size() + 1);
112
113 if (!text.isEmpty())
114 parts.append(text);
115
116 for (const auto &child : children)
117 parts.append(child.plainText());
118
119 return parts.join(u""_s);
120}
121
122} // namespace IR
123
124QT_END_NAMESPACE
Definition builder.cpp:14
static QString spanRoleId(SpanRole role)
Combined button and popup list for selecting options.
Represents a single span within a structured signature.
QString plainText() const
Returns the concatenated plain text of this span and all its children, recursively.
QJsonObject toJson() const
Converts the SignatureSpan to a QJsonObject for template rendering.