aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmlls/quick/qqmllsquickplugin.cpp
blob: 67b95b074a231bc9d0a719d2670622485443ab0a (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qqmllsquickplugin_p.h"
#include <QtQmlLS/private/qqmllsutils_p.h>
#include <QtQmlLS/private/qqmllscompletion_p.h>

using namespace QLspSpecification;
using namespace QQmlJS::Dom;

QT_BEGIN_NAMESPACE

std::unique_ptr<QQmlLSCompletionPlugin> QQmlLSQuickPlugin::createCompletionPlugin() const
{
    return std::make_unique<QQmlLSQuickCompletionPlugin>();
}

void QQmlLSQuickCompletionPlugin::suggestSnippetsForLeftHandSideOfBinding(
        const DomItem &itemAtPosition, BackInsertIterator result) const
{
    auto file = itemAtPosition.containingFile().as<QmlFile>();
    if (!file)
        return;

    // check if QtQuick has been imported
    const auto &imports = file->imports();
    auto it = std::find_if(imports.constBegin(), imports.constEnd(), [](const Import &import) {
        return import.uri.moduleUri() == u"QtQuick";
    });
    if (it == imports.constEnd()) {
        return;
    }

    // for default bindings:
    suggestSnippetsForRightHandSideOfBinding(itemAtPosition, result);

    // check if the user already typed some qualifier, remove its dot and compare it to QtQuick's
    // qualified name
    const QString userTypedQualifier = QQmlLSUtils::qualifiersFrom(itemAtPosition);
    if (!userTypedQualifier.isEmpty()
        && !it->importId.startsWith(QStringView(userTypedQualifier).chopped(1))) {
        return;
    }

    const QByteArray prefixForSnippet =
            userTypedQualifier.isEmpty() ? it->importId.toUtf8() : QByteArray();
    const QByteArray prefixWithDotForSnippet =
            prefixForSnippet.isEmpty() ? QByteArray() : QByteArray(prefixForSnippet).append(u'.');

    auto resolver = file->typeResolver();
    if (!resolver)
        return;
    const auto qquickItemScope = resolver->typeForName(prefixWithDotForSnippet + u"Item"_s);
    const QQmlJSScope::ConstPtr ownerScope = itemAtPosition.qmlObject().semanticScope();
    if (!ownerScope || !qquickItemScope)
        return;

    if (ownerScope->inherits(qquickItemScope)) {
        result = QQmlLSCompletion::makeSnippet(
                "states binding with PropertyChanges in State",
                "states: [\n"
                "\t"_ba.append(prefixWithDotForSnippet)
                        .append("State {\n"
                                "\t\tname: \"${1:name}\"\n"
                                "\t\t"_ba.append(prefixWithDotForSnippet)
                                        .append("PropertyChanges {\n"
                                                "\t\t\ttarget: ${2:object}\n"
                                                "\t\t}\n"
                                                "\t}\n"
                                                "]")));
        result = QQmlLSCompletion::makeSnippet("transitions binding with Transition",
                                               "transitions: [\n"
                                               "\t"_ba.append(prefixWithDotForSnippet)
                                                       .append("Transition {\n"
                                                               "\t\tfrom: \"${1:fromState}\"\n"
                                                               "\t\tto: \"${2:fromState}\"\n"
                                                               "\t}\n"
                                                               "]"));
    }
}

void QQmlLSQuickCompletionPlugin::suggestSnippetsForRightHandSideOfBinding(
        const DomItem &itemAtPosition, BackInsertIterator result) const
{
    auto file = itemAtPosition.containingFile().as<QmlFile>();
    if (!file)
        return;

    // check if QtQuick has been imported
    const auto &imports = file->imports();
    auto it = std::find_if(imports.constBegin(), imports.constEnd(), [](const Import &import) {
        return import.uri.moduleUri() == u"QtQuick";
    });
    if (it == imports.constEnd()) {
        return;
    }

    // check if the user already typed some qualifier, remove its dot and compare it to QtQuick's
    // qualified name
    const QString userTypedQualifier = QQmlLSUtils::qualifiersFrom(itemAtPosition);
    if (!userTypedQualifier.isEmpty()
        && !it->importId.startsWith(QStringView(userTypedQualifier).chopped(1))) {
        return;
    }

    const QByteArray prefixForSnippet =
            userTypedQualifier.isEmpty() ? it->importId.toUtf8() : QByteArray();
    const QByteArray prefixWithDotForSnippet =
            prefixForSnippet.isEmpty() ? QByteArray() : QByteArray(prefixForSnippet).append(u'.');

    // Quick completions from Qt Creator's code model
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "BorderImage snippet",
                                           "BorderImage {\n"
                                           "\tid: ${1:name}\n"
                                           "\tsource: \"${2:file}\"\n"
                                           "\twidth: ${3:100}; height: ${4:100}\n"
                                           "\tborder.left: ${5: 5}; border.top: ${5}\n"
                                           "\tborder.right: ${5}; border.bottom: ${5}\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "ColorAnimation snippet",
                                           "ColorAnimation {\n"
                                           "\tfrom: \"${1:white}\"\n"
                                           "\tto: \"${2:black}\"\n"
                                           "\tduration: ${3:200}\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "Image snippet",
                                           "Image {\n"
                                           "\tid: ${1:name}\n"
                                           "\tsource: \"${2:file}\"\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "Item snippet",
                                           "Item {\n"
                                           "\tid: ${1:name}\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "NumberAnimation snippet",
                                           "NumberAnimation {\n"
                                           "\ttarget: ${1:object}\n"
                                           "\tproperty: \"${2:name}\"\n"
                                           "\tduration: ${3:200}\n"
                                           "\teasing.type: "_ba.append(prefixWithDotForSnippet)
                                                   .append("Easing.${4:InOutQuad}\n"
                                                           "}"));
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "NumberAnimation with targets snippet",
                                           "NumberAnimation {\n"
                                           "\ttargets: [${1:object}]\n"
                                           "\tproperties: \"${2:name}\"\n"
                                           "\tduration: ${3:200}\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "PauseAnimation snippet",
                                           "PauseAnimation {\n"
                                           "\tduration: ${1:200}\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "PropertyAction snippet",
                                           "PropertyAction {\n"
                                           "\ttarget: ${1:object}\n"
                                           "\tproperty: \"${2:name}\"\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "PropertyAction with targets snippet",
                                           "PropertyAction {\n"
                                           "\ttargets: [${1:object}]\n"
                                           "\tproperties: \"${2:name}\"\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "PropertyChanges snippet",
                                           "PropertyChanges {\n"
                                           "\ttarget: ${1:object}\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "State snippet",
                                           "State {\n"
                                           "\tname: ${1:name}\n"
                                           "\t"_ba.append(prefixWithDotForSnippet)
                                                   .append("PropertyChanges {\n"
                                                           "\t\ttarget: ${2:object}\n"
                                                           "\t}\n"
                                                           "}"));
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "Text snippet",
                                           "Text {\n"
                                           "\tid: ${1:name}\n"
                                           "\ttext: qsTr(\"${2:text}\")\n"
                                           "}");
    result = QQmlLSCompletion::makeSnippet(prefixForSnippet, "Transition snippet",
                                           "Transition {\n"
                                           "\tfrom: \"${1:fromState}\"\n"
                                           "\tto: \"${2:toState}\"\n"
                                           "}");
}

QT_END_NAMESPACE