Xtext,运行 插件选择

Xtext, running plugin from selection

我有一个 DSL myDsl我 运行 使用此代码 它在 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)
        }
    }

我想从方法 launch(ISelection selection, String mode) 启动执行 我必须使用资源并调用 myDslInterpreter 类。我该怎么做?

这里是处理程序调用文件上的 Xtext IGenerator 的代码。您应该能够根据您的情况调整它

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

        }
    }