1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 09:25:31 +02:00

fix for 40101

This commit is contained in:
Chris Recoskie 2006-07-05 18:06:57 +00:00
parent d65e25dbd5
commit da21b10884
2 changed files with 255 additions and 141 deletions

View file

@ -23,7 +23,9 @@ 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.text.Region;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.commands.ICommand;
@ -31,89 +33,110 @@ import org.eclipse.ui.commands.IKeySequenceBinding;
import org.eclipse.ui.keys.KeySequence;
/**
* AbstractCEditorTextHover
* Abstract class for providing hover information for C elements.
* AbstractCEditorTextHover Abstract class for providing hover information for C
* elements.
*
*/
public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverExtension {
public class AbstractCEditorTextHover implements ICEditorTextHover,
ITextHoverExtension {
private IEditorPart fEditor;
private ICommand fCommand;
// {
// ICommandManager commandManager= PlatformUI.getWorkbench().getCommandSupport().getCommandManager();
// fCommand= commandManager.getCommand(ICEditorActionDefinitionIds.SHOW_JAVADOC);
// if (!fCommand.isDefined())
// fCommand= null;
// }
// {
// ICommandManager commandManager=
// PlatformUI.getWorkbench().getCommandSupport().getCommandManager();
// fCommand=
// commandManager.getCommand(ICEditorActionDefinitionIds.SHOW_JAVADOC);
// if (!fCommand.isDefined())
// fCommand= null;
// }
/*
* @see IJavaEditorTextHover#setEditor(IEditorPart)
*/
public void setEditor(IEditorPart editor) {
fEditor= editor;
fEditor = editor;
}
protected IEditorPart getEditor() {
return fEditor;
}
/*
* @see ITextHover#getHoverRegion(ITextViewer, int)
*/
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
return CWordFinder.findWord(textViewer.getDocument(), offset);
if (textViewer != null) {
/*
* If the hover offset falls within the selection range return the
* region for the whole selection.
*/
Point selectedRange = textViewer.getSelectedRange();
if (selectedRange.x >= 0 && selectedRange.y > 0
&& offset >= selectedRange.x
&& offset <= selectedRange.x + selectedRange.y)
return new Region(selectedRange.x, selectedRange.y);
else {
return CWordFinder.findWord(textViewer.getDocument(), offset);
}
}
return null;
}
// protected ICodeAssist getCodeAssist() {
// if (fEditor != null) {
// IEditorInput input= fEditor.getEditorInput();
// if (input instanceof IClassFileEditorInput) {
// IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
// return cfeInput.getClassFile();
// }
//
// IWorkingCopyManager manager= CUIPlugin.getDefault().getWorkingCopyManager();
// return manager.getWorkingCopy(input);
// }
//
// return null;
// }
// protected ICodeAssist getCodeAssist() {
// if (fEditor != null) {
// IEditorInput input= fEditor.getEditorInput();
// if (input instanceof IClassFileEditorInput) {
// IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
// return cfeInput.getClassFile();
// }
//
// IWorkingCopyManager manager=
// CUIPlugin.getDefault().getWorkingCopyManager();
// return manager.getWorkingCopy(input);
// }
//
// return null;
// }
/*
* @see ITextHover#getHoverInfo(ITextViewer, IRegion)
*/
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
// ICodeAssist resolve= getCodeAssist();
// if (resolve != null) {
// try {
// ICElement[] result= null;
//
// synchronized (resolve) {
// result= resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
// }
//
// if (result == null)
// return null;
//
// int nResults= result.length;
// if (nResults == 0)
// return null;
//
// return getHoverInfo(result);
//
// } catch (CModelException x) {
// CUIPlugin.log(x.getStatus());
// }
// }
// ICodeAssist resolve= getCodeAssist();
// if (resolve != null) {
// try {
// ICElement[] result= null;
//
// synchronized (resolve) {
// result= resolve.codeSelect(hoverRegion.getOffset(),
// hoverRegion.getLength());
// }
//
// if (result == null)
// return null;
//
// int nResults= result.length;
// if (nResults == 0)
// return null;
//
// return getHoverInfo(result);
//
// } catch (CModelException x) {
// CUIPlugin.log(x.getStatus());
// }
// }
return null;
}
/**
* Provides hover information for the given C elements.
*
* @param cElements the C elements for which to provide hover information
* @param cElements
* the C elements for which to provide hover information
* @return the hover information string
*/
protected String getHoverInfo(ICElement[] cElements) {
@ -127,33 +150,39 @@ public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverEx
public IInformationControlCreator getHoverControlCreator() {
return new IInformationControlCreator() {
public IInformationControl createInformationControl(Shell parent) {
return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true), getTooltipAffordanceString());
return new DefaultInformationControl(parent, SWT.NONE,
new HTMLTextPresenter(true),
getTooltipAffordanceString());
}
};
}
/**
* Returns the tool tip affordance string.
*
* @return the affordance string or <code>null</code> if disabled or no key binding is defined
* @return the affordance string or <code>null</code> if disabled or no
* key binding is defined
* @since 3.0
*/
protected String getTooltipAffordanceString() {
// if (!CUIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
// return null;
//
// KeySequence[] sequences= getKeySequences();
// if (sequences == null)
// return null;
//
// String keySequence= sequences[0].format();
// return CHoverMessages.getFormattedString("JavaTextHover.makeStickyHint", keySequence); //$NON-NLS-1$
// if
// (!CUIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
// return null;
//
// KeySequence[] sequences= getKeySequences();
// if (sequences == null)
// return null;
//
// String keySequence= sequences[0].format();
// return
// CHoverMessages.getFormattedString("JavaTextHover.makeStickyHint",
// keySequence); //$NON-NLS-1$
return null;
}
/**
* Returns the array of valid key sequence bindings for the
* show tool tip description command.
* Returns the array of valid key sequence bindings for the show tool tip
* description command.
*
* @return the array with the {@link KeySequence}s
*
@ -161,14 +190,15 @@ public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverEx
*/
private KeySequence[] getKeySequences() {
if (fCommand != null) {
List list= fCommand.getKeySequenceBindings();
List list = fCommand.getKeySequenceBindings();
if (!list.isEmpty()) {
KeySequence[] keySequences= new KeySequence[list.size()];
for (int i= 0; i < keySequences.length; i++) {
keySequences[i]= ((IKeySequenceBinding) list.get(i)).getKeySequence();
KeySequence[] keySequences = new KeySequence[list.size()];
for (int i = 0; i < keySequences.length; i++) {
keySequences[i] = ((IKeySequenceBinding) list.get(i))
.getKeySequence();
}
return keySequences;
}
}
}
return null;
}

View file

@ -23,18 +23,63 @@ 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.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import java.util.regex.*;
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 class LanguageOperators protects some language specific
* operator information used by the DebugTextHover class.
*/
class LanguageOperators {
public String getAssignmentOperator() {
return "=";
}
public String getGreaterThanEqualToOperator() {
return ">=";
}
public String getEqualToOperator() {
return "==";
}
public String getNotEqualToOperator() {
return "!=";
}
public String getLessThenEqualToOperator() {
return "<=";
}
public String getValueChangeOperatorsRegex() {
return "(\\+\\+)|(\\-\\-)|(\\+\\=)|"
+ "(\\-\\=)|(\\*\\=)|(/\\=)|(\\&\\=)"
+ "(\\%\\=)|(\\^\\=)|(\\|\\=)|(\\<\\<\\=)|(\\>\\>\\=)";
}
public String getEqualToOperatorsRegex() {
return "\\=\\=|\\<\\=|\\>\\=|!\\=";
}
public String getIdentifierRegex() {
return "[_A-Za-z][_A-Za-z0-9]*";
}
}
/**
* The text hovering support for C/C++ debugger.
*/
public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, ISelectionListener, IPartListener {
public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension,
ISelectionListener, IPartListener {
static final private int MAX_HOVER_INFO_SIZE = 100;
@ -52,38 +97,75 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
* @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
* org.eclipse.jface.text.IRegion)
*/
public String getHoverInfo( ITextViewer textViewer, IRegion hoverRegion ) {
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
ICStackFrame frame = getFrame();
if ( frame != null && frame.canEvaluate() ) {
if (frame != null && frame.canEvaluate()) {
try {
IDocument document = textViewer.getDocument();
if ( document == null )
if (document == null)
return null;
String expression = document.get( hoverRegion.getOffset(), hoverRegion.getLength() );
if ( expression == null )
String expression = document.get(hoverRegion.getOffset(),
hoverRegion.getLength());
if (expression == null)
return null;
expression = expression.trim();
if ( expression.length() == 0 )
if (expression.length() == 0)
return null;
LanguageOperators operatorsObj = new LanguageOperators();
Pattern pattern = Pattern.compile(operatorsObj
.getValueChangeOperatorsRegex());
Matcher matcher = pattern.matcher(expression);
boolean match_found = matcher.find();
// Get matching string
// If the expression has some operators which can change the
// value of a variable, that expresssion should not be
// evaluated.
if (match_found) {
return null;
} else {
pattern = Pattern.compile(operatorsObj
.getEqualToOperatorsRegex());
String[] tokens = pattern.split(expression);
for (int i = 0; i < tokens.length; i++) {
//If the expression contains assignment operator that
// can change the value of a variable, the expression
// should not be evaluated.
if (tokens[i].indexOf(operatorsObj
.getAssignmentOperator()) != -1)
return null;
}
//Supressing function calls from evaluation.
String functionCallRegex = operatorsObj
.getIdentifierRegex()
+ "\\s*\\(";
pattern = Pattern.compile(functionCallRegex);
matcher = pattern.matcher(expression);
match_found = matcher.find();
if (match_found) {
return null;
}
}
StringBuffer buffer = new StringBuffer();
String result = evaluateExpression( frame, expression );
if ( result == null )
String result = evaluateExpression(frame, expression);
if (result == null)
return null;
try {
if ( result != null )
appendVariable( buffer, makeHTMLSafe( expression ), makeHTMLSafe( result.trim() ) );
if (result != null)
appendVariable(buffer, makeHTMLSafe(expression),
makeHTMLSafe(result.trim()));
} 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 ) {
CDebugUIPlugin.log( x );
} catch (BadLocationException x) {
CDebugUIPlugin.log(x);
}
}
return null;
@ -94,22 +176,21 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
*
* @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
*/
public IRegion getHoverRegion( ITextViewer viewer, 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 );
if (viewer != null)
return CDebugUIUtils.findWord(viewer.getDocument(), offset);
return null;
}
private String evaluateExpression( ICStackFrame frame, String expression ) {
private String evaluateExpression(ICStackFrame frame, String expression) {
String result = null;
try {
result = frame.evaluateExpressionToString( expression );
}
catch( DebugException e ) {
result = frame.evaluateExpressionToString(expression);
} catch (DebugException e) {
// ignore
}
return result;
@ -118,14 +199,15 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
/**
* Append HTML for the given variable to the given buffer
*/
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$
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$
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$
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$
}
/*
@ -133,20 +215,21 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
*
* @see org.eclipse.cdt.ui.text.c.hover.ICEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
*/
public void setEditor( IEditorPart editor ) {
if ( editor != null ) {
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 );
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 );
fSelection = page
.getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
}
};
CDebugUIPlugin.getStandardDisplay().asyncExec( r );
CDebugUIPlugin.getStandardDisplay().asyncExec(r);
}
}
@ -155,7 +238,7 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
*
* @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
*/
public void selectionChanged( IWorkbenchPart part, ISelection selection ) {
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
fSelection = selection;
}
@ -164,7 +247,7 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
*
* @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
*/
public void partActivated( IWorkbenchPart part ) {
public void partActivated(IWorkbenchPart part) {
}
/*
@ -172,7 +255,7 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
*
* @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
*/
public void partBroughtToTop( IWorkbenchPart part ) {
public void partBroughtToTop(IWorkbenchPart part) {
}
/*
@ -180,11 +263,11 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
*
* @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
*/
public void partClosed( IWorkbenchPart part ) {
if ( part.equals( fEditor ) ) {
public void partClosed(IWorkbenchPart part) {
if (part.equals(fEditor)) {
IWorkbenchPage page = fEditor.getSite().getPage();
page.removeSelectionListener( IDebugUIConstants.ID_DEBUG_VIEW, this );
page.removePartListener( this );
page.removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
page.removePartListener(this);
fSelection = null;
fEditor = null;
}
@ -195,7 +278,7 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
*
* @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
*/
public void partDeactivated( IWorkbenchPart part ) {
public void partDeactivated(IWorkbenchPart part) {
}
/*
@ -203,7 +286,7 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
*
* @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
*/
public void partOpened( IWorkbenchPart part ) {
public void partOpened(IWorkbenchPart part) {
}
/**
@ -212,12 +295,13 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
* @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 ) {
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 );
if (el instanceof IAdaptable) {
return (ICStackFrame) ((IAdaptable) el)
.getAdapter(ICStackFrame.class);
}
}
}
@ -236,23 +320,23 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
/**
* Replace any characters in the given String that would confuse an HTML parser with their escape sequences.
*/
private static String makeHTMLSafe( String string ) {
StringBuffer buffer = new StringBuffer( string.length() );
for( int i = 0; i != string.length(); i++ ) {
char ch = string.charAt( i );
switch( ch ) {
case '&':
buffer.append( "&amp;" ); //$NON-NLS-1$
break;
case '<':
buffer.append( "&lt;" ); //$NON-NLS-1$
break;
case '>':
buffer.append( "&gt;" ); //$NON-NLS-1$
break;
default:
buffer.append( ch );
break;
private static String makeHTMLSafe(String string) {
StringBuffer buffer = new StringBuffer(string.length());
for (int i = 0; i != string.length(); i++) {
char ch = string.charAt(i);
switch (ch) {
case '&':
buffer.append("&amp;"); //$NON-NLS-1$
break;
case '<':
buffer.append("&lt;"); //$NON-NLS-1$
break;
case '>':
buffer.append("&gt;"); //$NON-NLS-1$
break;
default:
buffer.append(ch);
break;
}
}
return buffer.toString();