mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 01:36:01 +02:00
Proper handling of visibility for extracted inline functions.
This commit is contained in:
parent
c73b8c8a4a
commit
73337a5882
4 changed files with 176 additions and 98 deletions
|
@ -1,12 +1,12 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2007, 2011 Institute for Software, HSR Hochschule fuer Technik
|
* Copyright (c) 2007, 2011 Institute for Software, HSR Hochschule fuer Technik
|
||||||
* Rapperswil, University of applied sciences and others
|
* Rapperswil, University of applied sciences and others
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Institute for Software - initial API and implementation
|
* Institute for Software - initial API and implementation
|
||||||
* Sergey Prigogin (Google)
|
* Sergey Prigogin (Google)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
@ -42,13 +42,54 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTVisibilityLabel;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum;
|
import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a declaration to an existing class via the ModificationCollector. Automatically determines
|
* Adds a declaration to an existing class via the ModificationCollector. Automatically determines
|
||||||
* an appropriate insertion point for the desired visibility.
|
* an appropriate insertion point for the desired visibility.
|
||||||
*
|
*
|
||||||
* @author Mirko Stocker
|
* @author Mirko Stocker
|
||||||
*/
|
*/
|
||||||
public class ClassMemberInserter {
|
public class ClassMemberInserter {
|
||||||
|
public static class InsertionInfo {
|
||||||
|
private final IASTNode parentNode;
|
||||||
|
/**
|
||||||
|
* The node before which the new node should be inserted. A null value indicates insertion
|
||||||
|
* to the end of parentNode
|
||||||
|
*/
|
||||||
|
private IASTNode insertBeforeNode; //
|
||||||
|
/** Visibility label to insert before the new node or null. */
|
||||||
|
private ICPPASTVisibilityLabel prologue;
|
||||||
|
/** Visibility label to insert after the new node or null. */
|
||||||
|
private ICPPASTVisibilityLabel epilogue;
|
||||||
|
|
||||||
|
public InsertionInfo(IASTNode parentNode, IASTNode insertBeforeNode) {
|
||||||
|
this.parentNode = parentNode;
|
||||||
|
this.insertBeforeNode = insertBeforeNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InsertionInfo(IASTNode parentNode) {
|
||||||
|
this(parentNode, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IASTNode getParentNode() {
|
||||||
|
return parentNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IASTNode getInsertBeforeNode() {
|
||||||
|
return insertBeforeNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICPPASTVisibilityLabel getPrologue() {
|
||||||
|
return prologue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICPPASTVisibilityLabel getEpilogue() {
|
||||||
|
return epilogue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not instantiatable. All methods are static.
|
||||||
|
private ClassMemberInserter() {
|
||||||
|
}
|
||||||
|
|
||||||
public static void createChange(ICPPASTCompositeTypeSpecifier classNode,
|
public static void createChange(ICPPASTCompositeTypeSpecifier classNode,
|
||||||
VisibilityEnum visibility, IASTNode nodeToAdd, boolean isField,
|
VisibilityEnum visibility, IASTNode nodeToAdd, boolean isField,
|
||||||
ModificationCollector collector) {
|
ModificationCollector collector) {
|
||||||
|
@ -58,7 +99,23 @@ public class ClassMemberInserter {
|
||||||
public static void createChange(ICPPASTCompositeTypeSpecifier classNode,
|
public static void createChange(ICPPASTCompositeTypeSpecifier classNode,
|
||||||
VisibilityEnum visibility, List<IASTNode> nodesToAdd, boolean isField,
|
VisibilityEnum visibility, List<IASTNode> nodesToAdd, boolean isField,
|
||||||
ModificationCollector collector) {
|
ModificationCollector collector) {
|
||||||
|
InsertionInfo info = findInsertionPoint(classNode, visibility, isField);
|
||||||
nodesToAdd = new ArrayList<IASTNode>(nodesToAdd);
|
nodesToAdd = new ArrayList<IASTNode>(nodesToAdd);
|
||||||
|
if (info.getPrologue() != null)
|
||||||
|
nodesToAdd.add(0, info.getPrologue());
|
||||||
|
if (info.getEpilogue() != null)
|
||||||
|
nodesToAdd.add(info.getEpilogue());
|
||||||
|
|
||||||
|
ASTRewrite rewrite = collector.rewriterForTranslationUnit(classNode.getTranslationUnit());
|
||||||
|
for (IASTNode node : nodesToAdd) {
|
||||||
|
rewrite.insertBefore(info.getParentNode(), info.getInsertBeforeNode(), node,
|
||||||
|
createEditDescription(classNode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InsertionInfo findInsertionPoint(ICPPASTCompositeTypeSpecifier classNode,
|
||||||
|
VisibilityEnum visibility, boolean isField) {
|
||||||
|
InsertionInfo info = new InsertionInfo(classNode);
|
||||||
VisibilityEnum defaultVisibility = classNode.getKey() == IASTCompositeTypeSpecifier.k_struct ?
|
VisibilityEnum defaultVisibility = classNode.getKey() == IASTCompositeTypeSpecifier.k_struct ?
|
||||||
VisibilityEnum.v_public : VisibilityEnum.v_private;
|
VisibilityEnum.v_public : VisibilityEnum.v_private;
|
||||||
VisibilityEnum currentVisibility = defaultVisibility;
|
VisibilityEnum currentVisibility = defaultVisibility;
|
||||||
|
@ -69,13 +126,13 @@ public class ClassMemberInserter {
|
||||||
int lastMatchingVisibilityIndex = -1;
|
int lastMatchingVisibilityIndex = -1;
|
||||||
int lastPrecedingVisibilityIndex = -1;
|
int lastPrecedingVisibilityIndex = -1;
|
||||||
IASTDeclaration[] members = classNode.getMembers();
|
IASTDeclaration[] members = classNode.getMembers();
|
||||||
|
|
||||||
// Find the insert location by iterating over the elements of the class
|
// Find the insert location by iterating over the elements of the class
|
||||||
// and remembering the last element with the matching visibility and the last element
|
// and remembering the last element with the matching visibility and the last element
|
||||||
// with preceding visibility (according to the visibility order preference).
|
// with preceding visibility (according to the visibility order preference).
|
||||||
for (int i = 0; i < members.length; i++) {
|
for (int i = 0; i < members.length; i++) {
|
||||||
IASTDeclaration declaration = members[i];
|
IASTDeclaration declaration = members[i];
|
||||||
|
|
||||||
if (declaration instanceof ICPPASTVisibilityLabel) {
|
if (declaration instanceof ICPPASTVisibilityLabel) {
|
||||||
currentVisibility = VisibilityEnum.from((ICPPASTVisibilityLabel) declaration);
|
currentVisibility = VisibilityEnum.from((ICPPASTVisibilityLabel) declaration);
|
||||||
}
|
}
|
||||||
|
@ -105,24 +162,19 @@ public class ClassMemberInserter {
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
index = lastPrecedingVisibilityIndex;
|
index = lastPrecedingVisibilityIndex;
|
||||||
index++;
|
index++;
|
||||||
IASTNode nextNode = index < members.length ? members[index] : null;
|
if (index < members.length)
|
||||||
|
info.insertBeforeNode = members[index];
|
||||||
|
|
||||||
if (lastMatchingVisibilityIndex < 0 &&
|
if (lastMatchingVisibilityIndex < 0 &&
|
||||||
!(index == 0 && classNode.getKey() == IASTCompositeTypeSpecifier.k_struct && visibility == defaultVisibility)) {
|
!(index == 0 && classNode.getKey() == IASTCompositeTypeSpecifier.k_struct &&
|
||||||
nodesToAdd.add(0, new CPPASTVisibilityLabel(visibility.getVisibilityLabelValue()));
|
visibility == defaultVisibility)) {
|
||||||
if (index == 0 && nextNode != null && !(nextNode instanceof ICPPASTVisibilityLabel)) {
|
info.prologue = new CPPASTVisibilityLabel(visibility.getVisibilityLabelValue());
|
||||||
nodesToAdd.add(new CPPASTVisibilityLabel(defaultVisibility.getVisibilityLabelValue()));
|
if (index == 0 && info.insertBeforeNode != null &&
|
||||||
|
!(info.insertBeforeNode instanceof ICPPASTVisibilityLabel)) {
|
||||||
|
info.epilogue = new CPPASTVisibilityLabel(defaultVisibility.getVisibilityLabelValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return info;
|
||||||
ASTRewrite rewrite = collector.rewriterForTranslationUnit(classNode.getTranslationUnit());
|
|
||||||
for (IASTNode node : nodesToAdd) {
|
|
||||||
rewrite.insertBefore(classNode, nextNode, node, createEditDescription(classNode));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not instantiatable. All methods are static.
|
|
||||||
private ClassMemberInserter() {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TextEditGroup createEditDescription(ICPPASTCompositeTypeSpecifier classNode) {
|
private static TextEditGroup createEditDescription(ICPPASTCompositeTypeSpecifier classNode) {
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2008, 2010 Institute for Software, HSR Hochschule fuer Technik
|
* Copyright (c) 2008, 2010 Institute for Software, HSR Hochschule fuer Technik
|
||||||
* Rapperswil, University of applied sciences and others
|
* Rapperswil, University of applied sciences and others
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Institute for Software - initial API and implementation
|
* Institute for Software - initial API and implementation
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.refactoring;
|
package org.eclipse.cdt.internal.ui.refactoring;
|
||||||
|
@ -26,8 +26,8 @@ import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a function or method and adds some useful helper methods to
|
* Represents a function or method and adds some useful helper methods to determine
|
||||||
* determine if methods are in the same class.
|
* if two methods are in the same class.
|
||||||
*/
|
*/
|
||||||
public class MethodContext {
|
public class MethodContext {
|
||||||
public enum ContextType { NONE, FUNCTION, METHOD }
|
public enum ContextType { NONE, FUNCTION, METHOD }
|
||||||
|
@ -55,7 +55,7 @@ public class MethodContext {
|
||||||
public IASTName getMethodDeclarationName() {
|
public IASTName getMethodDeclarationName() {
|
||||||
return declarationName;
|
return declarationName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IASTDeclaration getMethodDeclaration() {
|
public IASTDeclaration getMethodDeclaration() {
|
||||||
IASTNode parent = declarationName.getParent().getParent();
|
IASTNode parent = declarationName.getParent().getParent();
|
||||||
if (parent instanceof IASTDeclaration) {
|
if (parent instanceof IASTDeclaration) {
|
||||||
|
@ -63,7 +63,7 @@ public class MethodContext {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Visibility getMethodDeclarationVisibility() {
|
public Visibility getMethodDeclarationVisibility() {
|
||||||
return Visibility.getVisibility(declarationName);
|
return Visibility.getVisibility(declarationName);
|
||||||
}
|
}
|
||||||
|
@ -71,17 +71,17 @@ public class MethodContext {
|
||||||
public void setMethodQName(ICPPASTQualifiedName qname) {
|
public void setMethodQName(ICPPASTQualifiedName qname) {
|
||||||
this.qname = qname;
|
this.qname = qname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICPPASTQualifiedName getMethodQName() {
|
public ICPPASTQualifiedName getMethodQName() {
|
||||||
return qname;
|
return qname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSameClass(ICPPASTQualifiedName qname1, ICPPASTQualifiedName qname2) {
|
public static boolean isSameClass(ICPPASTQualifiedName qname1, ICPPASTQualifiedName qname2) {
|
||||||
ICPPClassType bind1 = getClassBinding(qname1);
|
ICPPClassType bind1 = getClassBinding(qname1);
|
||||||
ICPPClassType bind2 = getClassBinding(qname2);
|
ICPPClassType bind2 = getClassBinding(qname2);
|
||||||
return bind1.equals(bind2);
|
return bind1.equals(bind2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSameOrSubClass(MethodContext context1, MethodContext contextOfSameOrSubclass) {
|
public static boolean isSameOrSubClass(MethodContext context1, MethodContext contextOfSameOrSubclass) {
|
||||||
ICPPInternalBinding bind1 = getICPPInternalBinding(context1);
|
ICPPInternalBinding bind1 = getICPPInternalBinding(context1);
|
||||||
ICPPInternalBinding subclassBind = getICPPInternalBinding(contextOfSameOrSubclass);
|
ICPPInternalBinding subclassBind = getICPPInternalBinding(contextOfSameOrSubclass);
|
||||||
|
@ -90,7 +90,7 @@ public class MethodContext {
|
||||||
}
|
}
|
||||||
return isSubclass(bind1,subclassBind);
|
return isSubclass(bind1,subclassBind);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isSubclass(ICPPInternalBinding bind1, ICPPInternalBinding subclassBind) {
|
private static boolean isSubclass(ICPPInternalBinding bind1, ICPPInternalBinding subclassBind) {
|
||||||
if (subclassBind instanceof ICPPClassType) {
|
if (subclassBind instanceof ICPPClassType) {
|
||||||
ICPPClassType classType = (ICPPClassType) subclassBind;
|
ICPPClassType classType = (ICPPClassType) subclassBind;
|
||||||
|
@ -110,7 +110,7 @@ public class MethodContext {
|
||||||
ICPPInternalBinding bind2 = getICPPInternalBinding(context2);
|
ICPPInternalBinding bind2 = getICPPInternalBinding(context2);
|
||||||
return isSameClass(bind1,bind2);
|
return isSameClass(bind1,bind2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isSameClass(ICPPBase base, ICPPInternalBinding bind2) {
|
private static boolean isSameClass(ICPPBase base, ICPPInternalBinding bind2) {
|
||||||
try {
|
try {
|
||||||
IBinding bind1 = base.getBaseClass();
|
IBinding bind1 = base.getBaseClass();
|
||||||
|
@ -118,12 +118,12 @@ public class MethodContext {
|
||||||
if (scope1 == null)
|
if (scope1 == null)
|
||||||
return false;
|
return false;
|
||||||
IASTNode node1 = ASTInternal.getPhysicalNodeOfScope(scope1);
|
IASTNode node1 = ASTInternal.getPhysicalNodeOfScope(scope1);
|
||||||
|
|
||||||
IScope scope2 = bind2.getScope();
|
IScope scope2 = bind2.getScope();
|
||||||
if (scope2 == null)
|
if (scope2 == null)
|
||||||
return false;
|
return false;
|
||||||
IASTNode node2 = ASTInternal.getPhysicalNodeOfScope(scope2);
|
IASTNode node2 = ASTInternal.getPhysicalNodeOfScope(scope2);
|
||||||
|
|
||||||
if (node1.equals(node2)) {
|
if (node1.equals(node2)) {
|
||||||
if (bind1 instanceof ICPPInternalBinding) {
|
if (bind1 instanceof ICPPInternalBinding) {
|
||||||
ICPPInternalBinding bind1int = (ICPPInternalBinding) bind1;
|
ICPPInternalBinding bind1int = (ICPPInternalBinding) bind1;
|
||||||
|
@ -134,30 +134,30 @@ public class MethodContext {
|
||||||
return false;
|
return false;
|
||||||
} catch (DOMException e) {
|
} catch (DOMException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isSameClass(ICPPInternalBinding bind1, ICPPInternalBinding bind2) {
|
private static boolean isSameClass(ICPPInternalBinding bind1, ICPPInternalBinding bind2) {
|
||||||
try {
|
try {
|
||||||
IScope scope1 = bind1.getScope();
|
IScope scope1 = bind1.getScope();
|
||||||
if (scope1 == null)
|
if (scope1 == null)
|
||||||
return false;
|
return false;
|
||||||
IASTNode node1 = ASTInternal.getPhysicalNodeOfScope(scope1);
|
IASTNode node1 = ASTInternal.getPhysicalNodeOfScope(scope1);
|
||||||
|
|
||||||
IScope scope2 = bind2.getScope();
|
IScope scope2 = bind2.getScope();
|
||||||
if (scope2 == null)
|
if (scope2 == null)
|
||||||
return false;
|
return false;
|
||||||
IASTNode node2 = ASTInternal.getPhysicalNodeOfScope(scope2);
|
IASTNode node2 = ASTInternal.getPhysicalNodeOfScope(scope2);
|
||||||
|
|
||||||
if (node1.equals(node2)) {
|
if (node1.equals(node2)) {
|
||||||
return bind1.getDefinition().equals(bind2.getDefinition());
|
return bind1.getDefinition().equals(bind2.getDefinition());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} catch (DOMException e) {
|
} catch (DOMException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ICPPInternalBinding getICPPInternalBinding(MethodContext context) {
|
public static ICPPInternalBinding getICPPInternalBinding(MethodContext context) {
|
||||||
IASTName decl = context.getMethodDeclarationName();
|
IASTName decl = context.getMethodDeclarationName();
|
||||||
IASTNode node = decl;
|
IASTNode node = decl;
|
||||||
|
@ -169,17 +169,17 @@ public class MethodContext {
|
||||||
}
|
}
|
||||||
node = node.getParent();
|
node = node.getParent();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInline() {
|
public boolean isInline() {
|
||||||
return qname == null;
|
return qname == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ICPPClassType getClassBinding(ICPPASTQualifiedName qname) {
|
private static ICPPClassType getClassBinding(ICPPASTQualifiedName qname) {
|
||||||
IASTName classname = qname.getNames()[qname.getNames().length - 2];
|
IASTName classname = qname.getNames()[qname.getNames().length - 2];
|
||||||
ICPPClassType bind = (ICPPClassType)classname.resolveBinding();
|
ICPPClassType bind = (ICPPClassType)classname.resolveBinding();
|
||||||
return bind;
|
return bind;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
|
* Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
|
||||||
* Rapperswil, University of applied sciences and others
|
* Rapperswil, University of applied sciences and others
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Institute for Software - initial API and implementation
|
* Institute for Software - initial API and implementation
|
||||||
* Sergey Prigogin (Google)
|
* Sergey Prigogin (Google)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
@ -31,15 +31,15 @@ public class ExtractFunctionInformation {
|
||||||
private String methodName;
|
private String methodName;
|
||||||
private boolean replaceDuplicates;
|
private boolean replaceDuplicates;
|
||||||
private List<NameInformation> parameters;
|
private List<NameInformation> parameters;
|
||||||
private NameInformation mandatoryReturnVariable;
|
private NameInformation mandatoryReturnVariable;
|
||||||
private ICPPASTFunctionDeclarator declarator;
|
private ICPPASTFunctionDeclarator declarator;
|
||||||
private MethodContext context;
|
private MethodContext context;
|
||||||
private boolean isExtractExpression;
|
private boolean isExtractExpression;
|
||||||
private boolean virtual;
|
private boolean virtual;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the function declarator of the method / function from were the statements
|
* Returns the function declarator of the method or function from were the statements
|
||||||
* are extacted from.
|
* are extracted from.
|
||||||
* @return the function declarator or null
|
* @return the function declarator or null
|
||||||
*/
|
*/
|
||||||
public ICPPASTFunctionDeclarator getDeclarator() {
|
public ICPPASTFunctionDeclarator getDeclarator() {
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
|
* Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
|
||||||
* Rapperswil, University of applied sciences and others
|
* Rapperswil, University of applied sciences and others
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Institute for Software - initial API and implementation
|
* Institute for Software - initial API and implementation
|
||||||
* Sergey Prigogin (Google)
|
* Sergey Prigogin (Google)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
@ -108,6 +108,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
|
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.CRefactoringDescriptor;
|
import org.eclipse.cdt.internal.ui.refactoring.CRefactoringDescriptor;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.ClassMemberInserter;
|
import org.eclipse.cdt.internal.ui.refactoring.ClassMemberInserter;
|
||||||
|
import org.eclipse.cdt.internal.ui.refactoring.ClassMemberInserter.InsertionInfo;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.Container;
|
import org.eclipse.cdt.internal.ui.refactoring.Container;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
|
import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.MethodContext.ContextType;
|
import org.eclipse.cdt.internal.ui.refactoring.MethodContext.ContextType;
|
||||||
|
@ -172,17 +173,17 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
ast = getAST(tu, sm.newChild(1));
|
ast = getAST(tu, sm.newChild(1));
|
||||||
nodeFactory = ast.getASTNodeFactory();
|
nodeFactory = ast.getASTNodeFactory();
|
||||||
container = findExtractableNodes();
|
container = findExtractableNodes();
|
||||||
|
|
||||||
if (isProgressMonitorCanceled(sm, initStatus))
|
if (isProgressMonitorCanceled(sm, initStatus))
|
||||||
return initStatus;
|
return initStatus;
|
||||||
|
|
||||||
if (container.isEmpty()) {
|
if (container.isEmpty()) {
|
||||||
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_NoStmtSelected);
|
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_NoStmtSelected);
|
||||||
return initStatus;
|
return initStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
checkForNonExtractableStatements(container, initStatus);
|
checkForNonExtractableStatements(container, initStatus);
|
||||||
|
|
||||||
List<NameInformation> returnValueCandidates = container.getReturnValueCandidates();
|
List<NameInformation> returnValueCandidates = container.getReturnValueCandidates();
|
||||||
if (returnValueCandidates.size() > 1) {
|
if (returnValueCandidates.size() > 1) {
|
||||||
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_TooManySelected);
|
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_TooManySelected);
|
||||||
|
@ -190,27 +191,27 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
} else if (returnValueCandidates.size() == 1) {
|
} else if (returnValueCandidates.size() == 1) {
|
||||||
info.setMandatoryReturnVariable(returnValueCandidates.get(0));
|
info.setMandatoryReturnVariable(returnValueCandidates.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
info.setParameters(container.getParameterCandidates());
|
info.setParameters(container.getParameterCandidates());
|
||||||
initStatus.merge(checkParameterAndReturnTypes());
|
initStatus.merge(checkParameterAndReturnTypes());
|
||||||
if (initStatus.hasFatalError())
|
if (initStatus.hasFatalError())
|
||||||
return initStatus;
|
return initStatus;
|
||||||
|
|
||||||
extractor = FunctionExtractor.createFor(container.getNodesToWrite());
|
extractor = FunctionExtractor.createFor(container.getNodesToWrite());
|
||||||
|
|
||||||
if (extractor.canChooseReturnValue() && info.getMandatoryReturnVariable() == null) {
|
if (extractor.canChooseReturnValue() && info.getMandatoryReturnVariable() == null) {
|
||||||
chooseReturnVariable();
|
chooseReturnVariable();
|
||||||
}
|
}
|
||||||
|
|
||||||
IPreferencesService preferences = Platform.getPreferencesService();
|
IPreferencesService preferences = Platform.getPreferencesService();
|
||||||
final boolean outFirst = preferences.getBoolean(CUIPlugin.PLUGIN_ID,
|
final boolean outFirst = preferences.getBoolean(CUIPlugin.PLUGIN_ID,
|
||||||
PreferenceConstants.FUNCTION_OUTPUT_PARAMETERS_BEFORE_INPUT, false,
|
PreferenceConstants.FUNCTION_OUTPUT_PARAMETERS_BEFORE_INPUT, false,
|
||||||
PreferenceConstants.getPreferenceScopes(project.getProject()));
|
PreferenceConstants.getPreferenceScopes(project.getProject()));
|
||||||
info.sortParameters(outFirst);
|
info.sortParameters(outFirst);
|
||||||
|
|
||||||
boolean isExtractExpression = container.getNodesToWrite().get(0) instanceof IASTExpression;
|
boolean isExtractExpression = container.getNodesToWrite().get(0) instanceof IASTExpression;
|
||||||
info.setExtractExpression(isExtractExpression);
|
info.setExtractExpression(isExtractExpression);
|
||||||
|
|
||||||
info.setDeclarator(getDeclaration(container.getNodesToWrite().get(0)));
|
info.setDeclarator(getDeclaration(container.getNodesToWrite().get(0)));
|
||||||
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0),
|
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0),
|
||||||
refactoringContext, sm.newChild(1));
|
refactoringContext, sm.newChild(1));
|
||||||
|
@ -347,7 +348,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
if (node != firstNodeToWrite) {
|
if (node != firstNodeToWrite) {
|
||||||
rewriter.remove(node, editGroup);
|
rewriter.remove(node, editGroup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertCallIntoTree(IASTNode methodCall, List<IASTNode> list, ASTRewrite rewriter,
|
private void insertCallIntoTree(IASTNode methodCall, List<IASTNode> list, ASTRewrite rewriter,
|
||||||
|
@ -379,9 +380,10 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createMethodDefinition(final IASTName methodName, MethodContext context,
|
private void createMethodDefinition(final IASTName methodName, MethodContext context,
|
||||||
IASTNode firstNode, ModificationCollector collector) {
|
IASTNode firstExtractedNode, ModificationCollector collector) {
|
||||||
IASTFunctionDefinition node = CPPVisitor.findAncestorWithType(firstNode, IASTFunctionDefinition.class);
|
IASTFunctionDefinition functionToExtractFrom =
|
||||||
if (node != null) {
|
CPPVisitor.findAncestorWithType(firstExtractedNode, IASTFunctionDefinition.class);
|
||||||
|
if (functionToExtractFrom != null) {
|
||||||
String title;
|
String title;
|
||||||
if (context.getType() == MethodContext.ContextType.METHOD) {
|
if (context.getType() == MethodContext.ContextType.METHOD) {
|
||||||
title = Messages.ExtractFunctionRefactoring_CreateMethodDef;
|
title = Messages.ExtractFunctionRefactoring_CreateMethodDef;
|
||||||
|
@ -389,8 +391,8 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
title = Messages.ExtractFunctionRefactoring_CreateFunctionDef;
|
title = Messages.ExtractFunctionRefactoring_CreateFunctionDef;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTRewrite rewriter = collector.rewriterForTranslationUnit(node.getTranslationUnit());
|
ASTRewrite rewriter = collector.rewriterForTranslationUnit(functionToExtractFrom.getTranslationUnit());
|
||||||
addMethod(methodName, context, rewriter, node, new TextEditGroup(title));
|
addMethod(methodName, context, rewriter, functionToExtractFrom, new TextEditGroup(title));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -584,7 +586,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addMethod(IASTName methodName, MethodContext context, ASTRewrite rewrite,
|
private void addMethod(IASTName methodName, MethodContext context, ASTRewrite rewrite,
|
||||||
IASTNode insertPoint, TextEditGroup group) {
|
IASTNode functionToExtractFrom, TextEditGroup group) {
|
||||||
ICPPASTQualifiedName qname = new CPPASTQualifiedName();
|
ICPPASTQualifiedName qname = new CPPASTQualifiedName();
|
||||||
if (context.getType() == ContextType.METHOD) {
|
if (context.getType() == ContextType.METHOD) {
|
||||||
if (context.getMethodQName() != null) {
|
if (context.getMethodQName() != null) {
|
||||||
|
@ -600,7 +602,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
|
|
||||||
IASTDeclSpecifier returnType = getReturnType();
|
IASTDeclSpecifier returnType = getReturnType();
|
||||||
func.setDeclSpecifier(returnType);
|
func.setDeclSpecifier(returnType);
|
||||||
|
|
||||||
IASTStandardFunctionDeclarator createdFunctionDeclarator =
|
IASTStandardFunctionDeclarator createdFunctionDeclarator =
|
||||||
extractor.createFunctionDeclarator(qname,
|
extractor.createFunctionDeclarator(qname,
|
||||||
info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(),
|
info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(),
|
||||||
|
@ -609,22 +611,46 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
|
|
||||||
IASTCompoundStatement compound = new CPPASTCompoundStatement();
|
IASTCompoundStatement compound = new CPPASTCompoundStatement();
|
||||||
func.setBody(compound);
|
func.setBody(compound);
|
||||||
|
|
||||||
ASTRewrite subRewrite;
|
ASTRewrite subRewrite;
|
||||||
IASTNode parent = insertPoint.getParent();
|
IASTNode parent = functionToExtractFrom.getParent();
|
||||||
|
IASTNode nodeToInsert = func;
|
||||||
if (parent instanceof ICPPASTTemplateDeclaration) {
|
if (parent instanceof ICPPASTTemplateDeclaration) {
|
||||||
ICPPASTTemplateDeclaration parentTemplate = (ICPPASTTemplateDeclaration) parent;
|
ICPPASTTemplateDeclaration parentTemplate = (ICPPASTTemplateDeclaration) parent;
|
||||||
CPPASTTemplateDeclaration templateDeclaration = new CPPASTTemplateDeclaration();
|
CPPASTTemplateDeclaration templateDeclaration = new CPPASTTemplateDeclaration();
|
||||||
templateDeclaration.setParent(ast);
|
templateDeclaration.setParent(ast);
|
||||||
|
|
||||||
for (ICPPASTTemplateParameter param : parentTemplate.getTemplateParameters()) {
|
for (ICPPASTTemplateParameter param : parentTemplate.getTemplateParameters()) {
|
||||||
templateDeclaration.addTemplateParameter(param.copy(CopyStyle.withLocations));
|
templateDeclaration.addTemplateParameter(param.copy(CopyStyle.withLocations));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
functionToExtractFrom = parentTemplate;
|
||||||
templateDeclaration.setDeclaration(func);
|
templateDeclaration.setDeclaration(func);
|
||||||
subRewrite = rewrite.insertBefore(parent.getParent(), parent, templateDeclaration, group);
|
nodeToInsert = templateDeclaration;
|
||||||
|
parent = parent.getParent();
|
||||||
|
}
|
||||||
|
|
||||||
|
InsertionInfo insertion;
|
||||||
|
if (parent instanceof ICPPASTCompositeTypeSpecifier) {
|
||||||
|
// Inserting into a class declaration
|
||||||
|
insertion = ClassMemberInserter.findInsertionPoint((ICPPASTCompositeTypeSpecifier) parent,
|
||||||
|
info.getVisibility(), false);
|
||||||
} else {
|
} else {
|
||||||
subRewrite = rewrite.insertBefore(parent, insertPoint, func, group);
|
// Inserting into a translation unit or a namespace.
|
||||||
|
// TODO(sprigogin): Use insertBeforeNode instead of functionToExtractFrom when creating InsertionInfo
|
||||||
|
// IASTNode insertBeforeNode = info.getMethodContext().getType() == ContextType.METHOD ?
|
||||||
|
// null : functionToExtractFrom;
|
||||||
|
insertion = new InsertionInfo(parent, functionToExtractFrom);
|
||||||
|
}
|
||||||
|
if (insertion.getPrologue() != null) {
|
||||||
|
rewrite.insertBefore(insertion.getParentNode(),
|
||||||
|
insertion.getInsertBeforeNode(), insertion.getPrologue(), group);
|
||||||
|
}
|
||||||
|
subRewrite = rewrite.insertBefore(insertion.getParentNode(),
|
||||||
|
insertion.getInsertBeforeNode(), nodeToInsert, group);
|
||||||
|
if (insertion.getEpilogue() != null) {
|
||||||
|
rewrite.insertBefore(insertion.getParentNode(),
|
||||||
|
insertion.getInsertBeforeNode(), insertion.getEpilogue(), group);
|
||||||
}
|
}
|
||||||
|
|
||||||
extractor.constructMethodBody(compound, container.getNodesToWrite(),
|
extractor.constructMethodBody(compound, container.getNodesToWrite(),
|
||||||
|
@ -777,7 +803,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
IASTNode node = container.getNodesToWrite().get(0);
|
IASTNode node = container.getNodesToWrite().get(0);
|
||||||
return extractor.createReturnAssignment(node, stmt, callExpression);
|
return extractor.createReturnAssignment(node, stmt, callExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IASTSimpleDeclaration getDeclaration(IASTName name) {
|
private IASTSimpleDeclaration getDeclaration(IASTName name) {
|
||||||
IASTSimpleDeclaration simpleDecl = new CPPASTSimpleDeclaration();
|
IASTSimpleDeclaration simpleDecl = new CPPASTSimpleDeclaration();
|
||||||
IASTStandardFunctionDeclarator declarator =
|
IASTStandardFunctionDeclarator declarator =
|
||||||
|
|
Loading…
Add table
Reference in a new issue