use of org.eclipse.xtext.parser.IParseResult in project xtext-eclipse by eclipse.
the class FormatterTester method createMissingEditReplacements.
protected ArrayList<TextReplacement> createMissingEditReplacements(XtextResource res, Collection<TextReplacement> edits, int offset, int length) {
Set<Integer> offsets = IterableExtensions.toSet(Iterables.transform(edits, (TextReplacement it) -> Integer.valueOf(it.getOffset())));
ArrayList<TextReplacement> result = new ArrayList<>();
int lastOffset = 0;
IParseResult parseResult = res.getParseResult();
if (parseResult != null) {
ICompositeNode rootNode = parseResult.getRootNode();
if (rootNode != null) {
for (ILeafNode leaf : rootNode.getLeafNodes()) {
if (!leaf.isHidden() || !StringExtensions.isNullOrEmpty(leaf.getText().trim())) {
ITextRegion leafRegion = leaf.getTextRegion();
if (lastOffset >= offset && leafRegion.getOffset() <= offset + length && !offsets.contains(Integer.valueOf(lastOffset))) {
result.add(new TextReplacement(lastOffset, leafRegion.getOffset() - lastOffset, "!!"));
}
lastOffset = leafRegion.getOffset() + leafRegion.getLength();
}
}
}
}
return result;
}
use of org.eclipse.xtext.parser.IParseResult 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);
}
}
use of org.eclipse.xtext.parser.IParseResult in project xtext-eclipse by eclipse.
the class PartialParsingProcessor method processFile.
@Override
public String processFile(String completeData, String data, int offset, int len, String change) throws Exception {
IParseResult initialParseResult = parser.parse(new StringReader(data));
String newData = applyDelta(data, offset, len, change);
ReplaceRegion replaceRegion = new ReplaceRegion(offset, len, change);
try {
IParseResult reparsed = parser.reparse(initialParseResult, replaceRegion);
IParseResult parsedFromScratch = parser.parse(new StringReader(newData));
assertEqual(data, newData, parsedFromScratch, reparsed);
return newData;
} catch (Throwable e) {
ComparisonFailure throwMe = new ComparisonFailure(e.getMessage(), newData, replaceRegion + DELIM + data);
throwMe.initCause(e);
throw throwMe;
}
}
use of org.eclipse.xtext.parser.IParseResult in project xtext-eclipse by eclipse.
the class XtextDocumentReconcileStrategy method createPostParseErrorMessage.
/**
* Creates an error message containing the source code of the {@code resource}
*
* @since 2.15
*/
protected String createPostParseErrorMessage(XtextResource resource) {
String message = "Error post-processing resource with content";
IParseResult parseResult = resource.getParseResult();
if (parseResult != null && parseResult.getRootNode() != null) {
message += ":\n" + parseResult.getRootNode().getText();
}
return message;
}
use of org.eclipse.xtext.parser.IParseResult in project xtext-eclipse by eclipse.
the class AstSelectionProvider method getTextRegion.
protected ITextRegion getTextRegion(EObject eObject) {
if (eObject == null)
return null;
IParseResult parseResult = ((XtextResource) eObject.eResource()).getParseResult();
if (parseResult == null)
return null;
ICompositeNode rootNode = parseResult.getRootNode();
Map<ILeafNode, EObject> comments = commentAssociater.associateCommentsWithSemanticEObjects(eObject, singleton(rootNode));
final ITextRegion result = locationProvider.getFullTextRegion(eObject);
int start = result.getOffset();
int end = result.getOffset() + result.getLength();
for (Entry<ILeafNode, EObject> entry : comments.entrySet()) {
if (entry.getValue() == eObject) {
ILeafNode node = entry.getKey();
if (node.getTotalOffset() < start) {
start = node.getTotalOffset();
}
if (node.getTotalEndOffset() > end) {
end = node.getTotalEndOffset();
}
}
}
return new TextRegion(start, end - start);
}
Aggregations