下面列出了怎么用org.eclipse.jface.text.ITextHover的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("deprecation")
@Test public void testNullHoverList () {
AbstractCompositeHover hover = new AbstractCompositeHover() {
@Override
protected List<ITextHover> createHovers() {
return null;
}
};
assertEquals (null, hover.getHovers());
assertNull (hover.getHoverRegion(editor.getInternalSourceViewer(), 0));
assertNull (hover.getHoverControlCreator());
assertNull (hover.getHoverInfo(editor.getInternalSourceViewer(), new Region(0,0)));
assertNull (hover.getHoverInfo2(editor.getInternalSourceViewer(), new Region(0,0)));
}
@SuppressWarnings("deprecation")
@Test public void testEmptyHoverList () {
AbstractCompositeHover hover = new AbstractCompositeHover() {
@Override
protected List<ITextHover> createHovers() {
List<ITextHover> hovers = Lists.newArrayList();
return hovers;
}
};
assertEquals (0, hover.getHovers().size());
assertNull (hover.getHoverRegion(editor.getInternalSourceViewer(), 0));
assertNull (hover.getHoverControlCreator());
assertNull (hover.getHoverInfo(editor.getInternalSourceViewer(), new Region(0,0)));
assertNull (hover.getHoverInfo2(editor.getInternalSourceViewer(), new Region(0,0)));
}
@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
if (getHovers() != null) {
for (ITextHover hover : getHovers()) {
IRegion region = hover.getHoverRegion(textViewer, offset);
if (region != null) {
// We always take the first that answers with a region
// In org.eclipse.xtext.ui.editor.hover.DefaultCompositeHover.createHovers() the AnnotationWithQuickFixesHover
// is always the first and answers with a region only when there is a problemmarker
// In all other cases an instance of org.eclipse.xtext.ui.editor.hover.DispatchingEObjectTextHover is the next in the order.
currentHover = hover;
return region;
}
}
}
currentHover = null;
return null;
}
/**
* {@inheritDoc}
*/
@Override
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
// Check through the contributed hover providers.
for (ITextHover hoverContributer : hoverContributors) {
@SuppressWarnings("deprecation")
String hoverText = hoverContributer.getHoverInfo(textViewer,
hoverRegion);
if (hoverText != null) {
lastReturnedHover = hoverContributer;
return hoverText;
}
}
return String.valueOf(getHoverInfo2(textViewer, hoverRegion));
}
@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
HtmlOffsetPair html = htmlOffsetPair(offset, textViewer);
if (html == null) {
return super.getHoverRegion(textViewer, offset);
}
synchronized (sourceViewer) {
try {
sourceViewer.setDocument(
DotEditorUtils.getDocument(injector, html.code));
} catch (Exception e) {
return super.getHoverRegion(textViewer, offset);
}
if (hover instanceof ITextHover) {
IRegion htmlRegion = ((ITextHover) hover)
.getHoverRegion(sourceViewer, offset - html.offset);
if (htmlRegion == null) {
return null;
}
return new Region(html.offset + htmlRegion.getOffset(),
htmlRegion.getLength());
}
}
return super.getHoverRegion(textViewer, offset);
}
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
checkTextHovers();
fBestHover= null;
if (fInstantiatedTextHovers == null)
return null;
for (Iterator<IJavaEditorTextHover> iterator= fInstantiatedTextHovers.iterator(); iterator.hasNext(); ) {
ITextHover hover= iterator.next();
if (hover == null)
continue;
String s= hover.getHoverInfo(textViewer, hoverRegion);
if (s != null && s.trim().length() > 0) {
fBestHover= hover;
return s;
}
}
return null;
}
@Override
public void setSourceViewer(ISourceViewer sourceViewer) {
if (getHovers() != null) {
for (ITextHover hover : getHovers()) {
if (hover instanceof ISourceViewerAware)
((ISourceViewerAware) hover).setSourceViewer(sourceViewer);
}
}
}
@Override
protected List<ITextHover> createHovers() {
List<ITextHover> list = new ArrayList<ITextHover>();
list.add (annotationHover);
if(htmlHover instanceof ITextHover)
list.add ((ITextHover) htmlHover);
return list;
}
@Override
public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
ITextHover hover = textHoverProvider.get();
if (hover instanceof ISourceViewerAware) {
((ISourceViewerAware) hover).setSourceViewer(sourceViewer);
}
return hover;
}
public ModulaEditorTextHover(ITextEditor editor, ITextHover defaultHover, ResourceRegistry resourceRegistry) {
this.editor = editor;
if (editor != null) {
this.project = CoreEditorUtils.getIProjectFrom(editor.getEditorInput());
}
else{
this.project = null;
}
this.defaultHover = defaultHover;
this.fDecorationColor = JFaceResources.getColorRegistry().get(JFacePreferences.DECORATIONS_COLOR);
this.resourceRegistry = resourceRegistry;
ModulaEditorHoverRegistry.get().contributions().stream()
.forEach(ModulaEditorTextHover::addContributer);
}
/**
* {@inheritDoc}
*/
@Override
public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
// Overridden from ITextHoverExtension2. We try and return the richest
// help available; this
// means trying to call getHoverInfo2() on any contributors, and falling
// back on getHoverInfo().
lastReturnedHover = null;
// Check through the contributed hover providers.
for (ITextHover hoverContributer : hoverContributors) {
if (hoverContributer instanceof ITextHoverExtension2) {
Object hoverInfo = ((ITextHoverExtension2) hoverContributer)
.getHoverInfo2(textViewer, hoverRegion);
if (hoverInfo != null) {
lastReturnedHover = hoverContributer;
return hoverInfo;
}
} else {
@SuppressWarnings("deprecation")
String hoverText = hoverContributer.getHoverInfo(textViewer,
hoverRegion);
if (hoverText != null) {
lastReturnedHover = hoverContributer;
return hoverText;
}
}
}
return getModulaDocHover(textViewer, hoverRegion);
}
@Override
public ITextHover getTextHover(ISourceViewer sourceViewer,
String contentType) {
if (contentType.equals(
DotTerminalsTokenTypeToPartitionMapper.HTML_STRING_PARTITION)) {
ITextHover hover = htmlHoverProvider.get();
if (hover instanceof ISourceViewerAware) {
((ISourceViewerAware) hover).setSourceViewer(sourceViewer);
}
return hover;
}
return super.getTextHover(sourceViewer, contentType);
}
@Override
public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
if (TLAPartitionScanner.TLA_PCAL.equals(contentType)) {
return new PCalHover();
}
return new ToolboxHover();
}
@Override
public ITextHover getTextHover(final ISourceViewer sourceViewer, String contentType) {
return new DefaultTextHover(sourceViewer) {
@Override
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
return coverage.getHoverInfo(hoverRegion.getOffset());
}
};
}
@SuppressWarnings("deprecation")
@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset)
{
activeTextHover = null;
List<TextHoverDescriptor> descriptors = getEnabledTextHoverDescriptors(textViewer, offset);
for (TextHoverDescriptor descriptor : descriptors)
{
ITextHover textHover = descriptor.createTextHover();
IRegion region = null;
if (textHover != null)
{
region = textHover.getHoverRegion(textViewer, offset);
}
if (region != null)
{
if (descriptors.size() > 1)
{
if (textHover instanceof ITextHoverExtension2)
{
if (((ITextHoverExtension2) textHover).getHoverInfo2(textViewer, region) == null)
{
continue;
}
}
else if (textHover.getHoverInfo(textViewer, region) == null)
{
continue;
}
}
activeTextHover = textHover;
return region;
}
}
return super.getHoverRegion(textViewer, offset);
}
/**
* Creates text hover
* @return
*/
public ITextHover createTextHover() {
try {
return (ITextHover) configurationElement.createExecutableExtension(ATT_CLASS);
} catch (CoreException e) {
IdeLog.logError(CommonEditorPlugin.getDefault(), e);
}
return null;
}
@Override
public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
JavaEditorTextHoverDescriptor[] hoverDescs= JavaPlugin.getDefault().getJavaEditorTextHoverDescriptors();
int i= 0;
while (i < hoverDescs.length) {
if (hoverDescs[i].isEnabled() && hoverDescs[i].getStateMask() == stateMask)
return new JavaEditorTextHoverProxy(hoverDescs[i], getEditor());
i++;
}
return null;
}
/**
* Returns the information which should be presented when a hover or persistent popup is shown
* for the specified hover region.
*
* @param textViewer the viewer on which the hover popup should be shown
* @param hoverRegion the text range in the viewer which is used to determine the hover display
* information
* @param forInformationProvider <code>true</code> iff the hover info is requested by the
* information presenter. In this case, the method only considers text hovers for
* which a proper IInformationControlCreator is available that can supply focusable
* and resizable information controls.
*
* @return the hover popup display information, or <code>null</code> if none available
*
* @see ITextHoverExtension2#getHoverInfo2(ITextViewer, IRegion)
* @since 3.8
*/
public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion, boolean forInformationProvider) {
checkTextHovers();
fBestHover= null;
if (fInstantiatedTextHovers == null)
return null;
for (Iterator<IJavaEditorTextHover> iterator= fInstantiatedTextHovers.iterator(); iterator.hasNext(); ) {
ITextHover hover= iterator.next();
if (hover == null)
continue;
if (hover instanceof ITextHoverExtension2) {
Object info= ((ITextHoverExtension2) hover).getHoverInfo2(textViewer, hoverRegion);
if (info != null && !(forInformationProvider && getInformationPresenterControlCreator(hover) == null)) {
fBestHover= hover;
return info;
}
} else {
String s= hover.getHoverInfo(textViewer, hoverRegion);
if (s != null && s.trim().length() > 0) {
fBestHover= hover;
return s;
}
}
}
return null;
}
private static IInformationControlCreator getInformationPresenterControlCreator(ITextHover hover) {
if (hover instanceof IInformationProviderExtension2) // this is wrong, but left here for backwards compatibility
return ((IInformationProviderExtension2)hover).getInformationPresenterControlCreator();
if (hover instanceof AbstractJavaEditorTextHover) {
return ((AbstractJavaEditorTextHover) hover).getInformationPresenterControlCreator();
}
return null;
}
public ITextHover getTextHover( ISourceViewer sourceViewer,
String contentType, int stateMask )
{
if ( !( JSPartitionScanner.JS_COMMENT.equals( contentType )
//|| JSPartitionScanner.JS_KEYWORD.equals( contentType )
|| JSPartitionScanner.JS_STRING.equals( contentType ) ) )
{
return new ScriptDebugHover( );
}
return super.getTextHover( sourceViewer, contentType, stateMask );
}
@Override
public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
/**
* Return the combining hover if the preferences are set accordingly and the state mask matches.
*/
if (PyHoverPreferencesPage.getCombineHoverInfo()) {
PyEditorTextHoverDescriptor combiningHover = PydevPlugin.getCombiningHoverDescriptor();
if (combiningHover.getStateMask() == stateMask) {
return new PyEditorTextHoverProxy(combiningHover, contentType);
}
}
/**
* We return the highest priority registered hover whose state mask matches. If two or more hovers
* have the highest priority, it is indeterminate which will be selected. The proper way to combine
* hover info is to select that option on the PyDev->Editor->Hover preference page. This will cause
* the combining hover to be returned by the code above.
*/
PyEditorTextHoverDescriptor[] hoverDescs = PydevPlugin.getDefault().getPyEditorTextHoverDescriptors();
int i = 0;
while (i < hoverDescs.length) {
if (hoverDescs[i].isEnabled() && hoverDescs[i].getStateMask() == stateMask) {
return new PyEditorTextHoverProxy(hoverDescs[i], contentType);
}
i++;
}
return null;
}
/**
* @Override
*/
public ITextHover getTextHover( ISourceViewer sourceViewer, String contentType){
return new MyTextHover();
}
@Override
public void setTextHover(ITextHover textViewerHover, String contentType) {
throw new UnsupportedOperationException();
}
@Override
public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
return null;
}
@Override
public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
return null;
}
protected IRegion computeRegion(ITextViewer textViewer, final int offset) {
if(hover instanceof ITextHover)
return ((ITextHover) hover).getHoverRegion(textViewer, offset);
return new Region(offset, 0);
}
public List<ITextHover> getHovers() {
if (hovers == null)
hovers = createHovers();
return hovers;
}
@Override
public void setTextHover(ITextHover textViewerHover, String contentType) {
throw new UnsupportedOperationException();
}
@Override
public void setTextHover(ITextHover textViewerHover, String contentType) {
throw new UnsupportedOperationException();
}
/**
* This allows text hover on Jimple Editor
*/
public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
return new SootAttributesJimpleHover(getEditor());
}