forked from qt-creator/qt-creator
QmlJSEditor: Simplify Qml completion code.
This commit is contained in:
@@ -595,6 +595,31 @@ static bool isLiteral(AST::Node *ast)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CodeCompletion::addCompletions(const QHash<QString, const Interpreter::Value *> &newCompletions,
|
||||||
|
const QIcon &icon)
|
||||||
|
{
|
||||||
|
QHashIterator<QString, const Interpreter::Value *> it(newCompletions);
|
||||||
|
while (it.hasNext()) {
|
||||||
|
it.next();
|
||||||
|
|
||||||
|
TextEditor::CompletionItem item(this);
|
||||||
|
item.text = it.key();
|
||||||
|
item.icon = icon;
|
||||||
|
m_completions.append(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CodeCompletion::addCompletions(const QStringList &newCompletions,
|
||||||
|
const QIcon &icon)
|
||||||
|
{
|
||||||
|
foreach (const QString &text, newCompletions) {
|
||||||
|
TextEditor::CompletionItem item(this);
|
||||||
|
item.text = text;
|
||||||
|
item.icon = icon;
|
||||||
|
m_completions.append(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
||||||
{
|
{
|
||||||
m_editor = editor;
|
m_editor = editor;
|
||||||
@@ -660,25 +685,8 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
|||||||
enumerateProperties.setGlobalCompletion(true);
|
enumerateProperties.setGlobalCompletion(true);
|
||||||
enumerateProperties.setEnumerateGeneratedSlots(true);
|
enumerateProperties.setEnumerateGeneratedSlots(true);
|
||||||
|
|
||||||
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties(qmlScopeType));
|
addCompletions(enumerateProperties(qmlScopeType), symbolIcon);
|
||||||
while (it.hasNext()) {
|
addCompletions(enumerateProperties(context.scopeChain().qmlTypes), symbolIcon);
|
||||||
it.next();
|
|
||||||
|
|
||||||
TextEditor::CompletionItem item(this);
|
|
||||||
item.text = it.key();
|
|
||||||
item.icon = symbolIcon;
|
|
||||||
m_completions.append(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
it = enumerateProperties(context.scopeChain().qmlTypes);
|
|
||||||
while (it.hasNext()) {
|
|
||||||
it.next();
|
|
||||||
|
|
||||||
TextEditor::CompletionItem item(this);
|
|
||||||
item.text = it.key();
|
|
||||||
item.icon = symbolIcon;
|
|
||||||
m_completions.append(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contextFinder.isInRhsOfBinding() && qmlScopeType) {
|
if (contextFinder.isInRhsOfBinding() && qmlScopeType) {
|
||||||
@@ -713,30 +721,18 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
|||||||
// It's a global completion.
|
// It's a global completion.
|
||||||
EnumerateProperties enumerateProperties(&context);
|
EnumerateProperties enumerateProperties(&context);
|
||||||
enumerateProperties.setGlobalCompletion(true);
|
enumerateProperties.setGlobalCompletion(true);
|
||||||
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties());
|
addCompletions(enumerateProperties(), symbolIcon);
|
||||||
while (it.hasNext()) {
|
|
||||||
it.next();
|
|
||||||
|
|
||||||
TextEditor::CompletionItem item(this);
|
|
||||||
item.text = it.key();
|
|
||||||
item.icon = symbolIcon;
|
|
||||||
m_completions.append(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doJsKeywordCompletion) {
|
if (doJsKeywordCompletion) {
|
||||||
// add js keywords
|
// add js keywords
|
||||||
foreach (const QString &word, Scanner::keywords()) {
|
addCompletions(Scanner::keywords(), keywordIcon);
|
||||||
TextEditor::CompletionItem item(this);
|
|
||||||
item.text = word;
|
|
||||||
item.icon = keywordIcon;
|
|
||||||
m_completions.append(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add qml extra words
|
// add qml extra words
|
||||||
if (doQmlKeywordCompletion && isQmlFile) {
|
if (doQmlKeywordCompletion && isQmlFile) {
|
||||||
static QStringList qmlWords;
|
static QStringList qmlWords;
|
||||||
|
static QStringList qmlWordsAlsoInJs;
|
||||||
|
|
||||||
if (qmlWords.isEmpty()) {
|
if (qmlWords.isEmpty()) {
|
||||||
qmlWords << QLatin1String("property")
|
qmlWords << QLatin1String("property")
|
||||||
@@ -744,29 +740,14 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
|||||||
<< QLatin1String("signal")
|
<< QLatin1String("signal")
|
||||||
<< QLatin1String("import");
|
<< QLatin1String("import");
|
||||||
}
|
}
|
||||||
|
if (qmlWordsAlsoInJs.isEmpty()) {
|
||||||
foreach (const QString &word, qmlWords) {
|
qmlWordsAlsoInJs << QLatin1String("default")
|
||||||
TextEditor::CompletionItem item(this);
|
<< QLatin1String("function");
|
||||||
item.text = word;
|
|
||||||
item.icon = keywordIcon;
|
|
||||||
m_completions.append(item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!doJsKeywordCompletion) {
|
addCompletions(qmlWords, keywordIcon);
|
||||||
{
|
if (!doJsKeywordCompletion)
|
||||||
TextEditor::CompletionItem item(this);
|
addCompletions(qmlWordsAlsoInJs, keywordIcon);
|
||||||
item.text = QLatin1String("default");
|
|
||||||
item.icon = keywordIcon;
|
|
||||||
m_completions.append(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
TextEditor::CompletionItem item(this);
|
|
||||||
item.text = QLatin1String("function");
|
|
||||||
item.icon = keywordIcon;
|
|
||||||
m_completions.append(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -787,15 +768,7 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
|||||||
|
|
||||||
if (value && completionOperator == QLatin1Char('.')) { // member completion
|
if (value && completionOperator == QLatin1Char('.')) { // member completion
|
||||||
EnumerateProperties enumerateProperties(&context);
|
EnumerateProperties enumerateProperties(&context);
|
||||||
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties(value));
|
addCompletions(enumerateProperties(value), symbolIcon);
|
||||||
while (it.hasNext()) {
|
|
||||||
it.next();
|
|
||||||
|
|
||||||
TextEditor::CompletionItem item(this);
|
|
||||||
item.text = it.key();
|
|
||||||
item.icon = symbolIcon;
|
|
||||||
m_completions.append(item);
|
|
||||||
}
|
|
||||||
} else if (value && completionOperator == QLatin1Char('(') && m_startPosition == editor->position()) {
|
} else if (value && completionOperator == QLatin1Char('(') && m_startPosition == editor->position()) {
|
||||||
// function completion
|
// function completion
|
||||||
if (const Interpreter::FunctionValue *f = value->asFunctionValue()) {
|
if (const Interpreter::FunctionValue *f = value->asFunctionValue()) {
|
||||||
|
|||||||
@@ -39,6 +39,12 @@ namespace TextEditor {
|
|||||||
class ITextEditable;
|
class ITextEditable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace QmlJS {
|
||||||
|
namespace Interpreter {
|
||||||
|
class Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
namespace QmlJSEditor {
|
namespace QmlJSEditor {
|
||||||
|
|
||||||
class ModelManagerInterface;
|
class ModelManagerInterface;
|
||||||
@@ -74,6 +80,11 @@ private:
|
|||||||
bool maybeTriggersCompletion(TextEditor::ITextEditable *editor);
|
bool maybeTriggersCompletion(TextEditor::ITextEditable *editor);
|
||||||
bool isDelimiter(const QChar &ch) const;
|
bool isDelimiter(const QChar &ch) const;
|
||||||
|
|
||||||
|
void addCompletions(const QHash<QString, const QmlJS::Interpreter::Value *> &newCompletions,
|
||||||
|
const QIcon &icon);
|
||||||
|
void addCompletions(const QStringList &newCompletions,
|
||||||
|
const QIcon &icon);
|
||||||
|
|
||||||
ModelManagerInterface *m_modelManager;
|
ModelManagerInterface *m_modelManager;
|
||||||
TextEditor::ITextEditable *m_editor;
|
TextEditor::ITextEditable *m_editor;
|
||||||
int m_startPosition;
|
int m_startPosition;
|
||||||
|
|||||||
Reference in New Issue
Block a user