forked from qt-creator/qt-creator
CppEditor: Fix possible wrong location for function definitions
Having no namespace when inserting generated functions may insert explicitly at the end of a header which is not always desired as we need to take care of e.g. header guards as well. Change-Id: I3b154ae936a96f2f8e7e34cda6b5bcdfcbc83faf Reviewed-by: Christian Kandeler <christian.kandeler@qt.io> Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
committed by
Christian Kandeler
parent
d3d683ec63
commit
1e0b82e77d
@@ -109,6 +109,7 @@ private slots:
|
|||||||
void test_quickfix_GenerateGetterSetter_onlyGetter_DontPreferGetterWithGet();
|
void test_quickfix_GenerateGetterSetter_onlyGetter_DontPreferGetterWithGet();
|
||||||
void test_quickfix_GenerateGetterSetter_onlySetter();
|
void test_quickfix_GenerateGetterSetter_onlySetter();
|
||||||
void test_quickfix_GenerateGetterSetter_onlySetterHeaderFile();
|
void test_quickfix_GenerateGetterSetter_onlySetterHeaderFile();
|
||||||
|
void test_quickfix_GenerateGetterSetter_onlySetterHeaderFileWithIncludeGuard();
|
||||||
void test_quickfix_GenerateGetterSetter_offerGetterWhenSetterPresent();
|
void test_quickfix_GenerateGetterSetter_offerGetterWhenSetterPresent();
|
||||||
void test_quickfix_GenerateGetterSetter_offerSetterWhenGetterPresent();
|
void test_quickfix_GenerateGetterSetter_offerSetterWhenGetterPresent();
|
||||||
void test_quickfix_GenerateGettersSetters_data();
|
void test_quickfix_GenerateGettersSetters_data();
|
||||||
|
|||||||
@@ -2267,6 +2267,38 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_onlySetterHeaderFile()
|
|||||||
QuickFixOperationTest(testDocuments, &factory, ProjectExplorer::HeaderPaths(), 2);
|
QuickFixOperationTest(testDocuments, &factory, ProjectExplorer::HeaderPaths(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_onlySetterHeaderFileWithIncludeGuard()
|
||||||
|
{
|
||||||
|
QList<QuickFixTestDocument::Ptr> testDocuments;
|
||||||
|
const QByteArray original =
|
||||||
|
"#ifndef FILE__H__DECLARED\n"
|
||||||
|
"#define FILE__H__DECLARED\n"
|
||||||
|
"class Foo\n"
|
||||||
|
"{\n"
|
||||||
|
"public:\n"
|
||||||
|
" int bar@;\n"
|
||||||
|
"};\n"
|
||||||
|
"#endif\n";
|
||||||
|
const QByteArray expected =
|
||||||
|
"#ifndef FILE__H__DECLARED\n"
|
||||||
|
"#define FILE__H__DECLARED\n"
|
||||||
|
"class Foo\n"
|
||||||
|
"{\n"
|
||||||
|
"public:\n"
|
||||||
|
" int bar@;\n"
|
||||||
|
" void setBar(int value);\n"
|
||||||
|
"};\n\n"
|
||||||
|
"inline void Foo::setBar(int value)\n"
|
||||||
|
"{\n"
|
||||||
|
" bar = value;\n"
|
||||||
|
"}\n"
|
||||||
|
"#endif\n";
|
||||||
|
|
||||||
|
testDocuments << QuickFixTestDocument::create("file.h", original, expected);
|
||||||
|
GenerateGetterSetter factory;
|
||||||
|
QuickFixOperationTest(testDocuments, &factory, ProjectExplorer::HeaderPaths(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
class CppCodeStyleSettingsChanger {
|
class CppCodeStyleSettingsChanger {
|
||||||
public:
|
public:
|
||||||
CppCodeStyleSettingsChanger(const CppCodeStyleSettings &settings);
|
CppCodeStyleSettingsChanger(const CppCodeStyleSettings &settings);
|
||||||
|
|||||||
@@ -226,10 +226,21 @@ InsertionLocation insertLocationForMethodDefinition(Symbol *symbol, const bool u
|
|||||||
const InsertionPointLocator locator(refactoring);
|
const InsertionPointLocator locator(refactoring);
|
||||||
const QList<InsertionLocation> list
|
const QList<InsertionLocation> list
|
||||||
= locator.methodDefinition(symbol, useSymbolFinder, fileName);
|
= locator.methodDefinition(symbol, useSymbolFinder, fileName);
|
||||||
for (int i = 0; i < list.count(); ++i) {
|
const bool isHeader = ProjectFile::isHeader(ProjectFile::classify(fileName));
|
||||||
|
const bool hasIncludeGuard = isHeader
|
||||||
|
&& !file->cppDocument()->includeGuardMacroName().isEmpty();
|
||||||
|
int lastLine;
|
||||||
|
if (hasIncludeGuard) {
|
||||||
|
const TranslationUnit * const tu = file->cppDocument()->translationUnit();
|
||||||
|
tu->getTokenStartPosition(tu->ast()->lastToken(), &lastLine);
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
for ( ; i < list.count(); ++i) {
|
||||||
InsertionLocation location = list.at(i);
|
InsertionLocation location = list.at(i);
|
||||||
if (!location.isValid() || location.fileName() != fileName)
|
if (!location.isValid() || location.fileName() != fileName)
|
||||||
continue;
|
continue;
|
||||||
|
if (hasIncludeGuard && location.line() == lastLine)
|
||||||
|
continue;
|
||||||
if (!requiredNamespaces.isEmpty()) {
|
if (!requiredNamespaces.isEmpty()) {
|
||||||
NSVisitor visitor(file.data(), requiredNamespaces,
|
NSVisitor visitor(file.data(), requiredNamespaces,
|
||||||
file->position(location.line(), location.column()));
|
file->position(location.line(), location.column()));
|
||||||
|
|||||||
Reference in New Issue
Block a user