2016-12-27 56 views
0

運行插件,我有一個DSL myDsl我用這個代碼運行 它在Xtend的Xtext,從選擇

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) 我到ressources和呼叫啓動執行myDslInterpreter類。我該怎麼做?

+0

你看看https://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-上下文菜單/? –

+0

不,我想看看我是否可以在我的情況下使用它 –

+0

@ChristianDietrich完成,它的工作 –

回答

0

這是處理程序在文件上調用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); 

     } 
    }