diff options
author | Christian Kandeler <christian.kandeler@qt.io> | 2020-11-12 12:12:13 +0100 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@qt.io> | 2020-11-13 12:49:51 +0000 |
commit | 3cd2e2c445e4e48622ddd26a3ac6fa28b254f873 (patch) | |
tree | 2d64e98276ac1f7fd64b062fa3eda53f98b847cd | |
parent | ccf1d17bfe842e65d353086b2826880d2b361b95 (diff) |
CppEditor: Improve "definition from declaration" for templates
- Make sure the template parameters are included in the definition.
- Find the correct insertion location when using an already-defined
template member function as an anchor.
Fixes: QTCREATORBUG-24848
Change-Id: I1272ba36403904e7aed81bcef48745793c5e2217
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
-rw-r--r-- | src/plugins/cppeditor/cppquickfix_test.cpp | 21 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppquickfixes.cpp | 1 | ||||
-rw-r--r-- | src/plugins/cpptools/insertionpointlocator.cpp | 11 |
3 files changed, 25 insertions, 8 deletions
diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 793f3113a89..5dd19adbf3b 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -3740,20 +3740,25 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_templateClass() "template<class T>\n" "class Foo\n" "{\n" - " void fun@c();\n" - "};\n"; + " void fun@c1();\n" + " void func2();\n" + "};\n\n" + "template<class T>\n" + "void Foo<T>::func2() {}\n"; QByteArray expected = "template<class T>\n" "class Foo\n" "{\n" - " void fun@c();\n" - "};\n" - "\n" + " void func1();\n" + " void func2();\n" + "};\n\n" "template<class T>\n" - "void Foo::func()\n" // Should really be Foo<T>::func() + "void Foo<T>::func1()\n" "{\n" "\n" - "}\n"; + "}\n\n" + "template<class T>\n" + "void Foo<T>::func2() {}\n"; InsertDefFromDecl factory; QuickFixOperationTest(singleDocument(original, expected), &factory); @@ -3775,7 +3780,7 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_templateFunction() "};\n" "\n" "template<class T>\n" - "void Foo::func()\n" + "void Foo::func<T>()\n" "{\n" "\n" "}\n"; diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 15bdd1e92b9..94bfef1a84e 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -3040,6 +3040,7 @@ public: oo.showReturnTypes = true; oo.showArgumentNames = true; oo.showEnclosingTemplate = true; + oo.showTemplateParameters = true; if (defPos == DefPosInsideClass) { const int targetPos = targetFile->position(loc.line(), loc.column()); diff --git a/src/plugins/cpptools/insertionpointlocator.cpp b/src/plugins/cpptools/insertionpointlocator.cpp index e0063567948..228d5344306 100644 --- a/src/plugins/cpptools/insertionpointlocator.cpp +++ b/src/plugins/cpptools/insertionpointlocator.cpp @@ -32,6 +32,7 @@ #include <coreplugin/icore.h> +#include <cplusplus/ASTPath.h> #include <cplusplus/LookupContext.h> #include <utils/qtcassert.h> @@ -545,6 +546,16 @@ static InsertionLocation nextToSurroundingDefinitions(Symbol *declaration, return noResult; targetFile->cppDocument()->translationUnit()->getTokenStartPosition(functionDefinition->firstToken(), &line, &column); + const QList<AST *> path = ASTPath(targetFile->cppDocument())(line, column); + for (auto it = path.rbegin(); it != path.rend(); ++it) { + if (const auto templateDecl = (*it)->asTemplateDeclaration()) { + if (templateDecl->declaration == functionDefinition) { + targetFile->cppDocument()->translationUnit()->getTokenStartPosition( + templateDecl->firstToken(), &line, &column); + } + break; + } + } } return InsertionLocation(QString::fromUtf8(definitionFunction->fileName()), prefix, suffix, line, column); |