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-
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 ;
3333import org .eclipse .debug .ui .actions .IWatchExpressionFactoryAdapter2 ;
3434import org .eclipse .jface .viewers .ISelection ;
3535import org .eclipse .jface .viewers .IStructuredSelection ;
36+ import org .eclipse .jface .viewers .TreePath ;
37+ import org .eclipse .jface .viewers .TreeSelection ;
3638import org .eclipse .ui .IViewPart ;
3739import org .eclipse .ui .IWorkbenchPage ;
3840import org .eclipse .ui .PartInitException ;
@@ -49,13 +51,41 @@ public class WatchHandler extends AbstractHandler {
4951 @ Override
5052 public Object execute (ExecutionEvent event ) throws ExecutionException {
5153 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 );
54+ if (selection instanceof IStructuredSelection structuredSelection ) {
55+ if (structuredSelection instanceof TreeSelection treeSelection ) {
56+ for (TreePath path : treeSelection .getPaths ()) {
57+ if (path .getSegmentCount () > 1 ) {
58+ StringBuilder expressionString = new StringBuilder ();
59+ boolean hasError = false ;
60+ for (int e = 0 ; e < path .getSegmentCount (); e ++) {
61+ IVariable variable = (IVariable ) path .getSegment (e );
62+ try {
63+ expressionString .append (variable .getName ());
64+ expressionString .append ("." ); //$NON-NLS-1$
65+ } catch (DebugException e1 ) {
66+ DebugUIPlugin .log (e1 );
67+ hasError = true ;
68+ break ;
69+ }
70+ }
71+ if (hasError || expressionString .length () == 0 ) {
72+ continue ;
73+ }
74+ expressionString .deleteCharAt (expressionString .length () - 1 );
75+ createWatchExpression (expressionString .toString ());
76+ } else {
77+ Object element = path .getFirstSegment ();
78+ createExpression (element );
79+ }
80+ }
81+ } else {
82+ for (Object element : structuredSelection .toArray ()) {
83+ createExpression (element );
84+ }
5785 }
86+ showExpressionsView ();
5887 }
88+
5989 return null ;
6090 }
6191
@@ -96,9 +126,13 @@ private void createExpression(Object element) {
96126 DebugUIPlugin .errorDialog (DebugUIPlugin .getShell (), ActionMessages .WatchAction_0 , ActionMessages .WatchAction_1 , e ); //
97127 return ;
98128 }
129+ createWatchExpression (expressionString );
130+ showExpressionsView ();
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 *
0 commit comments