From d51a419458c035dddacb4ecd316aa62cbd6315a7 Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Mon, 22 Jun 2020 21:33:45 +0200 Subject: 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 --- src/plugins/cpptools/cpprefactoringchanges.cpp | 37 ++++++++++++++++++-------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'src/plugins/cpptools/cpprefactoringchanges.cpp') diff --git a/src/plugins/cpptools/cpprefactoringchanges.cpp b/src/plugins/cpptools/cpprefactoringchanges.cpp index c063e1efdd..5ebd490ea6 100644 --- a/src/plugins/cpptools/cpprefactoringchanges.cpp +++ b/src/plugins/cpptools/cpprefactoringchanges.cpp @@ -34,6 +34,11 @@ #include +#include +#include +#include +#include + #include using namespace CPlusPlus; @@ -42,6 +47,13 @@ namespace CppTools { class CppRefactoringChangesData : public TextEditor::RefactoringChangesData { + static std::unique_ptr createIndenter(QTextDocument *textDocument) + { + TextEditor::ICodeStylePreferencesFactory *factory + = TextEditor::TextEditorSettings::codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID); + return std::unique_ptr(factory->createIndenter(textDocument)); + } + public: explicit CppRefactoringChangesData(const Snapshot &snapshot) : m_snapshot(snapshot) @@ -53,23 +65,26 @@ public: const QString &fileName, const TextEditor::TextDocument *textDocument) const override { - const TextEditor::TabSettings &tabSettings = - ProjectExplorer::actualTabSettings(fileName, textDocument); - - CppQtStyleIndenter indenter(selection.document()); - indenter.indent(selection, QChar::Null, tabSettings); + if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat + textDocument->indenter()->indent(selection, QChar::Null, textDocument->tabSettings()); + } else { + const auto &tabSettings = ProjectExplorer::actualTabSettings(fileName, textDocument); + auto indenter = createIndenter(selection.document()); + indenter->indent(selection, QChar::Null, tabSettings); + } } void reindentSelection(const QTextCursor &selection, const QString &fileName, const TextEditor::TextDocument *textDocument) const override { - const TextEditor::TabSettings &tabSettings = - ProjectExplorer::actualTabSettings(fileName, textDocument); - - CppQtStyleIndenter indenter(selection.document()); - indenter.reindent(selection, - tabSettings); + if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat + textDocument->indenter()->reindent(selection, textDocument->tabSettings()); + } else { + const auto &tabSettings = ProjectExplorer::actualTabSettings(fileName, textDocument); + auto indenter = createIndenter(selection.document()); + indenter->reindent(selection, tabSettings); + } } void fileChanged(const QString &fileName) override -- cgit v1.2.1