Xtext: Calling the Generator from a Context Menu

好久不见. 提交于 2019-12-12 00:39:01

问题


Following

https://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/

and using EclipseResourceFileSystemAccess2 instead of EclipseResourceFileSystemAccess when the line

final EclipseResourceFileSystemAccess2 fsa = fileAccessProvider.get();

give an exception. The only information I have is

// Compiled from InvocationTargetException.java (version 1.8 : 52.0, super bit)
public class java.lang.reflect.InvocationTargetException extends java.lang.ReflectiveOperationException {

I don't know how to get the stack trace in Eclipse.

does the code in the blog still function in the most recent release of Xtext?

Update 1

Snippets from plugin.xml

Handler:

<extension
   point="org.eclipse.ui.handlers">
  <handler
      class="tuks.mcrl2.dsl.ui.handlers.Mcrl22Lps"
      commandId="tuks.mcrl2.dsl.ui.commands.mcrl2lps">
  </handler>
 </extension> 

Commands:

<extension
   point="org.eclipse.ui.commands">
 <command
      categoryId="tuks.mcrl2.dsl.ui.category.processalgebra"
      defaultHandler="tuks.mcrl2.dsl.ui.handlers.Mcrl22Lps"
      description="Conver a mclr2 file to lps"
      id="tuks.mcrl2.dsl.ui.commands.mcrl2lps"
      name="mcrl22lps">
 </command>
 <category
      id="tuks.mcrl2.dsl.ui.category.processalgebra"
      name="Process Algebra">
 </category>
</extension>  

回答1:


it basically works, if you do the update from EclipseResourceFileSystemAccess and Stuff and (maybe) IGenerator. I assume in your case you dont set the Accesses ProgressMonitor and other props.

package org.xtext.example.mydsl.ui.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.xtext.builder.EclipseResourceFileSystemAccess2;
import org.eclipse.xtext.generator.GeneratorContext;
import org.eclipse.xtext.generator.IGenerator2;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.ui.resource.IResourceSetProvider;

import com.google.inject.Inject;
import com.google.inject.Provider;

public class GenerationHandler extends AbstractHandler implements IHandler {

    @Inject
    private IGenerator2 generator;

    @Inject
    private Provider<EclipseResourceFileSystemAccess2> fileAccessProvider;

    @Inject
    IResourceDescriptions resourceDescriptions;

    @Inject
    IResourceSetProvider resourceSetProvider;

    @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 EclipseResourceFileSystemAccess2 fsa = fileAccessProvider.get();
                fsa.setProject(project);
                fsa.setOutputPath("src-gen");
                fsa.setMonitor(new NullProgressMonitor()); 
                URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
                ResourceSet rs = resourceSetProvider.get(project);
                Resource r = rs.getResource(uri, true);
                generator.doGenerate(r, fsa, new GeneratorContext());

            }
        }
        return null;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}

and make sure you register the handler properly. the

class="org.xtext.example.mydsl.ui.MyDslExecutableExtensionFactory:org.xtext.example.mydsl.ui.handler.GenerationHandler"

is crucial, especially that it consists of 2 parts, the ExtensionFactory followed by a : followed by the actual class name



来源:https://stackoverflow.com/questions/41665691/xtext-calling-the-generator-from-a-context-menu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!