Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6feaf454d8b16554ff41feb7e963813bbb29612b (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*******************************************************************************
 * Copyright (c) 2000, 2016 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
 * ARM Limited - https://bugs.eclipse.org/bugs/show_bug.cgi?id=186981
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui;

import java.net.URI;
import java.util.Iterator;

import org.eclipse.cdt.debug.core.model.ICBreakpoint;
import org.eclipse.cdt.debug.core.model.ICDebugElementStatus;
import org.eclipse.cdt.debug.core.model.ICStackFrame;
import org.eclipse.cdt.debug.core.model.ICType;
import org.eclipse.cdt.debug.core.model.ICValue;
import org.eclipse.cdt.debug.core.model.IEnableDisableTarget;
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.cdt.debug.ui.breakpoints.CBreakpointPropertyDialogAction;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.debug.core.model.IVariable;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.debug.ui.contexts.IDebugContextProvider;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IVerticalRulerInfo;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.SimpleMarkerAnnotation;

import com.ibm.icu.text.MessageFormat;

/**
 * Utility methods for C/C++ Debug UI.
 */
public class CDebugUIUtils {

	static public IRegion findWord(IDocument document, int offset) {
		int start = -1;
		int end = -1;
		try {
			int pos = offset;
			char c;
			while (pos >= 0) {
				c = document.getChar(pos);
				if (!Character.isJavaIdentifierPart(c))
					break;
				--pos;
			}
			start = pos;
			pos = offset;
			int length = document.getLength();
			while (pos < length) {
				c = document.getChar(pos);
				if (!Character.isJavaIdentifierPart(c))
					break;
				++pos;
			}
			end = pos;
		} catch (BadLocationException x) {
		}
		if (start > -1 && end > -1) {
			if (start == offset && end == offset)
				return new Region(offset, 0);
			else if (start == offset)
				return new Region(start, end - start);
			else
				return new Region(start + 1, end - start - 1);
		}
		return null;
	}

	/**
	 * Returns the currently selected stack frame or the topmost frame
	 * in the currently selected thread in the Debug view
	 * of the current workbench page. Returns <code>null</code>
	 * if no stack frame or thread is selected, or if not called from the UI thread.
	 *
	 * @return the currently selected stack frame or the topmost frame
	 * 		   in the currently selected thread
	 */
	static public ICStackFrame getCurrentStackFrame() {
		IAdaptable context = DebugUITools.getDebugContext();
		return (context != null) ? (ICStackFrame) context.getAdapter(ICStackFrame.class) : null;
	}

	/**
	 * Moved from CDebugModelPresentation because it is also used by CVariableLabelProvider.
	 */
	static public String getValueText(IValue value) {
		StringBuilder label = new StringBuilder();
		if (value instanceof ICDebugElementStatus && !((ICDebugElementStatus) value).isOK()) {
			label.append(MessageFormat.format(CDebugUIMessages.getString("CDTDebugModelPresentation.4"), //$NON-NLS-1$
					(Object[]) new String[] { ((ICDebugElementStatus) value).getMessage() }));
		} else if (value instanceof ICValue) {
			ICType type = null;
			try {
				type = ((ICValue) value).getType();
			} catch (DebugException e) {
			}
			try {
				String valueString = value.getValueString();
				if (valueString != null) {
					valueString = valueString.trim();
					if (type != null && type.isCharacter()) {
						if (valueString.length() == 0)
							valueString = "."; //$NON-NLS-1$
						label.append(valueString);
					} else if (valueString.length() > 0) {
						label.append(valueString);
					}
				}
			} catch (DebugException e1) {
			}
		}
		return label.toString();
	}

	/**
	 * Moved from CDebugModelPresentation because it is also used by CVariableLabelProvider.
	 */
	public static String getVariableTypeName(ICType type) {
		StringBuilder result = new StringBuilder();
		if (type != null) {
			String typeName = type.getName();
			if (typeName != null)
				typeName = typeName.trim();
			if (type.isArray() && typeName != null) {
				int index = typeName.indexOf('[');
				if (index != -1)
					typeName = typeName.substring(0, index).trim();
			}
			if (typeName != null && typeName.length() > 0) {
				result.append(typeName);
				if (type.isArray()) {
					int[] dims = type.getArrayDimensions();
					for (int i = 0; i < dims.length; ++i) {
						result.append('[');
						result.append(dims[i]);
						result.append(']');
					}
				}
			}
		}
		return result.toString();
	}

	public static String getVariableName(IVariable variable) throws DebugException {
		return decorateText(variable, variable.getName());
	}

	public static String getEditorFilePath(IEditorInput input) throws CoreException {
		if (input instanceof IFileEditorInput) {
			IPath location = ((IFileEditorInput) input).getFile().getLocation();
			if (location != null) {
				return location.toOSString();
			}
			URI locationURI = ((IFileEditorInput) input).getFile().getLocationURI();
			if (locationURI != null) {
				IPath uriPath = URIUtil.toPath(locationURI);
				if (uriPath != null) {
					return uriPath.toOSString();
				}
			}
			return ""; //$NON-NLS-1$
		}
		if (input instanceof IStorageEditorInput) {
			return ((IStorageEditorInput) input).getStorage().getFullPath().toOSString();
		}
		if (input instanceof IPathEditorInput) {
			return ((IPathEditorInput) input).getPath().toOSString();
		}
		if (input instanceof IURIEditorInput) {
			IPath uriPath = URIUtil.toPath(((IURIEditorInput) input).getURI());
			if (uriPath != null)
				return uriPath.toOSString();
		}
		return ""; //$NON-NLS-1$
	}

	public static String decorateText(Object element, String text) {
		if (text == null)
			return null;
		StringBuilder baseText = new StringBuilder(text);
		if (element instanceof ICDebugElementStatus && !((ICDebugElementStatus) element).isOK()) {
			baseText.append(
					MessageFormat.format(" <{0}>", new Object[] { ((ICDebugElementStatus) element).getMessage() })); //$NON-NLS-1$
		}
		if (element instanceof IAdaptable) {
			IEnableDisableTarget target = ((IAdaptable) element).getAdapter(IEnableDisableTarget.class);
			if (target != null) {
				if (!target.isEnabled()) {
					baseText.append(' ');
					baseText.append(CDebugUIMessages.getString("CDTDebugModelPresentation.25")); //$NON-NLS-1$
				}
			}
		}
		return baseText.toString();
	}

	/**
	 * Helper function to open an error dialog.
	 * @param title
	 * @param message
	 * @param e
	 */
	static public void openError(final String title, final String message, final Exception e) {
		UIJob uiJob = new UIJob("open error") { //$NON-NLS-1$

			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				// open error for the exception
				String detail = ""; //$NON-NLS-1$
				if (e != null)
					detail = e.getMessage();

				Shell shell = CDebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell();

				MessageDialog.openError(shell, title, message + "\n" + detail); //$NON-NLS-1$
				return Status.OK_STATUS;
			}
		};
		uiJob.setSystem(true);
		uiJob.schedule();
	}

	/**
	 * Resolves the {@link IBreakpoint} from the given editor and ruler information. Returns <code>null</code>
	 * if no breakpoint exists or the operation fails.
	 *
	 * @param editor the editor
	 * @param info the current ruler information
	 * @return the {@link IBreakpoint} from the current editor position or <code>null</code>
	 */
	public static IBreakpoint getBreakpointFromEditor(ITextEditor editor, IVerticalRulerInfo info) {
		IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
		IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
		if (annotationModel != null) {
			Iterator<Annotation> iterator = annotationModel.getAnnotationIterator();
			while (iterator.hasNext()) {
				Object object = iterator.next();
				if (object instanceof SimpleMarkerAnnotation) {
					SimpleMarkerAnnotation markerAnnotation = (SimpleMarkerAnnotation) object;
					IMarker marker = markerAnnotation.getMarker();
					try {
						if (marker.isSubtypeOf(IBreakpoint.BREAKPOINT_MARKER)) {
							Position position = annotationModel.getPosition(markerAnnotation);
							int line = document.getLineOfOffset(position.getOffset());
							if (line == info.getLineOfLastMouseButtonActivity()) {
								IBreakpoint breakpoint = DebugPlugin.getDefault().getBreakpointManager()
										.getBreakpoint(marker);
								if (breakpoint != null) {
									return breakpoint;
								}
							}
						}
					} catch (CoreException e) {
					} catch (BadLocationException e) {
					}
				}
			}
		}
		return null;
	}

	public static void editBreakpointProperties(IWorkbenchPart part, final ICBreakpoint bp) {
		final ISelection debugContext = DebugUITools.getDebugContextForPart(part);
		CBreakpointPropertyDialogAction propertiesAction = new CBreakpointPropertyDialogAction(part.getSite(),
				new ISelectionProvider() {
					@Override
					public ISelection getSelection() {
						return new StructuredSelection(bp);
					}

					@Override
					public void addSelectionChangedListener(ISelectionChangedListener listener) {
					}

					@Override
					public void removeSelectionChangedListener(ISelectionChangedListener listener) {
					}

					@Override
					public void setSelection(ISelection selection) {
					}
				}, new IDebugContextProvider() {
					@Override
					public ISelection getActiveContext() {
						return debugContext;
					}

					@Override
					public void addDebugContextListener(IDebugContextListener listener) {
					}

					@Override
					public void removeDebugContextListener(IDebugContextListener listener) {
					}

					@Override
					public IWorkbenchPart getPart() {
						return null;
					}

				});
		propertiesAction.run();
		propertiesAction.dispose();
	}

	/**
	 * Formats the given key stroke or click name and the modifier keys
	 * to a key binding string that can be used in action texts.
	 *
	 * @param modifierKeys the modifier keys
	 * @param keyOrClick a key stroke or click, e.g. "Double Click"
	 * @return the formatted keyboard shortcut string, e.g. "Shift+Double Click"
	 *
	 * @since 8.1
	 */
	public static final String formatKeyBindingString(int modifierKeys, String keyOrClick) {
		// this should actually all be delegated to KeyStroke class
		return KeyStroke.getInstance(modifierKeys, KeyStroke.NO_KEY).format() + keyOrClick;
	}
}

Back to the top