Implementing Eclipse Editor Plugin

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):

  1. Creating parser for the language
  2. Creating Editor plugin
  3. Code Assist
  4. Error recovery in parser
  5. Code Outline
  6. Reconciler
  7. Syntax coloring
  8. Code Folding
  9. Error reporting
  10. 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

67 Replies to “Implementing Eclipse Editor Plugin”

  1. 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

    1. 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.

  2. 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.

    1. 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.

  3. 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?

    1. 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.

  4. 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.

  5. 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.

  6. 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

    1. 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.

    1. 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 .

        1. 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.

  7. 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

  8. 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.

    1. 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).

      1. 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

  9. 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

      1. 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.

    1. 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.

  10. 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

    1. 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).

  11. 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

    1. I think what you have here is output of your parser. Overall you need to do following –

      1. When code assist is invoked, get offset in the document
      2. From the parser output (hopefully you have AST, Abstract Syntax Tree, generated by the parser), find node at the offset
      3. Generate code assist proposals based on the type of node.
        1. 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.

          1. 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

          2. 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.

  12. 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

    1. 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.

      1. 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

          1. 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

  13. 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

    1. 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.

  14. 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

  15. 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.

  16. 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.

    1. 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.

  17. 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.

  18. 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

    1. 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.

      1. 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.

  19. 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

    1. 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?

  20. 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.

Leave a Reply to Anisha Silva Cancel reply

Social