Commit beb7724d authored by Erik Verbruggen's avatar Erik Verbruggen
Browse files

Change rewriting to use the indentation depth of the underlying text

editor.
parent d76fa87f
......@@ -75,18 +75,18 @@ void AddObjectVisitor::insertInto(QmlJS::AST::UiObjectInitializer *ast)
UiObjectMemberList *insertAfter = searchMemberToInsertAfter(ast->members, QString(), m_propertyOrder);
int insertionPoint;
int indentDepth;
int depth;
QString textToInsert;
if (insertAfter && insertAfter->member) {
insertionPoint = insertAfter->member->lastSourceLocation().end();
indentDepth = calculateIndentDepth(insertAfter->member->lastSourceLocation());
depth = calculateIndentDepth(insertAfter->member->lastSourceLocation());
textToInsert += QLatin1String("\n");
} else {
insertionPoint = ast->lbraceToken.end();
indentDepth = calculateIndentDepth(ast->lbraceToken) + 4;
depth = calculateIndentDepth(ast->lbraceToken) + indentDepth();
}
textToInsert += addIndentation(m_content, indentDepth);
textToInsert += addIndentation(m_content, depth);
replace(insertionPoint, 0, QLatin1String("\n") + textToInsert);
setDidRewriting(true);
......
......@@ -83,7 +83,7 @@ void AddPropertyVisitor::addInMembers(QmlJS::AST::UiObjectInitializer *initializ
UiObjectMemberList *insertAfter = searchMemberToInsertAfter(initializer->members, m_name, m_propertyOrder);
SourceLocation endOfPreviousMember;
SourceLocation startOfNextMember;
unsigned indentDepth;
unsigned depth;
if (insertAfter == 0 || insertAfter->member == 0) {
// insert as first member
......@@ -94,7 +94,7 @@ void AddPropertyVisitor::addInMembers(QmlJS::AST::UiObjectInitializer *initializ
else
startOfNextMember = initializer->rbraceToken;
indentDepth = calculateIndentDepth(endOfPreviousMember) + 4;
depth = calculateIndentDepth(endOfPreviousMember) + indentDepth();
} else {
endOfPreviousMember = insertAfter->member->lastSourceLocation();
......@@ -103,7 +103,7 @@ void AddPropertyVisitor::addInMembers(QmlJS::AST::UiObjectInitializer *initializ
else
startOfNextMember = initializer->rbraceToken;
indentDepth = calculateIndentDepth(endOfPreviousMember);
depth = calculateIndentDepth(endOfPreviousMember);
}
QString newPropertyTemplate;
......@@ -129,7 +129,7 @@ void AddPropertyVisitor::addInMembers(QmlJS::AST::UiObjectInitializer *initializ
if (isOneLiner)
newPropertyTemplate += QLatin1Char('\n');
const QString newPropertyText = addIndentation(newPropertyTemplate.arg(m_name, m_value), indentDepth);
const QString newPropertyText = addIndentation(newPropertyTemplate.arg(m_name, m_value), depth);
replace(endOfPreviousMember.end(), 0, QLatin1Char('\n') + newPropertyText);
setDidRewriting(true);
......
......@@ -184,8 +184,8 @@ void ChangePropertyVisitor::insertIntoArray(QmlJS::AST::UiArrayBinding *ast)
return;
const int insertionPoint = lastMember->lastSourceLocation().end();
const int indentDepth = calculateIndentDepth(lastMember->firstSourceLocation());
const QString indentedArrayMember = addIndentation(m_value, indentDepth);
const int depth = calculateIndentDepth(lastMember->firstSourceLocation());
const QString indentedArrayMember = addIndentation(m_value, depth);
replace(insertionPoint, 0, QLatin1String(",\n") + indentedArrayMember);
setDidRewriting(true);
}
......@@ -61,6 +61,8 @@ protected:
QString textBetween(int startPosition, int endPosition) const;
QString textAt(const QmlJS::AST::SourceLocation &location) const;
int indentDepth() const
{ return textModifier()->indentDepth(); }
unsigned calculateIndentDepth(const QmlJS::AST::SourceLocation &position) const;
static QString addIndentation(const QString &text, unsigned depth);
static QString removeIndentation(const QString &text, unsigned depth);
......
......@@ -42,6 +42,8 @@ public:
BaseTextEditModifier(QPlainTextEdit *textEdit);
virtual void indent(int offset, int length);
virtual int indentDepth() const;
};
} // namespace QmlDesigner
......
......@@ -45,6 +45,8 @@ public:
virtual void move(const MoveInfo &moveInfo);
virtual void indent(int offset, int length);
virtual int indentDepth() const;
virtual void startGroup();
virtual void flushGroup();
virtual void commitGroup();
......
......@@ -70,6 +70,8 @@ public:
virtual void move(const MoveInfo &moveInfo) = 0;
virtual void indent(int offset, int length) = 0;
virtual int indentDepth() const = 0;
virtual void startGroup() = 0;
virtual void flushGroup() = 0;
virtual void commitGroup() = 0;
......
......@@ -30,6 +30,7 @@
#include "basetexteditmodifier.h"
#include <texteditor/basetexteditor.h>
#include <texteditor/tabsettings.h>
using namespace QmlDesigner;
......@@ -58,3 +59,13 @@ void BaseTextEditModifier::indent(int offset, int length)
// qDebug() << "**** Skipping indentation";
}
}
int BaseTextEditModifier::indentDepth() const
{
if (TextEditor::BaseTextEditor *bte = dynamic_cast<TextEditor::BaseTextEditor*>(plainTextEdit())) {
return bte->tabSettings().m_indentSize;
} else {
Q_ASSERT(false && "BaseTextEditModifier does not have a BaseTextEditor");
return 0;
}
}
......@@ -63,6 +63,11 @@ void ComponentTextModifier::indent(int offset, int length)
m_originalModifier->indent(offset, length);
}
int ComponentTextModifier::indentDepth() const
{
return m_originalModifier->indentDepth();
}
void ComponentTextModifier::startGroup()
{
m_originalModifier->startGroup();
......
......@@ -41,8 +41,6 @@
#include <QDebug>
#define INDENT_DEPTH 4
namespace {
enum {
DebugRewriteActions = 0
......@@ -92,6 +90,9 @@ void ModelToTextMerger::propertiesChanged(const QList<AbstractProperty>& propert
continue;
ModelNode containedModelNode;
const int indentDepth = m_rewriterView->textModifier()->indentDepth();
const QString propertyTextValue = QmlTextGenerator(getPropertyOrder(),
indentDepth)(property);
switch (propertyChange) {
case AbstractView::PropertiesAdded:
......@@ -99,7 +100,7 @@ void ModelToTextMerger::propertiesChanged(const QList<AbstractProperty>& propert
containedModelNode = property.toNodeProperty().modelNode();
schedule(new AddPropertyRewriteAction(property,
QmlTextGenerator(getPropertyOrder(), INDENT_DEPTH)(property),
propertyTextValue,
propertyType(property),
containedModelNode));
break;
......@@ -109,7 +110,7 @@ void ModelToTextMerger::propertiesChanged(const QList<AbstractProperty>& propert
containedModelNode = property.toNodeProperty().modelNode();
schedule(new ChangePropertyRewriteAction(property,
QmlTextGenerator(getPropertyOrder(), INDENT_DEPTH)(property),
propertyTextValue,
propertyType(property),
containedModelNode));
break;
......
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