Skip to content

Commit 19310a5

Browse files
committed
Honor expression context when creating Watches
When selecting a field under an object, both the Watch command and Drag & Drop previously created a non-contextual expression using only the field name, which caused evaluation failures. This fix ensures that both Watch and Drag & Drop now generate proper context-aware expressions such as myobj.myfield instead of just the field name. Fixes : https://bugs.eclipse.org/bugs/show_bug.cgi?id=321289
1 parent 8a99f8c commit 19310a5

File tree

2 files changed

+84
-23
lines changed

2 files changed

+84
-23
lines changed

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/expressions/WatchHandler.java

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2015 Wind River Systems and others.
2+
* Copyright (c) 2008, 2026 Wind River Systems and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,16 +10,16 @@
1010
*
1111
* Contributors:
1212
* Wind River Systems - initial API and implementation
13+
* IBM Corporation - Improved expression creation
1314
*******************************************************************************/
1415
package org.eclipse.debug.internal.ui.actions.expressions;
1516

16-
import java.util.Iterator;
17-
1817
import org.eclipse.core.commands.AbstractHandler;
1918
import org.eclipse.core.commands.ExecutionEvent;
2019
import org.eclipse.core.commands.ExecutionException;
2120
import org.eclipse.core.runtime.CoreException;
2221
import org.eclipse.core.runtime.IAdaptable;
22+
import org.eclipse.debug.core.DebugException;
2323
import org.eclipse.debug.core.DebugPlugin;
2424
import org.eclipse.debug.core.ILaunch;
2525
import org.eclipse.debug.core.model.IDebugElement;
@@ -33,6 +33,8 @@
3333
import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapter2;
3434
import org.eclipse.jface.viewers.ISelection;
3535
import org.eclipse.jface.viewers.IStructuredSelection;
36+
import org.eclipse.jface.viewers.TreePath;
37+
import org.eclipse.jface.viewers.TreeSelection;
3638
import org.eclipse.ui.IViewPart;
3739
import org.eclipse.ui.IWorkbenchPage;
3840
import org.eclipse.ui.PartInitException;
@@ -49,13 +51,41 @@ public class WatchHandler extends AbstractHandler {
4951
@Override
5052
public Object execute(ExecutionEvent event) throws ExecutionException {
5153
ISelection selection = HandlerUtil.getCurrentSelection(event);
52-
if (selection instanceof IStructuredSelection) {
53-
Iterator<?> iter = ((IStructuredSelection)selection).iterator();
54-
while (iter.hasNext()) {
55-
Object element = iter.next();
56-
createExpression(element);
54+
if (selection instanceof IStructuredSelection structuredSelection) {
55+
if (structuredSelection instanceof TreeSelection treeSelection) {
56+
for (TreePath path : treeSelection.getPaths()) {
57+
if (path.getSegmentCount() > 1) {
58+
StringBuilder expressionString = new StringBuilder();
59+
boolean hasError = false;
60+
for (int e = 0; e < path.getSegmentCount(); e++) {
61+
IVariable variable = (IVariable) path.getSegment(e);
62+
try {
63+
expressionString.append(variable.getName());
64+
expressionString.append("."); //$NON-NLS-1$
65+
} catch (DebugException e1) {
66+
DebugUIPlugin.log(e1);
67+
hasError = true;
68+
break;
69+
}
70+
}
71+
if (hasError || expressionString.length() == 0) {
72+
continue;
73+
}
74+
expressionString.deleteCharAt(expressionString.length() - 1);
75+
createWatchExpression(expressionString.toString());
76+
} else {
77+
Object element = path.getFirstSegment();
78+
createExpression(element);
79+
}
80+
}
81+
} else {
82+
for (Object element : structuredSelection.toArray()) {
83+
createExpression(element);
84+
}
5785
}
86+
showExpressionsView();
5887
}
88+
5989
return null;
6090
}
6191

@@ -96,9 +126,13 @@ private void createExpression(Object element) {
96126
DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), ActionMessages.WatchAction_0, ActionMessages.WatchAction_1, e); //
97127
return;
98128
}
129+
createWatchExpression(expressionString);
130+
showExpressionsView();
131+
}
99132

133+
private void createWatchExpression(String expressionString) {
100134
IWatchExpression expression;
101-
expression = DebugPlugin.getDefault().getExpressionManager().newWatchExpression(expressionString);
135+
expression = DebugPlugin.getDefault().getExpressionManager().newWatchExpression(expressionString);
102136
DebugPlugin.getDefault().getExpressionManager().addExpression(expression);
103137
IAdaptable object = DebugUITools.getDebugContext();
104138
IDebugElement context = null;
@@ -108,10 +142,8 @@ private void createExpression(Object element) {
108142
context = ((ILaunch) object).getDebugTarget();
109143
}
110144
expression.setExpressionContext(context);
111-
showExpressionsView();
112145
}
113146

114-
115147
/**
116148
* Returns the factory adapter for the given variable or <code>null</code> if none.
117149
*

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/expression/ExpressionDropAdapter.java

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2007, 2013 IBM Corporation and others.
2+
* Copyright (c) 2007, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -23,6 +23,7 @@
2323
import org.eclipse.core.runtime.IAdaptable;
2424
import org.eclipse.core.runtime.IStatus;
2525
import org.eclipse.core.runtime.Status;
26+
import org.eclipse.debug.core.DebugException;
2627
import org.eclipse.debug.core.DebugPlugin;
2728
import org.eclipse.debug.core.IExpressionManager;
2829
import org.eclipse.debug.core.ILaunch;
@@ -40,6 +41,8 @@
4041
import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapterExtension;
4142
import org.eclipse.jface.util.LocalSelectionTransfer;
4243
import org.eclipse.jface.viewers.IStructuredSelection;
44+
import org.eclipse.jface.viewers.ITreeSelection;
45+
import org.eclipse.jface.viewers.TreePath;
4346
import org.eclipse.jface.viewers.ViewerDropAdapter;
4447
import org.eclipse.swt.dnd.DND;
4548
import org.eclipse.swt.dnd.DropTargetEvent;
@@ -373,19 +376,45 @@ private boolean performExpressionDrop(IStructuredSelection selection) {
373376
*/
374377
private boolean performVariableOrWatchAdaptableDrop(IStructuredSelection selection) {
375378
List<IExpression> expressions = new ArrayList<>(selection.size());
376-
for (Iterator<?> itr = selection.iterator(); itr.hasNext();) {
377-
Object element = itr.next();
378-
String expressionText = createExpressionString(element);
379-
if (expressionText != null){
380-
IExpression expression = createExpression(expressionText);
381-
if (expression != null){
382-
expressions.add(expression);
379+
IExpression expression;
380+
if (selection instanceof ITreeSelection treeSelection) {
381+
for (TreePath path : treeSelection.getPaths()) {
382+
expression = null;
383+
if (path.getSegmentCount() > 1) {
384+
boolean failed = false;
385+
StringBuilder expressionString = new StringBuilder();
386+
for (int e = 0; e < path.getSegmentCount(); e++) {
387+
IVariable variable = (IVariable) path.getSegment(e);
388+
try {
389+
expressionString.append(variable.getName());
390+
expressionString.append("."); //$NON-NLS-1$
391+
} catch (DebugException e1) {
392+
DebugUIPlugin.log(e1);
393+
failed = true;
394+
break;
395+
}
396+
}
397+
if (!failed && expressionString.length() > 0) {
398+
expressionString.deleteCharAt(expressionString.length() - 1);
399+
expression = createExpression(expressionString.toString());
400+
}
383401
} else {
384-
DebugUIPlugin.log(new Status(IStatus.ERROR,DebugUIPlugin.getUniqueIdentifier(),"Drop failed. Watch expression could not be created for the text " + expressionText)); //$NON-NLS-1$
385-
return false;
402+
Object element = path.getFirstSegment();
403+
String expressionText = createExpressionString(element);
404+
if (expressionText == null) {
405+
return false;
406+
}
407+
expression = createExpression(expressionText);
386408
}
387-
} else {
388-
return false;
409+
if (expression != null) {
410+
expressions.add(expression);
411+
}
412+
}
413+
} else {
414+
for (Object element : selection) {
415+
String expressionText = createExpressionString(element);
416+
expression = createExpression(expressionText);
417+
expressions.add(expression);
389418
}
390419
}
391420
if (expressions.size() == selection.size()){

0 commit comments

Comments
 (0)