I had presented a tutorial on implementing Eclipse Editor plugin a couple of years ago. Recently when I started implementing a new Eclipse editor, I was looking for that presentation, but couldn’t find it easily. I like to use my blog as reference for myself, so I thought I would post my Eclipse editor tutorial here.
For the tutorial, I created a new language called VGL (Vector Graphics Language) and built Eclipse editor plugin for it. The language is not important, and just serves the purpose for explaining how to create an editor. The tutorial starts with defining the language and then incrementally builds features of a typical Eclipse editor. I created separate projects for each step in the tutorial. There are nine Eclipse projects. Download and import project in Eclipse for each step if you want to see how it is built incrementally. The last project contains all the editor features covered in this tutorial.
Projects (as Zip files):
- Creating parser for the language
- Creating Editor plugin
- Code Assist
- Error recovery in parser
- Code Outline
- Reconciler
- Syntax coloring
- Code Folding
- Error reporting
- Code Hyperlink
I had plans to extend this tutorial to include launcher and debugger (which requires me to implement runtime for the language first), but haven’t got time to do that yet. May be sometime in future…
I was evaluating options for putting presentation in my blog. I like to keep all assets of my blog (code, images, presentations etc.) on my hosting server and do not like to have them distributes across multiple sites. So I ruled out hosting options like SlideShare etc. I then decided to implement a simple presentation viewer myself. Turned out, it wasn’t so difficult. You can see the interface below. All slides are HTML pages. You can navigate to next/prev slides. If you want to go to a specific slide, type the slide number in the text box and press Enter.The interface is not very fancy, but I think it is good enough for the presentation I wanted to share.
-Ram Kulkarni
after making an editor plugin project it is not running
Kindly let me know the steps to set run as workerbench
or the better solution to run an EDITOR plugin progect
Thanks in advance
Create a new Eclipse workspace. Then import any one of the editor tutorial projects – select File | Import. From the wizard, select General | ‘Existing Projects into Workspace’ option. Select any of the tutorial project as the root directory e.g. the last tutorial folder ‘Tutorial -9 (Code Hyperlink)’. You should see the project listed in the Projects list box. Click Finish button and close the wizard. The project should appear in the Navigator or Package Explorer view. Right click on the project select Run As | Eclipse Application menu option. This will launch another instance of Eclipse. In that instance, create a general project and create a file with .vgl extension. This file should be opened in the editor we created in the tutorial. Try out different editor features like code colorization, code assist etc.
hi, i tried your editor plugin and wrote “Function test {” then the highliting and outline went off. Code assistance also didn’t work for me but still very good work.
The parser is written to have curly braces on new lines –
function test
{
}
I know it is does not make much sense to mandate curly braces on new lines, but I wanted to keep the parser simple.
If I could anything in the tutorial then it would be the way colorization is implemented. I would configure PresentationReconciler in the SourceViewConfiguration and then use RuleBaseScanner to define rules for tokens. Currently I am setting styles directly on StyledText of the viewer.
Does the styling work better directly? Because you would have to create many rules if you would use PresentationReconciler i think. Could you explain me what the hyperlink Detector/Presenter is for?
When you have many partitions in your document, then I think PresentationReconciler works better. The framework exactly tells you which partition is to be colorized and from which offset. Implementation in the tutorial colorizes the line being modified. But if colors of surrounding lines also need to be changed because of typing, then it may not work properly. Also using PresentationReconciler makes colorization independent of AST parser. Regarding creating may rules, you can have one rule based scanner per partition, in which you will identify tokens and specify colors for those tokens.
Hyperlink detector is invoked when you hover mouse ans press Command/Ctrl over, for example, function call – this will take you to definition of the function.
Hi
you are doing a great work and explain every thing nicely
would you please help me out in developing a plugin with multiple page editor it is already given in template.
I face the problem when i run it in new eclipse window .
there it shows some errors like home path not set and gits not set .
if possible then plz give information step wise in running a plugin given in template
plz guide me in resolving this problem.
Thanks in advance.
I tried the template and did not get any error. I don’t think you need to set any home path and git.
kindly make some pictorial tutorial for this i hope it will not take much time to run that particular template test it and give it some kind of product form.
Hi Ram,
I Tutorial 2, class VGLParserTest.java, what does the string variable file path refer too.
Should I change it to some folder when I am running the project.
Thanks
You don’t need VGLParserTest.java. I should have removed that. It is a class I had created to test the parser. It is not required to follow the tutorial. The filePath points to test .vgl file to parse, so yes, if you want to execute this class then change its value to a file path on your machine.
Couple you please explain how does the Code Assist works in the project. (the extended version Error recovery in parser). I am not getting it working
Thanks.
Anisha, I am not sure what part of code assist you are not able to get working. Are you not able to get it working in the sample projects I have uploaded? Regarding how code assist works – you need to override getContentAssistant method of SourceViewConfiguration class of your editor. Create an instance of IContentAssistant, and for each content type in your document (VGL editor has only one content type – default), set content assist processor (call setContentAssistProcessor of IContentAssistant).
In you content assist processor class (implementation of IContentAssistProcessor), override computeCompletionProposals method. This method is called whenever editor needs to display content assist proposals. Calculate valid proposals at the given offset in this method and return array of ICompletionProposal instances.
I know this is a very brief description of how code assist works. Take a look at this link – http://wiki.eclipse.org/FAQ_How_do_I_add_Content_Assist_to_my_editor%3F .
Hi Ram
I am not able to get the sample project “Code Assist” to work?
Import “Tutorial 3” project. Right click on the project and run it as Eclipse application. Create a project and create .vgl file. If you press CTRL+SPACE in that file you should see code assist window with proposals like Canvas, Circle etc.
Thanks, so the program is set up only to popup the keywords in the beginning of the line.
No, it also shows code assist for point and line names. Please go though the slides, I have explained syntax of VGL too.
HI,
When i debug the tutorial 9 the program the main thread suspends at Class.getDeclaredConstructors0(boolean) line: not available [native method] and says source not found. I dont have any breakpoint in the program at all. Why does this happen.
And how do I avoid debugging creating the workbench.
Appreciate a response. thanks
What version of Eclipse are you using and what OS. I tried executing the project in the latest Eclipse (4.3) and it worked fine.
Hi, I am using Eclipse Standard/SDK Version: Kepler Service Release 1 Build id: 20130919-0819, on OS windows 7 professional.
When the user press C in the beginning, it displays the words in C. in which method is this done?
appreciate a response at your convenience.
Why is the popup menu coming only in the beginning of the editor. not in the middle of a line
Thanks, Appreciate a reply
If by pop-up menu you mean code assist proposal window, then it is also displayed in the middle of line if any proposal makes sense there. Type following in a .vgl file –
Point 10 10 P1
Point 20 20 P2
Line
Then invoke code assist after ‘Line’. You should see proposals for point variable names (P1 and P2).
The pop up, i meant for the proposal window.
Where is the logic for the Line is defined, ex: When i type Line and then Ctrl+Space the P1 and P2 display in the code assist?
appreciate a reply thank you
I’m try to get the proposal window to update automatically when the user types a letter. how do i do this. was looking at the tutorial 9 but couldn’t really understand it. Can you please explain it.
thanks
Take a look at VGLCompletionProposal.validate method. This method validates if the given proposal is valid, as user types.
Hi Ram,
I created the plugin project and added the below code.
private static final String[] commands = new String[] { “Line”, “Point”,
“Canvas”, “Circle” };
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
int offset) {
ArrayList proposalList = new ArrayList();
for (int i = 0; i < commands.length; i++)
proposalList.add(new CompletionProposal(commands[i], offset, 0, commands[i].length()));
return proposalList.toArray(new ICompletionProposal[proposalList.size()]);
}
Then I add the VGLCompletionProposal in to my project. how do i get the validate method to trigger. I am doing this without a parser.
Hi I would like to know from where the VGLCompletionProposal , validate method triggers.
validate method of ICompletionProposal is called by Eclipse when code assist proposal window is already up and user try to filter proposal buy typing in the editor.
Hi, have a question relating to AST, parser and grammar.
how do I create a grammer and parser in groovy when I have a AST in a txt.
Appreciate a reply
thanks
Do you want to write a parser in Grrovy or you want to write one for Groovy script? If it is the former then I don’t have much information. But if you want to write a parser for Groovy in Java, you might want to take a look at two parser generators – JavaCC and ANTLR. There could also be publicly available Grovvy parsers that you could use.
Hi Ram, Thanks for your efforts but I got a question here. I am student of Software Engineering and doing my fyp. I am stucked at one place…. Actually I have to identify blocks in my java code e.g
class A{//block1
public static void main (String args[]){//block2
int a=3;
if(a>3)//block3
System.out.println(“Good”);
}
}
So my program should compile a java source file and mark block nos everywhere in code.
For this I thought to use code folding technique as code folding is done for a specific block. So it means that firstly a block is identified then it is folded. So I want to reuse the first part of identifying block and editing second part by numbering blocks instead of folding them.
So how can I use your plugin??
A positive and problem solving reply will be appreciative.
Thanks …
Regards
Jamil Shahzad
You don’t need Eclipse editor plugin if you just want to identify code blocks. You will need to implement a parser, generate AST (Abstract Syntax Tree) and then traverse the tree to find statement blocks.
I would suggest you take a look at two parser generators ANTLR and JavaCC. You can easily find grammer files for these parser generators – for ANTLR, you can find Java grammer at http://www.antlr3.org/grammar/list.html. And for JavaCC you can find it in the examples folder (once you download and unzip JavaCC).
Hi Ram,
I have a question regarding the parser. I want to implement the content assist and I’m having difficulty in. The sample parse is below.
id=app1 [String]
#type=app [String]
mainView=top
name=First App [String]
children: size=1
=== child[0] ===
class=View
id=top [String]
#type=View [String]
#line=8 [Integer]
#info=
name=View [String]
parent=app1
#pos=0 [String]
children: size=4
=== child[0.0] ===
class=Widget
id=l1 [String]
#type=Label [String]
text=Hello [String]
#line=9 [Integer]
#info=
name=Label [String]
parent=top
#pos=0.0 [String]
=== child[0.1] ===
class=Widget
id=b1 [String]
#type=Button [String]
text=Press [String]
#line=10 [Integer]
#info=
name=Button [String]
parent=top
#pos=0.1 [String]
=== child[0.2] ===
class=Widget
id=b2 [String]
#type=Button [String]
text=Exit [String]
#line=11 [Integer]
#info=
name=Button [String]
parent=top
#pos=0.2 [String]
=== child[0.3] ===
class=View
id=row3 [String]
#type=Panel [String]
orientation=horizontal [String]
#line=12 [Integer]
#info=
name=Panel [String]
parent=top
#pos=0.3 [String]
children: size=2
=== child[0.3.0] ===
class=Widget
id=l2 [String]
#type=Label [String]
text=Name [String]
#line=13 [Integer]
#info=
name=Label [String]
parent=row3
#pos=0.3.0 [String]
=== child[0.3.1] ===
class=Widget
id=t1 [String]
#type=Text [String]
text=Enter text [String]
#line=14 [Integer]
#info=
name=Text [String]
parent=row3
#pos=0.3.1 [String]
How do i use this to implement the code assist. Where do I start from.Greatly appreciate a response.
Thanks
I think what you have here is output of your parser. Overall you need to do following –
Hi Ram, as mentioned in point 2 how do I get the AST tree from the parser I have? Thanks.
So do I have to write the text that I posted (which is in a txt file) in to the .jjt file to generate the AST tree? Appreciate a reply on this
thanks
To be clear, you can implement code assist without AST also. all you need to know is what code assist proposals make sense at a given position in the document. Typically you will need to know the context in which code assist was invoked, like code surrounding it before you make decision on what proposals to show. AST makes understanding this context easier and more accurate. You could do it without AST too.
I don’t know how you created the txt file you posted earlier. Bust based on that txt file if you could find the context and calculate code assist proposals, then you don’t need AST.
If you want to use a parser generated by JavaCC then I suggest you take a look at JavaCC documentation.
Hi Ram, I’m not that familiar with this to implement it without the AST. Question on the VGL grammar – I was going through the vvl_grammer.jjt file. I want to know where is the grammar for Point is defined in. e.g. Point 10 30 P1. Is it defined in void Point() :{Token tk = null;} .I don’t see the p1 defined in the grammar.
Thanks
Point is defined in the grammar. it’s definition is four terminals – <POINT> <INTEGER_LITERAL> <INTEGER_LITERAL> <IDENTIFIER>. In the code P1 will be processed as <IDENTIFIER>.
This is not an easy topic that I can explain in blog comments. I sincerely suggest that you read up JavaCC documents and tutorials.
Hi Ram could you point to a tutorial that would explain it more.
1. How do I connect this to the eclipse code.? 2. Do I have to code to get the AST ?
Where do I start in implementing this. Appreciate a help.
Thanks
I have already explained this in the presentation.
For example, creating language parser is from slide #5 and implementing code assist starts from slide #14.
I have also provided links to download project for each editor feature.
yes, I looked into it several time and i’m still not clear on it. Is there additional post of yours or any other link you can direct me too. I looked in a lot, but there is only a few resources on this area. appreciate a reply, thanks
Not sure if you looked at Eclipse’s built-in Plugin help. Go to Help->Help Contents menu and then “Platform Plug-in Developer Guide”->”Programmer’s Guide”->Editor. Also take a look at Eclipse FAQ (especially Part III — The Eclipse IDE Platform) at http://wiki.eclipse.org/The_Official_Eclipse_FAQs
thanks i did look into these.
Sorry for all the question, im trying to understand how you have implemented the parser and created the AST.
Did u first write the .jj & .jjt file in JavaCC, and then create the ASTButton.java etc file.
Appreciate a reply, thanks
I wrote .jjt file first, and run it though JavaCC compiler (there is a JavaCC Ant task available or you can use JavaCC Eclipse plugin). This creates .jj file and all other classes starting with AST.
Hi planing a Eclipse Editor Plug IN. I want to copy the openDeclaration functinallity, like if you press f3 in eclipse. Do you have an idea where i should start? Is hyperlinking the rigth way to do it? or Are there any other way? Maybe you can help me, please. Matt
Yes, hyperlink is one of the ways. I explained that from slide #40. I haven’t implemented F3, but I guess some of the code you write for hyperlink detection would be useful in the case of handling command and key binding for F3.
Hi Ram, Question regarding vgl_grammer.jjt (Tutorial 9). I deleted all the files in ram.tutorial.editor.parser folder except the vgl_grammer.jjt & VGLParserBase.java files. When I click the .jjt file and Complie as JavaCC all the files other files are generated, but there is a few errors showing up in the VGLParser.java. (setPointName, setLineName, setFunctionName, setClosingCurlyBraceToken, addSkippedToken etc methods are not identifies). What do I have to to avoid these errors. Appreciate a reply, Thanks
Hi Ram, I came up with the .jjt for my grammar, and Im getting an error saying the ‘ thyme hierarchy is not inconsistent for MADLParser’. Is there a way i can correct it. (the q is in http://stackoverflow.com/questions/22080452/javacc-grammar-and-jjt)
thanks
Hi ram ,help me with this.i want to create drag and drop plugin like Windowbuilder.i want a view with button from where if i drag and drop on java editor it should generate java code on java editor.heres link to my question http://stackoverflow.com/questions/22253936/eclipse-drag-and-drop-on-text-editor .thanks inadvance
I have not implemented drag-n-drop in Eclipse plugins. But this link might be useful to you – http://www.eclipse.org/articles/Article-Workbench-DND/drag_drop.html
Good afternoon,
Mr Ram Kulkarni, my name is Guillermo Olarte and I’m making a plug in for eclipse focused on best practices in programming and using JFLEX cup for the scanner, I wonder if you could guide me on how to send a result to “bulb” (warning) of eclipse.
If you are taking about error/warning reporting in Eclipse, then download Error reporting demo project from this blog post. I have also briefly discussed this in slide the slide #38
hi sir,
i am developing a development tool for a new language. the compiler is already defined. and i dont have the grammer. i just know the structure and keywords of the language. how can i implement a model for that language like java model in jdt core. and how can i compare it with the file.
You could implement colorization without knowing the grammar. Override getPresentationReconciler of your SourceViewConfiguration class, create DefaultDamagerRepairer with your own RuleBasedScanner. See http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/guide/editors_highlighting.htm.
Code assist can also be implemented (though not very accurately) without knowing grammar, by inspecting code around the position where code assist is invoked. Syntax checking can also be implemented if compiler is already implemented and you can get errors from compiler.
But I think for features like Outline, code hyper link, etc. you will need to know the grammar.
Sir,
This blog post was very useful and it helped a lot to develop an editor plugin. Will you please add build and debug for the same.If you upload the rest of the source code,it will be a big help as we are following your source code.
Sir, i am doing an editor with syntax coloring.I need to follow the same approch that you follow .I try to change the keywords in your source code.but i didnt know where exactly i need to change the string.will you please specify the same
VLGColorizer class handles code colorization in my example. Colors are initilized in loadColors method and colors are actually assigned in colorizeNode method, depending on type of the node.
i need to change the keywords which are given in source code of parser. i try to change that but its doesn’t shows any change when i run the test case. i change function keyword to channel . but when i run the test file if i change that to channel it shows errors . exactly where i need to change the keywords.
If you want to change the grammar then do that in vlg_grammer.jjt file. Note that you will have to generate parser again using JavaCC compiler.
thank you sir..
sir ,
i follow your code for doing my plugin. with that i did sytax coloring , error viewing and i did my own wizards for creating new file and project and i did project explorer view. now i need to run my code. i go through many documents and i dont have any idea how to do that.. will you please help me to do that.. or give some sugestions. if u can please complete your post with builder and debuger, then it will be a big help for beginers like me.. will please help me
Implementing run and debug configurations for my tutorial is going to take time, which right now, I am not in a position to allocate to.
But I will point you to two Eclipse articles which explain how to implement them –
1. We Have Lift-off: The Launching Framework in Eclipse
2. How to write an Eclipse debugger
Dear ammu
I just want to do the same thing you have down. I want to develop an eclipse plugin for my own language. I am confused on a plugin for project. Can you send me materials for developing an eclipse project wizard plugin?
Sorry for the late reply… actually what you exactly want?? just a wizard to create new project ,????
Seems nice. But I still could not piece key elements together: why do we need two parsers? Can we exploit only one, ANTLR parser and the tree it has built not only for Outlining but also for partitioning/coloring? I wonder what happens if you do not update the Outline as you type. I had to create a question at the place where they are closed before answered, http://programmers.stackexchange.com/questions/273417.
Hi Guys ,
I need help….
I want to add Link With Editor in my editor using java J2SE and I am new to this. I am not getting idea from where to start.