1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-08 11:03:28 +02:00

Implementation of text hovering.

This commit is contained in:
Mikhail Khodjaiants 2002-09-13 18:32:41 +00:00
parent 10e834b26a
commit 26f32a37d5
4 changed files with 233 additions and 5 deletions

View file

@ -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();
}

View file

@ -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();
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}