Commit eb53e71d authored by Christian Kamm's avatar Christian Kamm
Browse files

QmlJSEditor: Simplify Qml completion code.

parent fc362809
......@@ -595,6 +595,31 @@ static bool isLiteral(AST::Node *ast)
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)
{
m_editor = editor;
......@@ -660,25 +685,8 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
enumerateProperties.setGlobalCompletion(true);
enumerateProperties.setEnumerateGeneratedSlots(true);
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties(qmlScopeType));
while (it.hasNext()) {
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);
}
addCompletions(enumerateProperties(qmlScopeType), symbolIcon);
addCompletions(enumerateProperties(context.scopeChain().qmlTypes), symbolIcon);
}
if (contextFinder.isInRhsOfBinding() && qmlScopeType) {
......@@ -713,30 +721,18 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
// It's a global completion.
EnumerateProperties enumerateProperties(&context);
enumerateProperties.setGlobalCompletion(true);
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties());
while (it.hasNext()) {
it.next();
TextEditor::CompletionItem item(this);
item.text = it.key();
item.icon = symbolIcon;
m_completions.append(item);
}
addCompletions(enumerateProperties(), symbolIcon);
}
if (doJsKeywordCompletion) {
// add js keywords
foreach (const QString &word, Scanner::keywords()) {
TextEditor::CompletionItem item(this);
item.text = word;
item.icon = keywordIcon;
m_completions.append(item);
}
addCompletions(Scanner::keywords(), keywordIcon);
}
// add qml extra words
if (doQmlKeywordCompletion && isQmlFile) {
static QStringList qmlWords;
static QStringList qmlWordsAlsoInJs;
if (qmlWords.isEmpty()) {
qmlWords << QLatin1String("property")
......@@ -744,29 +740,14 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
<< QLatin1String("signal")
<< QLatin1String("import");
}
foreach (const QString &word, qmlWords) {
TextEditor::CompletionItem item(this);
item.text = word;
item.icon = keywordIcon;
m_completions.append(item);
if (qmlWordsAlsoInJs.isEmpty()) {
qmlWordsAlsoInJs << QLatin1String("default")
<< QLatin1String("function");
}
if (!doJsKeywordCompletion) {
{
TextEditor::CompletionItem item(this);
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);
}
}
addCompletions(qmlWords, keywordIcon);
if (!doJsKeywordCompletion)
addCompletions(qmlWordsAlsoInJs, keywordIcon);
}
}
......@@ -787,15 +768,7 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
if (value && completionOperator == QLatin1Char('.')) { // member completion
EnumerateProperties enumerateProperties(&context);
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties(value));
while (it.hasNext()) {
it.next();
TextEditor::CompletionItem item(this);
item.text = it.key();
item.icon = symbolIcon;
m_completions.append(item);
}
addCompletions(enumerateProperties(value), symbolIcon);
} else if (value && completionOperator == QLatin1Char('(') && m_startPosition == editor->position()) {
// function completion
if (const Interpreter::FunctionValue *f = value->asFunctionValue()) {
......
......@@ -39,6 +39,12 @@ namespace TextEditor {
class ITextEditable;
}
namespace QmlJS {
namespace Interpreter {
class Value;
}
}
namespace QmlJSEditor {
class ModelManagerInterface;
......@@ -74,6 +80,11 @@ private:
bool maybeTriggersCompletion(TextEditor::ITextEditable *editor);
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;
TextEditor::ITextEditable *m_editor;
int m_startPosition;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment