From 63668f8119d61dfc6470be16b7f09cdcb51dbd46 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Fri, 29 Jan 2021 14:25:23 +0100 Subject: [PATCH] TextEditor: RefactoringChanges improve readability Change-Id: I0626f3e97eda6228130fe094c596ccbb3901b079 Reviewed-by: Christian Stenger --- src/plugins/texteditor/refactoringchanges.cpp | 25 +++++++++---------- src/plugins/texteditor/refactoringchanges.h | 6 ++--- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/plugins/texteditor/refactoringchanges.cpp b/src/plugins/texteditor/refactoringchanges.cpp index f40a00c02b7..9732972b010 100644 --- a/src/plugins/texteditor/refactoringchanges.cpp +++ b/src/plugins/texteditor/refactoringchanges.cpp @@ -361,8 +361,8 @@ bool RefactoringFile::apply() m_changes.apply(&c); m_changes.clear(); - indentOrReindent(&RefactoringChangesData::indentSelection, indentSelections); - indentOrReindent(&RefactoringChangesData::reindentSelection, reindentSelections); + indentOrReindent(indentSelections, Indent); + indentOrReindent(reindentSelections, Reindent); c.endEditBlock(); @@ -386,18 +386,17 @@ bool RefactoringFile::apply() return result; } -void RefactoringFile::indentOrReindent(void (RefactoringChangesData::*mf)(const QTextCursor &, - const QString &, - const TextDocument *) const, - const RefactoringSelections &ranges) +void RefactoringFile::indentOrReindent(const RefactoringSelections &ranges, + RefactoringFile::IndentType indent) { - using CursorPair = QPair; - - foreach (const CursorPair &p, ranges) { - QTextCursor selection(p.first.document()); - selection.setPosition(p.first.position()); - selection.setPosition(p.second.position(), QTextCursor::KeepAnchor); - ((*m_data).*(mf))(selection, m_fileName, m_editor ? m_editor->textDocument() : nullptr); + TextDocument * document = m_editor ? m_editor->textDocument() : nullptr; + for (const auto &[position, anchor]: ranges) { + QTextCursor selection(anchor); + selection.setPosition(position.position(), QTextCursor::KeepAnchor); + if (indent == Indent) + m_data->indentSelection(selection, m_fileName, document); + else + m_data->reindentSelection(selection, m_fileName, document); } } diff --git a/src/plugins/texteditor/refactoringchanges.h b/src/plugins/texteditor/refactoringchanges.h index 807f536242b..b975757f24b 100644 --- a/src/plugins/texteditor/refactoringchanges.h +++ b/src/plugins/texteditor/refactoringchanges.h @@ -93,10 +93,8 @@ protected: // derived classes may want to clear language specific extra data virtual void fileChanged(); - void indentOrReindent(void (RefactoringChangesData::*mf)(const QTextCursor &, - const QString &, - const TextDocument *) const, - const RefactoringSelections &ranges); + enum IndentType {Indent, Reindent}; + void indentOrReindent(const RefactoringSelections &ranges, IndentType indent); protected: QString m_fileName;