summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@digia.com>2013-11-19 11:32:01 +0100
committerChristian Kandeler <christian.kandeler@digia.com>2013-11-21 15:20:22 +0100
commit823b16071b85b7e2f8f340f64fbf4ddabeb7db41 (patch)
tree906e13f28db41a0f717349baeaf9274c2185435c
parenta1b43acbad72e5ea3bd468e572158bc87342b1de (diff)
downloadqbs-823b16071b85b7e2f8f340f64fbf4ddabeb7db41.tar.gz
API: Allow adding to a non-literal files list.
Change-Id: I03153c3ace25c28fbd8490a4e054772b3112a867 Reviewed-by: Joerg Bornemann <joerg.bornemann@digia.com>
-rw-r--r--src/lib/api/projectfileupdater.cpp35
-rw-r--r--tests/auto/api/tst_api.cpp5
2 files changed, 29 insertions, 11 deletions
diff --git a/src/lib/api/projectfileupdater.cpp b/src/lib/api/projectfileupdater.cpp
index d4c60abad..4849ef6c4 100644
--- a/src/lib/api/projectfileupdater.cpp
+++ b/src/lib/api/projectfileupdater.cpp
@@ -198,6 +198,13 @@ ProjectFileFilesAdder::ProjectFileFilesAdder(const ProductData &product, const G
{
}
+static QString getNodeRepresentation(const QString &fileContent, const Node *node)
+{
+ const quint32 start = node->firstSourceLocation().offset;
+ const quint32 end = node->lastSourceLocation().end();
+ return fileContent.mid(start, end - start);
+}
+
void ProjectFileFilesAdder::doApply(QString &fileContent, UiProgram *ast)
{
// Find the item containing the "files" binding.
@@ -242,11 +249,8 @@ void ProjectFileFilesAdder::doApply(QString &fileContent, UiProgram *ast)
const ElementList *elem
= static_cast<ArrayLiteral *>(exprStatement->expression)->elements;
while (elem) {
- const quint32 start = elem->expression->firstSourceLocation().offset;
- const quint32 end = elem->expression->lastSourceLocation().end();
- const QString elemRepr = fileContent.mid(start, end - start);
filesString += QString(arrayElemIndentation, QLatin1Char(' '));
- filesString += elemRepr;
+ filesString += getNodeRepresentation(fileContent, elem->expression);
filesString += QLatin1String(",\n");
elem = elem->next;
}
@@ -273,10 +277,25 @@ void ProjectFileFilesAdder::doApply(QString &fileContent, UiProgram *ast)
filesString, Rewriter::ScriptBinding);
break;
}
- default:
- qDebug("unsupported kind: %d", exprStatement->expression->kind);
- // TODO: If it's an identifier, call "concat()".
- throw ErrorInfo(Tr::tr("Javascript construct in source file is too complex."));
+ default: {
+ // Note that we can often do better than simply concatenating: For instance,
+ // in the case where the existing list is of the form ["a", "b"].concat(myProperty),
+ // we could keep on parsing until we find the array literal and then merge it with
+ // the new files, preventing cascading concat() calls.
+ // But this is not essential and can be implemented when we have some downtime.
+ const QString rhsRepr = getNodeRepresentation(fileContent, exprStatement->expression);
+ QString filesString = QLatin1String("[\n");
+ filesString += newFilesString;
+ filesString += QLatin1Char('\n');
+ filesString += QString(bindingIndentation, QLatin1Char(' '));
+
+ // It cannot be the other way around, since the existing right-hand side could
+ // have string type.
+ filesString += QString::fromLatin1("].concat(%1)").arg(rhsRepr);
+
+ rewriter.changeBinding(itemFinder.item()->initializer, QLatin1String("files"),
+ filesString, Rewriter::ScriptBinding);
+ }
}
} else { // Can happen for the product itself, for which the "files" binding is not mandatory.
newFilesString.prepend(QLatin1String("[\n"));
diff --git a/tests/auto/api/tst_api.cpp b/tests/auto/api/tst_api.cpp
index 1e1e883d3..c33f88010 100644
--- a/tests/auto/api/tst_api.cpp
+++ b/tests/auto/api/tst_api.cpp
@@ -196,15 +196,14 @@ void TestApi::changeContent()
errorInfo = project.addFiles(product, qbs::GroupData(), QStringList() << "newfile2.txt");
QVERIFY2(!errorInfo.hasError(), qPrintable(errorInfo.toString()));
- // Add files to list represented as an identifier (not yet implmented).
+ // Add files to list represented as an identifier.
projectData = project.projectData();
QVERIFY(projectData.products().count() == 1);
product = projectData.products().first();
group = findGroup(product, "Existing Group 2");
QVERIFY(group.isValid());
errorInfo = project.addFiles(product, group, QStringList() << "newfile3.txt");
- QVERIFY(errorInfo.hasError());
- QVERIFY2(errorInfo.toString().contains("complex"), qPrintable(errorInfo.toString()));
+ QVERIFY2(!errorInfo.hasError(), qPrintable(errorInfo.toString()));
// Add files to list represented as a block of code (not yet implemented).
projectData = project.projectData();