From 26f32a37d5299e11956d6198d8aacb56eab31882 Mon Sep 17 00:00:00 2001 From: Mikhail Khodjaiants Date: Fri, 13 Sep 2002 18:32:41 +0000 Subject: [PATCH] Implementation of text hovering. --- .../cdt/debug/core/ICExpressionEvaluator.java | 34 +++++ .../internal/core/model/CDebugTarget.java | 30 ++++- .../cdt/debug/internal/ui/CDebugUIUtils.java | 56 +++++++++ .../internal/ui/editors/DebugTextHover.java | 118 +++++++++++++++++- 4 files changed, 233 insertions(+), 5 deletions(-) create mode 100644 debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/ICExpressionEvaluator.java diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/ICExpressionEvaluator.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/ICExpressionEvaluator.java new file mode 100644 index 00000000000..dcf06b36dc9 --- /dev/null +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/ICExpressionEvaluator.java @@ -0,0 +1,34 @@ +/* + *(c) Copyright QNX Software Systems Ltd. 2002. + * All Rights Reserved. + * + */ +package org.eclipse.cdt.debug.core; + +import org.eclipse.debug.core.DebugException; + +/** + * + * Supports the evaluation of C/C++ expressions. + * + * @since Sep 13, 2002 + */ +public interface ICExpressionEvaluator +{ + /** + * Evaluates the specified expression and returns evaluation result + * as a string. + * + * @param expression the expression to evaluate + * @return the evaluation result + * @throws DebugException on failure. Reasons include: + */ + String evaluateExpressionToString( String expression ) throws DebugException; + + /** + * Returns whether this object can currently evaluate an expression. + * + * @return whether this object can currently evaluate an expression + */ + boolean canEvaluate(); +} diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CDebugTarget.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CDebugTarget.java index d93f3112564..31f9ca9ddb9 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CDebugTarget.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CDebugTarget.java @@ -14,6 +14,7 @@ import java.util.List; import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugModel; import org.eclipse.cdt.debug.core.ICBreakpoint; +import org.eclipse.cdt.debug.core.ICExpressionEvaluator; import org.eclipse.cdt.debug.core.ICLineBreakpoint; import org.eclipse.cdt.debug.core.ICSourceLocator; import org.eclipse.cdt.debug.core.ICWatchpoint; @@ -77,7 +78,8 @@ public class CDebugTarget extends CDebugElement IRestart, IFormattedMemoryRetrieval, IState, - ILaunchListener + ILaunchListener, + ICExpressionEvaluator { /** * Threads contained in this debug target. When a thread @@ -740,6 +742,8 @@ public class CDebugTarget extends CDebugElement return fCDITarget; if ( adapter.equals( IState.class ) ) return this; + if ( adapter.equals( ICExpressionEvaluator.class ) ) + return this; return super.getAdapter( adapter ); } @@ -1568,4 +1572,28 @@ public class CDebugTarget extends CDebugElement } return null; } + + /* (non-Javadoc) + * @see org.eclipse.cdt.debug.core.ICExpressionEvaluator#evaluateExpressionToString(String) + */ + public String evaluateExpressionToString( String expression ) throws DebugException + { + try + { + return getCDITarget().evaluateExpressionToString( expression ); + } + catch( CDIException e ) + { + targetRequestFailed( e.getMessage(), null ); + } + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.debug.core.ICExpressionEvaluator#canEvaluate() + */ + public boolean canEvaluate() + { + return isAvailable() && isSuspended(); + } } diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDebugUIUtils.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDebugUIUtils.java index fb32ffbd41d..321ca1bb7c3 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDebugUIUtils.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDebugUIUtils.java @@ -6,6 +6,11 @@ package org.eclipse.cdt.debug.internal.ui; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.Region; + /** * * Enter type comment. @@ -24,4 +29,55 @@ public class CDebugUIUtils prefix[i] = '0'; return new String( prefix ) + tmp; } + + static public IRegion findWord( IDocument document, int offset ) + { + int start = -1; + int end = -1; + + try + { + int pos = offset; + char c; + + while( pos >= 0 ) + { + c = document.getChar( pos ); + if ( !Character.isJavaIdentifierPart( c ) ) + break; + --pos; + } + + start = pos; + + pos = offset; + int length = document.getLength(); + + while( pos < length ) + { + c = document.getChar( pos ); + if ( !Character.isJavaIdentifierPart( c ) ) + break; + ++pos; + } + + end = pos; + + } + catch( BadLocationException x ) + { + } + + if ( start > -1 && end > -1 ) + { + if ( start == offset && end == offset ) + return new Region( offset, 0 ); + else if ( start == offset ) + return new Region( start, end - start ); + else + return new Region( start + 1, end - start - 1 ); + } + + return null; + } } diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/editors/DebugTextHover.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/editors/DebugTextHover.java index 2ff15e230bc..fdd7bf516b1 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/editors/DebugTextHover.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/editors/DebugTextHover.java @@ -6,19 +6,33 @@ package org.eclipse.cdt.debug.internal.ui.editors; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.cdt.debug.core.ICExpressionEvaluator; +import org.eclipse.cdt.debug.internal.ui.CDebugUIUtils; +import org.eclipse.cdt.debug.ui.CDebugUIPlugin; +import org.eclipse.debug.core.DebugException; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunchManager; +import org.eclipse.debug.core.model.IDebugTarget; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextHover; import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.Region; +import org.eclipse.swt.graphics.Point; /** * - * Enter type comment. + * The text hovering support for C/C++ debugger. * * @since: Sep 12, 2002 */ public class DebugTextHover implements ITextHover { - /** * Constructor for DebugTextHover. */ @@ -32,15 +46,111 @@ public class DebugTextHover implements ITextHover */ public String getHoverInfo( ITextViewer textViewer, IRegion hoverRegion ) { - return "Test"; + DebugPlugin debugPlugin = DebugPlugin.getDefault(); + if ( debugPlugin == null ) + { + return null; + } + ILaunchManager launchManager = debugPlugin.getLaunchManager(); + if ( launchManager == null ) + { + return null; + } + + IDebugTarget[] targets = launchManager.getDebugTargets(); + if ( targets != null && targets.length > 0 ) + { + try + { + IDocument document = textViewer.getDocument(); + if ( document == null ) + return null; + + String expression = document.get( hoverRegion.getOffset(), hoverRegion.getLength() ); + List targetList = new ArrayList( targets.length ); + for ( int i = 0; i < targets.length; i++ ) + { + ICExpressionEvaluator ee = (ICExpressionEvaluator)targets[i].getAdapter( ICExpressionEvaluator.class ); + if ( ee != null ) + { + targetList.add( i, targets[i] ); + } + } + StringBuffer buffer = new StringBuffer(); + boolean showDebugTarget = targetList.size() > 1; + Iterator iterator = targetList.iterator(); + while ( iterator.hasNext() ) + { + IDebugTarget target = (IDebugTarget)iterator.next(); + ICExpressionEvaluator ee = (ICExpressionEvaluator)target.getAdapter( ICExpressionEvaluator.class ); + boolean first = true; + if ( ee.canEvaluate() ) + { + String result = evaluateExpression( ee, expression ); + try + { + if ( result != null ) + { + if ( !first ) + { + buffer.append( '\n' ); + } + first = false; + if ( showDebugTarget ) + { + buffer.append( '[' ); + buffer.append( target.getName() ); + buffer.append( "]: " ); + } + buffer.append( result ); + } + } + catch( DebugException x ) + { + CDebugUIPlugin.log( x ); + } + } + } + if ( buffer.length() > 0 ) + { + return buffer.toString(); + } + } + catch ( BadLocationException x ) + { + CDebugUIPlugin.log( x ); + } + } + return null; } /** * @see org.eclipse.jface.text.ITextHover#getHoverRegion(ITextViewer, int) */ - public IRegion getHoverRegion( ITextViewer textViewer, int offset ) + public IRegion getHoverRegion( ITextViewer viewer, int offset ) { + Point selectedRange = viewer.getSelectedRange(); + if ( selectedRange.x >= 0 && + selectedRange.y > 0 && + offset >= selectedRange.x && + offset <= selectedRange.x + selectedRange.y ) + return new Region( selectedRange.x, selectedRange.y ); + if ( viewer != null ) + return CDebugUIUtils.findWord( viewer.getDocument(), offset ); return null; } + private String evaluateExpression( ICExpressionEvaluator ee, String expression ) + { + String result = null; + try + { + result = ee.evaluateExpressionToString( expression ); + } + catch( DebugException e ) + { + // ignore + } + return result; + } }