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.
Related
I'm writing a custom editor in Eclipse and just integrated custom error recognition. Now I'm facing a strange issue: I can add Markers to my editor that get displayed all fine, I can also delete them while the editor is running.
What doesn't work: When I close my editor I want the markers to disappear/get deleted.
What I'm doing right now, is
creating the Markers with the transient property set like this: marker.setAttribute(IMarker.TRANSIENT, true); This doesn't seem to change anything though.
trying to delete all Annotations via the source viewers annotation-model. This doesn't work, cause when I try to hook into my editors dispose() method or add a DisposeListener to my sourceviewers textwidget, the sourceviewer already has been disposed of and getSourceViewer().getAnnotationModel(); returns null.
My deleteMarkers method:
private void deleteMarkers() {
IAnnotationModel anmod = getSourceViewer().getAnnotationModel();
Iterator<Annotation> it = anmod.getAnnotationIterator();
while (it.hasNext()) {
SimpleMarkerAnnotation a = (SimpleMarkerAnnotation) it.next();
anmod.removeAnnotation(a);
try {
a.getMarker().delete();
} catch (CoreException e) {
e.printStackTrace();
}
}
}
Any help is appreciated ^^
Hook into your editor close event, get a reference to the IResource for the editor (i believe you get can that on IEditorInput) and call IResource#deleteMarkers() on the relevant resource which will delete them when you close your editor. By design eclipse does not delete markers when editors are closed.
Here is some reference:
http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/IResource.html#deleteMarkers(java.lang.String, boolean, int)
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.
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
}
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 ;-)
I am working on a multi page editor that loads opens multiple files (e.g. java, html) in separate tabs of a multi page editor. The files get opened with the default editors associated with the file type and these default editors are embedded in the multi page editor as tabs.
Here is how I am determining which editor to load (for a file type):
void createPage() throws PartInitException
{
// get editor registry
IEditorRegistry editorRegistry = Activator.getDefault().getWorkbench().getEditorRegistry();
// loop through mappings until the extension matches.
IEditorDescriptor editorDescriptor = editorRegistry.getDefaultEditor(((IFileEditorInput)getEditorInput()).getFile().getName());
// if no editor was found that is associated to the file extension
if (editorDescriptor == null)
{
IEditorRegistry registry = Activator.getDefault().getWorkbench().getEditorRegistry();
editorDescriptor = registry.findEditor(EditorsUI.DEFAULT_TEXT_EDITOR_ID);
}
IConfigurationElement configuration = ((EditorDescriptor) editorDescriptor).getConfigurationElement();
String className = configuration.getAttribute("class");
IEditorPart editor;
try
{
editor = (IEditorPart) WorkbenchPlugin.createExtension(configuration, "class");
} catch (CoreException e) {
throw new RuntimeException(e);
}
final int index = addPage(editor, getEditorInput());
setPageText(index, "TAB_NAME");
}
The multi tab editor gets created without any problems and the correct editors are loaded within the tabs.
But the ‘Mark Occurrences’ functionality is not working in the Java Editor when loaded in a tab.
I validated that mark occurrences is turned on. When I select a variable in the java editor in my multi page editor tab it does not highlight the other occurrences of the variable.
But if I open the file in my multi tab editor and in a separate java editor at the same time and select a variable in the separate java editor it will highlight the other occurrences in the separate java editor as well as the java editor that is embedded in my multi page editor.
So the functionality seems to be enabled and loaded, it just does not execute the mark occurrences functionality when the selecting happens in the embedded editor.
What needs to be changed so that I can use the mark occurrences functionality from within the java editor that is embedded in my multi tab editor?
My understanding is that Mark Occurences is a central service so I assume I am missing the part that updates this service when something gets selected in my editor. Any idea on what needs to be done so the service gets updated?
Note: This problem only happens if the java editor is embedded in a multi page editor.
This functionality is build into org.eclipse.jdt.internal.ui.javaeditor.JavaEditor of org.eclipse.jdt.ui
As you see it's an internal class. However you can ignore that and subclass it. The org.eclipse.jdt.internal.ui.javaeditor.ToggleMarkOccurrencesAction will work for all open JavaEditors (try opening the same class twice with the standard CompilationUnitEditor and you will see two "mark occurences" markings).This is because a central property PreferenceConstants.EDITOR_MARK_OCCURRENCES is set in the PreferenceStore of the JavaPlugin.
In order to show the ToggleMarkOccurrencesAction Button you will need to provide a IEditorActionBarContributor (Take a look at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditorActionContributor)