Search in sources :

Example 1 with INodeModelFormatter

use of org.eclipse.xtext.formatting.INodeModelFormatter in project xtext-core by eclipse.

the class AnotherFormatterTest method loadModel.

@Test
public void loadModel() throws Exception {
    ParseHelper<Root> parseHelper = getInjector().getInstance(Key.get(new TypeLiteral<ParseHelper<Root>>() {
    }));
    INodeModelFormatter formatter = getInjector().getInstance(INodeModelFormatter.class);
    ArrayList<String> lines = Lists.newArrayList("test sample {", "	// just a comment", "\t}", "");
    Root root = parseHelper.parse(Joiner.on(System.lineSeparator()).join(lines));
    Assert.assertNotNull(root);
    EList<Resource.Diagnostic> errors = root.eResource().getErrors();
    Assert.assertTrue("Unexpected errors: " + Joiner.on(", ").join(errors), errors.isEmpty());
    ICompositeNode rootNode = ((XtextResource) root.eResource()).getParseResult().getRootNode();
    ArrayList<String> expectedLines = Lists.newArrayList("test sample {", "", "// just a comment", "}");
    String expected = Joiner.on(System.lineSeparator()).join(expectedLines);
    INodeModelFormatter.IFormattedRegion formattedRegion = formatter.format(rootNode, rootNode.getTotalOffset(), rootNode.getTotalLength());
    Assert.assertEquals(expected, formattedRegion.getFormattedText());
    formattedRegion = formatter.format(rootNode, rootNode.getTotalOffset(), rootNode.getTotalLength());
    Assert.assertEquals(expected, formattedRegion.getFormattedText());
}
Also used : Root(org.eclipse.xtext.parsetree.formatter.formattertestlanguage2.Root) TypeLiteral(com.google.inject.TypeLiteral) INodeModelFormatter(org.eclipse.xtext.formatting.INodeModelFormatter) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Test(org.junit.Test)

Aggregations

TypeLiteral (com.google.inject.TypeLiteral)1 INodeModelFormatter (org.eclipse.xtext.formatting.INodeModelFormatter)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 Root (org.eclipse.xtext.parsetree.formatter.formattertestlanguage2.Root)1 Test (org.junit.Test)1