mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-24 01:15:29 +02:00
Evaluate expressions on stack frame instead of target to provide evaluation context.
Evaluate the hovering expression for the selected context only.
This commit is contained in:
parent
1c54168e72
commit
c9db949d2b
2 changed files with 147 additions and 111 deletions
|
@ -1,3 +1,8 @@
|
|||
2004-09-20 Mikhail Khodjaiants
|
||||
Evaluate expressions on stack frame instead of target to provide evaluation context.
|
||||
Evaluate the hovering expression for the selected context only.
|
||||
* DebugTextHover.java
|
||||
|
||||
2004-09-17 Alain Magloire
|
||||
Support for 64 bits application
|
||||
PR 74056. Pathc from Artyom Kuanbekov
|
||||
|
|
|
@ -8,174 +8,211 @@
|
|||
* Contributors:
|
||||
* QNX Software Systems - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
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.model.ICExpressionEvaluator;
|
||||
import org.eclipse.cdt.debug.core.model.ICStackFrame;
|
||||
import org.eclipse.cdt.debug.internal.ui.CDebugUIUtils;
|
||||
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
|
||||
import org.eclipse.cdt.ui.text.c.hover.ICEditorTextHover;
|
||||
import org.eclipse.core.runtime.IAdaptable;
|
||||
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.debug.ui.IDebugUIConstants;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.IInformationControlCreator;
|
||||
import org.eclipse.jface.text.IRegion;
|
||||
import org.eclipse.jface.text.ITextHoverExtension;
|
||||
import org.eclipse.jface.text.ITextViewer;
|
||||
import org.eclipse.jface.viewers.ISelection;
|
||||
import org.eclipse.jface.viewers.IStructuredSelection;
|
||||
import org.eclipse.ui.IEditorPart;
|
||||
import org.eclipse.ui.IPartListener;
|
||||
import org.eclipse.ui.ISelectionListener;
|
||||
import org.eclipse.ui.IWorkbenchPage;
|
||||
import org.eclipse.ui.IWorkbenchPart;
|
||||
|
||||
/**
|
||||
*
|
||||
* The text hovering support for C/C++ debugger.
|
||||
*
|
||||
* @since: Sep 12, 2002
|
||||
*/
|
||||
public class DebugTextHover implements ICEditorTextHover
|
||||
{
|
||||
public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, ISelectionListener, IPartListener {
|
||||
|
||||
static final private int MAX_HOVER_INFO_SIZE = 100;
|
||||
|
||||
protected ISelection fSelection = null;
|
||||
|
||||
protected IEditorPart fEditor;
|
||||
|
||||
/**
|
||||
* Constructor for DebugTextHover.
|
||||
*/
|
||||
public DebugTextHover()
|
||||
{
|
||||
public DebugTextHover() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.eclipse.jface.text.ITextHover#getHoverInfo(ITextViewer, IRegion)
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
|
||||
*/
|
||||
public String getHoverInfo( ITextViewer textViewer, IRegion hoverRegion )
|
||||
{
|
||||
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
|
||||
{
|
||||
public String getHoverInfo( ITextViewer textViewer, IRegion hoverRegion ) {
|
||||
ICStackFrame frame = getFrame();
|
||||
if ( frame != null && frame.canEvaluate() ) {
|
||||
try {
|
||||
IDocument document = textViewer.getDocument();
|
||||
if ( document == null )
|
||||
return null;
|
||||
|
||||
String expression = document.get( hoverRegion.getOffset(), hoverRegion.getLength() );
|
||||
if ( expression == null )
|
||||
return null;
|
||||
expression = expression.trim();
|
||||
if ( expression.length() == 0 )
|
||||
return null;
|
||||
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(targets[i] );
|
||||
}
|
||||
return null;
|
||||
StringBuffer buffer= new StringBuffer();
|
||||
String result = evaluateExpression( frame, expression );
|
||||
if ( result == null )
|
||||
return null;
|
||||
try {
|
||||
if ( result != null )
|
||||
appendVariable( buffer, makeHTMLSafe( expression ), makeHTMLSafe( result.trim() ) );
|
||||
}
|
||||
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 );
|
||||
if ( ee.canEvaluate() )
|
||||
{
|
||||
String result = evaluateExpression( ee, expression );
|
||||
try
|
||||
{
|
||||
if ( result != null )
|
||||
appendVariable( buffer, expression, result.trim(), showDebugTarget ? target.getName() : null );
|
||||
}
|
||||
catch( DebugException x )
|
||||
{
|
||||
CDebugUIPlugin.log( x );
|
||||
}
|
||||
}
|
||||
catch( DebugException x ) {
|
||||
CDebugUIPlugin.log( x );
|
||||
}
|
||||
if ( buffer.length() > 0 )
|
||||
{
|
||||
if ( buffer.length() > 0 ) {
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
catch ( BadLocationException x )
|
||||
{
|
||||
catch( BadLocationException x ) {
|
||||
CDebugUIPlugin.log( x );
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.eclipse.jface.text.ITextHover#getHoverRegion(ITextViewer, int)
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
|
||||
*/
|
||||
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 );
|
||||
*/
|
||||
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 )
|
||||
{
|
||||
private String evaluateExpression( ICStackFrame frame, String expression ) {
|
||||
String result = null;
|
||||
try
|
||||
{
|
||||
result = ee.evaluateExpressionToString( expression );
|
||||
try {
|
||||
result = frame.evaluateExpressionToString( expression );
|
||||
}
|
||||
catch( DebugException e )
|
||||
{
|
||||
catch( DebugException e ) {
|
||||
// ignore
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* A variable gets one line for each debug target it appears in.
|
||||
* Append HTML for the given variable to the given buffer
|
||||
*/
|
||||
private static void appendVariable( StringBuffer buffer,
|
||||
String expression,
|
||||
String value,
|
||||
String debugTargetName ) throws DebugException
|
||||
{
|
||||
private static void appendVariable( StringBuffer buffer, String expression, String value ) throws DebugException {
|
||||
if ( value.length() > MAX_HOVER_INFO_SIZE )
|
||||
value = value.substring( 0, MAX_HOVER_INFO_SIZE ) + " ..."; //$NON-NLS-1$
|
||||
buffer.append( "<p>" ); //$NON-NLS-1$
|
||||
if ( debugTargetName != null )
|
||||
{
|
||||
buffer.append( '[' + debugTargetName + "] " ); //$NON-NLS-1$
|
||||
}
|
||||
buffer.append( makeHTMLSafe( expression ) );
|
||||
buffer.append( " = " ); //$NON-NLS-1$
|
||||
|
||||
String safeValue = "<b>" + makeHTMLSafe( value ) + "</b>"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
buffer.append( safeValue );
|
||||
buffer.append( "<pre>" ).append( expression ).append( "</pre>" ); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
buffer.append( " =" ); //$NON-NLS-1$
|
||||
buffer.append( "<b><pre>" ).append( value ).append( "</pre></b>" ); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
buffer.append( "</p>" ); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.ui.text.c.hover.ICEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
|
||||
*/
|
||||
public void setEditor( IEditorPart editor ) {
|
||||
if ( editor != null ) {
|
||||
fEditor = editor;
|
||||
final IWorkbenchPage page = editor.getSite().getPage();
|
||||
page.addSelectionListener( IDebugUIConstants.ID_DEBUG_VIEW, this );
|
||||
page.addPartListener( this );
|
||||
// initialize selection
|
||||
Runnable r = new Runnable() {
|
||||
|
||||
public void run() {
|
||||
fSelection = page.getSelection( IDebugUIConstants.ID_DEBUG_VIEW );
|
||||
}
|
||||
};
|
||||
CDebugUIPlugin.getStandardDisplay().asyncExec( r );
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
|
||||
*/
|
||||
public void selectionChanged( IWorkbenchPart part, ISelection selection ) {
|
||||
fSelection = selection;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
|
||||
*/
|
||||
public void partActivated( IWorkbenchPart part ) {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
|
||||
*/
|
||||
public void partBroughtToTop( IWorkbenchPart part ) {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
|
||||
*/
|
||||
public void partClosed( IWorkbenchPart part ) {
|
||||
if ( part.equals( fEditor ) ) {
|
||||
IWorkbenchPage page = fEditor.getSite().getPage();
|
||||
page.removeSelectionListener( IDebugUIConstants.ID_DEBUG_VIEW, this );
|
||||
page.removePartListener( this );
|
||||
fSelection = null;
|
||||
fEditor = null;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
|
||||
*/
|
||||
public void partDeactivated( IWorkbenchPart part ) {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
|
||||
*/
|
||||
public void partOpened( IWorkbenchPart part ) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the evaluation stack frame, or <code>null</code> if none.
|
||||
*
|
||||
* @return the evaluation stack frame, or <code>null</code> if none
|
||||
*/
|
||||
protected ICStackFrame getFrame() {
|
||||
if ( fSelection instanceof IStructuredSelection ) {
|
||||
IStructuredSelection selection = (IStructuredSelection)fSelection;
|
||||
if ( selection.size() == 1 ) {
|
||||
Object el = selection.getFirstElement();
|
||||
if ( el instanceof IAdaptable ) {
|
||||
return (ICStackFrame)((IAdaptable)el).getAdapter( ICStackFrame.class );
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
|
||||
*/
|
||||
public IInformationControlCreator getHoverControlCreator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace any characters in the given String that would confuse an HTML
|
||||
* parser with their escape sequences.
|
||||
|
@ -209,10 +246,4 @@ public class DebugTextHover implements ICEditorTextHover
|
|||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.ui.text.c.hover.ICEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
|
||||
*/
|
||||
public void setEditor(IEditorPart editor) {
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue