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 GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "effectsautocomplete.h"
#include <texteditor/codeassist/genericproposal.h>
#include <texteditor/texteditorwidget.h>
#include <texteditor/texteditorsettings.h>
#include <utils/qtcassert.h>
#include <qmljseditor/qmljscompletionassist.h>
namespace {
enum CompletionOrder {
EnumValueOrder = -5,
SnippetOrder = -15,
PropertyOrder = -10,
SymbolOrder = -20,
KeywordOrder = -25,
TypeOrder = -30
};
/*!
Returns a number defining how well \a searchStr matches \a str.
Quite simplistic, looks only at the first match, and prefers contiguous
matches, or matches to capitalized or separated words.
Match to the last character is also preferred.
*/
int matchStrength(const QString &searchStr, const QString &str)
{
QString::const_iterator i = searchStr.constBegin(), iEnd = searchStr.constEnd(),
j = str.constBegin(), jEnd = str.constEnd();
bool lastWasNotUpper = true, lastWasSpacer = true, lastWasMatch = false, didJump = false;
int res = 0;
while (i != iEnd && j != jEnd) {
bool thisIsUpper = (*j).isUpper();
bool thisIsLetterOrNumber = (*j).isLetterOrNumber();
if ((*i).toLower() == (*j).toLower()) {
if (lastWasMatch || (lastWasNotUpper && thisIsUpper) || (thisIsUpper && (*i).isUpper())
|| (lastWasSpacer && thisIsLetterOrNumber))
++res;
lastWasMatch = true;
++i;
} else {
didJump = true;
lastWasMatch = false;
}
++j;
lastWasNotUpper = !thisIsUpper;
lastWasSpacer = !thisIsLetterOrNumber;
}
if (i != iEnd)
return i - iEnd;
if (j == jEnd)
++res;
if (!didJump)
res += 2;
return res;
}
bool isIdentifierChar(const QChar &c, bool atStart, bool acceptDollar)
{
switch (c.unicode()) {
case '_':
return true;
case '$':
if (acceptDollar)
return true;
return false;
default:
if (atStart)
return c.isLetter();
else
return c.isLetterOrNumber();
}
}
class CompleteFunctionCall
{
public:
CompleteFunctionCall(bool hasArguments = true)
: hasArguments(hasArguments)
{}
bool hasArguments;
};
class QmlJSLessThan
{
using AssistProposalItemInterface = TextEditor::AssistProposalItemInterface;
public:
QmlJSLessThan(const QString &searchString)
: m_searchString(searchString)
{}
bool operator()(const AssistProposalItemInterface *a, const AssistProposalItemInterface *b)
{
if (a->order() != b->order())
return a->order() > b->order();
else if (a->text().isEmpty() && !b->text().isEmpty())
return true;
else if (b->text().isEmpty())
return false;
else if (a->text().at(0).isUpper() && b->text().at(0).isLower())
return false;
else if (a->text().at(0).isLower() && b->text().at(0).isUpper())
return true;
int m1 = ::matchStrength(m_searchString, a->text());
int m2 = ::matchStrength(m_searchString, b->text());
if (m1 != m2)
return m1 > m2;
return a->text() < b->text();
}
private:
QString m_searchString;
};
} // namespace
namespace EffectComposer {
class EffectsCodeAssistProposalItem final : public TextEditor::AssistProposalItem
{
using TextEditorSettings = TextEditor::TextEditorSettings;
public:
bool prematurelyApplies(const QChar &c) const final
{
if (data().canConvert<QString>()) // snippet
return false;
return (text().endsWith(QLatin1String(": ")) && c == QLatin1Char(':'))
|| (text().endsWith(QLatin1Char('.')) && c == QLatin1Char('.'));
}
void applyContextualContent(
TextEditor::TextEditorWidget *textEditorWidget, int basePosition) const final
{
std::function<int()> currentPosition = [&]() -> int {
return textEditorWidget->position();
};
textEditorWidget->replace(basePosition, currentPosition() - basePosition, QString());
QString content = text();
int cursorOffset = 0;
const bool autoInsertBrackets
= TextEditorSettings::completionSettings().m_autoInsertBrackets;
if (autoInsertBrackets && data().canConvert<CompleteFunctionCall>()) {
CompleteFunctionCall function = data().value<CompleteFunctionCall>();
content += QLatin1String("()");
if (function.hasArguments)
cursorOffset = -1;
}
QString replaceable = content;
int replacedLength = 0;
for (int i = 0; i < replaceable.length(); ++i) {
const QChar a = replaceable.at(i);
const QChar b = textEditorWidget->characterAt(currentPosition() + i);
if (a == b)
++replacedLength;
else
break;
}
const int length = currentPosition() - basePosition + replacedLength;
textEditorWidget->replace(basePosition, length, content);
if (cursorOffset) {
textEditorWidget->setCursorPosition(currentPosition() + cursorOffset);
textEditorWidget->setAutoCompleteSkipPosition(textEditorWidget->textCursor());
}
}
};
class EffectsAssistProposalModel : public TextEditor::GenericProposalModel
{
using AssistProposalItemInterface = TextEditor::AssistProposalItemInterface;
using AssistReason = TextEditor::AssistReason;
public:
EffectsAssistProposalModel(const QList<TextEditor::AssistProposalItemInterface *> &items)
{
loadContent(items);
}
void filter(const QString &prefix) override;
void sort(const QString &prefix) override;
bool keepPerfectMatch(TextEditor::AssistReason reason) const override;
};
void EffectsAssistProposalModel::filter(const QString &prefix)
{
GenericProposalModel::filter(prefix);
if (prefix.startsWith(QLatin1String("__")))
return;
QList<AssistProposalItemInterface *> newCurrentItems;
newCurrentItems.reserve(m_currentItems.size());
for (AssistProposalItemInterface *item : std::as_const(m_currentItems)) {
if (!item->text().startsWith(QLatin1String("__")))
newCurrentItems << item;
}
m_currentItems = newCurrentItems;
}
void EffectsAssistProposalModel::sort(const QString &prefix)
{
std::sort(m_currentItems.begin(), m_currentItems.end(), QmlJSLessThan(prefix));
}
bool EffectsAssistProposalModel::keepPerfectMatch(AssistReason reason) const
{
return reason == TextEditor::ExplicitlyInvoked;
}
void addCompletion(
QList<TextEditor::AssistProposalItemInterface *> *completions,
const QString &text,
const QIcon &icon,
int order,
const QVariant &data = QVariant())
{
if (text.isEmpty())
return;
TextEditor::AssistProposalItem *item = new EffectsCodeAssistProposalItem;
item->setText(text);
item->setIcon(icon);
item->setOrder(order);
item->setData(data);
completions->append(item);
}
void addCompletions(
QList<TextEditor::AssistProposalItemInterface *> *completions,
const QStringList &newCompletions,
const QIcon &icon,
int order)
{
for (const QString &text : newCompletions)
addCompletion(completions, text, icon, order);
}
EffectsCompletionAssistProcessor::EffectsCompletionAssistProcessor()
: m_startPosition(0)
{}
TextEditor::IAssistProposal *EffectsCompletionAssistProcessor::performAsync()
{
using QmlJSEditor::QmlJSCompletionAssistInterface;
auto completionInterface = static_cast<const EffectsCompletionAssistInterface *>(interface());
QTC_ASSERT(completionInterface, return {});
m_startPosition = completionInterface->position();
QTextDocument *textDocument = completionInterface->textDocument();
while (isIdentifierChar(textDocument->characterAt(m_startPosition - 1), false, false))
--m_startPosition;
m_completions.clear();
// The completionOperator is the character under the cursor or directly before the
// identifier under cursor. Use in conjunction with onIdentifier. Examples:
// a + b<complete> -> ' '
// a +<complete> -> '+'
// a +b<complete> -> '+'
QChar completionOperator;
if (m_startPosition > 0)
completionOperator = textDocument->characterAt(m_startPosition - 1);
if (completionOperator != QLatin1Char('.')) {
addCompletions(
&m_completions,
completionInterface->uniformNames(),
QmlJSCompletionAssistInterface::keywordIcon(),
KeywordOrder);
}
if (!m_completions.isEmpty()) {
TextEditor::GenericProposalModelPtr model(new EffectsAssistProposalModel(m_completions));
return new TextEditor::GenericProposal(m_startPosition, model);
}
return nullptr;
}
TextEditor::IAssistProcessor *EffectsCompeletionAssistProvider::createProcessor(
const TextEditor::AssistInterface *) const
{
return new EffectsCompletionAssistProcessor;
}
} // namespace EffectComposer
|