diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractFunctionTemplates.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractFunctionTemplates.rts index 76a9813b3e2..d991d695471 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractFunctionTemplates.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractFunctionTemplates.rts @@ -17,8 +17,7 @@ int tempFunct(){ //= void test(){ } -template -void exp(T i) +template void exp(T i) { i++; i += 3; @@ -29,7 +28,7 @@ int tempFunct(){ T i; i = 0; - exp(i); + exp(i); return 0; } @@ -50,8 +49,7 @@ int tempFunct(T p){ //= void test(){ } -template -void exp(T p) +template void exp(T p) { ++p; p + 4; @@ -59,8 +57,7 @@ void exp(T p) template int tempFunct(T p){ - - exp(p); + exp(p); return 0; } @@ -82,8 +79,7 @@ int tempFunct(){ //= void test(){ } -template -T exp() +template T exp() { T p; p = 0; @@ -93,8 +89,7 @@ T exp() template int tempFunct(){ - - T p = exp(); + T p = exp(); p + 2; return 0; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java index c45ce101a43..0c23e1c0860 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java @@ -66,6 +66,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTOperatorName; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; @@ -85,6 +86,7 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReturnStatement; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclaration; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateDeclaration; import org.eclipse.cdt.internal.ui.refactoring.AddDeclarationNodeToClassChange; import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; @@ -328,7 +330,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { IASTNode node = firstNode; boolean found = false; - boolean templateFunction = false; while (node != null && !found) { node = node.getParent(); @@ -338,7 +339,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { } if (found && node != null) { - templateFunction = node.getParent() instanceof ICPPASTTemplateDeclaration; String title; if (context.getType() == MethodContext.ContextType.METHOD) { @@ -347,18 +347,8 @@ public class ExtractFunctionRefactoring extends CRefactoring { title = Messages.ExtractFunctionRefactoring_CreateFunctionDef; } - ASTRewrite rewriter = collector.rewriterForTranslationUnit(node - .getTranslationUnit()); - getMethod(astMethodName, context, rewriter, node, - new TextEditGroup(title)); - - if (templateFunction) { - // egtodo -// methodContent = getTemplateDeclarationString( -// (ICPPASTTemplateDeclaration) node.getParent(), -// getTemplateParameterNames()) -// + methodContent; - } + ASTRewrite rewriter = collector.rewriterForTranslationUnit(node.getTranslationUnit()); + addMethod(astMethodName, context, rewriter, node, new TextEditGroup(title)); } } @@ -373,75 +363,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { .getVisibility(), methodDeclaration, false, collector); } -// egtodo -// private Set getTemplateParameterNames() { -// Set names = new TreeSet(new Comparator() { -// -// public int compare(IASTName o1, IASTName o2) { -// return o1.toString().compareTo(o2.toString()); -// } -// }); -// for (NameInformation nameInfo : container.getNames()) { -// IASTName declName = nameInfo.getDeclaration(); -// IBinding binding = declName.resolveBinding(); -// if (binding instanceof CPPVariable) { -// CPPVariable cppVariable = (CPPVariable) binding; -// IASTNode defNode = cppVariable.getDefinition(); -// if (defNode.getParent().getParent() instanceof IASTSimpleDeclaration) { -// IASTSimpleDeclaration decl = (IASTSimpleDeclaration) defNode -// .getParent().getParent(); -// if (decl.getDeclSpecifier() instanceof CPPASTNamedTypeSpecifier) { -// CPPASTNamedTypeSpecifier namedSpecifier = (CPPASTNamedTypeSpecifier) decl -// .getDeclSpecifier(); -// if (namedSpecifier.getName().resolveBinding() instanceof CPPTemplateTypeParameter) { -// names.add(namedSpecifier.getName()); -// } -// } -// } -// -// } else if (binding instanceof CPPParameter) { -// CPPParameter parameter = (CPPParameter) binding; -// IASTNode decNode = parameter.getDeclarations()[0]; -// if (decNode.getParent().getParent() instanceof ICPPASTParameterDeclaration) { -// ICPPASTParameterDeclaration paraDecl = (ICPPASTParameterDeclaration) decNode -// .getParent().getParent(); -// if (paraDecl.getDeclSpecifier() instanceof ICPPASTNamedTypeSpecifier) { -// ICPPASTNamedTypeSpecifier namedDeclSpec = (ICPPASTNamedTypeSpecifier) paraDecl -// .getDeclSpecifier(); -// if (namedDeclSpec.getName().resolveBinding() instanceof ICPPTemplateTypeParameter) { -// names.add(namedDeclSpec.getName()); -// } -// } -// } -// } -// } -// return names; -// } -// -// private String getTemplateDeclarationString( -// ICPPASTTemplateDeclaration templateDeclaration, Set names) { -// if (names.isEmpty()) { -// return EMPTY_STRING; -// } else { -// StringBuffer buf = new StringBuffer(); -// -// buf.append(TEMPLATE_START); -// for (Iterator it = names.iterator(); it.hasNext();) { -// IASTName name = it.next(); -// buf.append(TYPENAME); -// buf.append(name.toString()); -// -// if (it.hasNext()) { -// buf.append(COMMA_SPACE); -// } -// } -// buf.append('>'); -// buf.append(CRefactoring.NEWLINE); -// -// return buf.toString(); -// } -// } - private boolean isMethodAllreadyDefined( IASTSimpleDeclaration methodDeclaration, ICPPASTCompositeTypeSpecifier classDeclaration) { @@ -688,8 +609,9 @@ public class ExtractFunctionRefactoring extends CRefactoring { return same.getObject().booleanValue(); } - private void getMethod(IASTName astMethodName, MethodContext context, + private void addMethod(IASTName astMethodName, MethodContext context, ASTRewrite rewriter, IASTNode insertpoint, TextEditGroup group) { + ICPPASTQualifiedName qname = new CPPASTQualifiedName(); if (context.getType() == ContextType.METHOD) { for (int i = 0; i < (context.getMethodQName().getNames().length - 1); i++) { @@ -708,13 +630,29 @@ public class ExtractFunctionRefactoring extends CRefactoring { IASTCompoundStatement compound = new CPPASTCompoundStatement(); func.setBody(compound); + + + if(insertpoint.getParent() instanceof ICPPASTTemplateDeclaration) { + + CPPASTTemplateDeclaration templateDeclaration = new CPPASTTemplateDeclaration(); + templateDeclaration.setParent(unit); + + for(ICPPASTTemplateParameter templateParameter : ((ICPPASTTemplateDeclaration) insertpoint.getParent()).getTemplateParameters()) { + templateDeclaration.addTemplateParamter(templateParameter); + } + + templateDeclaration.setDeclaration(func); - ASTRewrite insertRW = rewriter.insertBefore(insertpoint.getParent(), - insertpoint, func, group); - insertRW = rewriter; + rewriter.insertBefore(insertpoint.getParent().getParent(), insertpoint.getParent(), templateDeclaration, group); + + } else { + + rewriter.insertBefore(insertpoint.getParent(), insertpoint, func, group); + } + extractedFunctionConstructionHelper.constructMethodBody(compound, - container.getNodesToWrite(), insertRW, group); + container.getNodesToWrite(), rewriter, group); // Set return value if (info.getReturnVariable() != null) { @@ -733,7 +671,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { } returnStmt.setReturnValue(expr); } - insertRW.insertBefore(compound, null, returnStmt, group); + rewriter.insertBefore(compound, null, returnStmt, group); } }