BR.Hamza BR.Hamza - 10 months ago 48
Java Question

Xtext, running plugin from selection

I have a DSL

myDsl
that i run with this code
It's in
Xtend


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

@Override
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();
            fsa.setOutputPath(srcGenFolder.getFullPath().toString());

            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