Search in sources :

Example 11 with LazyLinkingResource

use of org.eclipse.xtext.linking.lazy.LazyLinkingResource in project xtext-xtend by eclipse.

the class AbstractSmokeTest method testSkipNodesInBetween.

@Test
public void testSkipNodesInBetween() throws Exception {
    for (String string : smokeTestModels) {
        LazyLinkingResource resource = createResource(string);
        ICompositeNode rootNode = resource.getParseResult().getRootNode();
        ReplaceRegion replaceRegion = null;
        for (INode node : rootNode.getAsTreeIterable()) {
            int offset = node.getTotalOffset();
            int length = node.getTotalLength();
            if (replaceRegion == null || replaceRegion.getOffset() != offset || replaceRegion.getLength() != length) {
                replaceRegion = new ReplaceRegion(offset, length, "");
                StringBuilder builder = new StringBuilder(string);
                replaceRegion.applyTo(builder);
                doParseAndCheckForSmoke(builder.toString());
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) LazyLinkingResource(org.eclipse.xtext.linking.lazy.LazyLinkingResource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Test(org.junit.Test)

Example 12 with LazyLinkingResource

use of org.eclipse.xtext.linking.lazy.LazyLinkingResource in project xtext-xtend by eclipse.

the class AbstractSmokeTest method createResource.

protected LazyLinkingResource createResource(final String model) throws IOException {
    if (logger.isTraceEnabled()) {
        logger.trace("createResource: " + model);
    }
    XtextResourceSet set = getResourceSet();
    typeProviderFactory.findOrCreateTypeProvider(set);
    LazyLinkingResource resource = (LazyLinkingResource) resourceFactory.createResource(URI.createURI("Test.xtend"));
    set.getResources().add(resource);
    resource.load(new StringInputStream(model), null);
    resource.resolveLazyCrossReferences(CancelIndicator.NullImpl);
    return resource;
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) LazyLinkingResource(org.eclipse.xtext.linking.lazy.LazyLinkingResource)

Example 13 with LazyLinkingResource

use of org.eclipse.xtext.linking.lazy.LazyLinkingResource in project xtext-xtend by eclipse.

the class SmokeTest method compareWithNewResource.

protected void compareWithNewResource(LazyLinkingResource resource, int offset, int length, String newText) throws IOException {
    String model = resource.getParseResult().getRootNode().getText();
    try {
        long start = System.currentTimeMillis();
        long time = System.currentTimeMillis();
        if (logger.isDebugEnabled()) {
            logger.debug("Partial parsing ...");
            logger.trace("Input is >>>" + resource.getParseResult().getRootNode().getText() + "<<<");
            logger.trace("Offset: " + offset);
            logger.trace("Length: " + length);
            logger.trace("New Text: >>>" + newText + "<<<");
        }
        resource.update(offset, length, newText);
        resource.resolveLazyCrossReferences(CancelIndicator.NullImpl);
        String text = resource.getParseResult().getRootNode().getText();
        if (logger.isDebugEnabled()) {
            logger.debug("... took " + (System.currentTimeMillis() - time));
            logger.debug("Loading second resource ...");
            time = System.currentTimeMillis();
        }
        LazyLinkingResource newResource = createResource(text);
        if (logger.isDebugEnabled()) {
            logger.debug("... took " + (System.currentTimeMillis() - time));
            logger.debug("Semantic equality check ...");
            time = System.currentTimeMillis();
        }
        assertEquals(text, resource.getContents().size(), newResource.getContents().size());
        EcoreUtil.resolveAll(resource);
        EcoreUtil.resolveAll(newResource);
        for (int i = 0; i < resource.getContents().size(); i++) {
            assertEquals(text, EmfFormatter.objToStr(newResource.getContents().get(i)), EmfFormatter.objToStr(resource.getContents().get(i)));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("... took " + (System.currentTimeMillis() - time));
            logger.debug("Node equality check ...");
            time = System.currentTimeMillis();
        }
        ICompositeNode rootNode = resource.getParseResult().getRootNode();
        ICompositeNode newRootNode = newResource.getParseResult().getRootNode();
        Iterator<INode> iterator = rootNode.getAsTreeIterable().iterator();
        Iterator<INode> newIterator = newRootNode.getAsTreeIterable().iterator();
        while (iterator.hasNext()) {
            assertTrue(newIterator.hasNext());
            assertEqualNodes(text, iterator.next(), newIterator.next());
        }
        assertFalse(iterator.hasNext());
        assertFalse(newIterator.hasNext());
        if (logger.isDebugEnabled()) {
            logger.debug("... took " + (System.currentTimeMillis() - time));
            logger.debug("... done - total time: " + (System.currentTimeMillis() - start));
        }
    } catch (Throwable e) {
        e.printStackTrace();
        String failureMessage = e.getMessage() + " : Model was >>>\n" + model + "\n<<<\n" + "offset: " + offset + "\n" + "length:" + length + "\n" + "newText: >>>" + newText + "<<<";
        assertEquals(failureMessage, model, "");
        // just to make sure we fail for empty model, too
        fail(failureMessage);
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) LazyLinkingResource(org.eclipse.xtext.linking.lazy.LazyLinkingResource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 14 with LazyLinkingResource

use of org.eclipse.xtext.linking.lazy.LazyLinkingResource in project xtext-core by eclipse.

the class AbstractSmokeTest method testSkipNodesInBetween.

@Test
public void testSkipNodesInBetween() throws Exception {
    for (String string : getSmokeTestModels()) {
        LazyLinkingResource resource = createResource(string);
        if (resource != null) {
            ICompositeNode rootNode = resource.getParseResult().getRootNode();
            ReplaceRegion replaceRegion = null;
            for (INode node : rootNode.getAsTreeIterable()) {
                int offset = node.getTotalOffset();
                int length = node.getTotalLength();
                if (replaceRegion == null || replaceRegion.getOffset() != offset || replaceRegion.getLength() != length) {
                    replaceRegion = new ReplaceRegion(offset, length, "");
                    StringBuilder builder = new StringBuilder(string);
                    replaceRegion.applyTo(builder);
                    processModel(builder.toString());
                }
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) LazyLinkingResource(org.eclipse.xtext.linking.lazy.LazyLinkingResource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Test(org.junit.Test)

Example 15 with LazyLinkingResource

use of org.eclipse.xtext.linking.lazy.LazyLinkingResource in project xtext-core by eclipse.

the class AbstractSmokeTest method testSkipNodesInBetweenWithoutResourceSet.

@Test
public void testSkipNodesInBetweenWithoutResourceSet() throws Exception {
    for (String string : getSmokeTestModels()) {
        LazyLinkingResource resource = createResource(string);
        if (resource != null) {
            ICompositeNode rootNode = resource.getParseResult().getRootNode();
            ReplaceRegion replaceRegion = null;
            for (INode node : rootNode.getAsTreeIterable()) {
                int offset = node.getTotalOffset();
                int length = node.getTotalLength();
                if (replaceRegion == null || replaceRegion.getOffset() != offset || replaceRegion.getLength() != length) {
                    replaceRegion = new ReplaceRegion(offset, length, "");
                    StringBuilder builder = new StringBuilder(string);
                    replaceRegion.applyTo(builder);
                    processModelWithoutResourceSet(builder.toString());
                }
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) LazyLinkingResource(org.eclipse.xtext.linking.lazy.LazyLinkingResource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Test(org.junit.Test)

Aggregations

LazyLinkingResource (org.eclipse.xtext.linking.lazy.LazyLinkingResource)16 Test (org.junit.Test)8 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)5 INode (org.eclipse.xtext.nodemodel.INode)5 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)4 XtextResource (org.eclipse.xtext.resource.XtextResource)3 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)3 Resource (org.eclipse.emf.ecore.resource.Resource)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 CommonToken (org.antlr.runtime.CommonToken)1 Token (org.antlr.runtime.Token)1 BasicDiagnostic (org.eclipse.emf.common.util.BasicDiagnostic)1 Diagnostic (org.eclipse.emf.ecore.resource.Resource.Diagnostic)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 InternalXtendLexer (org.eclipse.xtend.core.parser.antlr.internal.InternalXtendLexer)1 ExceptionDiagnostic (org.eclipse.xtext.diagnostics.ExceptionDiagnostic)1 Lexer (org.eclipse.xtext.parser.antlr.Lexer)1 StringInputStream (org.eclipse.xtext.util.StringInputStream)1