aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/designercore/projectstorage/qmldocumentparser.cpp
blob: 8d0c9048799c6cb057314276573fe41a5fac5d33 (plain)
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
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "qmldocumentparser.h"

#include "projectstorage.h"
#include "sourcepathcache.h"

#include <sqlitedatabase.h>

#include <qmldom/qqmldomtop_p.h>

#include <filesystem>
#include <QDateTime>

namespace QmlDesigner {

namespace QmlDom = QQmlJS::Dom;

namespace {

using QualifiedImports = std::map<QString, Storage::Import>;

int convertVersionNumber(qint32 versionNumber)
{
    return versionNumber < 0 ? -1 : versionNumber;
}

Storage::Version convertVersion(QmlDom::Version version)
{
    return Storage::Version{convertVersionNumber(version.majorVersion),
                            convertVersionNumber(version.minorVersion)};
}

Utils::PathString convertUri(const QString &uri)
{
    QStringView localPath{uri.begin() + 7, uri.end()};

    std::filesystem::path path{
        std::u16string_view{localPath.utf16(), static_cast<std::size_t>(localPath.size())}};

    auto x = std::filesystem::weakly_canonical(path);

    return Utils::PathString{x.generic_string()};
}

Storage::Import createImport(const QmlDom::Import &qmlImport,
                             SourceId sourceId,
                             Utils::SmallStringView directoryPath,
                             QmlDocumentParser::ProjectStorage &storage)
{
    if (qmlImport.uri == u"file://.") {
        auto moduleId = storage.moduleId(directoryPath);
        return Storage::Import(moduleId, Storage::Version{}, sourceId);
    }

    if (qmlImport.uri.startsWith(u"file://")) {
        auto moduleId = storage.moduleId(convertUri(qmlImport.uri));
        return Storage::Import(moduleId, Storage::Version{}, sourceId);
    }

    auto moduleId = storage.moduleId(Utils::SmallString{qmlImport.uri});
    return Storage::Import(moduleId, convertVersion(qmlImport.version), sourceId);
}

QualifiedImports filterQualifiedImports(const QList<QmlDom::Import> &qmlImports,
                                        SourceId sourceId,
                                        Utils::SmallStringView directoryPath,
                                        QmlDocumentParser::ProjectStorage &storage)
{
    QualifiedImports qualifiedImports;

    for (const QmlDom::Import &qmlImport : qmlImports) {
        if (!qmlImport.importId.isEmpty())
            qualifiedImports.try_emplace(qmlImport.importId,
                                         createImport(qmlImport, sourceId, directoryPath, storage));
    }

    return qualifiedImports;
}

void addImports(Storage::Imports &imports,
                const QList<QmlDom::Import> &qmlImports,
                SourceId sourceId,
                Utils::SmallStringView directoryPath,
                QmlDocumentParser::ProjectStorage &storage)
{
    for (const QmlDom::Import &qmlImport : qmlImports)
        imports.push_back(createImport(qmlImport, sourceId, directoryPath, storage));

    auto end = imports.end();
    auto begin = std::prev(end, qmlImports.size());

    std::sort(begin, end);
    imports.erase(std::unique(begin, end), end);
}

Storage::ImportedTypeName createImportedTypeName(const QStringView rawtypeName,
                                                 const QList<QmlDom::Import> &qmlImports,
                                                 SourceId sourceId,
                                                 Utils::SmallStringView directoryPath,
                                                 QmlDocumentParser::ProjectStorage &storage)
{
    if (!rawtypeName.contains('.')) {
        return Storage::ImportedType{Utils::SmallString{rawtypeName}};
    }

    auto qualifiedImports = filterQualifiedImports(qmlImports, sourceId, directoryPath, storage);

    auto foundDot = std::find(rawtypeName.begin(), rawtypeName.end(), '.');

    QStringView alias(rawtypeName.begin(), foundDot);

    auto foundImport = qualifiedImports.find(alias.toString());

    QStringView typeName(std::next(foundDot), rawtypeName.end());

    return Storage::QualifiedImportedType{Utils::SmallString{typeName.toString()},
                                          foundImport->second};
}

void addPropertyDeclarations(Storage::Type &type, const QmlDom::QmlObject &rootObject)
{
    for (const QmlDom::PropertyDefinition &propertyDeclaration : rootObject.propertyDefs()) {
        type.propertyDeclarations.emplace_back(Utils::SmallString{propertyDeclaration.name},
                                               Storage::ImportedType{
                                                   Utils::SmallString{propertyDeclaration.typeName}},
                                               Storage::PropertyDeclarationTraits::None);
    }
}

void addParameterDeclaration(Storage::ParameterDeclarations &parameterDeclarations,
                             const QList<QmlDom::MethodParameter> &parameters)
{
    for (const QmlDom::MethodParameter &parameter : parameters) {
        parameterDeclarations.emplace_back(Utils::SmallString{parameter.name},
                                           Utils::SmallString{parameter.typeName});
    }
}

void addFunctionAndSignalDeclarations(Storage::Type &type, const QmlDom::QmlObject &rootObject)
{
    for (const QmlDom::MethodInfo &methodInfo : rootObject.methods()) {
        if (methodInfo.methodType == QmlDom::MethodInfo::Method) {
            auto &functionDeclaration = type.functionDeclarations.emplace_back(
                Utils::SmallString{methodInfo.name}, "", Storage::ParameterDeclarations{});
            addParameterDeclaration(functionDeclaration.parameters, methodInfo.parameters);
        } else {
            auto &signalDeclaration = type.signalDeclarations.emplace_back(
                Utils::SmallString{methodInfo.name});
            addParameterDeclaration(signalDeclaration.parameters, methodInfo.parameters);
        }
    }
}

Storage::EnumeratorDeclarations createEnumerators(const QmlDom::EnumDecl &enumeration)
{
    Storage::EnumeratorDeclarations enumeratorDeclarations;
    for (const QmlDom::EnumItem &enumerator : enumeration.values()) {
        enumeratorDeclarations.emplace_back(Utils::SmallString{enumerator.name()},
                                            static_cast<long long>(enumerator.value()));
    }
    return enumeratorDeclarations;
}

void addEnumeraton(Storage::Type &type, const QmlDom::Component &component)
{
    for (const QmlDom::EnumDecl &enumeration : component.enumerations()) {
        Storage::EnumeratorDeclarations enumeratorDeclarations = createEnumerators(enumeration);
        type.enumerationDeclarations.emplace_back(Utils::SmallString{enumeration.name()},
                                                  std::move(enumeratorDeclarations));
    }
}

} // namespace

Storage::Type QmlDocumentParser::parse(const QString &sourceContent,
                                       Storage::Imports &imports,
                                       SourceId sourceId)
{
    Storage::Type type;

    QmlDom::DomItem environment = QmlDom::DomEnvironment::create(
        {},
        QmlDom::DomEnvironment::Option::SingleThreaded
            | QmlDom::DomEnvironment::Option::NoDependencies);

    QmlDom::DomItem items;

    QString filePath{m_pathCache.sourcePath(sourceId)};

    environment.loadFile(
        filePath,
        filePath,
        sourceContent,
        QDateTime{},
        [&](QmlDom::Path, const QmlDom::DomItem &, const QmlDom::DomItem &newItems) {
            items = newItems;
        },
        QmlDom::LoadOption::DefaultLoad,
        QmlDom::DomType::QmlFile);

    environment.loadPendingDependencies();

    QmlDom::DomItem file = items.field(QmlDom::Fields::currentItem);
    const QmlDom::QmlFile *qmlFile = file.as<QmlDom::QmlFile>();
    const auto &components = qmlFile->components();

    if (components.empty())
        return type;

    const auto &component = components.first();
    const auto &objects = component.objects();

    if (objects.empty())
        return type;

    const QmlDom::QmlObject &qmlObject = objects.front();

    const auto qmlImports = qmlFile->imports();
    auto directoryPath{m_pathCache.sourceContextPath(m_pathCache.sourceContextId(sourceId))};

    type.prototype = createImportedTypeName(qmlObject.name(),
                                            qmlImports,
                                            sourceId,
                                            directoryPath,
                                            m_storage);

    addImports(imports, qmlFile->imports(), sourceId, directoryPath, m_storage);

    addPropertyDeclarations(type, qmlObject);
    addFunctionAndSignalDeclarations(type, qmlObject);
    addEnumeraton(type, component);

    return type;
}

} // namespace QmlDesigner