From c453212542359d382867a3bd1342638c2503054a Mon Sep 17 00:00:00 2001
From: Erik Verbruggen <erik.verbruggen@nokia.com>
Date: Wed, 3 Feb 2010 17:27:49 +0100
Subject: [PATCH] Added indenting of dirty areas.

---
 .../core/include/componenttextmodifier.h      |  2 ++
 .../core/include/plaintexteditmodifier.h      |  2 ++
 .../qmldesigner/core/include/textmodifier.h   |  2 ++
 .../core/model/componenttextmodifier.cpp      |  5 ++++
 .../core/model/modeltotextmerger.cpp          | 17 ++++++-----
 .../core/model/plaintexteditmodifier.cpp      | 30 ++++++++++++++++---
 6 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/src/plugins/qmldesigner/core/include/componenttextmodifier.h b/src/plugins/qmldesigner/core/include/componenttextmodifier.h
index 723425531e6..b164fa2c22d 100644
--- a/src/plugins/qmldesigner/core/include/componenttextmodifier.h
+++ b/src/plugins/qmldesigner/core/include/componenttextmodifier.h
@@ -43,6 +43,8 @@ public:
 
     virtual void replace(int offset, int length, const QString& replacement);
     virtual void move(const MoveInfo &moveInfo);
+    virtual void indent(int offset, int length);
+
     virtual void startGroup();
     virtual void flushGroup();
     virtual void commitGroup();
diff --git a/src/plugins/qmldesigner/core/include/plaintexteditmodifier.h b/src/plugins/qmldesigner/core/include/plaintexteditmodifier.h
index 29a33b57250..a7090f9fdad 100644
--- a/src/plugins/qmldesigner/core/include/plaintexteditmodifier.h
+++ b/src/plugins/qmldesigner/core/include/plaintexteditmodifier.h
@@ -64,6 +64,8 @@ public:
 
     virtual void replace(int offset, int length, const QString &replacement);
     virtual void move(const MoveInfo &moveInfo);
+    virtual void indent(int offset, int length);
+
     virtual void startGroup();
     virtual void flushGroup();
     virtual void commitGroup();
diff --git a/src/plugins/qmldesigner/core/include/textmodifier.h b/src/plugins/qmldesigner/core/include/textmodifier.h
index 79092e38368..0931d9b8ebb 100644
--- a/src/plugins/qmldesigner/core/include/textmodifier.h
+++ b/src/plugins/qmldesigner/core/include/textmodifier.h
@@ -68,6 +68,8 @@ public:
 
     virtual void replace(int offset, int length, const QString& replacement) = 0;
     virtual void move(const MoveInfo &moveInfo) = 0;
+    virtual void indent(int offset, int length) = 0;
+
     virtual void startGroup() = 0;
     virtual void flushGroup() = 0;
     virtual void commitGroup() = 0;
diff --git a/src/plugins/qmldesigner/core/model/componenttextmodifier.cpp b/src/plugins/qmldesigner/core/model/componenttextmodifier.cpp
index 98a78bd9557..cfd1f5da20b 100644
--- a/src/plugins/qmldesigner/core/model/componenttextmodifier.cpp
+++ b/src/plugins/qmldesigner/core/model/componenttextmodifier.cpp
@@ -58,6 +58,11 @@ void ComponentTextModifier::move(const MoveInfo &moveInfo)
     m_originalModifier->move(moveInfo);
 }
 
+void ComponentTextModifier::indent(int offset, int length)
+{
+    m_originalModifier->indent(offset, length);
+}
+
 void ComponentTextModifier::startGroup()
 {
     m_originalModifier->startGroup();
diff --git a/src/plugins/qmldesigner/core/model/modeltotextmerger.cpp b/src/plugins/qmldesigner/core/model/modeltotextmerger.cpp
index 09ba7b31b31..d7e89a09ea0 100644
--- a/src/plugins/qmldesigner/core/model/modeltotextmerger.cpp
+++ b/src/plugins/qmldesigner/core/model/modeltotextmerger.cpp
@@ -287,15 +287,16 @@ void ModelToTextMerger::applyChanges()
     }
 }
 
-void ModelToTextMerger::reindent(const QMap<int, int> &/*dirtyAreas*/) const
+void ModelToTextMerger::reindent(const QMap<int, int> &dirtyAreas) const
 {
-//    QList<int> offsets = dirtyAreas.keys();
-//    qSort(offsets);
-//
-//    foreach (const int offset, offsets) {
-//        const int length = dirtyAreas[offset];
-//        xxxx
-//    }
+    QList<int> offsets = dirtyAreas.keys();
+    qSort(offsets);
+    TextModifier *textModifier = m_rewriterView->textModifier();
+
+    foreach (const int offset, offsets) {
+        const int length = dirtyAreas[offset];
+        textModifier->indent(offset, length);
+    }
 }
 
 void ModelToTextMerger::schedule(RewriteAction *action)
diff --git a/src/plugins/qmldesigner/core/model/plaintexteditmodifier.cpp b/src/plugins/qmldesigner/core/model/plaintexteditmodifier.cpp
index 76ff4e510c4..5ed763a388d 100644
--- a/src/plugins/qmldesigner/core/model/plaintexteditmodifier.cpp
+++ b/src/plugins/qmldesigner/core/model/plaintexteditmodifier.cpp
@@ -27,14 +27,15 @@
 **
 **************************************************************************/
 
-#include <QtCore/QDebug>
+#include "plaintexteditmodifier.h"
+
+#include <texteditor/basetexteditor.h>
+#include <utils/changeset.h>
 
 #include <QtGui/QPlainTextEdit>
 #include <QtGui/QUndoStack>
 
-#include <utils/changeset.h>
-
-#include "plaintexteditmodifier.h"
+#include <QtCore/QDebug>
 
 using namespace Utils;
 using namespace QmlDesigner;
@@ -112,6 +113,27 @@ void PlainTextEditModifier::move(const MoveInfo &moveInfo)
     }
 }
 
+void PlainTextEditModifier::indent(int offset, int length)
+{
+    if (length == 0 || offset < 0 || offset + length >= text().length())
+        return;
+
+//    qDebug() << "PlainTextEditModifier::indent(" << offset << "," << length << ")";
+    if (TextEditor::BaseTextEditor *bte = dynamic_cast<TextEditor::BaseTextEditor*>(m_textEdit)) {
+//        qDebug() << "**** Doing indentation";
+        // find the applicable block:
+        QTextDocument *doc = bte->document();
+        QTextCursor tc(doc);
+        tc.beginEditBlock();
+        tc.setPosition(offset);
+        tc.setPosition(offset + length, QTextCursor::KeepAnchor);
+        bte->indentInsertedText(tc);
+        tc.endEditBlock();
+    } else {
+//        qDebug() << "**** Skipping indentation";
+    }
+}
+
 void PlainTextEditModifier::startGroup()
 {
     if (!m_changeSet)
-- 
GitLab