1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qqmllshelputils_p.h"
#include <QtQmlLS/private/qqmllsutils_p.h>
#include <QtCore/private/qfactoryloader_p.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/qdiriterator.h>
#include <QtCore/qdir.h>
#include <QtQmlCompiler/private/qqmljstyperesolver_p.h>
#include <optional>
QT_BEGIN_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(QQmlLSHelpUtilsLog, "qt.languageserver.helpUtils")
using namespace QQmlJS::Dom;
static QStringList documentationFiles(const QString &qtInstallationPath)
{
QStringList result;
QDirIterator dirIterator(qtInstallationPath, QStringList{ "*.qch"_L1 }, QDir::Files);
while (dirIterator.hasNext()) {
const auto fileInfo = dirIterator.nextFileInfo();
result << fileInfo.absoluteFilePath();
}
return result;
}
HelpManager::HelpManager()
{
const QFactoryLoader pluginLoader(QQmlLSHelpPluginInterface_iid, u"/help"_s);
const auto keys = pluginLoader.metaDataKeys();
for (qsizetype i = 0; i < keys.size(); ++i) {
auto instance = qobject_cast<QQmlLSHelpPluginInterface *>(pluginLoader.instance(i));
if (instance) {
m_helpPlugin =
instance->initialize(QDir::tempPath() + "/collectionFile.qhc"_L1, nullptr);
break;
}
}
}
void HelpManager::setDocumentationRootPath(const QString &path)
{
if (m_docRootPath == path)
return;
m_docRootPath = path;
const auto foundQchFiles = documentationFiles(path);
if (foundQchFiles.isEmpty()) {
qCWarning(QQmlLSHelpUtilsLog)
<< "No documentation files found in the Qt doc installation path: " << path;
return;
}
return registerDocumentations(foundQchFiles);
}
QString HelpManager::documentationRootPath() const
{
return m_docRootPath;
}
void HelpManager::registerDocumentations(const QStringList &docs) const
{
if (!m_helpPlugin)
return;
std::for_each(docs.cbegin(), docs.cend(),
[this](const auto &file) { m_helpPlugin->registerDocumentation(file); });
}
std::optional<QByteArray> HelpManager::extractDocumentation(const DomItem &item) const
{
if (item.internalKind() == DomType::ScriptIdentifierExpression) {
const auto resolvedType =
QQmlLSUtils::resolveExpressionType(item, QQmlLSUtils::ResolveOwnerType);
if (!resolvedType)
return std::nullopt;
return extractDocumentationForIdentifiers(item, resolvedType.value());
} else {
return extractDocumentationForDomElements(item);
}
Q_UNREACHABLE_RETURN(std::nullopt);
}
std::optional<QByteArray>
HelpManager::extractDocumentationForIdentifiers(const DomItem &item,
QQmlLSUtils::ExpressionType expr) const
{
const auto links = collectDocumentationLinks(item, expr.semanticScope, expr.name.value_or(item.name()));
if (links.empty())
return std::nullopt;
switch (expr.type) {
case QQmlLSUtils::QmlObjectIdIdentifier:
case QQmlLSUtils::JavaScriptIdentifier:
case QQmlLSUtils::GroupedPropertyIdentifier:
case QQmlLSUtils::PropertyIdentifier: {
ExtractDocumentation extractor(DomType::PropertyDefinition);
return tryExtract(extractor, links, expr.name.value());
}
case QQmlLSUtils::PropertyChangedSignalIdentifier:
case QQmlLSUtils::PropertyChangedHandlerIdentifier:
case QQmlLSUtils::SignalIdentifier:
case QQmlLSUtils::SignalHandlerIdentifier:
case QQmlLSUtils::MethodIdentifier: {
ExtractDocumentation extractor(DomType::MethodInfo);
return tryExtract(extractor, links, expr.name.value());
}
case QQmlLSUtils::SingletonIdentifier:
case QQmlLSUtils::AttachedTypeIdentifier:
case QQmlLSUtils::QmlComponentIdentifier: {
const auto &keyword = item.field(Fields::identifier).value().toString();
// The keyword is a qmlobject. Keyword search should be sufficient.
// TODO: Still there can be multiple qmlobject documentation, with
// different Qt versions. We should pick the best one.
ExtractDocumentation extractor(DomType::QmlObject);
return tryExtract(extractor, m_helpPlugin->documentsForKeyword(keyword), keyword);
}
// Not implemented yet
case QQmlLSUtils::EnumeratorIdentifier:
case QQmlLSUtils::EnumeratorValueIdentifier:
default:
qCDebug(QQmlLSHelpUtilsLog)
<< "Documentation extraction for" << expr.name.value() << "was not implemented";
return std::nullopt;
}
Q_UNREACHABLE_RETURN(std::nullopt);
}
std::optional<QByteArray> HelpManager::extractDocumentationForDomElements(const DomItem &item) const
{
const auto qmlFile = item.containingFile().as<QmlFile>();
if (!qmlFile)
return std::nullopt;
const auto name = item.field(Fields::name).value().toString();
std::vector<QQmlLSHelpProviderBase::DocumentLink> links;
switch (item.internalKind()) {
case DomType::QmlObject: {
links = collectDocumentationLinks(item, item.nearestSemanticScope(), name);
break;
}
case DomType::PropertyDefinition: {
links = collectDocumentationLinks(
item, QQmlLSUtils::findDefiningScopeForProperty(item.nearestSemanticScope(), name),
name);
break;
}
case DomType::Binding: {
links = collectDocumentationLinks(
item, QQmlLSUtils::findDefiningScopeForBinding(item.nearestSemanticScope(), name),
name);
break;
}
case DomType::MethodInfo: {
links = collectDocumentationLinks(
item, QQmlLSUtils::findDefiningScopeForMethod(item.nearestSemanticScope(), name),
name);
break;
}
default:
qCDebug(QQmlLSHelpUtilsLog)
<< item.internalKindStr() << "was not implemented for documentation extraction";
return std::nullopt;
}
ExtractDocumentation extractor(item.internalKind());
return tryExtract(extractor, links, name);
}
std::optional<QByteArray>
HelpManager::tryExtract(ExtractDocumentation &extractor,
const std::vector<QQmlLSHelpProviderBase::DocumentLink> &links,
const QString &name) const
{
if (!m_helpPlugin)
return std::nullopt;
for (auto &&link : links) {
const auto fileData = m_helpPlugin->fileData(link.url);
if (fileData.isEmpty()) {
qCDebug(QQmlLSHelpUtilsLog) << "No documentation found for" << link.url;
continue;
}
const auto &documentation = extractor.execute(QString::fromUtf8(fileData), name,
HtmlExtractor::ExtractionMode::Simplified);
if (documentation.isEmpty())
continue;
return documentation.toUtf8();
}
return std::nullopt;
}
std::optional<QByteArray>
HelpManager::documentationForItem(const DomItem &file, QLspSpecification::Position position)
{
if (!m_helpPlugin)
return std::nullopt;
if (m_helpPlugin->registeredNamespaces().empty())
return std::nullopt;
// Prepare Cpp types to Qml types mapping.
const auto fileItem = file.containingFile().as<QmlFile>();
if (!fileItem)
return std::nullopt;
const auto typeResolver = fileItem->typeResolver();
if (typeResolver) {
const auto &names = typeResolver->importedNames();
for (auto &&[scope, qmlName] : names.asKeyValueRange()) {
auto sc = scope;
// in some situations, scope->internalName() could be the same
// as qmlName. In those cases, the key we are looking for is the
// first scope which is non-composite type.
// This is mostly the case for templated controls.
// Popup <-> Popup
// T.Popup <-> Popup
// QQuickPopup <-> Popup
if (sc && sc->internalName() == qmlName) {
while (sc && sc->isComposite())
sc = sc->baseType();
}
if (sc && !m_cppTypesToQmlTypes.contains(sc->internalName()))
m_cppTypesToQmlTypes.insert(sc->internalName(), qmlName);
}
}
std::optional<QByteArray> result;
const auto [line, character] = position;
const auto itemLocations = QQmlLSUtils::itemsFromTextLocation(file, line, character);
// Process found item's internalKind and fetch its documentation.
for (const auto &entry : itemLocations) {
result = extractDocumentation(entry.domItem);
if (result.has_value())
break;
}
return result;
}
/*
* Returns the list of potential documentation links for the given item.
* A keyword is not necessarily a unique name, so we need to find the scope where
* the keyword is defined. If the item is a property, method or binding, it will
* search for the defining scope and return the documentation links by looking at
* the imported names. If the item is a QmlObject, it will return the documentation
* links for qmlobject name.
*/
std::vector<QQmlLSHelpProviderBase::DocumentLink>
HelpManager::collectDocumentationLinks(const DomItem &item, const QQmlJSScope::ConstPtr &definingScope,
const QString &name) const
{
if (!(m_helpPlugin && definingScope))
return {};
const auto &qmlFile = item.containingFile().as<QmlFile>();
if (!qmlFile)
return {};
const auto typeResolver = qmlFile->typeResolver();
if (!typeResolver)
return {};
std::vector<QQmlLSHelpProviderBase::DocumentLink> links;
const auto &foundScopeName = definingScope->internalName();
if (m_cppTypesToQmlTypes.contains(foundScopeName)) {
const QString id = m_cppTypesToQmlTypes.value(foundScopeName) + u"::"_s + name;
links = m_helpPlugin->documentsForIdentifier(id);
if (!links.empty())
return links;
}
const auto &containingObjectName = item.qmlObject().name();
auto scope = item.nearestSemanticScope();
while (scope && scope->isComposite()) {
const QString id = containingObjectName + u"::"_s + name;
links = m_helpPlugin->documentsForIdentifier(id);
if (!links.empty())
return links;
scope = scope->baseType();
}
while (scope && !m_cppTypesToQmlTypes.contains(scope->internalName())) {
const QString id = m_cppTypesToQmlTypes.value(scope->internalName()) + u"::"_s + name;
links = m_helpPlugin->documentsForIdentifier(id);
if (!links.empty())
return links;
scope = scope->baseType();
}
return m_helpPlugin->documentsForKeyword(name);
}
QT_END_NAMESPACE
|