Commit 3bc4bb3c authored by Eike Ziller's avatar Eike Ziller
Browse files

TextEditors: Move manual (un)indent code to document



Change-Id: I01e93a15b8280a55c31db6be28fe6eb39c7babb0
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 8aedff7f
...@@ -60,6 +60,8 @@ class BaseTextDocumentPrivate ...@@ -60,6 +60,8 @@ class BaseTextDocumentPrivate
public: public:
explicit BaseTextDocumentPrivate(BaseTextDocument *q); explicit BaseTextDocumentPrivate(BaseTextDocument *q);
QTextCursor indentOrUnindent(const QTextCursor &textCursor, bool doIndent);
QString m_defaultPath; QString m_defaultPath;
QString m_suggestedFileName; QString m_suggestedFileName;
QString m_mimeType; QString m_mimeType;
...@@ -84,6 +86,58 @@ BaseTextDocumentPrivate::BaseTextDocumentPrivate(BaseTextDocument *q) : ...@@ -84,6 +86,58 @@ BaseTextDocumentPrivate::BaseTextDocumentPrivate(BaseTextDocument *q) :
{ {
} }
QTextCursor BaseTextDocumentPrivate::indentOrUnindent(const QTextCursor &textCursor, bool doIndent)
{
QTextCursor cursor = textCursor;
cursor.beginEditBlock();
if (cursor.hasSelection()) {
// Indent or unindent the selected lines
int pos = cursor.position();
int anchor = cursor.anchor();
int start = qMin(anchor, pos);
int end = qMax(anchor, pos);
QTextBlock startBlock = m_document->findBlock(start);
QTextBlock endBlock = m_document->findBlock(end-1).next();
if (startBlock.next() == endBlock
&& (start > startBlock.position() || end < endBlock.position() - 1)) {
// Only one line partially selected.
cursor.removeSelectedText();
} else {
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text();
int indentPosition = m_tabSettings.lineIndentPosition(text);
if (!doIndent && !indentPosition)
indentPosition = m_tabSettings.firstNonSpace(text);
int targetColumn = m_tabSettings.indentedColumn(m_tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.insertText(m_tabSettings.indentationString(0, targetColumn, block));
cursor.setPosition(block.position());
cursor.setPosition(block.position() + indentPosition, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
cursor.endEditBlock();
return textCursor;
}
}
// Indent or unindent at cursor position
QTextBlock block = cursor.block();
QString text = block.text();
int indentPosition = cursor.positionInBlock();
int spaces = m_tabSettings.spacesLeftFromPosition(text, indentPosition);
int startColumn = m_tabSettings.columnAt(text, indentPosition - spaces);
int targetColumn = m_tabSettings.indentedColumn(m_tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.setPosition(block.position() + indentPosition - spaces, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(m_tabSettings.indentationString(startColumn, targetColumn, block));
cursor.endEditBlock();
return cursor;
}
BaseTextDocument::BaseTextDocument() : d(new BaseTextDocumentPrivate(this)) BaseTextDocument::BaseTextDocument() : d(new BaseTextDocumentPrivate(this))
{ {
connect(d->m_document, SIGNAL(modificationChanged(bool)), this, SIGNAL(changed())); connect(d->m_document, SIGNAL(modificationChanged(bool)), this, SIGNAL(changed()));
...@@ -180,6 +234,16 @@ void BaseTextDocument::autoReindent(const QTextCursor &cursor) ...@@ -180,6 +234,16 @@ void BaseTextDocument::autoReindent(const QTextCursor &cursor)
d->m_indenter->reindent(d->m_document, cursor, d->m_tabSettings); d->m_indenter->reindent(d->m_document, cursor, d->m_tabSettings);
} }
QTextCursor BaseTextDocument::indent(const QTextCursor &cursor)
{
return d->indentOrUnindent(cursor, true);
}
QTextCursor BaseTextDocument::unindent(const QTextCursor &cursor)
{
return d->indentOrUnindent(cursor, false);
}
const ExtraEncodingSettings &BaseTextDocument::extraEncodingSettings() const const ExtraEncodingSettings &BaseTextDocument::extraEncodingSettings() const
{ {
return d->m_extraEncodingSettings; return d->m_extraEncodingSettings;
......
...@@ -77,6 +77,8 @@ public: ...@@ -77,6 +77,8 @@ public:
Indenter *indenter() const; Indenter *indenter() const;
void autoIndent(const QTextCursor &cursor, QChar typedChar = QChar::Null); void autoIndent(const QTextCursor &cursor, QChar typedChar = QChar::Null);
void autoReindent(const QTextCursor &cursor); void autoReindent(const QTextCursor &cursor);
QTextCursor indent(const QTextCursor &cursor);
QTextCursor unindent(const QTextCursor &cursor);
ITextMarkable *markableInterface() const; ITextMarkable *markableInterface() const;
......
...@@ -997,12 +997,12 @@ void BaseTextEditorWidget::lowercaseSelection() ...@@ -997,12 +997,12 @@ void BaseTextEditorWidget::lowercaseSelection()
void BaseTextEditorWidget::indent() void BaseTextEditorWidget::indent()
{ {
indentOrUnindent(true); setTextCursor(baseTextDocument()->indent(textCursor()));
} }
void BaseTextEditorWidget::unindent() void BaseTextEditorWidget::unindent()
{ {
indentOrUnindent(false); setTextCursor(baseTextDocument()->unindent(textCursor()));
} }
void BaseTextEditorWidget::openLinkUnderCursor() void BaseTextEditorWidget::openLinkUnderCursor()
...@@ -1701,7 +1701,10 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e) ...@@ -1701,7 +1701,10 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e)
} }
d->m_document->autoIndent(cursor); d->m_document->autoIndent(cursor);
} else { } else {
indentOrUnindent(e->key() == Qt::Key_Tab); if (e->key() == Qt::Key_Tab)
indent();
else
unindent();
} }
e->accept(); e->accept();
return; return;
...@@ -4462,61 +4465,6 @@ const MarginSettings &BaseTextEditorWidget::marginSettings() const ...@@ -4462,61 +4465,6 @@ const MarginSettings &BaseTextEditorWidget::marginSettings() const
return d->m_marginSettings; return d->m_marginSettings;
} }
void BaseTextEditorWidget::indentOrUnindent(bool doIndent)
{
const TextEditor::TabSettings &tabSettings = d->m_document->tabSettings();
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
if (cursor.hasSelection()) {
// Indent or unindent the selected lines
int pos = cursor.position();
int anchor = cursor.anchor();
int start = qMin(anchor, pos);
int end = qMax(anchor, pos);
QTextDocument *doc = document();
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock(end-1).next();
if (startBlock.next() == endBlock
&& (start > startBlock.position() || end < endBlock.position() - 1)) {
// Only one line partially selected.
cursor.removeSelectedText();
} else {
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text();
int indentPosition = tabSettings.lineIndentPosition(text);
if (!doIndent && !indentPosition)
indentPosition = tabSettings.firstNonSpace(text);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.insertText(tabSettings.indentationString(0, targetColumn, block));
cursor.setPosition(block.position());
cursor.setPosition(block.position() + indentPosition, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
cursor.endEditBlock();
return;
}
}
// Indent or unindent at cursor position
QTextBlock block = cursor.block();
QString text = block.text();
int indentPosition = cursor.positionInBlock();
int spaces = tabSettings.spacesLeftFromPosition(text, indentPosition);
int startColumn = tabSettings.columnAt(text, indentPosition - spaces);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.setPosition(block.position() + indentPosition - spaces, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(tabSettings.indentationString(startColumn, targetColumn, block));
cursor.endEditBlock();
setTextCursor(cursor);
}
void BaseTextEditorWidget::handleHomeKey(bool anchor) void BaseTextEditorWidget::handleHomeKey(bool anchor)
{ {
QTextCursor cursor = textCursor(); QTextCursor cursor = textCursor();
...@@ -4621,7 +4569,7 @@ void BaseTextEditorWidget::handleBackspaceKey() ...@@ -4621,7 +4569,7 @@ void BaseTextEditorWidget::handleBackspaceKey()
d->m_snippetOverlay->clear(); d->m_snippetOverlay->clear();
cursorWithinSnippet = false; cursorWithinSnippet = false;
} }
indentOrUnindent(false); unindent();
} }
handled = true; handled = true;
} }
......
...@@ -513,7 +513,6 @@ signals: ...@@ -513,7 +513,6 @@ signals:
private: private:
void ctor(const QSharedPointer<BaseTextDocument> &doc); void ctor(const QSharedPointer<BaseTextDocument> &doc);
void indentOrUnindent(bool doIndent);
void handleHomeKey(bool anchor); void handleHomeKey(bool anchor);
void handleBackspaceKey(); void handleBackspaceKey();
void moveLineUpDown(bool up); void moveLineUpDown(bool up);
......
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