diff --git a/src/plugins/qmljseditor/qmljscodecompletion.cpp b/src/plugins/qmljseditor/qmljscodecompletion.cpp index 1096b4ff80d4f3f7a42132474bf84c55df5046aa..42182e2f5005728dd41580e0a6ac25fc3e70f81f 100644 --- a/src/plugins/qmljseditor/qmljscodecompletion.cpp +++ b/src/plugins/qmljseditor/qmljscodecompletion.cpp @@ -620,6 +620,12 @@ bool CodeCompletion::completeUrl(const QString &relativeBasePath, const QString if (fileName.isEmpty()) return false; + return completeFileName(relativeBasePath, fileName); +} + +bool CodeCompletion::completeFileName(const QString &relativeBasePath, const QString &fileName, + const QStringList &patterns) +{ const QFileInfo fileInfo(fileName); QString directoryPrefix; if (fileInfo.isRelative()) { @@ -632,12 +638,10 @@ bool CodeCompletion::completeUrl(const QString &relativeBasePath, const QString if (!QFileInfo(directoryPrefix).exists()) return false; - QDirIterator dirIterator(directoryPrefix); + QDirIterator dirIterator(directoryPrefix, patterns, QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot); while (dirIterator.hasNext()) { dirIterator.next(); const QString fileName = dirIterator.fileName(); - if (fileName == QLatin1String(".") || fileName == QLatin1String("..")) - continue; TextEditor::CompletionItem item(this); item.text += fileName; @@ -779,19 +783,29 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor) qmlScopeType = context->lookupType(document.data(), contextFinder.qmlObjectTypeName()); if (contextFinder.isInStringLiteral()) { - const Interpreter::Value *value = getPropertyValue(qmlScopeType, contextFinder.bindingPropertyName(), context); + // get the text of the literal up to the cursor position + QTextCursor tc = edit->textCursor(); + QmlExpressionUnderCursor expressionUnderCursor; + expressionUnderCursor(tc); + QString literalText = expressionUnderCursor.text(); + QTC_ASSERT(!literalText.isEmpty() && ( + literalText.at(0) == QLatin1Char('"') + || literalText.at(0) == QLatin1Char('\'')), return -1); + literalText = literalText.mid(1); + + if (contextFinder.isInImport()) { + QStringList patterns; + patterns << QLatin1String("*.qml") << QLatin1String("*.js"); + if (completeFileName(document->path(), literalText, patterns)) + return m_startPosition; + return -1; + } + const Interpreter::Value *value = getPropertyValue(qmlScopeType, contextFinder.bindingPropertyName(), context); if (!value) { // do nothing } else if (value->asUrlValue()) { - QTextCursor tc = edit->textCursor(); - QmlExpressionUnderCursor expressionUnderCursor; - expressionUnderCursor(tc); - QString text = expressionUnderCursor.text(); - QTC_ASSERT(!text.isEmpty() && text.at(0) == QLatin1Char('"'), return -1); - text = text.mid(1); - - if (completeUrl(document->path(), text)) + if (completeUrl(document->path(), literalText)) return m_startPosition; } diff --git a/src/plugins/qmljseditor/qmljscodecompletion.h b/src/plugins/qmljseditor/qmljscodecompletion.h index ea9084120a2f1319122ed81c3c633311558aa13d..6aced15ca65322480cfb07ced8a027cba0b88c42 100644 --- a/src/plugins/qmljseditor/qmljscodecompletion.h +++ b/src/plugins/qmljseditor/qmljscodecompletion.h @@ -81,7 +81,9 @@ private: bool maybeTriggersCompletion(TextEditor::ITextEditable *editor); bool isDelimiter(QChar ch) const; - bool completeUrl(const QString &relativeBasePath, const QString &name); + bool completeUrl(const QString &relativeBasePath, const QString &urlString); + bool completeFileName(const QString &relativeBasePath, const QString &fileName, + const QStringList &patterns = QStringList()); void addCompletions(const QHash<QString, const QmlJS::Interpreter::Value *> &newCompletions, const QIcon &icon, int relevance);