Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a1ea8336546422ad72b56042b08ea6577b8cb9b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * Copyright (c) 2011, 2012 Institute for Software, HSR Hochschule fuer Technik  
 * Rapperswil, University of applied sciences and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html  
 * 
 * Contributors: 
 * 	   Martin Schwab & Thomas Kallenberg - initial API and implementation
 *     Sergey Prigogin (Google)
 ******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.togglefunction;

import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNode.CopyStyle;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionWithTryBlock;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite.CommentPosition;

import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTLiteralNode;

import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector;
import org.eclipse.cdt.internal.ui.refactoring.utils.CPPASTAllVisitor;

public class ToggleFromImplementationToHeaderOrClassStrategy implements IToggleRefactoringStrategy {
	private ToggleRefactoringContext context;
	private TextEditGroup infoText;
	private IASTTranslationUnit otherAst;
	private ASTLiteralNode includeNode;

	public ToggleFromImplementationToHeaderOrClassStrategy(ToggleRefactoringContext context) {
		this.context = context;
		this.infoText = new TextEditGroup(Messages.EditGroupName);
	}

	private boolean isFreeFunction(IASTFunctionDefinition definition) {
		return definition.getDeclarator().getName() instanceof ICPPASTQualifiedName;
	}
	
	@Override
	public void run(ModificationCollector modifications) throws CoreException {
		newFileCheck();
		ASTRewrite implAst = modifications.rewriterForTranslationUnit(context.getDefinitionAST());
		List<IASTComment>leadingComments = implAst.getComments(context.getDefinition(), CommentPosition.leading);
		removeDefinitionFromImplementation(implAst);
		if (includeNode != null) {
			implAst.insertBefore(context.getDefinitionAST(), 
					context.getDefinitionAST().getChildren()[0], includeNode, infoText);
		}
		if (context.getDeclarationAST() != null) {
			addDefinitionToClass(modifications, leadingComments);
		} else {
			addDefinitionToHeader(modifications, leadingComments);
		}
	}

	private void newFileCheck() throws CoreException {
		if (context.getDeclarationAST() == null) {
			if (isFreeFunction(context.getDefinition())) {
				throw new NotSupportedException(Messages.ToggleFromImplementationToHeaderOrClassStrategy_CanNotToggle);
			}
			otherAst = context.getASTForPartnerFile();
			if (otherAst == null) {
				ToggleFileCreator fileCreator = new ToggleFileCreator(context, ".h"); //$NON-NLS-1$
				if (fileCreator.askUserForFileCreation(context)) {
					IFile file = fileCreator.createNewFile();
					otherAst = context.getAST(file, null);
					includeNode = new ASTLiteralNode(fileCreator.getIncludeStatement() + "\n\n"); //$NON-NLS-1$
				} else {
					throw new NotSupportedException(Messages.ToggleFromImplementationToHeaderOrClassStrategy_CanNotCreateNewFile);
				}
			}
		}
	}

	private void addDefinitionToHeader(ModificationCollector modifications, List<IASTComment> leadingComments) {
		ASTRewrite headerRewrite = modifications.rewriterForTranslationUnit(otherAst);
		IASTFunctionDefinition newDefinition = ToggleNodeHelper.createFunctionSignatureWithEmptyBody(
				context.getDefinition().getDeclSpecifier().copy(CopyStyle.withLocations),
				context.getDefinition().getDeclarator().copy(CopyStyle.withLocations),
				context.getDefinition().copy(CopyStyle.withLocations));
		newDefinition.setParent(otherAst);
		headerRewrite.insertBefore(otherAst.getTranslationUnit(), null, newDefinition, infoText);
		restoreBody(headerRewrite, newDefinition, modifications);
		for (IASTComment comment : leadingComments) {			
			headerRewrite.addComment(newDefinition, comment, CommentPosition.leading);
		}
	}

	private void addDefinitionToClass(ModificationCollector modifications, List<IASTComment> leadingComments) {
		ASTRewrite headerRewrite = modifications.rewriterForTranslationUnit(
				context.getDeclarationAST());
		IASTFunctionDefinition newDefinition = ToggleNodeHelper.createInClassDefinition(
				context.getDeclaration(), context.getDefinition(), context.getDeclarationAST());
		newDefinition.setParent(getParent());
		restoreBody(headerRewrite, newDefinition, modifications);
		headerRewrite.replace(context.getDeclaration().getParent(), newDefinition, infoText);
		for (IASTComment comment : leadingComments) {			
			headerRewrite.addComment(newDefinition, comment, CommentPosition.leading);
		}
	}

	private IASTNode getParent() {
		IASTNode parent = CPPVisitor.findAncestorWithType(context.getDefinition(),
				ICPPASTCompositeTypeSpecifier.class);
		IASTNode parentnode = null;
		if (parent != null) {
			parentnode  = parent;
		} else {
			parentnode =context.getDeclarationAST();
		}
		return parentnode;
	}

	private void restoreBody(ASTRewrite headerRewrite, IASTFunctionDefinition newDefinition,
			ModificationCollector modifications) {
		IASTFunctionDefinition oldDefinition = context.getDefinition();
		newDefinition.setBody(oldDefinition.getBody().copy(CopyStyle.withLocations));
		
		if (newDefinition instanceof ICPPASTFunctionWithTryBlock &&
				oldDefinition instanceof ICPPASTFunctionWithTryBlock) {
			ICPPASTFunctionWithTryBlock newTryDef = (ICPPASTFunctionWithTryBlock) newDefinition;
			ICPPASTFunctionWithTryBlock oldTryDef = (ICPPASTFunctionWithTryBlock) oldDefinition;
			for (ICPPASTCatchHandler handler : oldTryDef.getCatchHandlers()) {
				newTryDef.addCatchHandler(handler.copy(CopyStyle.withLocations));
			}
		}
		copyAllCommentsToNewLocation(oldDefinition, modifications.rewriterForTranslationUnit(oldDefinition.getTranslationUnit()), headerRewrite);
	}
	
	private void copyAllCommentsToNewLocation(IASTNode node, final ASTRewrite oldRw, final ASTRewrite newRw) {
		node.accept(new CPPASTAllVisitor() {
			@Override
			public int visitAll(IASTNode node){
				copyComments(oldRw, newRw, node, CommentPosition.leading);
				copyComments(oldRw, newRw, node, CommentPosition.trailing);
				copyComments(oldRw, newRw, node, CommentPosition.freestanding);
				return PROCESS_CONTINUE;
			}

			private void copyComments(final ASTRewrite oldRw, final ASTRewrite newRw, IASTNode node,
					CommentPosition pos) {
				List<IASTComment> comments = oldRw.getComments(node, pos);
				for (IASTComment comment : comments) {
					newRw.addComment(node, comment, pos);
				}
			}
		});
	}

	private void removeDefinitionFromImplementation(ASTRewrite implast) {
		ICPPASTNamespaceDefinition ns =
				CPPVisitor.findAncestorWithType(context.getDefinition(), ICPPASTNamespaceDefinition.class);
		if (ns != null && isSingleElementInNamespace(ns, context.getDefinition())) {
			implast.remove(ns, infoText);
		} else {
			implast.remove(context.getDefinition(), infoText);
		}
	}

	private boolean isSingleElementInNamespace(ICPPASTNamespaceDefinition ns,
			IASTFunctionDefinition definition) {
		return ns.getChildren().length == 2 && (ns.contains(definition));
	}
}

Back to the top