14
14
import java .util .Map ;
15
15
import java .util .Map .Entry ;
16
16
17
- import org .eclipse .cdt .debug .core .CDebugCorePlugin ;
18
17
import org .eclipse .cdt .debug .core .ICDTLaunchConfigurationConstants ;
19
18
import org .eclipse .cdt .dsf .gdb .IGDBLaunchConfigurationConstants ;
20
19
import org .eclipse .cdt .dsf .gdb .launching .GDBRemoteSerialLaunchTargetProvider ;
26
25
import org .eclipse .debug .core .ILaunchConfiguration ;
27
26
import org .eclipse .debug .core .ILaunchConfigurationType ;
28
27
import org .eclipse .debug .core .ILaunchConfigurationWorkingCopy ;
29
- import org .eclipse .debug .core .ILaunchManager ;
30
28
import org .eclipse .launchbar .core .AbstractLaunchConfigProvider ;
31
29
import org .eclipse .launchbar .core .ILaunchDescriptor ;
32
30
import org .eclipse .launchbar .core .ProjectLaunchDescriptor ;
33
31
import org .eclipse .launchbar .core .target .ILaunchTarget ;
34
- import org .eclipse .launchbar .core .target .ILaunchTargetManager ;
32
+ import org .eclipse .launchbar .core .target .LaunchTargetUtils ;
35
33
36
34
/*
37
35
* TODO: Refactor this and CoreBuildLocalLaunchConfigProvider and
@@ -62,18 +60,36 @@ public ILaunchConfigurationType getLaunchConfigurationType(ILaunchDescriptor des
62
60
return DebugPlugin .getDefault ().getLaunchManager ().getLaunchConfigurationType (TYPE_ID );
63
61
}
64
62
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 ();
69
65
String targetTypeId = target .getTypeId ();
70
- String prefix = descriptor .getName () + " " + target .getId (); //$NON-NLS-1$
71
66
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$
75
68
}
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 );
77
93
ILaunchConfigurationType type = getLaunchConfigurationType (descriptor , target );
78
94
ILaunchConfigurationWorkingCopy workingCopy = type .newInstance (null , name );
79
95
@@ -82,21 +98,6 @@ protected ILaunchConfiguration createLaunchConfiguration(ILaunchDescriptor descr
82
98
return workingCopy .doSave ();
83
99
}
84
100
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
-
100
101
@ Override
101
102
public ILaunchConfiguration getLaunchConfiguration (ILaunchDescriptor descriptor , ILaunchTarget target )
102
103
throws CoreException {
@@ -109,14 +110,58 @@ public ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor,
109
110
configs .put (project , projectConfigs );
110
111
}
111
112
112
- config = projectConfigs .get (getTargetConfigKey ( target ));
113
+ config = projectConfigs .get (launchConfigName ( descriptor , target ));
113
114
if (config == null ) {
114
115
config = createLaunchConfiguration (descriptor , target );
116
+ } else {
117
+ updateLaunchConfiguration (config , target );
115
118
}
116
119
}
117
120
return config ;
118
121
}
119
122
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
+
120
165
@ Override
121
166
protected void populateLaunchConfiguration (ILaunchDescriptor descriptor , ILaunchTarget target ,
122
167
ILaunchConfigurationWorkingCopy workingCopy ) throws CoreException {
@@ -161,21 +206,7 @@ public boolean launchConfigurationAdded(ILaunchConfiguration configuration) thro
161
206
projectConfigs = new HashMap <>();
162
207
configs .put (project , projectConfigs );
163
208
}
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 );
179
210
return true ;
180
211
}
181
212
return false ;
@@ -213,22 +244,18 @@ public void launchDescriptorRemoved(ILaunchDescriptor descriptor) throws CoreExc
213
244
214
245
@ Override
215
246
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
- }
224
247
248
+ // Remove all launch configurations that were created for the given target.
225
249
for (Entry <IProject , Map <String , ILaunchConfiguration >> projectEntry : configs .entrySet ()) {
226
250
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
+ }
230
258
}
231
259
}
232
-
233
260
}
234
261
}
0 commit comments