xtext

How to invoke formatter programmatically in Xtext and get output string


Instead of calling save on resource I would like to convert model to string programmatically. I have already implemented formatter and tests for it so I would like to reuse it.

Is it possible, and if possible how?


Solution

  •     @Inject
        private Provider<ResourceSet> rsp;
        
        @Inject
        private ISerializer ser;
    
        ...
    
        ResourceSet rs = rsp.get();
        Resource r = rs.createResource(URI.createURI("dummy.mydsl"));
        r.getContents().add(myModel);
        System.out.println(ser.serialize(myModel, SaveOptions.newBuilder().format().getOptions());