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
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 .ArrayList ;
18+ import java .util .List ;
1719
1820import org .eclipse .core .commands .AbstractHandler ;
1921import org .eclipse .core .commands .ExecutionEvent ;
3335import org .eclipse .debug .ui .actions .IWatchExpressionFactoryAdapter2 ;
3436import org .eclipse .jface .viewers .ISelection ;
3537import 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,12 +53,39 @@ 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 IStructuredSelection structuredSelection ) {
57+ if (structuredSelection instanceof TreeSelection treeSelection ) {
58+ List <IVariable > variables = new ArrayList <>();
59+ for (TreePath path : treeSelection .getPaths ()) {
60+ if (path .getSegmentCount () > 1 ) {
61+ for (int e = 0 ; e < path .getSegmentCount (); e ++) {
62+ IVariable variable = (IVariable ) path .getSegment (e );
63+ variables .add (variable );
64+ }
65+ IWatchExpressionFactoryAdapter2 factory = getFactory2 (variables );
66+ if (factory != null ) {
67+ boolean canCreate = factory .canCreateWatchExpression (variables );
68+ if (canCreate ) {
69+ try {
70+ String expression = factory .createWatchExpression (variables );
71+ createWatchExpression (expression );
72+ } catch (CoreException e ) {
73+ DebugPlugin .log (e );
74+ break ;
75+ }
76+ }
77+ }
78+ } else {
79+ Object element = path .getFirstSegment ();
80+ createExpression (element );
81+ }
82+ }
83+ } else {
84+ for (Object element : structuredSelection .toArray ()) {
85+ createExpression (element );
86+ }
5787 }
88+ showExpressionsView ();
5889 }
5990 return null ;
6091 }
@@ -96,9 +127,12 @@ private void createExpression(Object element) {
96127 DebugUIPlugin .errorDialog (DebugUIPlugin .getShell (), ActionMessages .WatchAction_0 , ActionMessages .WatchAction_1 , e ); //
97128 return ;
98129 }
130+ createWatchExpression (expressionString );
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 *
@@ -132,6 +164,16 @@ static IWatchExpressionFactoryAdapter2 getFactory2(Object element) {
132164 if (element instanceof IAdaptable ) {
133165 return ((IAdaptable )element ).getAdapter (IWatchExpressionFactoryAdapter2 .class );
134166 }
167+ if (element instanceof List <?> ExpressionList ) {
168+ for (Object obj : ExpressionList ) {
169+ if (!(obj instanceof IAdaptable )) {
170+ return null ;
171+ }
172+ }
173+ if (ExpressionList .getFirst () instanceof IVariable variable ) {
174+ return variable .getAdapter (IWatchExpressionFactoryAdapter2 .class );
175+ }
176+ }
135177 return null ;
136178 }
137179
0 commit comments