Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Extending the editor
Extending the editor [message #1064359] Tue, 18 June 2013 21:00 Go to next message
Kevin Regan is currently offline Kevin ReganFriend
Messages: 33
Registered: May 2013
I need to extend the Java Editor in eclipse, via plugin, to syntax highlight a particular pattern and also be capable of taking text that fits this pattern and puts it into a custom view - basically, the java code will have variables in it for example:

public class {{^className^}}

and the view, having two columns, would contain a row:

className MyClass

I want to highlight that pattern in the source code (like a yellow background or something) and build in some integration between the custom view and the editor. Also, I will need to, at compile time, do a replace to put the actual value in palce of the variable in order for the compilation to use the value instead of the variable (while still displaying the variable in the editor).

I have read quite a bit now about markers, annotations and also about damage/repair reconciler type things but I'm not really sure which of these is appropriate for what I'm trying to do. I'm also not really sure how to hook them into the plugin via extension points.

If anyone can give me some starting pointers or point me to some decent documentation on all this (I have been googling quite a bit) I'd appreciate it.
Re: Extending the editor [message #1064410 is a reply to message #1064359] Wed, 19 June 2013 06:54 Go to previous messageGo to next message
Luigi P is currently offline Luigi PFriend
Messages: 10
Registered: April 2013
Location: Italy
Junior Member

To start you can extends the Java Editor as follows:
public class MyOwnEditor extends CompilationUnitEditor {
    public MyOwnEditor() {
	javaTextTools = JavaPlugin.getDefault().getJavaTextTools();
        JavaSourceViewerConfiguration sourceViewerConfiguration = 
            new JavaSourceViewerConfiguration(javaTextTools.getColorManager(), 
                this, IJavaPartitions.JAVA_PARTITIONING);

    public void createPartControl(Composite composite) {
        /* Put your listeners here like this
        getSourceViewer().getTextWidget().addLineBackgroundListener(new LineBackgroundListener() {
	    public void lineGetBackground(LineBackgroundEvent event) {
        ... etc

To extract class, methods and other you can use a java parser (
I hope this can help.
Re: Extending the editor [message #1064509 is a reply to message #1064410] Wed, 19 June 2013 14:54 Go to previous messageGo to next message
Kevin Regan is currently offline Kevin ReganFriend
Messages: 33
Registered: May 2013
That example does allow me to create a custom editor that gets the syntax highlighting of the usual java editor - but the auto-indenting doesn't work. I'm worried about this approach because I do not want to have to reinvent the wheel and get the custom editor working like the default java editor. Since the goal is to make some small changes to a java editor for java files - I am hoping to be able to simply hook into the existing default java editor instead of creating my own custom editor via the editors extension point.

It seems like some combination of markers, annotations, or reconciler is where I want to go but my problem is how do I hook into some default java editor evnets in order to create them? Also, recognizing my pattern to highlight it seems to require me adding in a new "scanner" and hooking that scanner to the reconciler.

I want to avoid creating a custom editor since the default java editor has most of what we need I just need to add this bit of functionality to it.

Would greatly appreciate if anybody has any advice - I seem to be stuck at the moment.

[Updated on: Wed, 19 June 2013 20:51]

Report message to a moderator

Re: Extending the editor [message #1065989 is a reply to message #1064509] Fri, 28 June 2013 16:55 Go to previous messageGo to next message
Kevin Regan is currently offline Kevin ReganFriend
Messages: 33
Registered: May 2013
I was able to accomplish this by using the CompilationParticipant extension point which has a "reconcile" function that can be overriden. Within that function I can find my pattern and add a marker at the position to highlight it. The next thing I need to do is remove the problem marker - e.g. in my previous example you get problem markers on the {{^class^}} as it is obviously not valid java syntax. I tried to remove it via:



				IMarker[] problemMarkers = resource.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_ZERO);
				for (IMarker marker : problemMarkers) {
					int problemMarkerStart = marker.getAttribute(IMarker.CHAR_START, -1);
					int problemMarkerEnd = marker.getAttribute(IMarker.CHAR_END, -1);

					if ((problemMarkerStart == myMarker.start()) && (problemMarkerEnd == myMarker.end())) {

The problem is that the findMarkers is not returning anything despite the red X in the vertical ruler and the red squigglys at the location of the {{^class^}}. Anybody know why this is?
Re: Extending the editor [message #1066269 is a reply to message #1065989] Mon, 01 July 2013 19:32 Go to previous messageGo to next message
Kevin Regan is currently offline Kevin ReganFriend
Messages: 33
Registered: May 2013
I debugged into the reconcile step and found that it appears to be doing some reconciliation/parsing of the Java code after it "notifies participants":

// notify reconcile participants only if working copy was not consistent or if forcing problem detection
			// (see
			if (!wasConsistent || ((this.reconcileFlags & ICompilationUnit.FORCE_PROBLEM_DETECTION) != 0)) {

				// recreate ast if one participant reset it
				if (this.ast == null)

So I cannot remove the problem markers in the reconcile step.

Is it at all possible to modify the existing Java AST Parser to make it recognize my pattern and ignore it instead of complain about it? I have been doing some reading on the AST and it seems you can use the visitor pattern to get notified about certain parts of the AST but I have yet to find any indication that you can modify the behavior of the parser that generates the AST so that I can have it ignore my pattern as an error. Is this at all possible? Am I going down the wrong path? Any help greatly appreciated.
Re: Extending the editor [message #1786770 is a reply to message #1066269] Thu, 10 May 2018 23:35 Go to previous message
Fitzroy Nembhard is currently offline Fitzroy NembhardFriend
Messages: 1
Registered: May 2018
Junior Member
Hello guys,
I am interested in using the external editor in my plugin in a similar way to what you are discussing. I have been having some difficulties. First, I am importing the org.eclipse.jdt.internal.ui.javaeditor.CompulationUnitEditor and extending it from a class like so:

public class MyOwnEditor extends CompilationUnitEditor {

but this shows an error that "the hierarchy of the type MyOwnEditor is inconsistent."
How did you guys import the CompilationUnitEditor? Did you have to include an external jar for jdt?
I am importing the class by following the instructions here: [//stackoverflow dot com/a/95354/5390705].
It would be nice if you could provide an example project where you got it to work.

Previous Topic:PKIX Path Building Exception When Updating Eclipse
Next Topic:Null Pointer Exception when compiling Java code
Goto Forum:

Current Time: Thu Apr 15 23:57:29 GMT 2021

Powered by FUDForum. Page generated in 0.02556 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top