diff options
author | Nikolai Kosjar <nikolai.kosjar@digia.com> | 2013-06-17 16:09:47 +0200 |
---|---|---|
committer | Nikolai Kosjar <nikolai.kosjar@digia.com> | 2013-06-25 09:57:23 +0200 |
commit | 021dbc2e96f7ad18c8ec4ae76f753e6223ff871f (patch) | |
tree | b2e44e9ec22f26ce5cc910616f5730701626689a /src/plugins/cppeditor | |
parent | c59ba462adff332fee2cb333cbb9a87972288011 (diff) | |
download | qt-creator-021dbc2e96f7ad18c8ec4ae76f753e6223ff871f.tar.gz |
CppEditor: Respect whitespace in operator names for more quick fixes
* Affected quick fixes: InsertDefFromDecl, MoveFuncDefOutside
* Fix also reformating pointer declaration of operator functions for
qualified name ids
Change-Id: I6a7578f496221557d103f5fdbb5dacc9540ee779
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
Diffstat (limited to 'src/plugins/cppeditor')
-rw-r--r-- | src/plugins/cppeditor/cppeditorplugin.h | 4 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppquickfix_test.cpp | 98 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppquickfixes.cpp | 58 |
3 files changed, 145 insertions, 15 deletions
diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h index f86ff59808..356677038a 100644 --- a/src/plugins/cppeditor/cppeditorplugin.h +++ b/src/plugins/cppeditor/cppeditorplugin.h @@ -163,6 +163,8 @@ private slots: void test_quickfix_InsertDefFromDecl_notTriggeringWhenDefinitionExists(); void test_quickfix_InsertDefFromDecl_notTriggeringStatement(); void test_quickfix_InsertDefFromDecl_findRightImplementationFile(); + void test_quickfix_InsertDefFromDecl_respectWsInOperatorNames1(); + void test_quickfix_InsertDefFromDecl_respectWsInOperatorNames2(); void test_quickfix_InsertDeclFromDef(); @@ -202,6 +204,8 @@ private slots: void test_quickfix_MoveFuncDefOutside_CtorWithInitialization1(); void test_quickfix_MoveFuncDefOutside_CtorWithInitialization2(); void test_quickfix_MoveFuncDefOutside_afterClass(); + void test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames1(); + void test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames2(); void test_quickfix_MoveFuncDefToDecl_MemberFunc(); void test_quickfix_MoveFuncDefToDecl_MemberFuncOutside(); diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 5969e36d9b..0bd5554f22 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -1178,6 +1178,58 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_findRightImplementationFil data.run(&factory); } +/// Check if whitespace is respected for operator functions +void CppEditorPlugin::test_quickfix_InsertDefFromDecl_respectWsInOperatorNames1() +{ + QByteArray original = + "class Foo\n" + "{\n" + " Foo &opera@tor =();\n" + "};\n"; + QByteArray expected = + "class Foo\n" + "{\n" + " Foo &operator =();\n" + "};\n" + "\n" + "\n" + "Foo &Foo::operator =()\n" + "{\n" + "\n" + "}\n" + "\n"; + + InsertDefFromDecl factory; + TestCase data(original, expected); + data.run(&factory); +} + +/// Check if whitespace is respected for operator functions +void CppEditorPlugin::test_quickfix_InsertDefFromDecl_respectWsInOperatorNames2() +{ + QByteArray original = + "class Foo\n" + "{\n" + " Foo &opera@tor=();\n" + "};\n"; + QByteArray expected = + "class Foo\n" + "{\n" + " Foo &operator=();\n" + "};\n" + "\n" + "\n" + "Foo &Foo::operator=()\n" + "{\n" + "\n" + "}\n" + "\n"; + + InsertDefFromDecl factory; + TestCase data(original, expected); + data.run(&factory); +} + // Function for one of InsertDeclDef section cases void insertToSectionDeclFromDef(const QByteArray §ion, int sectionIndex) { @@ -2458,6 +2510,52 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_afterClass() data.run(&factory, 1); } +/// Check if whitespace is respected for operator functions +void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames1() +{ + QByteArray original = + "class Foo\n" + "{\n" + " Foo &opera@tor =() {}\n" + "};\n"; + QByteArray expected = + "class Foo\n" + "{\n" + " Foo &operator =();\n" + "};\n" + "\n" + "\n" + "Foo &Foo::operator =() {}\n" + "\n"; + + MoveFuncDefOutside factory; + TestCase data(original, expected); + data.run(&factory); +} + +/// Check if whitespace is respected for operator functions +void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames2() +{ + QByteArray original = + "class Foo\n" + "{\n" + " Foo &opera@tor=() {}\n" + "};\n"; + QByteArray expected = + "class Foo\n" + "{\n" + " Foo &operator=();\n" + "};\n" + "\n" + "\n" + "Foo &Foo::operator=() {}\n" + "\n"; + + MoveFuncDefOutside factory; + TestCase data(original, expected); + data.run(&factory); +} + /// Check: revert test_quickfix_MoveFuncDefOutside_MemberFuncToCpp() void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFunc() { diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 19e4665d80..14d8db4ed9 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -258,6 +258,12 @@ void insertNewIncludeDirective(const QString &include, CppRefactoringFilePtr fil file->apply(); } +bool nameIncludesOperatorName(const Name *name) +{ + return name->isOperatorNameId() + || (name->isQualifiedNameId() && name->asQualifiedNameId()->name()->isOperatorNameId()); +} + } // anonymous namespace namespace { @@ -2482,10 +2488,12 @@ class InsertDefOperation: public CppQuickFixOperation public: // Make sure that either loc is valid or targetFileName is not empty. InsertDefOperation(const QSharedPointer<const CppQuickFixAssistInterface> &interface, - Declaration *decl, const InsertionLocation &loc, const DefPos defpos, - const QString &targetFileName = QString(), bool freeFunction = false) + Declaration *decl, DeclaratorAST *declAST, const InsertionLocation &loc, + const DefPos defpos, const QString &targetFileName = QString(), + bool freeFunction = false) : CppQuickFixOperation(interface, 0) , m_decl(decl) + , m_declAST(declAST) , m_loc(loc) , m_defpos(defpos) , m_targetFileName(targetFileName) @@ -2558,6 +2566,11 @@ public: const FullySpecifiedType tn = rewriteType(m_decl->type(), &env, control); // rewrite the function name + if (nameIncludesOperatorName(m_decl->name())) { + CppRefactoringFilePtr file = refactoring.file(fileName()); + const QString operatorNameText = file->textOf(m_declAST->core_declarator); + oo.includeWhiteSpaceInOperatorName = operatorNameText.contains(QLatin1Char(' ')); + } const QString name = oo.prettyName(LookupContext::minimalName(m_decl, targetCoN, control)); @@ -2590,6 +2603,7 @@ public: private: Declaration *m_decl; + DeclaratorAST *m_declAST; InsertionLocation m_loc; const DefPos m_defpos; const QString m_targetFileName; @@ -2622,6 +2636,7 @@ void InsertDefFromDecl::match(const CppQuickFixInterface &interface, QuickFixOpe } // Insert Position: Implementation File + DeclaratorAST *declAST = simpleDecl->declarator_list->value; InsertDefOperation *op = 0; ProjectFile::Kind kind = ProjectFile::classify(interface->fileName()); const bool isHeaderFile = ProjectFile::isHeader(kind); @@ -2634,7 +2649,7 @@ void InsertDefFromDecl::match(const CppQuickFixInterface &interface, QuickFixOpe foreach (const InsertionLocation &location, locator.methodDefinition(decl, false, QString())) { if (location.isValid()) { - op = new InsertDefOperation(interface, decl, + op = new InsertDefOperation(interface, decl, declAST, InsertionLocation(), DefPosImplementationFile, location.fileName()); @@ -2649,8 +2664,8 @@ void InsertDefFromDecl::match(const CppQuickFixInterface &interface, QuickFixOpe // Insert Position: Outside Class if (!isFreeFunction) { - op = new InsertDefOperation(interface, decl, InsertionLocation(), - DefPosOutsideClass, + op = new InsertDefOperation(interface, decl, declAST, + InsertionLocation(), DefPosOutsideClass, interface->fileName()); result.append(CppQuickFixOperation::Ptr(op)); } @@ -2663,8 +2678,9 @@ void InsertDefFromDecl::match(const CppQuickFixInterface &interface, QuickFixOpe const InsertionLocation loc = InsertionLocation(interface->fileName(), QString(), QString(), line, column); - op = new InsertDefOperation(interface, decl, loc, DefPosInsideClass, - QString() , isFreeFunction); + op = new InsertDefOperation(interface, decl, declAST, loc, + DefPosInsideClass, QString(), + isFreeFunction); result.append(CppQuickFixOperation::Ptr(op)); return; @@ -3719,14 +3735,19 @@ void ApplyDeclDefLinkChanges::match(const CppQuickFixInterface &interface, namespace { -QString getDefinitionSignature(const CppQuickFixAssistInterface *assist, Function *func, - CppRefactoringFilePtr &file, Scope *scope) +QString definitionSignature(const CppQuickFixAssistInterface *assist, + FunctionDefinitionAST *functionDefinitionAST, + CppRefactoringFilePtr &baseFile, + CppRefactoringFilePtr &targetFile, + Scope *scope) { QTC_ASSERT(assist, return QString()); - QTC_ASSERT(func, return QString()); + QTC_ASSERT(functionDefinitionAST, return QString()); QTC_ASSERT(scope, return QString()); + Function *func = functionDefinitionAST->symbol; + QTC_ASSERT(func, return QString()); - LookupContext cppContext(file->cppDocument(), assist->snapshot()); + LookupContext cppContext(targetFile->cppDocument(), assist->snapshot()); ClassOrNamespace *cppCoN = cppContext.lookupType(scope); if (!cppCoN) cppCoN = cppContext.globalNamespace(); @@ -3740,10 +3761,16 @@ QString getDefinitionSignature(const CppQuickFixAssistInterface *assist, Functio oo.showFunctionSignatures = true; oo.showReturnTypes = true; oo.showArgumentNames = true; + const Name *name = func->name(); + if (nameIncludesOperatorName(name)) { + CoreDeclaratorAST *coreDeclarator = functionDefinitionAST->declarator->core_declarator; + const QString operatorNameText = baseFile->textOf(coreDeclarator); + oo.includeWhiteSpaceInOperatorName = operatorNameText.contains(QLatin1Char(' ')); + } + const QString nameText = oo.prettyName(LookupContext::minimalName(func, cppCoN, control)); const FullySpecifiedType tn = rewriteType(func->type(), &env, control); - const QString name = oo.prettyName(LookupContext::minimalName(func, cppCoN, control)); - return oo.prettyType(tn, name); + return oo.prettyType(tn, nameText); } class MoveFuncDefOutsideOp : public CppQuickFixOperation @@ -3791,8 +3818,9 @@ public: Scope *scopeAtInsertPos = toFile->cppDocument()->scopeAt(l.line(), l.column()); // construct definition - const QString funcDec = getDefinitionSignature(assistInterface(), m_func, toFile, - scopeAtInsertPos); + const QString funcDec = definitionSignature(assistInterface(), m_funcDef, + fromFile, toFile, + scopeAtInsertPos); QString funcDef = prefix + funcDec; const int startPosition = fromFile->endOf(m_funcDef->declarator); const int endPosition = fromFile->endOf(m_funcDef->function_body); |