Commit 29c58eab authored by Tim Jenssen's avatar Tim Jenssen
Browse files

QmlDesigner: refactor indent feature



* add indentLines() method
* refactor indent to use indentLines()

Change-Id: Ie36c7ab6217c31e9e3913a4dd5da2196364200c8
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@theqtcompany.com>
parent 81bdb9cd
......@@ -41,6 +41,7 @@ class QMLDESIGNERCORE_EXPORT BaseTextEditModifier: public PlainTextEditModifier
public:
BaseTextEditModifier(TextEditor::TextEditorWidget *textEdit);
void indentLines(int startLine, int endLine) override;
void indent(int offset, int length) override;
int indentDepth() const override;
......
......@@ -39,6 +39,7 @@ public:
void replace(int offset, int length, const QString& replacement) override;
void move(const MoveInfo &moveInfo) override;
void indent(int offset, int length) override;
void indentLines(int startLine, int endLine) override;
int indentDepth() const override;
......
......@@ -58,6 +58,7 @@ public:
void replace(int offset, int length, const QString &replacement) override;
void move(const MoveInfo &moveInfo) override;
void indent(int offset, int length) override = 0;
void indentLines(int startLine, int endLine) override = 0;
int indentDepth() const override = 0;
......@@ -102,10 +103,12 @@ public:
: PlainTextEditModifier(textEdit)
{}
virtual void indent(int /*offset*/, int /*length*/)
void indent(int /*offset*/, int /*length*/) override
{}
void indentLines(int /*offset*/, int /*length*/) override
{}
virtual int indentDepth() const
int indentDepth() const override
{ return 0; }
};
......
......@@ -64,6 +64,7 @@ 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 indentLines(int startLine, int endLine) = 0;
virtual int indentDepth() const = 0;
......
......@@ -43,41 +43,41 @@ BaseTextEditModifier::BaseTextEditModifier(TextEditor::TextEditorWidget *textEdi
{
}
void BaseTextEditModifier::indent(int offset, int length)
void BaseTextEditModifier::indentLines(int startLine, int endLine)
{
if (length == 0 || offset < 0 || offset + length >= text().length())
if (startLine < 0)
return;
TextEditor::TextEditorWidget *baseTextEditorWidget = qobject_cast<TextEditor::TextEditorWidget*>(plainTextEdit());
if (!baseTextEditorWidget)
return;
if (TextEditor::TextEditorWidget *baseTextEditorWidget = qobject_cast<TextEditor::TextEditorWidget*>(plainTextEdit())) {
TextEditor::TextDocument *baseTextEditorDocument = baseTextEditorWidget->textDocument();
QTextDocument *textDocument = baseTextEditorWidget->document();
int startLine = -1;
int endLine = -1;
int column;
QTextDocument *textDocument = plainTextEdit()->document();
TextEditor::TextDocument *baseTextEditorDocument = baseTextEditorWidget->textDocument();
TextEditor::TabSettings tabSettings = baseTextEditorDocument->tabSettings();
QTextCursor tc(textDocument);
baseTextEditorWidget->convertPosition(offset, &startLine, &column); //get line
baseTextEditorWidget->convertPosition(offset + length, &endLine, &column); //get line
tc.beginEditBlock();
for (int i = startLine; i <= endLine; i++) {
QTextBlock start = textDocument->findBlockByNumber(i);
QTextDocument *doc = baseTextEditorDocument->document();
QTextCursor tc(doc);
tc.beginEditBlock();
if (start.isValid()) {
QmlJSEditor::Internal::Indenter indenter;
indenter.indentBlock(textDocument, start, QChar::Null, tabSettings);
}
}
tc.endEditBlock();
}
if (startLine > 0) {
TextEditor::TabSettings tabSettings = baseTextEditorDocument->tabSettings();
for (int i = startLine; i <= endLine; i++) {
QTextBlock start = textDocument->findBlockByNumber(i);
void BaseTextEditModifier::indent(int offset, int length)
{
if (length == 0 || offset < 0 || offset + length >= text().length())
return;
if (start.isValid()) {
QmlJSEditor::Internal::Indenter indenter;
indenter.indentBlock(textDocument, start, QChar::Null, tabSettings);
}
}
}
int startLine = getLineInDocument(textDocument(), offset);
int endLine = getLineInDocument(textDocument(), offset + length);
tc.endEditBlock();
}
if (startLine > -1 && endLine > -1)
indentLines(startLine, endLine);
}
int BaseTextEditModifier::indentDepth() const
......
......@@ -56,6 +56,10 @@ void ComponentTextModifier::move(const MoveInfo &moveInfo)
void ComponentTextModifier::indent(int offset, int length)
{
m_originalModifier->indent(offset, length);
void ComponentTextModifier::indentLines(int startLine, int endLine)
{
m_originalModifier->indentLines(startLine, endLine);
}
int ComponentTextModifier::indentDepth() const
......
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