1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-10 12:03:16 +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,17 +33,22 @@ 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);
// ICommandManager commandManager=
// PlatformUI.getWorkbench().getCommandSupport().getCommandManager();
// fCommand=
// commandManager.getCommand(ICEditorActionDefinitionIds.SHOW_JAVADOC);
// if (!fCommand.isDefined())
// fCommand= null;
// }
@ -61,8 +68,22 @@ public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverEx
* @see ITextHover#getHoverRegion(ITextViewer, int)
*/
public IRegion getHoverRegion(ITextViewer textViewer, int 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) {
@ -72,14 +93,14 @@ public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverEx
// return cfeInput.getClassFile();
// }
//
// IWorkingCopyManager manager= CUIPlugin.getDefault().getWorkingCopyManager();
// IWorkingCopyManager manager=
// CUIPlugin.getDefault().getWorkingCopyManager();
// return manager.getWorkingCopy(input);
// }
//
// return null;
// }
/*
* @see ITextHover#getHoverInfo(ITextViewer, IRegion)
*/
@ -91,7 +112,8 @@ public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverEx
// ICElement[] result= null;
//
// synchronized (resolve) {
// result= resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
// result= resolve.codeSelect(hoverRegion.getOffset(),
// hoverRegion.getLength());
// }
//
// if (result == null)
@ -113,7 +135,8 @@ public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverEx
/**
* 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,7 +150,9 @@ 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());
}
};
}
@ -135,11 +160,13 @@ public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverEx
/**
* 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))
// if
// (!CUIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
// return null;
//
// KeySequence[] sequences= getKeySequences();
@ -147,13 +174,15 @@ public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverEx
// return null;
//
// String keySequence= sequences[0].format();
// return CHoverMessages.getFormattedString("JavaTextHover.makeStickyHint", keySequence); //$NON-NLS-1$
// 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
*
@ -165,7 +194,8 @@ public class AbstractCEditorTextHover implements ICEditorTextHover, ITextHoverEx
if (!list.isEmpty()) {
KeySequence[] keySequences = new KeySequence[list.size()];
for (int i = 0; i < keySequences.length; i++) {
keySequences[i]= ((IKeySequenceBinding) list.get(i)).getKeySequence();
keySequences[i] = ((IKeySequenceBinding) list.get(i))
.getKeySequence();
}
return keySequences;
}

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,7 +97,8 @@ 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) {
ICStackFrame frame = getFrame();
@ -61,28 +107,64 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
IDocument document = textViewer.getDocument();
if (document == null)
return null;
String expression = document.get( hoverRegion.getOffset(), hoverRegion.getLength() );
String expression = document.get(hoverRegion.getOffset(),
hoverRegion.getLength());
if (expression == null)
return null;
expression = expression.trim();
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)
return null;
try {
if (result != null)
appendVariable( buffer, makeHTMLSafe( expression ), makeHTMLSafe( result.trim() ) );
}
catch( DebugException x ) {
appendVariable(buffer, makeHTMLSafe(expression),
makeHTMLSafe(result.trim()));
} catch (DebugException x) {
CDebugUIPlugin.log(x);
}
if (buffer.length() > 0) {
return buffer.toString();
}
}
catch( BadLocationException x ) {
} catch (BadLocationException x) {
CDebugUIPlugin.log(x);
}
}
@ -108,8 +190,7 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
String result = null;
try {
result = frame.evaluateExpressionToString(expression);
}
catch( DebugException e ) {
} catch (DebugException e) {
// ignore
}
return result;
@ -118,7 +199,8 @@ 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 {
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$
@ -143,7 +225,8 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
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);
@ -217,7 +300,8 @@ public class DebugTextHover implements ICEditorTextHover, ITextHoverExtension, I
if (selection.size() == 1) {
Object el = selection.getFirstElement();
if (el instanceof IAdaptable) {
return (ICStackFrame)((IAdaptable)el).getAdapter( ICStackFrame.class );
return (ICStackFrame) ((IAdaptable) el)
.getAdapter(ICStackFrame.class);
}
}
}