From 11ccb57111c9974190469613eb21d2be449b53c9 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Thu, 14 May 2009 14:09:00 +0200
Subject: [PATCH] Improved the DuiDocument and keep a working copy of the
 document in the editor, also use the QML api.

---
 src/plugins/duieditor/duidocument.cpp     | 42 ++++++++++++++----
 src/plugins/duieditor/duidocument.h       | 52 ++++++++++++++---------
 src/plugins/duieditor/duieditor.cpp       |  3 +-
 src/plugins/duieditor/duieditor.h         | 12 ++++--
 src/plugins/duieditor/duihoverhandler.cpp |  2 +-
 5 files changed, 77 insertions(+), 34 deletions(-)

diff --git a/src/plugins/duieditor/duidocument.cpp b/src/plugins/duieditor/duidocument.cpp
index bd987e46bb3..5cee3404c85 100644
--- a/src/plugins/duieditor/duidocument.cpp
+++ b/src/plugins/duieditor/duidocument.cpp
@@ -1,3 +1,32 @@
+/**************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact:  Qt Software Information (qt-info@nokia.com)
+**
+** Commercial Usage
+**
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+**
+**************************************************************************/
+
 #include "duidocument.h"
 #include "parser/javascriptast_p.h"
 #include "parser/javascriptlexer_p.h"
@@ -31,7 +60,7 @@ AST::UiProgram *DuiDocument::program() const
     return _program;
 }
 
-QList<JavaScriptParser::DiagnosticMessage> DuiDocument::diagnosticMessages() const
+QList<DiagnosticMessage> DuiDocument::diagnosticMessages() const
 {
     return _diagnosticMessages;
 }
@@ -47,20 +76,15 @@ bool DuiDocument::parse()
     Q_ASSERT(! _pool);
     Q_ASSERT(! _program);
 
-    _engine = new JavaScriptEnginePrivate();
+    _engine = new Engine();
     _pool = new NodePool(_fileName, _engine);
 
-    JavaScriptParser parser;
-
-    NodePool nodePool(_fileName, _engine);
-    _engine->setNodePool(_pool);
-
     Lexer lexer(_engine);
-    _engine->setLexer(&lexer);
+    Parser parser(_engine);
 
     lexer.setCode(_source, /*line = */ 1);
 
-    bool parsed = parser.parse(_engine);
+    bool parsed = parser.parse();
     _program = parser.ast();
     _diagnosticMessages = parser.diagnosticMessages();
     return parsed;
diff --git a/src/plugins/duieditor/duidocument.h b/src/plugins/duieditor/duidocument.h
index 4449cb8d886..fc95b4c64de 100644
--- a/src/plugins/duieditor/duidocument.h
+++ b/src/plugins/duieditor/duidocument.h
@@ -1,3 +1,31 @@
+/**************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact:  Qt Software Information (qt-info@nokia.com)
+**
+** Commercial Usage
+**
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+**
+**************************************************************************/
 #ifndef DUIDOCUMENT_H
 #define DUIDOCUMENT_H
 
@@ -5,22 +33,8 @@
 #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
-
-
+#include "parser/javascriptengine_p.h"
+#include "parser/javascriptastfwd_p.h"
 
 namespace DuiEditor {
 namespace Internal {
@@ -39,16 +53,16 @@ public:
     static DuiDocument::Ptr create(const QString &fileName);
 
     JavaScript::AST::UiProgram *program() const;
-    QList<JavaScriptParser::DiagnosticMessage> diagnosticMessages() const;
+    QList<JavaScript::DiagnosticMessage> diagnosticMessages() const;
 
     void setSource(const QString &source);
     bool parse();
 
 private:
-    JavaScriptEnginePrivate *_engine;
+    JavaScript::Engine *_engine;
     JavaScript::NodePool *_pool;
     JavaScript::AST::UiProgram *_program;
-    QList<JavaScriptParser::DiagnosticMessage> _diagnosticMessages;
+    QList<JavaScript::DiagnosticMessage> _diagnosticMessages;
     QString _fileName;
     QString _source;
 };
diff --git a/src/plugins/duieditor/duieditor.cpp b/src/plugins/duieditor/duieditor.cpp
index ddae6b2939d..4f934feea2f 100644
--- a/src/plugins/duieditor/duieditor.cpp
+++ b/src/plugins/duieditor/duieditor.cpp
@@ -438,6 +438,7 @@ void ScriptEditor::updateDocumentNow()
     DuiDocument::Ptr doc = DuiDocument::create(fileName);
     doc->setSource(source);
     bool parsed = doc->parse();
+    m_document = doc;
 
     FindIdDeclarations updateIds;
     m_ids = updateIds(doc->program());
@@ -470,7 +471,7 @@ void ScriptEditor::updateDocumentNow()
 
     m_diagnosticMessages = doc->diagnosticMessages();
 
-    foreach (const JavaScriptParser::DiagnosticMessage &d, m_diagnosticMessages) {
+    foreach (const DiagnosticMessage &d, m_diagnosticMessages) {
         int line = d.loc.startLine;
         int column = d.loc.startColumn;
 
diff --git a/src/plugins/duieditor/duieditor.h b/src/plugins/duieditor/duieditor.h
index 33d52bc9525..d0de6ca4830 100644
--- a/src/plugins/duieditor/duieditor.h
+++ b/src/plugins/duieditor/duieditor.h
@@ -31,8 +31,11 @@
 #define DUIDITORW_H
 
 #include <texteditor/basetexteditor.h>
-#include <parser/javascriptast_p.h>
-#include <parser/javascriptparser_p.h>
+
+#include "parser/javascriptastfwd_p.h"
+#include "parser/javascriptengine_p.h"
+
+#include "duidocument.h"
 
 QT_BEGIN_NAMESPACE
 class QComboBox;
@@ -98,7 +101,7 @@ public:
     QStringList words() const;
     QStringList keywords() const;
 
-    QList<JavaScriptParser::DiagnosticMessage> diagnosticMessages() const
+    QList<JavaScript::DiagnosticMessage> diagnosticMessages() const
     { return m_diagnosticMessages; }
 
 public slots:
@@ -133,7 +136,8 @@ private:
     QList<Declaration> m_declarations;
     QStringList m_words;
     QMap<QString, QList<JavaScript::AST::SourceLocation> > m_ids; // ### use QMultiMap
-    QList<JavaScriptParser::DiagnosticMessage> m_diagnosticMessages;
+    QList<JavaScript::DiagnosticMessage> m_diagnosticMessages;
+    DuiDocument::Ptr m_document;
 };
 
 } // namespace Internal
diff --git a/src/plugins/duieditor/duihoverhandler.cpp b/src/plugins/duieditor/duihoverhandler.cpp
index c836326ead3..d00f4ec4c02 100644
--- a/src/plugins/duieditor/duihoverhandler.cpp
+++ b/src/plugins/duieditor/duihoverhandler.cpp
@@ -92,7 +92,7 @@ void DuiHoverHandler::showToolTip(TextEditor::ITextEditor *editor, const QPoint
     tc.setPosition(pos);
     const unsigned line = tc.block().blockNumber() + 1;
 
-    foreach (const JavaScriptParser::DiagnosticMessage &m, ed->diagnosticMessages()) {
+    foreach (const JavaScript::DiagnosticMessage &m, ed->diagnosticMessages()) {
         if (m.loc.startLine == line) {
             m_toolTip.append(m.message);
             break;
-- 
GitLab