BR.Hamza BR.Hamza - 6 months ago 26
Java Question

Xtext, running plugin from selection

I have a DSL

that i run with this code
It's in

class LaunchMydslShortcut implements ILaunchShortcut {
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)
val program = resource.contents.head as Script
new MyDslInterpreter().exec(program)

I want to launch the the execution from the method
launch(ISelection selection, String mode)

I have to the ressources and call the myDslInterpreter clas. how can I do it?

Answer Source

Here is the code from a Handler calling a Xtext IGenerator on a File. You should be able to adapt it for your case

public Object execute(ExecutionEvent event) throws ExecutionException {

    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        Object firstElement = structuredSelection.getFirstElement();
        if (firstElement instanceof IFile) {
            IFile file = (IFile) firstElement;
            IProject project = file.getProject();
            IFolder srcGenFolder = project.getFolder("src-gen");
            if (!srcGenFolder.exists()) {
                try {
                    srcGenFolder.create(true, true,
                            new NullProgressMonitor());
                } catch (CoreException e) {
                    return null;

            final EclipseResourceFileSystemAccess fsa = fileAccessProvider.get();

            URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
            ResourceSet rs = resourceSetProvider.get(project);
            Resource r = rs.getResource(uri, true);
            generator.doGenerate(r, fsa);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download