Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 73251a594b44b55f28e61c8fd13f33d65f478535 (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
/*******************************************************************************
 * Copyright (c) 2014, 2015 Google, Inc and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * 	   Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.rename;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.ISharableParticipant;
import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;
import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;

/**
 * Updates include statements and include guards in response to a file or a folder move.
 */
public class HeaderFileMoveParticipant extends MoveParticipant implements ISharableParticipant {
	private Map<IResource, MoveArguments> movedResources;
	private Change change;

	public HeaderFileMoveParticipant() {
	}

	@Override
	protected boolean initialize(Object element) {
		addElement(element, getArguments());
		return movedResources != null;
	}

	@Override
	public void addElement(Object element, RefactoringArguments arguments) {
		if (element instanceof IResource && arguments instanceof MoveArguments) {
			if (movedResources == null)
				movedResources = new HashMap<>();
			movedResources.put((IResource) element, (MoveArguments) arguments);
		}
	}

	@Override
	public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context)
			throws OperationCanceledException {
		try {
			if (movedResources == null)
				return RefactoringStatus.create(Status.OK_STATUS);

			// Maps the affected files to new, not yet existing, files.
			final Map<IFile, IFile> movedFiles = new HashMap<>();

			for (Map.Entry<IResource, MoveArguments> entry : movedResources.entrySet()) {
				IResource movedResource = entry.getKey();
				MoveArguments args = entry.getValue();
				if (!args.getUpdateReferences())
					continue;
				if (movedResource.isLinked())
					continue;

				Object destinationResource = args.getDestination();
				if (!(destinationResource instanceof IContainer))
					continue;
				final IContainer destination = (IContainer) destinationResource;
				final IPath destinationLocation = destination.getLocation();
				if (destinationLocation.equals(movedResource.getLocation().removeLastSegments(1)))
					continue;

				if (movedResource instanceof IFolder) {
					IFolder folder = (IFolder) movedResource;
					final int prefixLength = folder.getFullPath().segmentCount() - 1;
					folder.accept(new IResourceProxyVisitor() {
						@Override
						public boolean visit(IResourceProxy proxy) throws CoreException {
							if (proxy.isLinked())
								return false;
							if (proxy.getType() == IResource.FILE) {
								IFile file = (IFile) proxy.requestResource();
								movedFiles.put(file,
										destination.getFile(file.getFullPath().removeFirstSegments(prefixLength)));
								return false;
							}
							return true;
						}
					}, IResource.NONE);
				} else if (movedResource instanceof IFile) {
					IFile file = (IFile) movedResource;
					movedFiles.put(file, destination.getFile(new Path(movedResource.getName())));
				}
			}

			HeaderFileReferenceAdjuster includeAdjuster = new HeaderFileReferenceAdjuster(movedFiles,
					Collections.<IContainer, IContainer>emptyMap(), getProcessor());
			change = includeAdjuster.createChange(context, pm);
		} catch (CoreException e) {
			return RefactoringStatus.create(e.getStatus());
		} finally {
			pm.done();
		}
		return RefactoringStatus.create(Status.OK_STATUS);
	}

	@Override
	public Change createPreChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
		change = RenameParticipantHelper.postprocessParticipantChange(change, this);
		pm.done();
		return change;
	}

	@Override
	public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
		pm.done();
		return null;
	}

	@Override
	public String getName() {
		return RenameMessages.HeaderFileMoveParticipant_name;
	}
}

Back to the top