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+ import java .util .LinkedList ;
18+ import java .util .List ;
1719
1820import org .eclipse .core .commands .AbstractHandler ;
1921import org .eclipse .core .commands .ExecutionEvent ;
2022import org .eclipse .core .commands .ExecutionException ;
2123import org .eclipse .core .runtime .CoreException ;
2224import org .eclipse .core .runtime .IAdaptable ;
25+ import org .eclipse .debug .core .DebugException ;
2326import org .eclipse .debug .core .DebugPlugin ;
2427import org .eclipse .debug .core .ILaunch ;
2528import org .eclipse .debug .core .model .IDebugElement ;
3235import org .eclipse .debug .ui .actions .IWatchExpressionFactoryAdapter ;
3336import org .eclipse .debug .ui .actions .IWatchExpressionFactoryAdapter2 ;
3437import 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 ;
3640import org .eclipse .ui .IViewPart ;
3741import org .eclipse .ui .IWorkbenchPage ;
3842import 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 *
0 commit comments