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