Search code examples
eclipse-pluginosgixtext

Using Xtext code in an Eclipse Plugin


I have written two plugins:

1- A text editor based on Xtext 2.3.1 which comprises of four projects. First is the source project, second is the UI, third is SDK, and last is the test. I know how to export this project as a plugin and use it inside Eclipse 4.2 (Juno). This editor works on the extensions of 'myDSL'.

2- A wizard which is triggered from File->Menu and creates a file with the extension 'myDSL'.

I would like to combine these two plugin projects into one. I have read the following section of Xtext documentation: Setup within Eclipse-Equinox (OSGi). Unfortunately, I could not get it to work. First of all, I am not sure if I need to combine the files of two projects or just add the Xtext project as the dependency of the other project. In either case, which one of the Xtext project should I consider? The main project or the UI project? (Actually, I do not know the exact difference between them). Second, I do not know what <MyDsl>ExecutableExtensionFactory should be replaced with.


Solution

  • To combine the Xtext projects and your project, you have to know the following:

    • The «mydsl».ui project contains all GUI (e.g. editor) and Eclipse-specific (e.g. registered builder) stuff; while the «mydsl» project (later on: core) contains basically the parser - this is useable in plain Java applications (outside Eclipse environment). This means, as you want to merge your Eclipse wizard code, you are interested in the ui project.
    • If you move the code from the ui project to your wizard project, then also update the mwe file in the core project. It refers the generated ui project, and is required when you update your grammar to regenerate your projects. If you move your existing code to the ui project, this is not required.
    • Finally, about the ExtensionFactory: this is an Eclipse mechanism to prepare your classes when instantiating them using the Eclipse extension mechanism. The «MyDSL»ExtensionFactory is required for the dependency injection magic of Xtext (or Google Guice) to work. This is only required to use, if you (1) define extensions (e.g. Eclipse wizard) and (2) want to add Eclipse services inside your extension using dependency injection.
    • If you need it, and you merge your ui and wizard projects, you could simply reuse the Xtext-generated ExtensionFactory without further customizations.

    However, if you do not want to merge the code explicitly, only package it together, you could simply refer your wizard project in the generated SDK feature and simply export the results as you have already stated to know. This way, your users can use the projects together, while you can maintain the different aspects of your code separately.

    In this case, if you need the Xtext services, extend the ExtensionFactory manually, and replace the bundle reference inside - but otherwise you can use it...