Search code examples
eclipsedslxtextxtend

Xtext, get Iselection Input in the LaunchMydslShortcut


I'm trying to make a simple DSL with Xtext and execute it with an interpreter, the grammar is the initial Hello Word project.
I can can execute the .Text file successfully.
In the org.xtext.example.mydsl.ui project I write this in the plugin.xml file To run the Project from my class LaunchMydslShortcut.

  <extension
    point="org.eclipse.debug.ui.launchShortcuts">
 <shortcut
       class="org.xtext.example.mydsl.ui.MyDslExecutableExtensionFactory:org.xtext.example.mydsl.ui.launch.LaunchMydslShortcut"
       icon="icon/sample.gif"
       id="org.xtext.example.mydsl.u.launchMyDsl"
       label="MyDsll"
       modes="run">

       <contextualLaunch>
       <enablement>
         <with variable="selection">
           <count value="1"/>
           <iterate
                 ifEmpty="false"
                 operator="and">
              <adapt type="org.eclipse.core.resources.IFile"/>
                <test property="org.eclipse.debug.ui.matchesPattern"
                      value="*.mydsl"/>
           </iterate>
         </with>
       </enablement>
       <contextLabel
          mode="run"
          label="Run Mydsl"/>
     </contextualLaunch>

 </shortcut>
</extension>

This is my LaunchMydslShortcut class :

class LaunchMydslShortcut implements ILaunchShortcut {
    @Inject
    private IResourceForEditorInputFactory resourceFactory;

    override launch(ISelection selection, String mode) {
        println("launch from selection")

    }

    override launch(IEditorPart editor, String mode) {
        val input = editor.editorInput

        if (editor instanceof XtextEditor && input instanceof FileEditorInput) {
            val resource = resourceFactory.createResource(input)
            resource.load(newHashMap())
            println("launch Doooone")
        }
    }
}

However, I'm expecting to use launch(IEditorPart editor, String mode) function, but it executes the launch(ISelection selection, String mode).
so the question would be, what's the différence between the two? why my project is using the first? and how do I use the second?


Solution

  • The first one void launch(ISelection selection, String mode) is called when you launch your generator from the package explorer for example.

    The second one void launch(IEditorPart editor, String mode) is called when launching from an editor context menu.

    You can use a Utility to get the input file you need and then create an IResource.

    Good example is in org.eclipse.xtext.xtext.launcher.WorkflowLaunchUtils.workflowFileFor(ISelection) and org.eclipse.xtext.xtext.launcher.WorkflowLaunchUtils.workflowFileFor(IEditorPart) it works with mwe2 files but it is easy to adopt it for your DSL.