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
1010 *
1111 * Contributors:
1212 * Wind River Systems - initial API and implementation
13+ * IBM Corporation - Improved expression creation
1314 *******************************************************************************/
1415package org .eclipse .debug .internal .ui .actions .expressions ;
1516
16- import java .util .Iterator ;
17-
1817import org .eclipse .core .commands .AbstractHandler ;
1918import org .eclipse .core .commands .ExecutionEvent ;
2019import org .eclipse .core .commands .ExecutionException ;
2120import org .eclipse .core .runtime .CoreException ;
2221import org .eclipse .core .runtime .IAdaptable ;
22+ import org .eclipse .debug .core .DebugException ;
2323import org .eclipse .debug .core .DebugPlugin ;
2424import org .eclipse .debug .core .ILaunch ;
2525import org .eclipse .debug .core .model .IDebugElement ;
3232import org .eclipse .debug .ui .actions .IWatchExpressionFactoryAdapter ;
3333import org .eclipse .debug .ui .actions .IWatchExpressionFactoryAdapter2 ;
3434import org .eclipse .jface .viewers .ISelection ;
35- import org .eclipse .jface .viewers .IStructuredSelection ;
35+ import org .eclipse .jface .viewers .TreePath ;
36+ import org .eclipse .jface .viewers .TreeSelection ;
3637import org .eclipse .ui .IViewPart ;
3738import org .eclipse .ui .IWorkbenchPage ;
3839import org .eclipse .ui .PartInitException ;
@@ -49,11 +50,26 @@ public class WatchHandler extends AbstractHandler {
4950 @ Override
5051 public Object execute (ExecutionEvent event ) throws ExecutionException {
5152 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 );
53+ if (selection instanceof TreeSelection treeSelection ) {
54+ for (TreePath path : treeSelection .getPaths ()) {
55+ if (path .getSegmentCount () > 1 ) {
56+ StringBuilder expressionString = new StringBuilder ();
57+ for (int e = 0 ; e < path .getSegmentCount (); e ++) {
58+ IVariable variable = (IVariable ) path .getSegment (e );
59+ try {
60+ expressionString .append (variable .getName ());
61+ expressionString .append ("." ); //$NON-NLS-1$
62+ } catch (DebugException e1 ) {
63+ DebugUIPlugin .log (e1 );
64+ }
65+ }
66+ expressionString .deleteCharAt (expressionString .length () - 1 );
67+ createWatchExpression (expressionString .toString ());
68+ } else {
69+ Object element = path .getFirstSegment ();
70+ createExpression (element );
71+ }
72+ showExpressionsView ();
5773 }
5874 }
5975 return null ;
@@ -96,9 +112,13 @@ private void createExpression(Object element) {
96112 DebugUIPlugin .errorDialog (DebugUIPlugin .getShell (), ActionMessages .WatchAction_0 , ActionMessages .WatchAction_1 , e ); //
97113 return ;
98114 }
115+ createWatchExpression (expressionString );
116+ showExpressionsView ();
117+ }
99118
119+ private void createWatchExpression (String expressionString ) {
100120 IWatchExpression expression ;
101- expression = DebugPlugin .getDefault ().getExpressionManager ().newWatchExpression (expressionString );
121+ expression = DebugPlugin .getDefault ().getExpressionManager ().newWatchExpression (expressionString );
102122 DebugPlugin .getDefault ().getExpressionManager ().addExpression (expression );
103123 IAdaptable object = DebugUITools .getDebugContext ();
104124 IDebugElement context = null ;
@@ -108,10 +128,8 @@ private void createExpression(Object element) {
108128 context = ((ILaunch ) object ).getDebugTarget ();
109129 }
110130 expression .setExpressionContext (context );
111- showExpressionsView ();
112131 }
113132
114-
115133 /**
116134 * Returns the factory adapter for the given variable or <code>null</code> if none.
117135 *
0 commit comments