Skip to content

Commit aa16fd1

Browse files
committed
New launch configuration name based on target name.
The new launch configuration name is only based on the target name and the type (tcp or serial). Changing a connection parameter will not result in a new launch configuration.
1 parent 3afacff commit aa16fd1

File tree

5 files changed

+136
-59
lines changed

5 files changed

+136
-59
lines changed

dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/launching/CoreBuildGdbManualRemoteLaunchConfigProvider.java

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.Map;
1515
import java.util.Map.Entry;
1616

17-
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
1817
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
1918
import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
2019
import org.eclipse.cdt.dsf.gdb.launching.GDBRemoteSerialLaunchTargetProvider;
@@ -26,12 +25,11 @@
2625
import org.eclipse.debug.core.ILaunchConfiguration;
2726
import org.eclipse.debug.core.ILaunchConfigurationType;
2827
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
29-
import org.eclipse.debug.core.ILaunchManager;
3028
import org.eclipse.launchbar.core.AbstractLaunchConfigProvider;
3129
import org.eclipse.launchbar.core.ILaunchDescriptor;
3230
import org.eclipse.launchbar.core.ProjectLaunchDescriptor;
3331
import org.eclipse.launchbar.core.target.ILaunchTarget;
34-
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
32+
import org.eclipse.launchbar.core.target.LaunchTargetUtils;
3533

3634
/*
3735
* TODO: Refactor this and CoreBuildLocalLaunchConfigProvider and
@@ -62,18 +60,36 @@ public ILaunchConfigurationType getLaunchConfigurationType(ILaunchDescriptor des
6260
return DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(TYPE_ID);
6361
}
6462

65-
@Override
66-
protected ILaunchConfiguration createLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target)
67-
throws CoreException {
68-
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
63+
private String getNameSuffix(ILaunchTarget target) {
64+
String suffix = target.getId();
6965
String targetTypeId = target.getTypeId();
70-
String prefix = descriptor.getName() + " " + target.getId(); //$NON-NLS-1$
7166
if (targetTypeId.equals(GDBRemoteTCPLaunchTargetProvider.TYPE_ID)) {
72-
prefix = prefix + " TCP"; //$NON-NLS-1$
73-
} else if (targetTypeId.equals(GDBRemoteSerialLaunchTargetProvider.TYPE_ID)) {
74-
prefix = prefix + " Serial"; //$NON-NLS-1$
67+
suffix += " TCP"; //$NON-NLS-1$
7568
}
76-
String name = launchManager.generateLaunchConfigurationName(prefix);
69+
if (targetTypeId.equals(GDBRemoteSerialLaunchTargetProvider.TYPE_ID)) {
70+
suffix += " Serial"; //$NON-NLS-1$
71+
}
72+
return LaunchTargetUtils.sanitizeLaunchConfigurationName(suffix);
73+
}
74+
75+
/**
76+
* Create a name for the launch configuration. We assume the name is unique.
77+
* If a launch configuration with the name already exists, it will be in the
78+
* configs Map, an no new one will be created.
79+
*
80+
* @param descriptor
81+
* @param target
82+
* @return
83+
*/
84+
private String launchConfigName(ILaunchDescriptor descriptor, ILaunchTarget target) {
85+
String name = descriptor.getName() + " " + getNameSuffix(target); //$NON-NLS-1$
86+
return name;
87+
}
88+
89+
@Override
90+
protected ILaunchConfiguration createLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target)
91+
throws CoreException {
92+
String name = launchConfigName(descriptor, target);
7793
ILaunchConfigurationType type = getLaunchConfigurationType(descriptor, target);
7894
ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null, name);
7995

@@ -82,21 +98,6 @@ protected ILaunchConfiguration createLaunchConfiguration(ILaunchDescriptor descr
8298
return workingCopy.doSave();
8399
}
84100

85-
private String getTargetConfigKey(ILaunchTarget target) {
86-
String targetTypeId = target.getTypeId();
87-
if (targetTypeId.equals(GDBRemoteTCPLaunchTargetProvider.TYPE_ID)) {
88-
String host = target.getAttribute(IGDBLaunchConfigurationConstants.ATTR_HOST, EMPTY);
89-
String port = target.getAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT, EMPTY);
90-
return host + '.' + port;
91-
}
92-
if (targetTypeId.equals(GDBRemoteSerialLaunchTargetProvider.TYPE_ID)) {
93-
String serialPort = target.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV, EMPTY);
94-
String baudRate = target.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV_SPEED, EMPTY);
95-
return serialPort + '.' + baudRate;
96-
}
97-
return target.getId(); // Fallback to target ID if no specific attributes are set
98-
}
99-
100101
@Override
101102
public ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target)
102103
throws CoreException {
@@ -109,14 +110,58 @@ public ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor,
109110
configs.put(project, projectConfigs);
110111
}
111112

112-
config = projectConfigs.get(getTargetConfigKey(target));
113+
config = projectConfigs.get(launchConfigName(descriptor, target));
113114
if (config == null) {
114115
config = createLaunchConfiguration(descriptor, target);
116+
} else {
117+
updateLaunchConfiguration(config, target);
115118
}
116119
}
117120
return config;
118121
}
119122

123+
/**
124+
* Update the given launch configuration to match the given target's attributes.
125+
*
126+
* @param config the launch configuration to update
127+
* @param target the launch target to get attributes from
128+
* @throws CoreException if unable to update the launch configuration
129+
*/
130+
@Override
131+
protected void updateLaunchConfiguration(ILaunchConfiguration config, ILaunchTarget target) throws CoreException {
132+
133+
ILaunchConfigurationWorkingCopy workingCopy = config.getWorkingCopy();
134+
135+
String targetTypeId = target.getTypeId();
136+
if (targetTypeId.equals(GDBRemoteTCPLaunchTargetProvider.TYPE_ID)) {
137+
String targetHost = target.getAttribute(IGDBLaunchConfigurationConstants.ATTR_HOST, EMPTY);
138+
String targetPort = target.getAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT, EMPTY);
139+
String configHost = workingCopy.getAttribute(IGDBLaunchConfigurationConstants.ATTR_HOST, EMPTY);
140+
String configPort = workingCopy.getAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT, EMPTY);
141+
if (!configHost.equals(targetHost)) {
142+
workingCopy.setAttribute(IGDBLaunchConfigurationConstants.ATTR_HOST, targetHost);
143+
}
144+
if (!configPort.equals(targetPort)) {
145+
workingCopy.setAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT, targetPort);
146+
}
147+
}
148+
if (targetTypeId.equals(GDBRemoteSerialLaunchTargetProvider.TYPE_ID)) {
149+
String targetSerialPort = target.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV, EMPTY);
150+
String targetBaudRate = target.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV_SPEED, EMPTY);
151+
String configSerialPort = workingCopy.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV, EMPTY);
152+
String configBaudRate = workingCopy.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV, EMPTY);
153+
if (!configSerialPort.equals(targetSerialPort)) {
154+
workingCopy.setAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV, targetSerialPort);
155+
}
156+
if (!configBaudRate.equals(targetBaudRate)) {
157+
workingCopy.setAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV_SPEED, targetBaudRate);
158+
}
159+
}
160+
if (workingCopy.isDirty()) {
161+
workingCopy.doSave();
162+
}
163+
}
164+
120165
@Override
121166
protected void populateLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target,
122167
ILaunchConfigurationWorkingCopy workingCopy) throws CoreException {
@@ -161,21 +206,7 @@ public boolean launchConfigurationAdded(ILaunchConfiguration configuration) thro
161206
projectConfigs = new HashMap<>();
162207
configs.put(project, projectConfigs);
163208
}
164-
boolean isTCP = configuration.getAttribute(IGDBLaunchConfigurationConstants.ATTR_REMOTE_TCP, true);
165-
if (isTCP) {
166-
// For TCP connections, we use host and port as the key
167-
String host = configuration.getAttribute(IGDBLaunchConfigurationConstants.ATTR_HOST, EMPTY);
168-
String port = configuration.getAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT, EMPTY);
169-
String targetConfig = host + '.' + port;
170-
projectConfigs.put(targetConfig, configuration);
171-
return true;
172-
} else {
173-
// For Serial connections, we use serial port and baud rate as the key
174-
String serialPort = configuration.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV, EMPTY);
175-
String baudRate = configuration.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEV_SPEED, EMPTY);
176-
String targetConfig = serialPort + '.' + baudRate;
177-
projectConfigs.put(targetConfig, configuration);
178-
}
209+
projectConfigs.put(configuration.getName(), configuration);
179210
return true;
180211
}
181212
return false;
@@ -213,22 +244,18 @@ public void launchDescriptorRemoved(ILaunchDescriptor descriptor) throws CoreExc
213244

214245
@Override
215246
public void launchTargetRemoved(ILaunchTarget target) throws CoreException {
216-
// Any other targets have the same host/port or device/baud-rate?
217-
ILaunchTargetManager targetManager = CDebugCorePlugin.getService(ILaunchTargetManager.class);
218-
for (ILaunchTarget t : targetManager.getLaunchTargets()) {
219-
if (!target.equals(t) && getTargetConfigKey(target).equals(getTargetConfigKey(t))) {
220-
// Yup, nothing to do then
221-
return;
222-
}
223-
}
224247

248+
// Remove all launch configurations that were created for the given target.
225249
for (Entry<IProject, Map<String, ILaunchConfiguration>> projectEntry : configs.entrySet()) {
226250
Map<String, ILaunchConfiguration> projectConfigs = projectEntry.getValue();
227-
ILaunchConfiguration config = projectConfigs.get(getTargetConfigKey(target));
228-
if (config != null) {
229-
config.delete();
251+
252+
for (Entry<String, ILaunchConfiguration> entry : projectConfigs.entrySet()) {
253+
ILaunchConfiguration config = entry.getValue();
254+
if (config.getName().endsWith(" " + getNameSuffix(target))) { //$NON-NLS-1$
255+
projectConfigs.remove(entry.getKey());
256+
config.delete();
257+
}
230258
}
231259
}
232-
233260
}
234261
}

launchbar/org.eclipse.launchbar.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.launchbar.core;singleton:=true
5-
Bundle-Version: 3.0.200
5+
Bundle-Version: 3.1.0.qualifier
66
Bundle-Activator: org.eclipse.launchbar.core.internal.Activator
77
Bundle-Vendor: %providerName
88
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.33.0,4)",

launchbar/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/AbstractLaunchConfigProvider.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ protected ILaunchConfiguration createLaunchConfiguration(ILaunchDescriptor descr
4343
return workingCopy.doSave();
4444
}
4545

46+
/**
47+
* Update an existing launch configuration to match the target's attributes.
48+
*
49+
* @param config the launch configuration to update
50+
* @param target the launch target to get attributes from
51+
* @throws CoreException if unable to update the launch configuration
52+
* @since 3.1
53+
*/
54+
protected void updateLaunchConfiguration(ILaunchConfiguration config, ILaunchTarget target) throws CoreException {
55+
56+
ILaunchConfigurationWorkingCopy workingCopy = config.getWorkingCopy();
57+
58+
// Leave our breadcrumb
59+
60+
if (workingCopy.isDirty()) {
61+
workingCopy.doSave();
62+
}
63+
}
64+
65+
/**
66+
* Populate the newly created launch configuration with the target's attributes.
67+
*
68+
* @param descriptor the launch descriptor
69+
* @param target the launch target
70+
* @return the launch configuration type
71+
* @throws CoreException if unable to get the launch configuration type
72+
*/
4673
protected void populateLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target,
4774
ILaunchConfigurationWorkingCopy workingCopy) throws CoreException {
4875
// Leave our breadcrumb

launchbar/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/target/ILaunchTargetManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public interface ILaunchTargetManager {
2727
*/
2828
final String localLaunchTargetTypeId = "org.eclipse.launchbar.core.launchTargetType.local"; //$NON-NLS-1$
2929
/**
30-
* @since 3.0
30+
* @since 3.1
3131
*/
3232
final String localLaunchTargetId = "Local"; //$NON-NLS-1$
3333

3434
/**
35-
* @since 3.0
35+
* @since 3.1
3636
*/
3737
default ILaunchTarget getLocalLaunchTarget() {
3838
return getLaunchTarget(localLaunchTargetTypeId, localLaunchTargetId);

launchbar/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/target/LaunchTargetUtils.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.util.regex.Pattern;
1414

1515
/**
16-
* @since 3.0
16+
* @since 3.1
1717
*/
1818
public class LaunchTargetUtils {
1919
/**
@@ -23,6 +23,14 @@ public class LaunchTargetUtils {
2323
*/
2424
private static final Pattern INVALID_NAME_PATTERN = Pattern
2525
.compile("[^\\pL\\pM\\p{Nd}\\p{Nl}\\p{Pc}[\\p{InEnclosedAlphanumerics}&&\\p{So}]]"); //$NON-NLS-1$
26+
/**
27+
* Disallowed characters for launch configuration names
28+
* '@' and '&' are disallowed because they corrupt menu items.
29+
* Copied from org.eclipse.debug.internal.core.LaunchConfigurationManager
30+
*
31+
*/
32+
private static final char[] DISALLOWED_CONFIG_NAME_CHARS = new char[] { '@', '&', '\\', '/', ':', '*', '?', '"',
33+
'<', '>', '|', '\0' };
2634

2735
private LaunchTargetUtils() {
2836
// empty
@@ -45,4 +53,19 @@ public static boolean isInvalidName(String name) {
4553
public static String sanitizeName(String name) {
4654
return INVALID_NAME_PATTERN.matcher(name).replaceAll("_"); //$NON-NLS-1$
4755
}
56+
57+
/**
58+
* Replace launch configuration name disallowed characters with underscores.
59+
* Copied from org.eclipse.debug.internal.core.LaunchConfigurationManager
60+
* LaunchManager.isValidLaunchConfigurationName() can be used to verify a name.
61+
*
62+
* @param name the name to sanitize.
63+
*/
64+
public static String sanitizeLaunchConfigurationName(String name) {
65+
//blanket replace all invalid chars
66+
for (char element : DISALLOWED_CONFIG_NAME_CHARS) {
67+
name = name.replace(element, '_');
68+
}
69+
return name;
70+
}
4871
}

0 commit comments

Comments
 (0)