CppEditor: Do not add redundant access specifier

... when inserting a member function declaration for a definition.

Fixes: QTCREATORBUG-5591
Change-Id: Ie85b435a1595832d0085abdcc0a4187d939820e0
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2021-02-05 13:12:41 +01:00
parent 8cd9300eda
commit 33d2d736ea
4 changed files with 36 additions and 19 deletions

View File

@@ -4707,6 +4707,9 @@ void insertToSectionDeclFromDef(const QByteArray &section, int sectionIndex)
QByteArray original; QByteArray original;
QByteArray expected; QByteArray expected;
QByteArray sectionString = section + ":\n";
if (sectionIndex == 4)
sectionString.clear();
// Header File // Header File
original = original =
@@ -4716,7 +4719,7 @@ void insertToSectionDeclFromDef(const QByteArray &section, int sectionIndex)
expected = expected =
"class Foo\n" "class Foo\n"
"{\n" "{\n"
+ section + ":\n" + + sectionString +
" Foo();\n" " Foo();\n"
"@};\n"; "@};\n";
testDocuments << QuickFixTestDocument::create("file.h", original, expected); testDocuments << QuickFixTestDocument::create("file.h", original, expected);

View File

@@ -3962,9 +3962,9 @@ protected:
const auto insertionPoint = m_headerInsertionPoints.find(spec); const auto insertionPoint = m_headerInsertionPoints.find(spec);
if (insertionPoint != m_headerInsertionPoints.end()) if (insertionPoint != m_headerInsertionPoints.end())
return *insertionPoint; return *insertionPoint;
const InsertionLocation loc = m_locator.methodDeclarationInClass(m_headerFile->fileName(), const InsertionLocation loc = m_locator.methodDeclarationInClass(
m_class, m_headerFile->fileName(), m_class, spec,
spec); InsertionPointLocator::ForceAccessSpec::Yes);
m_headerInsertionPoints.insert(spec, loc); m_headerInsertionPoints.insert(spec, loc);
return loc; return loc;
} }

View File

@@ -118,6 +118,7 @@ private:
void findMatch(const QList<AccessRange> &ranges, void findMatch(const QList<AccessRange> &ranges,
InsertionPointLocator::AccessSpec xsSpec, InsertionPointLocator::AccessSpec xsSpec,
InsertionPointLocator::Position positionInAccessSpec, InsertionPointLocator::Position positionInAccessSpec,
InsertionPointLocator::ForceAccessSpec forceAccessSpec,
unsigned &beforeToken, unsigned &beforeToken,
bool &needsLeadingEmptyLine, bool &needsLeadingEmptyLine,
bool &needsPrefix, bool &needsPrefix,
@@ -128,8 +129,10 @@ void findMatch(const QList<AccessRange> &ranges,
const bool atEnd = positionInAccessSpec == InsertionPointLocator::AccessSpecEnd; const bool atEnd = positionInAccessSpec == InsertionPointLocator::AccessSpecEnd;
needsLeadingEmptyLine = false; needsLeadingEmptyLine = false;
// try an exact match, and ignore the first (default) access spec: // Try an exact match. Ignore the default access spec unless there is no explicit one.
for (int i = lastIndex; i > 0; --i) { const int firstIndex = ranges.size() == 1
&& forceAccessSpec == InsertionPointLocator::ForceAccessSpec::No ? 0 : 1;
for (int i = lastIndex; i >= firstIndex; --i) {
const AccessRange &range = ranges.at(i); const AccessRange &range = ranges.at(i);
if (range.xsSpec == xsSpec) { if (range.xsSpec == xsSpec) {
beforeToken = atEnd ? range.end : range.beforeStart; beforeToken = atEnd ? range.end : range.beforeStart;
@@ -270,23 +273,25 @@ InsertionPointLocator::InsertionPointLocator(const CppRefactoringChanges &refact
InsertionLocation InsertionPointLocator::methodDeclarationInClass( InsertionLocation InsertionPointLocator::methodDeclarationInClass(
const QString &fileName, const QString &fileName,
const Class *clazz, const Class *clazz,
AccessSpec xsSpec) const AccessSpec xsSpec,
ForceAccessSpec forceAccessSpec) const
{ {
const Document::Ptr doc = m_refactoringChanges.file(fileName)->cppDocument(); const Document::Ptr doc = m_refactoringChanges.file(fileName)->cppDocument();
if (doc) { if (doc) {
FindInClass find(doc->translationUnit(), clazz); FindInClass find(doc->translationUnit(), clazz);
ClassSpecifierAST *classAST = find(); ClassSpecifierAST *classAST = find();
return methodDeclarationInClass(doc->translationUnit(), classAST, xsSpec); return methodDeclarationInClass(doc->translationUnit(), classAST, xsSpec, AccessSpecEnd,
forceAccessSpec);
} else { } else {
return InsertionLocation(); return InsertionLocation();
} }
} }
InsertionLocation InsertionPointLocator::methodDeclarationInClass( InsertionLocation InsertionPointLocator::methodDeclarationInClass(const TranslationUnit *tu,
const TranslationUnit *tu,
const ClassSpecifierAST *clazz, const ClassSpecifierAST *clazz,
InsertionPointLocator::AccessSpec xsSpec, InsertionPointLocator::AccessSpec xsSpec,
Position pos) const Position pos,
ForceAccessSpec forceAccessSpec) const
{ {
if (!clazz) if (!clazz)
return {}; return {};
@@ -302,7 +307,8 @@ InsertionLocation InsertionPointLocator::methodDeclarationInClass(
bool needsLeadingEmptyLine = false; bool needsLeadingEmptyLine = false;
bool needsPrefix = false; bool needsPrefix = false;
bool needsSuffix = false; bool needsSuffix = false;
findMatch(ranges, xsSpec, pos, beforeToken, needsLeadingEmptyLine, needsPrefix, needsSuffix); findMatch(ranges, xsSpec, pos, forceAccessSpec, beforeToken, needsLeadingEmptyLine,
needsPrefix, needsSuffix);
int line = 0, column = 0; int line = 0, column = 0;
if (pos == InsertionPointLocator::AccessSpecEnd) if (pos == InsertionPointLocator::AccessSpecEnd)

View File

@@ -91,17 +91,25 @@ public:
AccessSpecEnd, AccessSpecEnd,
}; };
enum class ForceAccessSpec { Yes, No };
public: public:
explicit InsertionPointLocator(const CppRefactoringChanges &refactoringChanges); explicit InsertionPointLocator(const CppRefactoringChanges &refactoringChanges);
InsertionLocation methodDeclarationInClass(const QString &fileName, InsertionLocation methodDeclarationInClass(
const CPlusPlus::Class *clazz, const QString &fileName,
AccessSpec xsSpec) const; const CPlusPlus::Class *clazz,
AccessSpec xsSpec,
ForceAccessSpec forceAccessSpec = ForceAccessSpec::No
) const;
InsertionLocation methodDeclarationInClass(const CPlusPlus::TranslationUnit *tu, InsertionLocation methodDeclarationInClass(
const CPlusPlus::ClassSpecifierAST *clazz, const CPlusPlus::TranslationUnit *tu,
AccessSpec xsSpec, const CPlusPlus::ClassSpecifierAST *clazz,
Position positionInAccessSpec = AccessSpecEnd) const; AccessSpec xsSpec,
Position positionInAccessSpec = AccessSpecEnd,
ForceAccessSpec forceAccessSpec = ForceAccessSpec::No
) const;
InsertionLocation constructorDeclarationInClass(const CPlusPlus::TranslationUnit *tu, InsertionLocation constructorDeclarationInClass(const CPlusPlus::TranslationUnit *tu,
const CPlusPlus::ClassSpecifierAST *clazz, const CPlusPlus::ClassSpecifierAST *clazz,