Commit a071871c authored by Tim Jenssen's avatar Tim Jenssen

QmlDesigner: refactoring load method

- we will use this method later for using internal
  text editor error checking
- no behavior change, mostly moved code into an internal method
- used m_document where it is possible

Change-Id: I689a05a543dd93766c6be14d28a44140be53e559
Reviewed-by: Thomas Hartmann's avatarThomas Hartmann <thomas.hartmann@qt.io>
parent 2908d42b
......@@ -886,6 +886,29 @@ void TextToModelMerger::setupUsedImports()
m_rewriterView->model()->setUsedImports(usedImports);
}
Document::MutablePtr TextToModelMerger::createParsedDocument(const QUrl &url, const QString &data, QList<RewriterError> *errors)
{
const QString fileName = url.toLocalFile();
Dialect dialect = ModelManagerInterface::guessLanguageOfFile(fileName);
if (dialect == Dialect::AnyLanguage
|| dialect == Dialect::NoLanguage)
dialect = Dialect::Qml;
Document::MutablePtr doc = Document::create(fileName.isEmpty() ? QStringLiteral("<internal>") : fileName, dialect);
doc->setSource(data);
doc->parseQml();
if (!doc->isParsedCorrectly()) {
if (errors) {
foreach (const QmlJS::DiagnosticMessage &message, doc->diagnosticMessages())
errors->append(RewriterError(message, QUrl::fromLocalFile(doc->fileName())));
}
return Document::MutablePtr();
}
return doc;
}
bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceHandler)
{
qCInfo(rewriterBenchmark) << Q_FUNC_INFO;
......@@ -906,49 +929,33 @@ bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceH
try {
Snapshot snapshot = m_rewriterView->textModifier()->qmljsSnapshot();
const QString fileName = url.toLocalFile();
Dialect dialect = ModelManagerInterface::guessLanguageOfFile(fileName);
if (dialect == Dialect::AnyLanguage
|| dialect == Dialect::NoLanguage)
dialect = Dialect::Qml;
Document::MutablePtr doc = Document::create(fileName.isEmpty() ? QStringLiteral("<internal>") : fileName, dialect);
doc->setSource(data);
doc->parseQml();
qCInfo(rewriterBenchmark) << "parsed correctly: " << doc->isParsedCorrectly() << time.elapsed();
if (!doc->isParsedCorrectly()) {
QList<RewriterError> errors;
foreach (const QmlJS::DiagnosticMessage &message, doc->diagnosticMessages())
errors.append(RewriterError(message, QUrl::fromLocalFile(doc->fileName())));
QList<RewriterError> errors;
QList<RewriterError> warnings;
if (Document::MutablePtr doc = createParsedDocument(url, data, &errors)) {
snapshot.insert(doc);
m_document = doc;
qCInfo(rewriterBenchmark) << "parsed correctly: " << time.elapsed();
} else {
qCInfo(rewriterBenchmark) << "did not parse correctly: " << time.elapsed();
m_rewriterView->setErrors(errors);
setActive(false);
return false;
}
snapshot.insert(doc);
m_vContext = ModelManagerInterface::instance()->defaultVContext(Dialect::Qml, doc, true);
ReadingContext ctxt(snapshot, doc, m_vContext);
m_vContext = ModelManagerInterface::instance()->defaultVContext(Dialect::Qml, m_document, true);
ReadingContext ctxt(snapshot, m_document, m_vContext);
m_scopeChain = QSharedPointer<const ScopeChain>(
new ScopeChain(ctxt.scopeChain()));
m_document = doc;
qCInfo(rewriterBenchmark) << "linked:" << time.elapsed();
QList<RewriterError> errors;
QList<RewriterError> warnings;
collectLinkErrors(&errors, ctxt);
setupImports(doc, differenceHandler);
setupImports(m_document, differenceHandler);
setupPossibleImports(snapshot, m_vContext);
collectImportErrors(&errors);
if (view()->checkSemanticErrors()) {
collectSemanticErrorsAndWarnings(&errors, &warnings);
if (!errors.isEmpty()) {
......@@ -962,7 +969,7 @@ bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceH
setupUsedImports();
AST::UiObjectMember *astRootNode = 0;
if (AST::UiProgram *program = doc->qmlProgram())
if (AST::UiProgram *program = m_document->qmlProgram())
if (program->members)
astRootNode = program->members->member;
ModelNode modelRootNode = m_rewriterView->rootModelNode();
......
......@@ -30,6 +30,7 @@
#include "nodelistproperty.h"
#include "modelnode.h"
#include <qmljs/qmljsscopechain.h>
#include <qmljs/qmljsscopechain.h>
#include <QStringList>
......@@ -51,6 +52,8 @@ class TextToModelMerger
TextToModelMerger &operator=(const TextToModelMerger&);
public:
static QmlJS::Document::MutablePtr createParsedDocument(const QUrl &url, const QString &data, QList<RewriterError> *errors);
TextToModelMerger(RewriterView *reWriterView);
bool isActive() const;
......
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