Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2015 Wind River Systems and others.
* Copyright (c) 2008, 2026 Wind River Systems and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -10,10 +10,12 @@
*
* 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 java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
Expand All @@ -33,6 +35,8 @@
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;
Expand All @@ -49,12 +53,39 @@ 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 IStructuredSelection structuredSelection) {
if (structuredSelection instanceof TreeSelection treeSelection) {
for (TreePath path : treeSelection.getPaths()) {
List<IVariable> variables = new ArrayList<>();
if (path.getSegmentCount() > 1) {
for (int e = 0; e < path.getSegmentCount(); e++) {
IVariable variable = (IVariable) path.getSegment(e);
variables.add(variable);
}
IWatchExpressionFactoryAdapter2 factory = getFactory2(variables);
if (factory != null) {
boolean canCreate = factory.canCreateWatchExpression(variables);
if (canCreate) {
try {
String expression = factory.createWatchExpression(variables);
createWatchExpression(expression);
} catch (CoreException e) {
DebugPlugin.log(e);
break;
}
}
}
} else {
Object element = path.getFirstSegment();
createExpression(element);
}
}
} else {
for (Object element : structuredSelection.toArray()) {
createExpression(element);
}
}
showExpressionsView();
}
return null;
}
Expand Down Expand Up @@ -96,9 +127,12 @@ private void createExpression(Object element) {
DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), ActionMessages.WatchAction_0, ActionMessages.WatchAction_1, e); //
return;
}
createWatchExpression(expressionString);
}

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;
Expand All @@ -108,10 +142,8 @@ private void createExpression(Object element) {
context = ((ILaunch) object).getDebugTarget();
}
expression.setExpressionContext(context);
showExpressionsView();
}


/**
* Returns the factory adapter for the given variable or <code>null</code> if none.
*
Expand All @@ -132,6 +164,16 @@ static IWatchExpressionFactoryAdapter2 getFactory2(Object element) {
if (element instanceof IAdaptable) {
return ((IAdaptable)element).getAdapter(IWatchExpressionFactoryAdapter2.class);
}
if (element instanceof List<?> ExpressionList) {
for (Object obj : ExpressionList) {
if (!(obj instanceof IAdaptable)) {
return null;
}
}
if (ExpressionList.getFirst() instanceof IVariable variable) {
return variable.getAdapter(IWatchExpressionFactoryAdapter2.class);
}
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2013 IBM Corporation and others.
* Copyright (c) 2007, 2026 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -40,6 +40,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.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
Expand Down Expand Up @@ -373,19 +375,46 @@ private boolean performExpressionDrop(IStructuredSelection selection) {
*/
private boolean performVariableOrWatchAdaptableDrop(IStructuredSelection selection) {
List<IExpression> 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 ITreeSelection treeSelection) {
for (TreePath path : treeSelection.getPaths()) {
List<IVariable> variables = new ArrayList<>();
expression = null;
if (path.getSegmentCount() > 1) {
for (int e = 0; e < path.getSegmentCount(); e++) {
IVariable variable = (IVariable) path.getSegment(e);
variables.add(variable);
}
IWatchExpressionFactoryAdapter2 factory = getFactory2(variables);
if (factory != null) {
boolean canCreate = factory.canCreateWatchExpression(variables);
if (canCreate) {
try {
String expressionText = factory.createWatchExpression(variables);
expression = createExpression(expressionText);
expressions.add(expression);
} catch (CoreException e) {
DebugPlugin.log(e);
break;
}
}
}
} else {
DebugUIPlugin.log(new Status(IStatus.ERROR,DebugUIPlugin.getUniqueIdentifier(),"Drop failed. Watch expression could not be created for the text " + expressionText)); //$NON-NLS-1$
Object element = path.getFirstSegment();
String expressionText = createExpressionString(element);
if (expressionText == null) {
return false;
}
expressions.add(createExpression(expressionText));
}
}
} else {
for (Object element : selection) {
String expressionText = createExpressionString(element);
if (expressionText == null) {
return false;
}
} else {
return false;
expressions.add(createExpression(expressionText));
}
}
if (expressions.size() == selection.size()){
Expand Down Expand Up @@ -498,6 +527,16 @@ private IWatchExpressionFactoryAdapter2 getFactory2(Object element) {
if (element instanceof IAdaptable) {
return ((IAdaptable)element).getAdapter(IWatchExpressionFactoryAdapter2.class);
}
if (element instanceof List<?> ExpressionList) {
for (Object obj : ExpressionList) {
if (!(obj instanceof IAdaptable)) {
return null;
}
}
if (ExpressionList.getFirst() instanceof IVariable variable) {
return variable.getAdapter(IWatchExpressionFactoryAdapter2.class);
}
}
return null;
}

Expand Down
Loading