TextEditor: fix autoIndent

e70b99c7da broke the auto indent for all
indenters because those indenters are all based on the text indenter.

Add a fallback indenter that is used if no other indenter is configured
for a document, and avoid the auto indentation only if that fallback
indenter is used.

Change-Id: I00e04a07ab14c5fc433a71d1b9826f8e4cc5c0a0
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
David Schulz
2024-01-18 09:12:40 +01:00
parent 1cb58e80d8
commit ec6732511f
4 changed files with 24 additions and 16 deletions

View File

@@ -341,7 +341,7 @@ void RefactoringFile::doFormatting()
ICodeStylePreferencesFactory * const factory ICodeStylePreferencesFactory * const factory
= TextEditorSettings::codeStyleFactory(indenterId()); = TextEditorSettings::codeStyleFactory(indenterId());
indenterOwner.reset(factory ? factory->createIndenter(document) indenterOwner.reset(factory ? factory->createIndenter(document)
: new TextIndenter(document)); : new PlainTextIndenter(document));
indenter = indenterOwner.get(); indenter = indenterOwner.get();
tabSettings = TabSettings::settingsForFile(filePath()); tabSettings = TabSettings::settingsForFile(filePath());
} }

View File

@@ -58,7 +58,7 @@ class TextDocumentPrivate
{ {
public: public:
TextDocumentPrivate() TextDocumentPrivate()
: m_indenter(new TextIndenter(&m_document)) : m_indenter(new PlainTextIndenter(&m_document))
{ {
} }

View File

@@ -6,7 +6,7 @@
#include <QTextDocument> #include <QTextDocument>
#include <QTextCursor> #include <QTextCursor>
using namespace TextEditor; namespace TextEditor {
TextIndenter::TextIndenter(QTextDocument *doc) TextIndenter::TextIndenter(QTextDocument *doc)
: Indenter(doc) : Indenter(doc)
@@ -51,15 +51,6 @@ int TextIndenter::indentFor(const QTextBlock &block,
return tabSettings.indentationColumn(previousText); return tabSettings.indentationColumn(previousText);
} }
void TextIndenter::autoIndent(const QTextCursor &cursor,
const TabSettings &tabSettings,
int cursorPositionInEditor)
{
Q_UNUSED(cursor);
Q_UNUSED(tabSettings);
Q_UNUSED(cursorPositionInEditor);
}
IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks, IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
const TabSettings &tabSettings, const TabSettings &tabSettings,
int /*cursorPositionInEditor*/) int /*cursorPositionInEditor*/)
@@ -135,3 +126,14 @@ std::optional<TabSettings> TextIndenter::tabSettings() const
{ {
return std::optional<TabSettings>(); return std::optional<TabSettings>();
} }
void PlainTextIndenter::autoIndent(const QTextCursor &cursor,
const TabSettings &tabSettings,
int cursorPositionInEditor)
{
Q_UNUSED(cursor);
Q_UNUSED(tabSettings);
Q_UNUSED(cursorPositionInEditor);
}
} // namespace TextEditor

View File

@@ -26,10 +26,6 @@ public:
const TabSettings &tabSettings, const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override; int cursorPositionInEditor = -1) override;
void autoIndent(const QTextCursor &cursor,
const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks, IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TabSettings &tabSettings, const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override; int cursorPositionInEditor = -1) override;
@@ -49,4 +45,14 @@ public:
std::optional<TabSettings> tabSettings() const override; std::optional<TabSettings> tabSettings() const override;
}; };
class TEXTEDITOR_EXPORT PlainTextIndenter : public TextIndenter
{
public:
using TextIndenter::TextIndenter;
void autoIndent(const QTextCursor &cursor,
const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
};
} // namespace TextEditor } // namespace TextEditor