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
|
||||||
INCLUDEPATH += $$PWD/../../shared/qmljs $$PWD/../../shared/qmljs/parser
|
INCLUDEPATH += $$PWD/../../shared/qmljs $$PWD/../../shared/qmljs/parser
|
||||||
|
|
||||||
DEPENDPATH += $$PWD/../../shared/qmljs
|
DEPENDPATH += $$PWD/../../shared/qmljs $$PWD/../../shared/qmljs/parser
|
||||||
LIBS *= -l$$qtLibraryTarget(QmlJS)
|
LIBS *= -l$$qtLibraryTarget(QmlJS)
|
||||||
|
|||||||
@@ -95,12 +95,12 @@ int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
|||||||
if (qmlDocument.isNull())
|
if (qmlDocument.isNull())
|
||||||
return pos;
|
return pos;
|
||||||
|
|
||||||
if (!qmlDocument->program())
|
if (!qmlDocument->qmlProgram())
|
||||||
qmlDocument = m_modelManager->snapshot().value(qmlDocument->fileName());
|
qmlDocument = m_modelManager->snapshot().value(qmlDocument->fileName());
|
||||||
|
|
||||||
// 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->program()) {
|
if (qmlDocument && qmlDocument->qmlProgram()) {
|
||||||
QmlJS::AST::UiProgram *program = qmlDocument->program();
|
QmlJS::AST::UiProgram *program = qmlDocument->qmlProgram();
|
||||||
// qDebug() << "*** program:" << program;
|
// qDebug() << "*** program:" << program;
|
||||||
|
|
||||||
if (program) {
|
if (program) {
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ namespace QmlJSEditor {
|
|||||||
_pos = pos;
|
_pos = pos;
|
||||||
_scopes.clear();
|
_scopes.clear();
|
||||||
_currentSymbol = 0;
|
_currentSymbol = 0;
|
||||||
Node::accept(doc->program(), this);
|
Node::accept(doc->qmlProgram(), this);
|
||||||
return _scopes;
|
return _scopes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -425,11 +425,11 @@ void QmlJSTextEditor::onDocumentUpdated(Qml::QmlDocument::Ptr doc)
|
|||||||
|
|
||||||
FindIdDeclarations updateIds;
|
FindIdDeclarations updateIds;
|
||||||
m_idsRevision = document()->revision();
|
m_idsRevision = document()->revision();
|
||||||
m_ids = updateIds(doc->program());
|
m_ids = updateIds(doc->qmlProgram());
|
||||||
|
|
||||||
if (doc->isParsedCorrectly()) {
|
if (doc->isParsedCorrectly()) {
|
||||||
FindDeclarations findDeclarations;
|
FindDeclarations findDeclarations;
|
||||||
m_declarations = findDeclarations(doc->program());
|
m_declarations = findDeclarations(doc->qmlProgram());
|
||||||
|
|
||||||
QStringList items;
|
QStringList items;
|
||||||
items.append(tr("<Select Symbol>"));
|
items.append(tr("<Select Symbol>"));
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ QmlSymbol *QmlLookupContext::resolveType(const QString &name, const QString &fil
|
|||||||
if (document.isNull())
|
if (document.isNull())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
UiProgram *prog = document->program();
|
UiProgram *prog = document->qmlProgram();
|
||||||
if (!prog)
|
if (!prog)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@@ -252,7 +252,7 @@ QList<QmlSymbol*> QmlLookupContext::visibleTypes()
|
|||||||
{
|
{
|
||||||
QList<QmlSymbol*> result;
|
QList<QmlSymbol*> result;
|
||||||
|
|
||||||
UiProgram *program = _doc->program();
|
UiProgram *program = _doc->qmlProgram();
|
||||||
if (!program)
|
if (!program)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
|
|||||||
@@ -27,21 +27,20 @@
|
|||||||
**
|
**
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
#include <QFile>
|
#include "qmljseditorconstants.h"
|
||||||
#include <QtConcurrentRun>
|
#include "qmlmodelmanager.h"
|
||||||
#include <qtconcurrent/runextensions.h>
|
|
||||||
#include <QTextStream>
|
|
||||||
|
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
#include <coreplugin/editormanager/editormanager.h>
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
#include <coreplugin/progressmanager/progressmanager.h>
|
#include <coreplugin/progressmanager/progressmanager.h>
|
||||||
|
#include <coreplugin/mimedatabase.h>
|
||||||
#include <texteditor/itexteditor.h>
|
#include <texteditor/itexteditor.h>
|
||||||
|
|
||||||
#include "qmljseditorconstants.h"
|
#include <QFile>
|
||||||
#include "qmlmodelmanager.h"
|
#include <QFileInfo>
|
||||||
|
#include <QtConcurrentRun>
|
||||||
#include <QtCore/QMetaType>
|
#include <qtconcurrent/runextensions.h>
|
||||||
|
#include <QTextStream>
|
||||||
|
|
||||||
using namespace QmlJSEditor;
|
using namespace QmlJSEditor;
|
||||||
using namespace QmlJSEditor::Internal;
|
using namespace QmlJSEditor::Internal;
|
||||||
@@ -156,7 +155,21 @@ void QmlModelManager::parse(QFutureInterface<void> &future,
|
|||||||
|
|
||||||
Qml::QmlDocument::Ptr doc = Qml::QmlDocument::create(fileName);
|
Qml::QmlDocument::Ptr doc = Qml::QmlDocument::create(fileName);
|
||||||
doc->setSource(contents);
|
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);
|
modelManager->emitDocumentUpdated(doc);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,13 +35,17 @@
|
|||||||
#include <qmljs/parser/qmljsnodepool_p.h>
|
#include <qmljs/parser/qmljsnodepool_p.h>
|
||||||
#include <qmljs/parser/qmljsastfwd_p.h>
|
#include <qmljs/parser/qmljsastfwd_p.h>
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
using namespace Qml;
|
using namespace Qml;
|
||||||
using namespace QmlJS;
|
using namespace QmlJS;
|
||||||
|
using namespace QmlJS::AST;
|
||||||
|
|
||||||
QmlDocument::QmlDocument(const QString &fileName)
|
QmlDocument::QmlDocument(const QString &fileName)
|
||||||
: _engine(0)
|
: _engine(0)
|
||||||
, _pool(0)
|
, _pool(0)
|
||||||
, _program(0)
|
, _uiProgram(0)
|
||||||
|
, _jsProgram(0)
|
||||||
, _fileName(fileName)
|
, _fileName(fileName)
|
||||||
, _parsedCorrectly(false)
|
, _parsedCorrectly(false)
|
||||||
{
|
{
|
||||||
@@ -70,9 +74,14 @@ QmlDocument::Ptr QmlDocument::create(const QString &fileName)
|
|||||||
return doc;
|
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
|
QList<DiagnosticMessage> QmlDocument::diagnosticMessages() const
|
||||||
@@ -90,11 +99,14 @@ void QmlDocument::setSource(const QString &source)
|
|||||||
_source = source;
|
_source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QmlDocument::parse()
|
bool QmlDocument::parseQml()
|
||||||
{
|
{
|
||||||
Q_ASSERT(! _engine);
|
Q_ASSERT(! _engine);
|
||||||
Q_ASSERT(! _pool);
|
Q_ASSERT(! _pool);
|
||||||
Q_ASSERT(! _program);
|
Q_ASSERT(! _uiProgram);
|
||||||
|
Q_ASSERT(! _jsProgram);
|
||||||
|
|
||||||
|
qDebug() << "QmlDocument::parseQml() for file" << _fileName;
|
||||||
|
|
||||||
_engine = new Engine();
|
_engine = new Engine();
|
||||||
_pool = new NodePool(_fileName, _engine);
|
_pool = new NodePool(_fileName, _engine);
|
||||||
@@ -106,11 +118,11 @@ bool QmlDocument::parse()
|
|||||||
lexer.setCode(_source, /*line = */ 1);
|
lexer.setCode(_source, /*line = */ 1);
|
||||||
|
|
||||||
_parsedCorrectly = parser.parse();
|
_parsedCorrectly = parser.parse();
|
||||||
_program = parser.ast();
|
_uiProgram = parser.ast();
|
||||||
_diagnosticMessages = parser.diagnosticMessages();
|
_diagnosticMessages = parser.diagnosticMessages();
|
||||||
|
|
||||||
if (_program) {
|
if (_uiProgram) {
|
||||||
for (QmlJS::AST::UiObjectMemberList *iter = _program->members; iter; iter = iter->next)
|
for (QmlJS::AST::UiObjectMemberList *iter = _uiProgram->members; iter; iter = iter->next)
|
||||||
if (iter->member)
|
if (iter->member)
|
||||||
_symbols.append(new QmlSymbolFromFile(_fileName, iter->member));
|
_symbols.append(new QmlSymbolFromFile(_fileName, iter->member));
|
||||||
|
|
||||||
@@ -123,6 +135,29 @@ bool QmlDocument::parse()
|
|||||||
return _parsedCorrectly;
|
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
|
QmlSymbolFromFile *QmlDocument::findSymbol(QmlJS::AST::Node *node) const
|
||||||
{
|
{
|
||||||
foreach (QmlSymbol *symbol, _symbols)
|
foreach (QmlSymbol *symbol, _symbols)
|
||||||
@@ -143,9 +178,7 @@ Snapshot::~Snapshot()
|
|||||||
|
|
||||||
void Snapshot::insert(const QmlDocument::Ptr &document)
|
void Snapshot::insert(const QmlDocument::Ptr &document)
|
||||||
{
|
{
|
||||||
if (!document || !document->program())
|
if (document && (document->qmlProgram() || document->jsProgram()))
|
||||||
return;
|
|
||||||
|
|
||||||
QMap<QString, QmlDocument::Ptr>::insert(document->fileName(), document);
|
QMap<QString, QmlDocument::Ptr>::insert(document->fileName(), document);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,13 +56,15 @@ public:
|
|||||||
|
|
||||||
static QmlDocument::Ptr create(const QString &fileName);
|
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;
|
QList<QmlJS::DiagnosticMessage> diagnosticMessages() const;
|
||||||
|
|
||||||
QString source() const;
|
QString source() const;
|
||||||
void setSource(const QString &source);
|
void setSource(const QString &source);
|
||||||
|
|
||||||
bool parse();
|
bool parseQml();
|
||||||
|
bool parseJavaScript();
|
||||||
|
|
||||||
bool isParsedCorrectly() const
|
bool isParsedCorrectly() const
|
||||||
{ return _parsedCorrectly; }
|
{ return _parsedCorrectly; }
|
||||||
@@ -80,7 +82,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
QmlJS::Engine *_engine;
|
QmlJS::Engine *_engine;
|
||||||
QmlJS::NodePool *_pool;
|
QmlJS::NodePool *_pool;
|
||||||
QmlJS::AST::UiProgram *_program;
|
QmlJS::AST::UiProgram *_uiProgram;
|
||||||
|
QmlJS::AST::Program *_jsProgram;
|
||||||
QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
|
QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
|
||||||
QString _fileName;
|
QString _fileName;
|
||||||
QString _path;
|
QString _path;
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ QMap<QString, QmlIdSymbol*> QmlIdCollector::operator()(Qml::QmlDocument &doc)
|
|||||||
_ids.clear();
|
_ids.clear();
|
||||||
_currentSymbol = 0;
|
_currentSymbol = 0;
|
||||||
|
|
||||||
Node::accept(doc.program(), this);
|
Node::accept(doc.qmlProgram(), this);
|
||||||
|
|
||||||
return _ids;
|
return _ids;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user