Commit 8aedff7f authored by Eike Ziller's avatar Eike Ziller
Browse files

TextEditors: Move some auto-indentation code to document



From the editor widget. Also name them autoIndent/autoReindent to
distinguish from the manual indent functions.

Change-Id: I4e1b9d503f53870b669fee66467e5e567eacefec
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 7422b479
...@@ -1961,7 +1961,7 @@ bool CPPEditorWidget::handleDocumentationComment(QKeyEvent *e) ...@@ -1961,7 +1961,7 @@ bool CPPEditorWidget::handleDocumentationComment(QKeyEvent *e)
cursor.setPosition(pos); cursor.setPosition(pos);
cursor.insertText(comment); cursor.insertText(comment);
cursor.setPosition(pos - 3, QTextCursor::KeepAnchor); cursor.setPosition(pos - 3, QTextCursor::KeepAnchor);
indent(document(), cursor, QChar::Null); baseTextDocument()->autoIndent(cursor);
cursor.endEditBlock(); cursor.endEditBlock();
e->accept(); e->accept();
return true; return true;
......
...@@ -309,7 +309,7 @@ static void addDeclaration(const Snapshot &snapshot, ...@@ -309,7 +309,7 @@ static void addDeclaration(const Snapshot &snapshot,
tc.beginEditBlock(); tc.beginEditBlock();
tc.insertText(loc.prefix() + declaration + loc.suffix()); tc.insertText(loc.prefix() + declaration + loc.suffix());
tc.setPosition(pos, QTextCursor::KeepAnchor); tc.setPosition(pos, QTextCursor::KeepAnchor);
editor->indentInsertedText(tc); editor->baseTextDocument()->autoIndent(tc);
tc.endEditBlock(); tc.endEditBlock();
} }
} }
......
...@@ -47,13 +47,14 @@ void BaseTextEditModifier::indent(int offset, int length) ...@@ -47,13 +47,14 @@ void BaseTextEditModifier::indent(int offset, int length)
return; return;
if (TextEditor::BaseTextEditorWidget *bte = qobject_cast<TextEditor::BaseTextEditorWidget*>(plainTextEdit())) { if (TextEditor::BaseTextEditorWidget *bte = qobject_cast<TextEditor::BaseTextEditorWidget*>(plainTextEdit())) {
TextEditor::BaseTextDocument *btd = bte->baseTextDocument();
// find the applicable block: // find the applicable block:
QTextDocument *doc = bte->document(); QTextDocument *doc = btd->document();
QTextCursor tc(doc); QTextCursor tc(doc);
tc.beginEditBlock(); tc.beginEditBlock();
tc.setPosition(offset); tc.setPosition(offset);
tc.setPosition(offset + length, QTextCursor::KeepAnchor); tc.setPosition(offset + length, QTextCursor::KeepAnchor);
bte->indentInsertedText(tc); btd->autoIndent(tc);
tc.endEditBlock(); tc.endEditBlock();
} }
} }
...@@ -61,7 +62,7 @@ void BaseTextEditModifier::indent(int offset, int length) ...@@ -61,7 +62,7 @@ void BaseTextEditModifier::indent(int offset, int length)
int BaseTextEditModifier::indentDepth() const int BaseTextEditModifier::indentDepth() const
{ {
if (TextEditor::BaseTextEditorWidget *bte = qobject_cast<TextEditor::BaseTextEditorWidget*>(plainTextEdit())) if (TextEditor::BaseTextEditorWidget *bte = qobject_cast<TextEditor::BaseTextEditorWidget*>(plainTextEdit()))
return bte->tabSettings().m_indentSize; return bte->baseTextDocument()->tabSettings().m_indentSize;
else else
return 0; return 0;
} }
......
...@@ -170,6 +170,16 @@ void BaseTextDocument::setExtraEncodingSettings(const ExtraEncodingSettings &ext ...@@ -170,6 +170,16 @@ void BaseTextDocument::setExtraEncodingSettings(const ExtraEncodingSettings &ext
d->m_extraEncodingSettings = extraEncodingSettings; d->m_extraEncodingSettings = extraEncodingSettings;
} }
void BaseTextDocument::autoIndent(const QTextCursor &cursor, QChar typedChar)
{
d->m_indenter->indent(d->m_document, cursor, typedChar, d->m_tabSettings);
}
void BaseTextDocument::autoReindent(const QTextCursor &cursor)
{
d->m_indenter->reindent(d->m_document, cursor, d->m_tabSettings);
}
const ExtraEncodingSettings &BaseTextDocument::extraEncodingSettings() const const ExtraEncodingSettings &BaseTextDocument::extraEncodingSettings() const
{ {
return d->m_extraEncodingSettings; return d->m_extraEncodingSettings;
......
...@@ -75,6 +75,8 @@ public: ...@@ -75,6 +75,8 @@ public:
void setIndenter(Indenter *indenter); void setIndenter(Indenter *indenter);
Indenter *indenter() const; Indenter *indenter() const;
void autoIndent(const QTextCursor &cursor, QChar typedChar = QChar::Null);
void autoReindent(const QTextCursor &cursor);
ITextMarkable *markableInterface() const; ITextMarkable *markableInterface() const;
......
...@@ -910,7 +910,7 @@ void BaseTextEditorWidget::copyLineUpDown(bool up) ...@@ -910,7 +910,7 @@ void BaseTextEditorWidget::copyLineUpDown(bool up)
move.setPosition(start); move.setPosition(start);
move.setPosition(end, QTextCursor::KeepAnchor); move.setPosition(end, QTextCursor::KeepAnchor);
indent(document(), move, QChar::Null); d->m_document->autoIndent(move);
move.endEditBlock(); move.endEditBlock();
setTextCursor(move); setTextCursor(move);
...@@ -959,7 +959,7 @@ void BaseTextEditorWidget::insertLineAbove() ...@@ -959,7 +959,7 @@ void BaseTextEditorWidget::insertLineAbove()
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
cursor.insertBlock(); cursor.insertBlock();
cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor);
indent(document(), cursor, QChar::Null); d->m_document->autoIndent(cursor);
cursor.endEditBlock(); cursor.endEditBlock();
setTextCursor(cursor); setTextCursor(cursor);
} }
...@@ -970,7 +970,7 @@ void BaseTextEditorWidget::insertLineBelow() ...@@ -970,7 +970,7 @@ void BaseTextEditorWidget::insertLineBelow()
cursor.beginEditBlock(); cursor.beginEditBlock();
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::MoveAnchor);
cursor.insertBlock(); cursor.insertBlock();
indent(document(), cursor, QChar::Null); d->m_document->autoIndent(cursor);
cursor.endEditBlock(); cursor.endEditBlock();
setTextCursor(cursor); setTextCursor(cursor);
} }
...@@ -1124,7 +1124,7 @@ void BaseTextEditorWidget::moveLineUpDown(bool up) ...@@ -1124,7 +1124,7 @@ void BaseTextEditorWidget::moveLineUpDown(bool up)
if (shouldReindent) { if (shouldReindent) {
// The text was not commented at all; re-indent. // The text was not commented at all; re-indent.
reindent(document(), move); d->m_document->autoReindent(move);
} }
move.endEditBlock(); move.endEditBlock();
...@@ -1581,7 +1581,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e) ...@@ -1581,7 +1581,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e)
QString previousIndentationString; QString previousIndentationString;
if (tps.m_autoIndent) { if (tps.m_autoIndent) {
cursor.insertBlock(); cursor.insertBlock();
indent(document(), cursor, QChar::Null); d->m_document->autoIndent(cursor);
} else { } else {
cursor.insertBlock(); cursor.insertBlock();
...@@ -1600,7 +1600,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e) ...@@ -1600,7 +1600,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e)
--extraBlocks; --extraBlocks;
ensureVisible.movePosition(QTextCursor::NextBlock); ensureVisible.movePosition(QTextCursor::NextBlock);
if (tps.m_autoIndent) if (tps.m_autoIndent)
indent(document(), ensureVisible, QChar::Null); d->m_document->autoIndent(ensureVisible);
else if (!previousIndentationString.isEmpty()) else if (!previousIndentationString.isEmpty())
ensureVisible.insertText(previousIndentationString); ensureVisible.insertText(previousIndentationString);
} }
...@@ -1699,7 +1699,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e) ...@@ -1699,7 +1699,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e)
cursor.setPosition(newPosition); cursor.setPosition(newPosition);
setTextCursor(cursor); setTextCursor(cursor);
} }
indent(document(), cursor, QChar::Null); d->m_document->autoIndent(cursor);
} else { } else {
indentOrUnindent(e->key() == Qt::Key_Tab); indentOrUnindent(e->key() == Qt::Key_Tab);
} }
...@@ -1868,7 +1868,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e) ...@@ -1868,7 +1868,7 @@ void BaseTextEditorWidget::keyPressEvent(QKeyEvent *e)
cursor.setPosition(pos, QTextCursor::KeepAnchor); cursor.setPosition(pos, QTextCursor::KeepAnchor);
} }
if (!electricChar.isNull() && d->m_autoCompleter->contextAllowsElectricCharacters(cursor)) if (!electricChar.isNull() && d->m_autoCompleter->contextAllowsElectricCharacters(cursor))
indent(document(), cursor, electricChar); d->m_document->autoIndent(cursor, electricChar);
if (!autoText.isEmpty()) if (!autoText.isEmpty())
cursor.setPosition(autoText.length() == 1 ? cursor.position() : cursor.anchor()); cursor.setPosition(autoText.length() == 1 ? cursor.position() : cursor.anchor());
...@@ -1919,7 +1919,7 @@ void BaseTextEditorWidget::insertCodeSnippet(const QTextCursor &cursor_arg, cons ...@@ -1919,7 +1919,7 @@ void BaseTextEditorWidget::insertCodeSnippet(const QTextCursor &cursor_arg, cons
} }
cursor.setPosition(startCursorPosition, QTextCursor::KeepAnchor); cursor.setPosition(startCursorPosition, QTextCursor::KeepAnchor);
indent(cursor.document(), cursor, QChar()); d->m_document->autoIndent(cursor);
cursor.endEditBlock(); cursor.endEditBlock();
setExtraSelections(BaseTextEditorWidget::SnippetPlaceholderSelection, selections); setExtraSelections(BaseTextEditorWidget::SnippetPlaceholderSelection, selections);
...@@ -4670,21 +4670,6 @@ void BaseTextEditorWidget::zoomReset() ...@@ -4670,21 +4670,6 @@ void BaseTextEditorWidget::zoomReset()
emit requestZoomReset(); emit requestZoomReset();
} }
void BaseTextEditorWidget::indentInsertedText(const QTextCursor &tc)
{
indent(tc.document(), tc, QChar::Null);
}
void BaseTextEditorWidget::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar)
{
d->m_document->indenter()->indent(doc, cursor, typedChar, tabSettings());
}
void BaseTextEditorWidget::reindent(QTextDocument *doc, const QTextCursor &cursor)
{
d->m_document->indenter()->reindent(doc, cursor, tabSettings());
}
BaseTextEditorWidget::Link BaseTextEditorWidget::findLinkAt(const QTextCursor &, bool, bool) BaseTextEditorWidget::Link BaseTextEditorWidget::findLinkAt(const QTextCursor &, bool, bool)
{ {
return Link(); return Link();
...@@ -5290,7 +5275,7 @@ void BaseTextEditorWidget::format() ...@@ -5290,7 +5275,7 @@ void BaseTextEditorWidget::format()
{ {
QTextCursor cursor = textCursor(); QTextCursor cursor = textCursor();
cursor.beginEditBlock(); cursor.beginEditBlock();
indent(document(), cursor, QChar::Null); d->m_document->autoIndent(cursor);
cursor.endEditBlock(); cursor.endEditBlock();
} }
...@@ -5939,7 +5924,7 @@ void BaseTextEditorWidget::insertFromMimeData(const QMimeData *source) ...@@ -5939,7 +5924,7 @@ void BaseTextEditorWidget::insertFromMimeData(const QMimeData *source)
c.setPosition(cursor.document()->findBlockByNumber(reindentBlockStart).position()); c.setPosition(cursor.document()->findBlockByNumber(reindentBlockStart).position());
c.setPosition(cursor.document()->findBlockByNumber(reindentBlockEnd).position(), c.setPosition(cursor.document()->findBlockByNumber(reindentBlockEnd).position(),
QTextCursor::KeepAnchor); QTextCursor::KeepAnchor);
reindent(document(), c); d->m_document->autoReindent(c);
} }
cursor.endEditBlock(); cursor.endEditBlock();
......
...@@ -443,10 +443,6 @@ protected: ...@@ -443,10 +443,6 @@ protected:
void showDefaultContextMenu(QContextMenuEvent *e, const Core::Id menuContextId); void showDefaultContextMenu(QContextMenuEvent *e, const Core::Id menuContextId);
public: public:
void indentInsertedText(const QTextCursor &tc);
void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar);
void reindent(QTextDocument *doc, const QTextCursor &cursor);
struct Link struct Link
{ {
Link(const QString &fileName = QString(), int line = 0, int column = 0) Link(const QString &fileName = QString(), int line = 0, int column = 0)
......
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