forked from qt-creator/qt-creator
Changed the QML/JS editor to treat .qs/.js files as JavaScript, and invoke the appropriate parser for it.
Reviewed-by: Christian Kamm
This commit is contained in:
committed by
Tim Jenssen
parent
2dfe4f620c
commit
0f8126f62d
@@ -1,5 +1,5 @@
|
||||
INCLUDEPATH += $$PWD/../../shared
|
||||
INCLUDEPATH += $$PWD/../../shared/qmljs $$PWD/../../shared/qmljs/parser
|
||||
|
||||
DEPENDPATH += $$PWD/../../shared/qmljs
|
||||
DEPENDPATH += $$PWD/../../shared/qmljs $$PWD/../../shared/qmljs/parser
|
||||
LIBS *= -l$$qtLibraryTarget(QmlJS)
|
||||
|
||||
@@ -95,12 +95,12 @@ int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
||||
if (qmlDocument.isNull())
|
||||
return pos;
|
||||
|
||||
if (!qmlDocument->program())
|
||||
if (!qmlDocument->qmlProgram())
|
||||
qmlDocument = m_modelManager->snapshot().value(qmlDocument->fileName());
|
||||
|
||||
// FIXME: this completion strategy is not going to work when the document was never parsed correctly.
|
||||
if (qmlDocument && qmlDocument->program()) {
|
||||
QmlJS::AST::UiProgram *program = qmlDocument->program();
|
||||
if (qmlDocument && qmlDocument->qmlProgram()) {
|
||||
QmlJS::AST::UiProgram *program = qmlDocument->qmlProgram();
|
||||
// qDebug() << "*** program:" << program;
|
||||
|
||||
if (program) {
|
||||
|
||||
@@ -130,7 +130,7 @@ namespace QmlJSEditor {
|
||||
_pos = pos;
|
||||
_scopes.clear();
|
||||
_currentSymbol = 0;
|
||||
Node::accept(doc->program(), this);
|
||||
Node::accept(doc->qmlProgram(), this);
|
||||
return _scopes;
|
||||
}
|
||||
|
||||
|
||||
@@ -425,11 +425,11 @@ void QmlJSTextEditor::onDocumentUpdated(Qml::QmlDocument::Ptr doc)
|
||||
|
||||
FindIdDeclarations updateIds;
|
||||
m_idsRevision = document()->revision();
|
||||
m_ids = updateIds(doc->program());
|
||||
m_ids = updateIds(doc->qmlProgram());
|
||||
|
||||
if (doc->isParsedCorrectly()) {
|
||||
FindDeclarations findDeclarations;
|
||||
m_declarations = findDeclarations(doc->program());
|
||||
m_declarations = findDeclarations(doc->qmlProgram());
|
||||
|
||||
QStringList items;
|
||||
items.append(tr("<Select Symbol>"));
|
||||
|
||||
@@ -137,7 +137,7 @@ QmlSymbol *QmlLookupContext::resolveType(const QString &name, const QString &fil
|
||||
if (document.isNull())
|
||||
return 0;
|
||||
|
||||
UiProgram *prog = document->program();
|
||||
UiProgram *prog = document->qmlProgram();
|
||||
if (!prog)
|
||||
return 0;
|
||||
|
||||
@@ -252,7 +252,7 @@ QList<QmlSymbol*> QmlLookupContext::visibleTypes()
|
||||
{
|
||||
QList<QmlSymbol*> result;
|
||||
|
||||
UiProgram *program = _doc->program();
|
||||
UiProgram *program = _doc->qmlProgram();
|
||||
if (!program)
|
||||
return result;
|
||||
|
||||
|
||||
@@ -27,21 +27,20 @@
|
||||
**
|
||||
**************************************************************************/
|
||||
|
||||
#include <QFile>
|
||||
#include <QtConcurrentRun>
|
||||
#include <qtconcurrent/runextensions.h>
|
||||
#include <QTextStream>
|
||||
#include "qmljseditorconstants.h"
|
||||
#include "qmlmodelmanager.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <coreplugin/progressmanager/progressmanager.h>
|
||||
|
||||
#include <coreplugin/mimedatabase.h>
|
||||
#include <texteditor/itexteditor.h>
|
||||
|
||||
#include "qmljseditorconstants.h"
|
||||
#include "qmlmodelmanager.h"
|
||||
|
||||
#include <QtCore/QMetaType>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QtConcurrentRun>
|
||||
#include <qtconcurrent/runextensions.h>
|
||||
#include <QTextStream>
|
||||
|
||||
using namespace QmlJSEditor;
|
||||
using namespace QmlJSEditor::Internal;
|
||||
@@ -156,7 +155,21 @@ void QmlModelManager::parse(QFutureInterface<void> &future,
|
||||
|
||||
Qml::QmlDocument::Ptr doc = Qml::QmlDocument::create(fileName);
|
||||
doc->setSource(contents);
|
||||
doc->parse();
|
||||
|
||||
{
|
||||
Core::MimeDatabase *db = Core::ICore::instance()->mimeDatabase();
|
||||
Core::MimeType jsSourceTy = db->findByType(QLatin1String("application/javascript"));
|
||||
Core::MimeType qmlSourceTy = db->findByType(QLatin1String("application/x-qml"));
|
||||
|
||||
const QFileInfo fileInfo(fileName);
|
||||
|
||||
if (jsSourceTy.matchesFile(fileInfo))
|
||||
doc->parseJavaScript();
|
||||
else if (qmlSourceTy.matchesFile(fileInfo))
|
||||
doc->parseQml();
|
||||
else
|
||||
qWarning() << "Don't know how to treat" << fileName;
|
||||
}
|
||||
|
||||
modelManager->emitDocumentUpdated(doc);
|
||||
}
|
||||
|
||||
@@ -35,13 +35,17 @@
|
||||
#include <qmljs/parser/qmljsnodepool_p.h>
|
||||
#include <qmljs/parser/qmljsastfwd_p.h>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
using namespace Qml;
|
||||
using namespace QmlJS;
|
||||
using namespace QmlJS::AST;
|
||||
|
||||
QmlDocument::QmlDocument(const QString &fileName)
|
||||
: _engine(0)
|
||||
, _pool(0)
|
||||
, _program(0)
|
||||
, _uiProgram(0)
|
||||
, _jsProgram(0)
|
||||
, _fileName(fileName)
|
||||
, _parsedCorrectly(false)
|
||||
{
|
||||
@@ -70,9 +74,14 @@ QmlDocument::Ptr QmlDocument::create(const QString &fileName)
|
||||
return doc;
|
||||
}
|
||||
|
||||
AST::UiProgram *QmlDocument::program() const
|
||||
AST::UiProgram *QmlDocument::qmlProgram() const
|
||||
{
|
||||
return _program;
|
||||
return _uiProgram;
|
||||
}
|
||||
|
||||
AST::Program *QmlDocument::jsProgram() const
|
||||
{
|
||||
return _jsProgram;
|
||||
}
|
||||
|
||||
QList<DiagnosticMessage> QmlDocument::diagnosticMessages() const
|
||||
@@ -90,11 +99,14 @@ void QmlDocument::setSource(const QString &source)
|
||||
_source = source;
|
||||
}
|
||||
|
||||
bool QmlDocument::parse()
|
||||
bool QmlDocument::parseQml()
|
||||
{
|
||||
Q_ASSERT(! _engine);
|
||||
Q_ASSERT(! _pool);
|
||||
Q_ASSERT(! _program);
|
||||
Q_ASSERT(! _uiProgram);
|
||||
Q_ASSERT(! _jsProgram);
|
||||
|
||||
qDebug() << "QmlDocument::parseQml() for file" << _fileName;
|
||||
|
||||
_engine = new Engine();
|
||||
_pool = new NodePool(_fileName, _engine);
|
||||
@@ -106,11 +118,11 @@ bool QmlDocument::parse()
|
||||
lexer.setCode(_source, /*line = */ 1);
|
||||
|
||||
_parsedCorrectly = parser.parse();
|
||||
_program = parser.ast();
|
||||
_uiProgram = parser.ast();
|
||||
_diagnosticMessages = parser.diagnosticMessages();
|
||||
|
||||
if (_program) {
|
||||
for (QmlJS::AST::UiObjectMemberList *iter = _program->members; iter; iter = iter->next)
|
||||
if (_uiProgram) {
|
||||
for (QmlJS::AST::UiObjectMemberList *iter = _uiProgram->members; iter; iter = iter->next)
|
||||
if (iter->member)
|
||||
_symbols.append(new QmlSymbolFromFile(_fileName, iter->member));
|
||||
|
||||
@@ -123,6 +135,29 @@ bool QmlDocument::parse()
|
||||
return _parsedCorrectly;
|
||||
}
|
||||
|
||||
bool QmlDocument::parseJavaScript()
|
||||
{
|
||||
Q_ASSERT(! _engine);
|
||||
Q_ASSERT(! _pool);
|
||||
Q_ASSERT(! _uiProgram);
|
||||
Q_ASSERT(! _jsProgram);
|
||||
|
||||
_engine = new Engine();
|
||||
_pool = new NodePool(_fileName, _engine);
|
||||
_ids.clear();
|
||||
|
||||
Lexer lexer(_engine);
|
||||
Parser parser(_engine);
|
||||
|
||||
lexer.setCode(_source, /*line = */ 1);
|
||||
|
||||
_parsedCorrectly = parser.parseProgram();
|
||||
_jsProgram = cast<Program*>(parser.rootNode());
|
||||
_diagnosticMessages = parser.diagnosticMessages();
|
||||
|
||||
return _parsedCorrectly;
|
||||
}
|
||||
|
||||
QmlSymbolFromFile *QmlDocument::findSymbol(QmlJS::AST::Node *node) const
|
||||
{
|
||||
foreach (QmlSymbol *symbol, _symbols)
|
||||
@@ -143,10 +178,8 @@ Snapshot::~Snapshot()
|
||||
|
||||
void Snapshot::insert(const QmlDocument::Ptr &document)
|
||||
{
|
||||
if (!document || !document->program())
|
||||
return;
|
||||
|
||||
QMap<QString, QmlDocument::Ptr>::insert(document->fileName(), document);
|
||||
if (document && (document->qmlProgram() || document->jsProgram()))
|
||||
QMap<QString, QmlDocument::Ptr>::insert(document->fileName(), document);
|
||||
}
|
||||
|
||||
QmlDocument::PtrList Snapshot::importedDocuments(const QmlDocument::Ptr &doc, const QString &importPath) const
|
||||
|
||||
@@ -56,13 +56,15 @@ public:
|
||||
|
||||
static QmlDocument::Ptr create(const QString &fileName);
|
||||
|
||||
QmlJS::AST::UiProgram *program() const;
|
||||
QmlJS::AST::UiProgram *qmlProgram() const;
|
||||
QmlJS::AST::Program *jsProgram() const;
|
||||
QList<QmlJS::DiagnosticMessage> diagnosticMessages() const;
|
||||
|
||||
QString source() const;
|
||||
void setSource(const QString &source);
|
||||
|
||||
bool parse();
|
||||
bool parseQml();
|
||||
bool parseJavaScript();
|
||||
|
||||
bool isParsedCorrectly() const
|
||||
{ return _parsedCorrectly; }
|
||||
@@ -80,7 +82,8 @@ public:
|
||||
private:
|
||||
QmlJS::Engine *_engine;
|
||||
QmlJS::NodePool *_pool;
|
||||
QmlJS::AST::UiProgram *_program;
|
||||
QmlJS::AST::UiProgram *_uiProgram;
|
||||
QmlJS::AST::Program *_jsProgram;
|
||||
QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
|
||||
QString _fileName;
|
||||
QString _path;
|
||||
|
||||
@@ -44,7 +44,7 @@ QMap<QString, QmlIdSymbol*> QmlIdCollector::operator()(Qml::QmlDocument &doc)
|
||||
_ids.clear();
|
||||
_currentSymbol = 0;
|
||||
|
||||
Node::accept(doc.program(), this);
|
||||
Node::accept(doc.qmlProgram(), this);
|
||||
|
||||
return _ids;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user