CppTools: Use indenter from TextDocument or create one

So if you use the ClangFormat Plugin, the ClangFormatIndenter is used
and not the CppQtStyleIndenter.

Change-Id: I7e71867cd4b48525ddc2f9b2dce8f13a65c3ad88
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Leander Schulten
2020-06-22 21:33:45 +02:00
parent 6ac8bf2f0a
commit d51a419458

View File

@@ -34,6 +34,11 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <cpptools/cpptoolsconstants.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditorsettings.h>
#include <QTextDocument> #include <QTextDocument>
using namespace CPlusPlus; using namespace CPlusPlus;
@@ -42,6 +47,13 @@ namespace CppTools {
class CppRefactoringChangesData : public TextEditor::RefactoringChangesData class CppRefactoringChangesData : public TextEditor::RefactoringChangesData
{ {
static std::unique_ptr<TextEditor::Indenter> createIndenter(QTextDocument *textDocument)
{
TextEditor::ICodeStylePreferencesFactory *factory
= TextEditor::TextEditorSettings::codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID);
return std::unique_ptr<TextEditor::Indenter>(factory->createIndenter(textDocument));
}
public: public:
explicit CppRefactoringChangesData(const Snapshot &snapshot) explicit CppRefactoringChangesData(const Snapshot &snapshot)
: m_snapshot(snapshot) : m_snapshot(snapshot)
@@ -53,23 +65,26 @@ public:
const QString &fileName, const QString &fileName,
const TextEditor::TextDocument *textDocument) const override const TextEditor::TextDocument *textDocument) const override
{ {
const TextEditor::TabSettings &tabSettings = if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
ProjectExplorer::actualTabSettings(fileName, textDocument); textDocument->indenter()->indent(selection, QChar::Null, textDocument->tabSettings());
} else {
CppQtStyleIndenter indenter(selection.document()); const auto &tabSettings = ProjectExplorer::actualTabSettings(fileName, textDocument);
indenter.indent(selection, QChar::Null, tabSettings); auto indenter = createIndenter(selection.document());
indenter->indent(selection, QChar::Null, tabSettings);
}
} }
void reindentSelection(const QTextCursor &selection, void reindentSelection(const QTextCursor &selection,
const QString &fileName, const QString &fileName,
const TextEditor::TextDocument *textDocument) const override const TextEditor::TextDocument *textDocument) const override
{ {
const TextEditor::TabSettings &tabSettings = if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
ProjectExplorer::actualTabSettings(fileName, textDocument); textDocument->indenter()->reindent(selection, textDocument->tabSettings());
} else {
CppQtStyleIndenter indenter(selection.document()); const auto &tabSettings = ProjectExplorer::actualTabSettings(fileName, textDocument);
indenter.reindent(selection, auto indenter = createIndenter(selection.document());
tabSettings); indenter->reindent(selection, tabSettings);
}
} }
void fileChanged(const QString &fileName) override void fileChanged(const QString &fileName) override