Skip to content

Commit 9cd2c8b

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 2cd05a8 commit 9cd2c8b

File tree

2 files changed

+63
-23
lines changed

2 files changed

+63
-23
lines changed

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

Lines changed: 35 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, 2025 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,19 @@
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+
import java.util.LinkedList;
18+
import java.util.List;
1719

1820
import org.eclipse.core.commands.AbstractHandler;
1921
import org.eclipse.core.commands.ExecutionEvent;
2022
import org.eclipse.core.commands.ExecutionException;
2123
import org.eclipse.core.runtime.CoreException;
2224
import org.eclipse.core.runtime.IAdaptable;
25+
import org.eclipse.debug.core.DebugException;
2326
import org.eclipse.debug.core.DebugPlugin;
2427
import org.eclipse.debug.core.ILaunch;
2528
import org.eclipse.debug.core.model.IDebugElement;
@@ -32,7 +35,8 @@
3235
import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapter;
3336
import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapter2;
3437
import org.eclipse.jface.viewers.ISelection;
35-
import org.eclipse.jface.viewers.IStructuredSelection;
38+
import org.eclipse.jface.viewers.TreePath;
39+
import org.eclipse.jface.viewers.TreeSelection;
3640
import org.eclipse.ui.IViewPart;
3741
import org.eclipse.ui.IWorkbenchPage;
3842
import org.eclipse.ui.PartInitException;
@@ -49,11 +53,29 @@ public class WatchHandler extends AbstractHandler {
4953
@Override
5054
public Object execute(ExecutionEvent event) throws ExecutionException {
5155
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);
56+
if (selection instanceof TreeSelection treeSelection) {
57+
for (TreePath path : treeSelection.getPaths()) {
58+
if (path.getSegmentCount() > 1) {
59+
List<IVariable> variableFlow = new LinkedList<>();
60+
for (int e = 0; e < path.getSegmentCount(); e++) {
61+
variableFlow.add((IVariable) path.getSegment(e));
62+
}
63+
StringBuilder expressionString = new StringBuilder();
64+
variableFlow.stream().forEach(e -> {
65+
try {
66+
expressionString.append(e.getName());
67+
expressionString.append("."); //$NON-NLS-1$
68+
} catch (DebugException e1) {
69+
DebugUIPlugin.log(e1);
70+
}
71+
});
72+
expressionString.deleteCharAt(expressionString.length() - 1);
73+
createWatchExpression(expressionString.toString());
74+
} else {
75+
Object element = path.getFirstSegment();
76+
createExpression(element);
77+
}
78+
showExpressionsView();
5779
}
5880
}
5981
return null;
@@ -96,9 +118,13 @@ private void createExpression(Object element) {
96118
DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), ActionMessages.WatchAction_0, ActionMessages.WatchAction_1, e); //
97119
return;
98120
}
121+
createWatchExpression(expressionString);
122+
showExpressionsView();
123+
}
99124

125+
private void createWatchExpression(String expressionString) {
100126
IWatchExpression expression;
101-
expression = DebugPlugin.getDefault().getExpressionManager().newWatchExpression(expressionString);
127+
expression = DebugPlugin.getDefault().getExpressionManager().newWatchExpression(expressionString);
102128
DebugPlugin.getDefault().getExpressionManager().addExpression(expression);
103129
IAdaptable object = DebugUITools.getDebugContext();
104130
IDebugElement context = null;
@@ -108,10 +134,8 @@ private void createExpression(Object element) {
108134
context = ((ILaunch) object).getDebugTarget();
109135
}
110136
expression.setExpressionContext(context);
111-
showExpressionsView();
112137
}
113138

114-
115139
/**
116140
* Returns the factory adapter for the given variable or <code>null</code> if none.
117141
*

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

Lines changed: 28 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, 2025 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
@@ -17,12 +17,14 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.Iterator;
20+
import java.util.LinkedList;
2021
import java.util.List;
2122

2223
import org.eclipse.core.runtime.CoreException;
2324
import org.eclipse.core.runtime.IAdaptable;
2425
import org.eclipse.core.runtime.IStatus;
2526
import org.eclipse.core.runtime.Status;
27+
import org.eclipse.debug.core.DebugException;
2628
import org.eclipse.debug.core.DebugPlugin;
2729
import org.eclipse.debug.core.IExpressionManager;
2830
import org.eclipse.debug.core.ILaunch;
@@ -40,6 +42,8 @@
4042
import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapterExtension;
4143
import org.eclipse.jface.util.LocalSelectionTransfer;
4244
import org.eclipse.jface.viewers.IStructuredSelection;
45+
import org.eclipse.jface.viewers.TreePath;
46+
import org.eclipse.jface.viewers.TreeSelection;
4347
import org.eclipse.jface.viewers.ViewerDropAdapter;
4448
import org.eclipse.swt.dnd.DND;
4549
import org.eclipse.swt.dnd.DropTargetEvent;
@@ -373,19 +377,31 @@ private boolean performExpressionDrop(IStructuredSelection selection) {
373377
*/
374378
private boolean performVariableOrWatchAdaptableDrop(IStructuredSelection selection) {
375379
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);
380+
IExpression expression;
381+
if (selection instanceof TreeSelection treeSelection) {
382+
for (TreePath path : treeSelection.getPaths()) {
383+
if (path.getSegmentCount() > 1) {
384+
List<IVariable> variableFlow = new LinkedList<>();
385+
for (int e = 0; e < path.getSegmentCount(); e++) {
386+
variableFlow.add((IVariable) path.getSegment(e));
387+
}
388+
StringBuilder expressionString = new StringBuilder();
389+
variableFlow.stream().forEach(e -> {
390+
try {
391+
expressionString.append(e.getName());
392+
expressionString.append("."); //$NON-NLS-1$
393+
} catch (DebugException e1) {
394+
DebugUIPlugin.log(e1);
395+
}
396+
});
397+
expressionString.deleteCharAt(expressionString.length() - 1);
398+
expression = createExpression(expressionString.toString());
383399
} 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;
400+
Object element = path.getFirstSegment();
401+
String expressionText = createExpressionString(element);
402+
expression = createExpression(expressionText);
386403
}
387-
} else {
388-
return false;
404+
expressions.add(expression);
389405
}
390406
}
391407
if (expressions.size() == selection.size()){

0 commit comments

Comments
 (0)