Some cleanup in the QML/JS code completion engine.

This commit is contained in:
Roberto Raggi
2010-01-21 11:21:55 +01:00
parent 9a01b634a3
commit e20d5a8289

View File

@@ -133,45 +133,48 @@ int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
QmlJS::Document::Ptr qmlDocument = edit->qmlDocument(); QmlJS::Document::Ptr qmlDocument = edit->qmlDocument();
// qDebug() << "*** document:" << qmlDocument; // qDebug() << "*** document:" << qmlDocument;
if (qmlDocument.isNull()) if (qmlDocument.isNull())
return pos; return -1;
if (!qmlDocument->qmlProgram()) if (!qmlDocument->qmlProgram())
qmlDocument = m_modelManager->snapshot().value(qmlDocument->fileName()); qmlDocument = m_modelManager->snapshot().value(qmlDocument->fileName());
const QIcon icon = iconForColor(Qt::darkGray);
const QIcon idIcon = iconForColor(Qt::darkGray);
QStringList ids = qmlDocument->ids().keys();
foreach (const QString &id, ids) {
if (id.isEmpty())
continue;
TextEditor::CompletionItem item(this);
item.text = id;
item.icon = idIcon;
m_completions.append(item);
}
const QIcon otherIcon = iconForColor(Qt::darkCyan);
// FIXME: this completion strategy is not going to work when the document was never parsed correctly. // FIXME: this completion strategy is not going to work when the document was never parsed correctly.
if (qmlDocument && qmlDocument->qmlProgram()) { if (qmlDocument->qmlProgram() != 0) {
QmlJS::AST::UiProgram *program = qmlDocument->qmlProgram(); // qDebug() << "*** program:" << program;
// qDebug() << "*** program:" << program; QmlExpressionUnderCursor expressionUnderCursor;
QTextCursor cursor(edit->document());
if (program) { cursor.setPosition(pos);
QmlExpressionUnderCursor expressionUnderCursor; expressionUnderCursor(cursor, qmlDocument);
QTextCursor cursor(edit->document());
cursor.setPosition(pos);
expressionUnderCursor(cursor, qmlDocument);
QmlLookupContext context(expressionUnderCursor.expressionScopes(), qmlDocument, m_modelManager->snapshot(), m_typeSystem); QmlLookupContext context(expressionUnderCursor.expressionScopes(), qmlDocument, m_modelManager->snapshot(), m_typeSystem);
QmlResolveExpression resolver(context); QmlResolveExpression resolver(context);
// qDebug()<<"*** expression under cursor:"<<expressionUnderCursor.expressionNode(); // qDebug()<<"*** expression under cursor:"<<expressionUnderCursor.expressionNode();
QList<QmlJS::Symbol*> symbols = resolver.visibleSymbols(expressionUnderCursor.expressionNode()); const QList<QmlJS::Symbol*> symbols = resolver.visibleSymbols(expressionUnderCursor.expressionNode());
// qDebug()<<"***"<<symbols.size()<<"visible symbols"; // qDebug()<<"***"<<symbols.size()<<"visible symbols";
foreach (QmlJS::Symbol *symbol, symbols) { foreach (QmlJS::Symbol *symbol, symbols) {
QString word; if (symbol->isIdSymbol())
continue; // nothing to do here.
if (symbol->isIdSymbol()) { const QString word = symbol->name();
word = symbol->asIdSymbol()->id(); if (! word.isEmpty()) {
} else {
word = symbol->name();
}
if (word.isEmpty())
continue;
TextEditor::CompletionItem item(this); TextEditor::CompletionItem item(this);
item.text = word; item.text = word;
item.icon = icon; item.icon = otherIcon;
m_completions.append(item); m_completions.append(item);
} }
} }