Creating a eclipse plugin with text hover - java

I would like to make a Eclipse plugin (text editor). I would "read" the text under the cursor and show a dynamical generated hover that depends on the text. Now I have the problem that I don't know how I can read the text and "add" the hover.
It's my first Eclipse Plugin so I am happy for each tip I can get.
Edit:
I'd like to integrate it into the default Eclipse Java editor. I have tried to create a new plugin with a editor template but I think it is the wrong way.
Last Edit:
The answer from PKeidel is exactly what I'm looking for :)
Thanks PKeidel

Your fault is that you created a completly new Editor instead of a plugin for the existing Java Editor. Plugins will be activated via extension points. In your case you have to use org.eclipse.jdt.ui.javaEditorTextHovers more....
<plugin>
<extension
point="org.eclipse.jdt.ui.javaEditorTextHovers">
<hover
activate="true"
class="path.to_your.hoverclass"
id="id.path.to_your.hoverclass">
</hover>
</extension>
</plugin>
The class argument holds the path to your Class that implements IJavaEditorTextHover.
public class LangHover implements IJavaEditorTextHover
{
#Override
public String getHoverInfo(ITextViewer textviewer, IRegion region)
{
if(youWantToShowAOwnHover)
return "Your own hover Text goes here"";
return null; // Shows the default Hover (Java Docs)
}
}
That should do it ;-)

Related

Can comments be added as markers in Eclipse RCP?

I am trying to make a plug-in that adds comments as markers to the Javascript editor (org.eclipse.wst.jsdt.internal.ui.javaeditor).
I can see from the tutorials that Eclipse does allow to attach markers to IResources:
<extension
id="org.eclipse.test.marker"
name="org.eclipse.test.marker"
point="org.eclipse.core.resources.markers">
<super
type="org.eclipse.core.resources.textmarker">
</super>
<persistent
value="true">
</persistent>
</extension>
And that these markers can also be attached to annotations, which adds an appearance to them (org.eclipse.ui.editors.markerAnnotationSpecification ).
This works fine for highlighting and changing the appearance of text in the editor.
My questions is, is there any kind of marker that also adds text comments to the editor without actually modifying the document?
As an alternative, I was considering adding the comments programmatically and attaching markers to them, but there are multiple drawbacks in this approach. I was wondering if Eclipse would have an automated alternative:
public void addCommentMarker(String commentText, int Offset) throws CoreException {
//Get the active editor
IEditorPart editorPart=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (!(editorPart instanceof AbstractTextEditor))
return;
ITextEditor editor = (ITextEditor)editorPart;
//Add comment programmatically to the file:
IDocumentProvider dp = editor.getDocumentProvider();
IDocument doc = dp.getDocument(editor.getEditorInput());
try {
doc.replace(Offset, 0, "//"+commentText); //$NON-NLS-1$
} catch (BadLocationException e) {
e.printStackTrace();
}
//Create a marker to monitor the new comment:
IFile file=getFileFromEditorInput(editor.getEditorInput());
IMarker m=file.createMarker(IMarker.TEXT);
//Marker begins when the comment starts:
m.setAttribute(IMarker.CHAR_START, Offset);
//Marker ends when the comment ends:
m.setAttribute(IMarker.CHAR_END, Offset+commentText.length()+2);
}
As mentioned by Nitind, my intention is to create the tag-like functionality. For example in the following screenshot, to be able to add tags programatically with a constant's value (//20).
Sample desired behavior
Since I am new to Eclipse, I am not sure if Eclipse provides an alternative for managing this tags programatically.

Eclipse Plugin: Link MulitPageEditorPart with Navigator

In an Eclipse-Plugin, I have an implementation of a MultiPageEditorPart and it is associated with a file-extension - let's call it f.
When I click on a file f in the standard Package Explorer the Editor is opend. This works fine so far.
But what is not working, is linking this Editor with the Package Explorer. Let's say someone has several open Editors and one of them is the custom MultiPageEditorPart. If the custom Editor becomes selected via the tabs (not the Package Explorer) the corresponding file in the Package Explorer should be selected / marked.
Following the example in "link with editor" for FormEditor I tried to achive this using an ILinkHelper and the Extension Point org.eclipse.ui.navigator.LinkHelper, but it is never called.
How is the ILinkHelper used together with a MultiPageEditorPart?
PS: I'm using Eclipse Oxygen
The MulitpageEditorPart (and other Editors) have an IEditorInput which owns a Method
<T> T getAdapter(Class<T> adapter)
If this Method is called with IResource.class or IFile.class it should return the IResource that is the source for the current Editor. This is then used by Eclipse to link the Editor with the View (Package Explorer).
Like this:
<T> T getAdapter(Class<T> adapter) {
if (IResource.class.isAssignableFrom(adapter) {
return (T) myFile;
}
}

Eclipse Plugin Development: How to access code written in the eclipse editor

I'm making an Eclipse plug-in that requires access to code written in the Eclipse editor. I've followed the process mentioned in the link.
Accessing Eclipse editor code
But it's showing filepath instead of code in the message box. The getEditorInput() of IEditorEditor class isn't doing what it should do according to the link. Here's my code. Please help me find what i'm doing wrong.
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()).getActiveEditor();
IEditorInput input = (IEditorInput) editor.getEditorInput(); // accessing code from eclipse editor
String code = input.toString();
MessageDialog.openInformation(
window.getShell(),
"Project",
code);
return null;
}
And here's snap of the output.
You can do this two different ways. This way works regardless of whether the contents are backed by a file on disk or not.
This method gets the text IDocument from the editor, which is how the contents are stored and accessed for most uses. StyledText is a widget, and unless you are doing something with Widgets and Controls, it's not the right way in. For that you're going to go from the editor part, through the ITextEditor interface, and then use the IDocumentProvider with the current editor input. This is skipping the instanceof check you'd want to do beforehand, as well as anything you might have to do if this is a page in a MultiPageEditorPart (there's no standard way for handling those).
org.eclipse.jface.text.IDocument document =
((org.eclipse.ui.texteditor.ITextEditor)editor).
getDocumentProvider().
getDocument(input);
You can get, and modify, the contents through the IDocument.

Can I use org.eclipse.core.variables... in defining attributes of other extensions

I'm creating an RCP 3.7 editor by using org.eclipse.ui.editors extension point. What I need is to dynamically define icon path based on some conditions during editor startup.
(EDIT: The editor is actually just restored after startup, but it's not selected as active yet, so you can see only tab with title and icon)
I tried to work with getImageDescriptor() method in class implementing IEditorInput, which doesn't seem to be used. The only way that has some effect on the icon is changing the icon path in definition of editor extension.
Therefore I started to play with org.eclipse.core.variables.valueVariables and org.eclipse.core.variables.dynamicVariables for use in icon attribute (showing valueVariables just for easy example):
<extension point="org.eclipse.ui.editors">
<editor name="%Editor_TITLE"
extensions="xml"
icon="${FOO}"
class="org.example.ExampleEditor"
id="org.example.ExampleEditor">
</editor>
</extension>
<extension point="org.eclipse.core.variables.valueVariables">
<variable name="FOO"
initialValue="images/obj16/editor.png">
</variable>
</extension>
However, that doesn't work either. Is there some way to use dynamically defined variable values (based on current condition) that could change the path of icon? ...or I'll be greatfull even for a workaround suggestion, that will lead to successful changing of the icon during startup (like making the ImageDescriptor work no startup).
Variables only work in places where they are explicitly support in the code. If the documentation for an extension point does not say they are supported then they won't work.
You get use the image descriptor from the editor input to set the editor title image by doing something like the following in your editor's init method:
public void init(IEditorSite site, IEditorInput input)
throws PartInitException
{
... other code
ImageDescriptor desc = input.getImageDescriptor();
Image image = desc.createImage();
setTitleImage(image);
... other code
}

Console. Write text with "link" to class/line

Whenever you display a stack trace, you can get a "url-like" text which if you click it opens the appropriate class at the appropriate line.
Is there a possibility to output a text in a way that the console recognizes it and make it clickable like that?
But how would you format the output so it would recognize it as a "link" ?
You can't and you don't.
AndroidStudio supports that feature. You just need to call
exception.printStackTrace() and you should be able to click it in the console tab of IDE.
You will see something like
java.lang.Exception
at whatever.Test.main(Test.java:22)
The text inside the bracket will be highlighted and you can click it.
Based on #JEeemy's answer I managed to do this
public static void printLinkToThisLine() {
System.out.println(Thread.currentThread().getStackTrace()[3]);
}
I works for me ...
I don't know if you're using Eclipse, but the Eclipse console parses based on a pattern: FileName.java:lineNumber.
MyFile.java:3
Would link you to the 3rd line of whatever class you specified as MyFile.
You could use:
.getClass().getName()
to the the name of the file programmatically.

Categories