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);
}
}
Aggregations