Search in sources :

Example 66 with IParseResult

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;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion) TextReplacement(org.eclipse.xtext.xbase.formatting.TextReplacement) ArrayList(java.util.ArrayList) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 67 with IParseResult

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

Example 68 with IParseResult

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;
    }
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ComparisonFailure(org.junit.ComparisonFailure) StringReader(java.io.StringReader) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 69 with IParseResult

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;
}
Also used : IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 70 with IParseResult

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);
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult)

Aggregations

IParseResult (org.eclipse.xtext.parser.IParseResult)70 EObject (org.eclipse.emf.ecore.EObject)27 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)19 StringReader (java.io.StringReader)16 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)13 XtextResource (org.eclipse.xtext.resource.XtextResource)13 INode (org.eclipse.xtext.nodemodel.INode)11 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)10 ParserRule (org.eclipse.xtext.ParserRule)8 ITextRegion (org.eclipse.xtext.util.ITextRegion)7 Test (org.junit.Test)7 TextRegion (org.eclipse.xtext.util.TextRegion)5 Resource (org.eclipse.emf.ecore.resource.Resource)4 ArrayList (java.util.ArrayList)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 ParseResult (org.eclipse.xtext.parser.ParseResult)3 Scope (org.yakindu.sct.model.sgraph.Scope)3 IOException (java.io.IOException)2 List (java.util.List)2 URI (org.eclipse.emf.common.util.URI)2