diff --git a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/expressions/WatchHandler.java b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/expressions/WatchHandler.java index a45587081a2..a546cb07c71 100644 --- a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/expressions/WatchHandler.java +++ b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/expressions/WatchHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2015 Wind River Systems and others. + * Copyright (c) 2008, 2025 Wind River Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,16 +10,16 @@ * * Contributors: * Wind River Systems - initial API and implementation + * IBM Corporation - Improved expression creation *******************************************************************************/ package org.eclipse.debug.internal.ui.actions.expressions; -import java.util.Iterator; - import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.debug.core.DebugException; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.model.IDebugElement; @@ -32,7 +32,8 @@ import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapter; import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapter2; import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.TreePath; +import org.eclipse.jface.viewers.TreeSelection; import org.eclipse.ui.IViewPart; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; @@ -49,11 +50,26 @@ public class WatchHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { ISelection selection = HandlerUtil.getCurrentSelection(event); - if (selection instanceof IStructuredSelection) { - Iterator iter = ((IStructuredSelection)selection).iterator(); - while (iter.hasNext()) { - Object element = iter.next(); - createExpression(element); + if (selection instanceof TreeSelection treeSelection) { + for (TreePath path : treeSelection.getPaths()) { + if (path.getSegmentCount() > 1) { + StringBuilder expressionString = new StringBuilder(); + for (int e = 0; e < path.getSegmentCount(); e++) { + IVariable variable = (IVariable) path.getSegment(e); + try { + expressionString.append(variable.getName()); + expressionString.append("."); //$NON-NLS-1$ + } catch (DebugException e1) { + DebugUIPlugin.log(e1); + } + } + expressionString.deleteCharAt(expressionString.length() - 1); + createWatchExpression(expressionString.toString()); + } else { + Object element = path.getFirstSegment(); + createExpression(element); + } + showExpressionsView(); } } return null; @@ -96,9 +112,13 @@ private void createExpression(Object element) { DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), ActionMessages.WatchAction_0, ActionMessages.WatchAction_1, e); // return; } + createWatchExpression(expressionString); + showExpressionsView(); + } + private void createWatchExpression(String expressionString) { IWatchExpression expression; - expression = DebugPlugin.getDefault().getExpressionManager().newWatchExpression(expressionString); + expression = DebugPlugin.getDefault().getExpressionManager().newWatchExpression(expressionString); DebugPlugin.getDefault().getExpressionManager().addExpression(expression); IAdaptable object = DebugUITools.getDebugContext(); IDebugElement context = null; @@ -108,10 +128,8 @@ private void createExpression(Object element) { context = ((ILaunch) object).getDebugTarget(); } expression.setExpressionContext(context); - showExpressionsView(); } - /** * Returns the factory adapter for the given variable or null if none. * diff --git a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/expression/ExpressionDropAdapter.java b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/expression/ExpressionDropAdapter.java index 7df7c13b770..032bcf97edc 100644 --- a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/expression/ExpressionDropAdapter.java +++ b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/expression/ExpressionDropAdapter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2013 IBM Corporation and others. + * Copyright (c) 2007, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -23,6 +23,7 @@ import org.eclipse.core.runtime.IAdaptable; 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.IExpressionManager; import org.eclipse.debug.core.ILaunch; @@ -40,6 +41,8 @@ import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapterExtension; import org.eclipse.jface.util.LocalSelectionTransfer; import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.TreePath; +import org.eclipse.jface.viewers.TreeSelection; import org.eclipse.jface.viewers.ViewerDropAdapter; import org.eclipse.swt.dnd.DND; import org.eclipse.swt.dnd.DropTargetEvent; @@ -373,19 +376,28 @@ private boolean performExpressionDrop(IStructuredSelection selection) { */ private boolean performVariableOrWatchAdaptableDrop(IStructuredSelection selection) { List expressions = new ArrayList<>(selection.size()); - for (Iterator itr = selection.iterator(); itr.hasNext();) { - Object element = itr.next(); - String expressionText = createExpressionString(element); - if (expressionText != null){ - IExpression expression = createExpression(expressionText); - if (expression != null){ - expressions.add(expression); + IExpression expression; + if (selection instanceof TreeSelection treeSelection) { + for (TreePath path : treeSelection.getPaths()) { + if (path.getSegmentCount() > 1) { + StringBuilder expressionString = new StringBuilder(); + for (int e = 0; e < path.getSegmentCount(); e++) { + IVariable variable = (IVariable) path.getSegment(e); + try { + expressionString.append(variable.getName()); + expressionString.append("."); //$NON-NLS-1$ + } catch (DebugException e1) { + DebugUIPlugin.log(e1); + } + } + expressionString.deleteCharAt(expressionString.length() - 1); + expression = createExpression(expressionString.toString()); } else { - DebugUIPlugin.log(new Status(IStatus.ERROR,DebugUIPlugin.getUniqueIdentifier(),"Drop failed. Watch expression could not be created for the text " + expressionText)); //$NON-NLS-1$ - return false; + Object element = path.getFirstSegment(); + String expressionText = createExpressionString(element); + expression = createExpression(expressionText); } - } else { - return false; + expressions.add(expression); } } if (expressions.size() == selection.size()){