Xtext, running plugin from selection

37 views Asked by At

I have a DSL myDslthat 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?

1

There are 1 answers

0
Christian Dietrich On BEST ANSWER

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

        }
    }