Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9c79d277f799020476da00f438feede5f953d6f7 (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
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems 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:
 * QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.qt.ui.resources;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.internal.qt.core.QtNature;
import org.eclipse.cdt.internal.qt.ui.Activator;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.runtime.CoreException;

/**
 * Detects the addition or removal of a file to a Qt project. If one of these
 * resource changes is found, it triggers an update of the project's *.pro file
 * to reflect the change.
 */
public class QtResourceChangeListener implements IResourceChangeListener {

	@Override
	public void resourceChanged(IResourceChangeEvent event) {
		// No need to check for any events other than POST_CHANGE
		if ((event.getType() & (IResourceChangeEvent.POST_CHANGE | IResourceChangeEvent.POST_BUILD)) == 0) {
			return;
		}

		final List<IResourceDelta> deltaList = new ArrayList<>();
		final List<IResourceDelta> qmlDeltaList = new ArrayList<>();
		IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {

			@Override
			public boolean visit(IResourceDelta delta) {
				IResource resource = delta.getResource();

				if (resource.getType() == IResource.ROOT) {
					// Always traverse children of the workspace root
					return true;
				} else if (resource.getType() == IResource.PROJECT) {
					// Only traverse children of Qt Projects
					try {
						IProject project = (IProject) resource;
						if (project.exists() && project.isOpen() && project.hasNature(QtNature.ID)) {
							return true;
						}
					} catch (CoreException e) {
						Activator.log(e);
					}
					return false;
				} else if (resource.getType() == IResource.FOLDER) {
					// First, make sure this isn't the "build" folder
					if (resource.getType() == IResource.FOLDER) {
						if (resource.getName().equals("build")) { //$NON-NLS-1$
							return false;
						}
					}

					// Then check to make sure that the folder lies in a Qt
					// Project
					try {
						IProject project = resource.getProject();
						if (project != null && project.hasNature(QtNature.ID)) {
							return true;
						}
					} catch (CoreException e) {
						Activator.log(e);
					}
					return false;
				}

				// We don't care about resources that have simply been updated
				if ((delta.getKind() & IResourceDelta.CHANGED) > 0) {
					return false;
				}

				// We only care about added and removed resources at this point
				if ((delta.getKind() & IResourceDelta.ADDED | IResourceDelta.REMOVED) == 0) {
					return false;
				}

				if ("cpp".equals(resource.getFileExtension()) //$NON-NLS-1$
						|| "h".equals(resource.getFileExtension())) { //$NON-NLS-1$
					// If we make it to this point, then we have a .cpp or .h
					// file that's been added to or removed from a Qt
					// Project. Add it to the list of deltas so we can update
					// the project file later.
					deltaList.add(delta);
				} else if ("qml".equals(resource.getFileExtension())) { //$NON-NLS-1$
					qmlDeltaList.add(delta);
				}

				// Doesn't really matter since this line can only be reached if
				// we're dealing with a file that shouldn't have
				// children anyway
				return false;
			}
		};

		try {
			// Check all projects starting at the workspace root
			event.getDelta().accept(visitor);
		} catch (CoreException e) {
			Activator.log(e);
		}

		// Schedule the job to update the .pro files
		if (!deltaList.isEmpty()) {
			new QtProjectFileUpdateJob(deltaList).schedule();
		}
		// Schedule the job to update the tern server with added/deleted qml
		// files
		if (!qmlDeltaList.isEmpty()) {
			new QMLTernFileUpdateJob(qmlDeltaList).schedule();
		}
	}
}

Back to the top