Commit deadeec1 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Introduced DuiDocument.

parent bb6f56f5
#include "duidocument.h"
#include "parser/javascriptast_p.h"
#include "parser/javascriptlexer_p.h"
#include "parser/javascriptparser_p.h"
#include "parser/javascriptengine_p.h"
#include "parser/javascriptnodepool_p.h"
using namespace DuiEditor;
using namespace DuiEditor::Internal;
using namespace JavaScript;
DuiDocument::DuiDocument(const QString &fileName)
: _engine(0), _pool(0), _program(0), _fileName(fileName)
{
}
DuiDocument::~DuiDocument()
{
delete _engine;
delete _pool;
}
DuiDocument::Ptr DuiDocument::create(const QString &fileName)
{
DuiDocument::Ptr doc(new DuiDocument(fileName));
return doc;
}
AST::UiProgram *DuiDocument::program() const
{
return _program;
}
QList<JavaScriptParser::DiagnosticMessage> DuiDocument::diagnosticMessages() const
{
return _diagnosticMessages;
}
void DuiDocument::setSource(const QString &source)
{
_source = source;
}
bool DuiDocument::parse()
{
Q_ASSERT(! _engine);
Q_ASSERT(! _pool);
Q_ASSERT(! _program);
_engine = new JavaScriptEnginePrivate();
_pool = new NodePool(_fileName, _engine);
JavaScriptParser parser;
NodePool nodePool(_fileName, _engine);
_engine->setNodePool(_pool);
Lexer lexer(_engine);
_engine->setLexer(&lexer);
lexer.setCode(_source, /*line = */ 1);
bool parsed = parser.parse(_engine);
_program = parser.ast();
_diagnosticMessages = parser.diagnosticMessages();
return parsed;
}
Snapshot::Snapshot()
{
}
Snapshot::~Snapshot()
{
}
#ifndef DUIDOCUMENT_H
#define DUIDOCUMENT_H
#include <QtCore/QSharedPointer>
#include <QtCore/QMap>
#include <QtCore/QString>
#include "parser/javascriptparser_p.h"
class JavaScriptEnginePrivate;
namespace JavaScript {
class NodePool;
namespace AST {
class UiProgram;
} // end of namespace AST
} // end of namespace JavaScript
namespace DuiEditor {
namespace Internal {
class DuiDocument
{
public:
typedef QSharedPointer<DuiDocument> Ptr;
protected:
DuiDocument(const QString &fileName);
public:
~DuiDocument();
static DuiDocument::Ptr create(const QString &fileName);
JavaScript::AST::UiProgram *program() const;
QList<JavaScriptParser::DiagnosticMessage> diagnosticMessages() const;
void setSource(const QString &source);
bool parse();
private:
JavaScriptEnginePrivate *_engine;
JavaScript::NodePool *_pool;
JavaScript::AST::UiProgram *_program;
QList<JavaScriptParser::DiagnosticMessage> _diagnosticMessages;
QString _fileName;
QString _source;
};
class Snapshot: public QMap<QString, DuiDocument>
{
public:
Snapshot();
~Snapshot();
};
} // end of namespace Internal
} // emd of namespace DuiEditor
#endif // DUIDOCUMENT_H
......@@ -31,15 +31,13 @@
#include "duieditorconstants.h"
#include "duihighlighter.h"
#include "duieditorplugin.h"
#include "duidocument.h"
#include "rewriter/rewriter.h"
#include "parser/javascriptengine_p.h"
#include "parser/javascriptparser_p.h"
#include "parser/javascriptlexer_p.h"
#include "parser/javascriptnodepool_p.h"
#include "parser/javascriptastvisitor_p.h"
#include "parser/javascriptast_p.h"
#include "parser/javascriptengine_p.h"
#include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actionmanager.h>
......@@ -435,29 +433,21 @@ void ScriptEditor::updateDocumentNow()
m_updateDocumentTimer->stop();
const QString fileName = file()->fileName();
const QString code = toPlainText();
JavaScriptParser parser;
JavaScriptEnginePrivate driver;
NodePool nodePool(fileName, &driver);
driver.setNodePool(&nodePool);
Lexer lexer(&driver);
lexer.setCode(code, /*line = */ 1);
driver.setLexer(&lexer);
const QString source = toPlainText();
bool parsed = parser.parse(&driver);
DuiDocument::Ptr doc = DuiDocument::create(fileName);
doc->setSource(source);
bool parsed = doc->parse();
FindIdDeclarations updateIds;
m_ids = updateIds(parser.ast());
m_ids = updateIds(doc->program());
if (parsed) {
FindDeclarations findDeclarations;
m_declarations = findDeclarations(parser.ast());
m_declarations = findDeclarations(doc->program());
FindWords findWords;
m_words = findWords(parser.ast());
m_words = findWords(doc->program());
QStringList items;
items.append(tr("<Select Symbol>"));
......@@ -478,7 +468,7 @@ void ScriptEditor::updateDocumentNow()
QTextEdit::ExtraSelection sel;
m_diagnosticMessages = parser.diagnosticMessages();
m_diagnosticMessages = doc->diagnosticMessages();
foreach (const JavaScriptParser::DiagnosticMessage &d, m_diagnosticMessages) {
int line = d.loc.startLine;
......
......@@ -27,7 +27,8 @@ duihighlighter.h \
duieditoractionhandler.h \
duicodecompletion.h \
duieditorconstants.h \
duihoverhandler.h
duihoverhandler.h \
duidocument.h
SOURCES += duieditor.cpp \
duieditorfactory.cpp \
......@@ -35,6 +36,7 @@ duieditorplugin.cpp \
duihighlighter.cpp \
duieditoractionhandler.cpp \
duicodecompletion.cpp \
duihoverhandler.cpp
duihoverhandler.cpp \
duidocument.cpp
RESOURCES += duieditor.qrc
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