Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9880db0e025498f3d4ebb0d36138a7637ae192d8 (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
/*******************************************************************************
 * Copyright (c) 2020 Torbjörn Svensson 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:
 *     Torbjörn Svensson - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.launchConfigurations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;

/**
 * Filters out the instance of ILaunchCOnfiguration where the, possibly nested,
 * launch configuration files are stored. If the launch configuration is local,
 * it's simply returned.
 */
public class UniqueLaunchConfigurationFileFilter extends ViewerFilter {
	@Override
	public Object[] filter(Viewer viewer, Object parent, Object[] elements) {
		int size = elements.length;
		if (size == 0) {
			return elements;
		}

		List<Object> filteredElements = new ArrayList<>(size);

		Map<String, List<ILaunchConfiguration>> configPathMap = new HashMap<>();
		for (Object element : elements) {
			if (element instanceof ILaunchConfiguration) {
				ILaunchConfiguration config = (ILaunchConfiguration) element;

				String path = toLaunchFileLocation(config);
				if (!configPathMap.containsKey(path)) {
					configPathMap.put(path, new ArrayList<>());
				}
				configPathMap.get(path).add(config);
			} else {
				filteredElements.add(element);
			}
		}

		for (Entry<String, List<ILaunchConfiguration>> entry : configPathMap.entrySet()) {
			List<ILaunchConfiguration> configsWithSamePath = entry.getValue();
			if (entry.getKey() == null) {
				// Local configurations ends up here, add them without further filter
				filteredElements.addAll(configsWithSamePath);
			} else if (configsWithSamePath.size() == 1) {
				// Only one configuration, add it
				filteredElements.add(configsWithSamePath.get(0));
			} else if (configsWithSamePath.size() > 1) {
				// More than one config with same path.
				// Order them with the shortest project relative path first
				configsWithSamePath.sort((o1, o2) -> {
					IPath path1 = o1.getFile().getProjectRelativePath();
					IPath path2 = o2.getFile().getProjectRelativePath();
					return Integer.compare(path1.segmentCount(), path2.segmentCount());
				});
				// Use the configuration with the shortest path.
				// The other configurations in the list are "identical" but with
				// the container set to one of the parent project(s) in the
				// workspace hierarchy.
				filteredElements.add(configsWithSamePath.get(0));
			}
		}

		return filteredElements.toArray();
	}

	private String toLaunchFileLocation(ILaunchConfiguration config) {
		if (!config.isLocal()) {
			IFile file = config.getFile();
			if (file != null) {
				IPath path = file.getLocation();
				if (path != null) {
					return path.toOSString();
				}
			}
		}
		return null;
	}

	@Override
	public boolean select(Viewer viewer, Object parentElement, Object element) {
		return true;
	}
}

Back to the top