TextEditor: Sort indent ranges before applying

It can easily happen that callers insert indent ranges out of order, and
without sorting the indentation done earlier in the file is not
considered for the later parts, leading to inconsistent results.

Fixes: QTCREATORBUG-18929
Change-Id: Ice2abe92d54446bcdd102c6a1f822262a8533543
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2020-06-12 13:41:00 +02:00
parent 9be4a5f839
commit 934641f45c
3 changed files with 44 additions and 1 deletions

View File

@@ -121,4 +121,9 @@ private:
bool m_error; bool m_error;
}; };
inline bool operator<(const ChangeSet::Range &r1, const ChangeSet::Range &r2)
{
return r1.start < r2.start;
}
} // namespace Utils } // namespace Utils

View File

@@ -1582,6 +1582,40 @@ void CppEditorPlugin::test_quickfix_data()
"};\n" "};\n"
); );
QTest::newRow("InsertQtPropertyMembersPrivateBeforePublic")
<< CppQuickFixFactoryPtr(new InsertQtPropertyMembers)
<< _("class XmarksTheSpot {\n"
"private:\n"
" @Q_PROPERTY(int it READ getIt WRITE setIt NOTIFY itChanged)\n"
"public:\n"
" void find();\n"
"};\n"
)
<< _("class XmarksTheSpot {\n"
"private:\n"
" Q_PROPERTY(int it READ getIt WRITE setIt NOTIFY itChanged)\n"
" int m_it;\n"
"\n"
"public:\n"
" void find();\n"
" int getIt() const\n"
" {\n"
" return m_it;\n"
" }\n"
"public slots:\n"
" void setIt(int it)\n"
" {\n"
" if (m_it == it)\n"
" return;\n"
"\n"
" m_it = it;\n"
" emit itChanged(m_it);\n"
" }\n"
"signals:\n"
" void itChanged(int it);\n"
"};\n"
);
// Escape String Literal as UTF-8 (no-trigger) // Escape String Literal as UTF-8 (no-trigger)
QTest::newRow("EscapeStringLiteral_notrigger") QTest::newRow("EscapeStringLiteral_notrigger")
<< CppQuickFixFactoryPtr(new EscapeStringLiteral) << CppQuickFixFactoryPtr(new EscapeStringLiteral)

View File

@@ -31,8 +31,9 @@
#include <coreplugin/dialogs/readonlyfilesdialog.h> #include <coreplugin/dialogs/readonlyfilesdialog.h>
#include <coreplugin/documentmanager.h> #include <coreplugin/documentmanager.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <utils/qtcassert.h> #include <utils/algorithm.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
@@ -343,6 +344,9 @@ bool RefactoringFile::apply()
else else
c.beginEditBlock(); c.beginEditBlock();
sort(m_indentRanges);
sort(m_reindentRanges);
// build indent selections now, applying the changeset will change locations // build indent selections now, applying the changeset will change locations
const RefactoringSelections &indentSelections = const RefactoringSelections &indentSelections =
RefactoringChanges::rangesToSelections(doc, m_indentRanges); RefactoringChanges::rangesToSelections(doc, m_indentRanges);