Search in sources :

Example 1 with AbstractFormatter

use of org.eclipse.xtext.xbase.formatting.AbstractFormatter in project xtext-eclipse by eclipse.

the class FormatterTester method assertFormatted.

public void assertFormatted(AssertingFormatterData it) {
    try {
        String fullToBeParsed = it.getPrefix() + it.getToBeFormatted() + it.getPostfix();
        EObject parsed = parseHelper.parse(fullToBeParsed);
        if (!it.isAllowErrors()) {
            Assert.assertEquals(Joiner.on("\n").join(parsed.eResource().getErrors()), 0, parsed.eResource().getErrors().size());
        }
        String oldDocument = null;
        IParseResult parseResult = ((XtextResource) parsed.eResource()).getParseResult();
        if (parseResult != null) {
            ICompositeNode rootNode = parseResult.getRootNode();
            if (rootNode != null) {
                oldDocument = rootNode.getText();
            }
        }
        if (formatter instanceof AbstractFormatter) {
            ((AbstractFormatter) formatter).setAllowIdentityEdits(true);
        }
        int start = it.getPrefix().length();
        int length = it.getToBeFormatted().length();
        Set<TextReplacement> edits = new LinkedHashSet<>();
        Iterables.addAll(edits, formatter.format((XtextResource) parsed.eResource(), start, length, it.getCfg()));
        if (formatter instanceof AbstractFormatter) {
            if (((AbstractFormatter) formatter).isConflictOccurred()) {
                throw new RuntimeException("There are conflicting text edits, see console for details.");
            }
        }
        if (!it.isAllowErrors()) {
            Iterables.addAll(edits, createMissingEditReplacements((XtextResource) parsed.eResource(), edits, start, length));
        }
        String newDocument = applyEdits(oldDocument, edits);
        try {
            Assert.assertEquals((it.getPrefix() + it.getExpectation() + it.getPostfix()).toString(), newDocument.toString());
        } catch (AssertionError e) {
            System.out.println(applyDebugEdits(oldDocument, edits));
            System.out.println();
            throw e;
        }
        String parsed2Doc = applyEdits(fullToBeParsed, formatter.format((XtextResource) parsed.eResource(), 0, fullToBeParsed.length(), it.getCfg()));
        EObject parsed2 = parseHelper.parse(parsed2Doc);
        if (!it.isAllowErrors()) {
            Assert.assertEquals(0, parsed2.eResource().getErrors().size());
        }
        List<TextReplacement> edits2 = formatter.format((XtextResource) parsed2.eResource(), 0, parsed2Doc.length(), it.getCfg());
        String newDocument2 = applyEdits(parsed2Doc, edits2);
        try {
            Assert.assertEquals(parsed2Doc, newDocument2.toString());
        } catch (AssertionError e) {
            System.out.println(applyDebugEdits(newDocument, edits2));
            System.out.println();
            throw e;
        }
    } catch (Throwable e) {
        throw Exceptions.sneakyThrow(e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TextReplacement(org.eclipse.xtext.xbase.formatting.TextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult) AbstractFormatter(org.eclipse.xtext.xbase.formatting.AbstractFormatter)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)1 EObject (org.eclipse.emf.ecore.EObject)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 IParseResult (org.eclipse.xtext.parser.IParseResult)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 AbstractFormatter (org.eclipse.xtext.xbase.formatting.AbstractFormatter)1 TextReplacement (org.eclipse.xtext.xbase.formatting.TextReplacement)1