TextEditor: Use simple text indentation as default

Simple indentation based on the previous line was already available in
the NormalIndenter class. Merge that up the hierarchy chain into
TextIndenter which is the base for other text-based indenters, and make
that the default indenter for text editor factories.

Text editor factories that don't have a special indenter get at least
basic indentation support for free that way.

Change-Id: Ib977a990f10a99bead82bc8a8348c02a106665f1
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2020-02-28 13:17:08 +01:00
parent a77ef4ca7f
commit 6959618d7b
12 changed files with 59 additions and 141 deletions

View File

@@ -36,6 +36,43 @@ TextIndenter::TextIndenter(QTextDocument *doc)
TextIndenter::~TextIndenter() = default;
// Indent a text block based on previous line.
// Simple text paragraph layout:
// aaaa aaaa
//
// bbb bb
// bbb bb
//
// - list
// list line2
//
// - listn
//
// ccc
//
// @todo{Add formatting to wrap paragraphs. This requires some
// hoops as the current indentation routines are not prepared
// for additional block being inserted. It might be possible
// to do in 2 steps (indenting/wrapping)}
int TextIndenter::indentFor(const QTextBlock &block,
const TabSettings &tabSettings,
int cursorPositionInEditor)
{
Q_UNUSED(tabSettings)
Q_UNUSED(cursorPositionInEditor)
QTextBlock previous = block.previous();
if (!previous.isValid())
return 0;
const QString previousText = previous.text();
// Empty line indicates a start of a new paragraph. Leave as is.
if (previousText.isEmpty() || previousText.trimmed().isEmpty())
return 0;
return tabSettings.indentationColumn(previousText);
}
IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
const TabSettings &tabSettings,
int /*cursorPositionInEditor*/)