Search in sources :

Example 6 with AbstractTraceRegion

use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-core by eclipse.

the class TracingSugarTest method testCodeGeneration.

@Test
public void testCodeGeneration() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("type String {}");
        _builder.newLine();
        _builder.append("type Foo {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("String name;");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final Model root = this.parseHelper.parse(LineDelimiters.toUnix(_builder.toString()));
        final InMemoryFileSystemAccess fsa = new InMemoryFileSystemAccess();
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                {
                    EList<Type> _types = root.getTypes();
                    for (final Type t : _types) {
                        IGeneratorNode __generateType = TracingSugarTest.this._generateType(t);
                        _builder.append(__generateType);
                        _builder.newLineIfNotEmpty();
                    }
                }
            }
        };
        this._myExtensions.generateTracedFile(fsa, "foo/bar.txt", root, _client);
        final CharSequence generated = fsa.getTextFiles().get((IFileSystemAccess.DEFAULT_OUTPUT + "foo/bar.txt"));
        StringConcatenation _builder_1 = new StringConcatenation();
        {
            EList<Type> _types = root.getTypes();
            for (final Type t : _types) {
                CharSequence _generateType = this.generateType(t);
                _builder_1.append(_generateType);
                _builder_1.newLineIfNotEmpty();
            }
        }
        Assert.assertEquals(LineDelimiters.toUnix(_builder_1.toString()), generated.toString());
        final AbstractTraceRegion trace = ((ITraceRegionProvider) generated).getTraceRegion();
        StringConcatenation _builder_2 = new StringConcatenation();
        _builder_2.append("CompletableTraceRegion [myOffset=0, myLength=55, useForDebugging=false] associations={");
        _builder_2.newLine();
        _builder_2.append("  ");
        _builder_2.append("LocationData [TextRegionWithLineInformation [0:41][lineNumber=0, endLineNumber=3]][path=__synthetic0.lazylinkingtestlanguage]");
        _builder_2.newLine();
        _builder_2.append("} nestedRegions={");
        _builder_2.newLine();
        _builder_2.append("  ");
        _builder_2.append("CompletableTraceRegion [myOffset=0, myLength=17, useForDebugging=false] associations={");
        _builder_2.newLine();
        _builder_2.append("    ");
        _builder_2.append("LocationData [TextRegionWithLineInformation [0:14][lineNumber=0, endLineNumber=0]][path=__synthetic0.lazylinkingtestlanguage]");
        _builder_2.newLine();
        _builder_2.append("  ");
        _builder_2.append("} nestedRegions={");
        _builder_2.newLine();
        _builder_2.append("    ");
        _builder_2.append("CompletableTraceRegion [myOffset=6, myLength=6, useForDebugging=false] associations={");
        _builder_2.newLine();
        _builder_2.append("      ");
        _builder_2.append("LocationData [TextRegionWithLineInformation [5:6][lineNumber=0, endLineNumber=0]][path=__synthetic0.lazylinkingtestlanguage]");
        _builder_2.newLine();
        _builder_2.append("    ");
        _builder_2.append("}");
        _builder_2.newLine();
        _builder_2.append("  ");
        _builder_2.append("}");
        _builder_2.newLine();
        _builder_2.append("  ");
        _builder_2.append("CompletableTraceRegion [myOffset=17, myLength=38, useForDebugging=false] associations={");
        _builder_2.newLine();
        _builder_2.append("    ");
        _builder_2.append("LocationData [TextRegionWithLineInformation [15:26][lineNumber=1, endLineNumber=3]][path=__synthetic0.lazylinkingtestlanguage]");
        _builder_2.newLine();
        _builder_2.append("  ");
        _builder_2.append("} nestedRegions={");
        _builder_2.newLine();
        _builder_2.append("    ");
        _builder_2.append("CompletableTraceRegion [myOffset=23, myLength=3, useForDebugging=false] associations={");
        _builder_2.newLine();
        _builder_2.append("      ");
        _builder_2.append("LocationData [TextRegionWithLineInformation [20:3][lineNumber=1, endLineNumber=1]][path=__synthetic0.lazylinkingtestlanguage]");
        _builder_2.newLine();
        _builder_2.append("    ");
        _builder_2.append("}");
        _builder_2.newLine();
        _builder_2.append("    ");
        _builder_2.append("CompletableTraceRegion [myOffset=30, myLength=24, useForDebugging=false] associations={");
        _builder_2.newLine();
        _builder_2.append("      ");
        _builder_2.append("LocationData [TextRegionWithLineInformation [27:12][lineNumber=2, endLineNumber=2]][path=__synthetic0.lazylinkingtestlanguage]");
        _builder_2.newLine();
        _builder_2.append("    ");
        _builder_2.append("} nestedRegions={");
        _builder_2.newLine();
        _builder_2.append("      ");
        _builder_2.append("CompletableTraceRegion [myOffset=39, myLength=4, useForDebugging=false] associations={");
        _builder_2.newLine();
        _builder_2.append("        ");
        _builder_2.append("LocationData [TextRegionWithLineInformation [34:4][lineNumber=2, endLineNumber=2]][path=__synthetic0.lazylinkingtestlanguage]");
        _builder_2.newLine();
        _builder_2.append("      ");
        _builder_2.append("}");
        _builder_2.newLine();
        _builder_2.append("      ");
        _builder_2.append("CompletableTraceRegion [myOffset=46, myLength=6, useForDebugging=false] associations={");
        _builder_2.newLine();
        _builder_2.append("        ");
        _builder_2.append("LocationData [TextRegionWithLineInformation [27:6][lineNumber=2, endLineNumber=2]][path=__synthetic0.lazylinkingtestlanguage]");
        _builder_2.newLine();
        _builder_2.append("      ");
        _builder_2.append("}");
        _builder_2.newLine();
        _builder_2.append("    ");
        _builder_2.append("}");
        _builder_2.newLine();
        _builder_2.append("  ");
        _builder_2.append("}");
        _builder_2.newLine();
        _builder_2.append("}");
        Assert.assertEquals(LineDelimiters.toUnix(_builder_2.toString()), trace.toString());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IGeneratorNode(org.eclipse.xtext.generator.trace.node.IGeneratorNode) Type(org.eclipse.xtext.linking.lazy.lazyLinking.Type) EList(org.eclipse.emf.common.util.EList) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Model(org.eclipse.xtext.linking.lazy.lazyLinking.Model) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) InMemoryFileSystemAccess(org.eclipse.xtext.generator.InMemoryFileSystemAccess) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider) Test(org.junit.Test)

Example 7 with AbstractTraceRegion

use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-core by eclipse.

the class JavaIoFileSystemAccess method generateTrace.

protected void generateTrace(String generatedFile, String outputConfigName, CharSequence contents) {
    try {
        if (contents instanceof ITraceRegionProvider) {
            OutputStream out = null;
            try {
                AbstractTraceRegion traceRegion = ((ITraceRegionProvider) contents).getTraceRegion();
                String traceFileName = traceFileNameProvider.getTraceFromJava(generatedFile);
                File traceFile = getFile(traceFileName, outputConfigName);
                out = new BufferedOutputStream(new FileOutputStream(traceFile));
                traceSerializer.writeTraceRegionTo(traceRegion, out);
                if (callBack != null)
                    callBack.fileAdded(traceFile);
            } catch (TraceNotFoundException e) {
            // ok
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeIOException(e);
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
}
Also used : RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) TraceNotFoundException(org.eclipse.xtext.generator.trace.TraceNotFoundException) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with AbstractTraceRegion

use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-core by eclipse.

the class AbstractTrace method toLocations.

protected Iterable<? extends ILocationInResource> toLocations(final Iterable<AbstractTraceRegion> allTraceRegions) {
    return new Iterable<ILocationInResource>() {

        @Override
        public Iterator<ILocationInResource> iterator() {
            return new AbstractIterator<ILocationInResource>() {

                private Iterator<AbstractTraceRegion> delegate = allTraceRegions.iterator();

                private AbstractTraceRegion region;

                private Iterator<ILocationData> locationDelegate;

                @Override
                protected ILocationInResource computeNext() {
                    while (true) {
                        if (locationDelegate == null || !locationDelegate.hasNext()) {
                            if (delegate.hasNext()) {
                                region = delegate.next();
                                locationDelegate = region.getAssociatedLocations().iterator();
                                if (!locationDelegate.hasNext()) {
                                    continue;
                                }
                            }
                        }
                        if (locationDelegate != null && locationDelegate.hasNext()) {
                            ILocationData locationData = locationDelegate.next();
                            ILocationInResource result = createLocationInResourceFor(locationData, region);
                            if (result != null) {
                                return result;
                            }
                            continue;
                        }
                        return endOfData();
                    }
                }
            };
        }
    };
}
Also used : Iterator(java.util.Iterator) AbstractIterator(com.google.common.collect.AbstractIterator) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) AbstractIterator(com.google.common.collect.AbstractIterator) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) ILocationData(org.eclipse.xtext.generator.trace.ILocationData)

Example 9 with AbstractTraceRegion

use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-core by eclipse.

the class AbstractTrace method mergeRegions.

protected ILocationInResource mergeRegions(AbstractTraceRegion left, AbstractTraceRegion right) {
    if (left == null) {
        if (right != null) {
            return getMergedLocationInResource(right);
        }
        return null;
    }
    if (right == null || left.equals(right)) {
        return getMergedLocationInResource(left);
    } else {
        SourceRelativeURI leftToPath = left.getAssociatedSrcRelativePath();
        SourceRelativeURI rightToPath = right.getAssociatedSrcRelativePath();
        if (ObjectExtensions.operator_equals(leftToPath, rightToPath)) {
            ITextRegionWithLineInformation leftRegion = left.getMyRegion();
            ITextRegionWithLineInformation rightRegion = right.getMyRegion();
            if (leftRegion.contains(rightRegion)) {
                return getMergedLocationInResource(left);
            } else if (rightRegion.contains(leftRegion)) {
                return getMergedLocationInResource(right);
            } else {
                AbstractTraceRegion parent = left.getParent();
                AbstractTraceRegion leftChild = left;
                while (parent != null) {
                    if (parent.getMyRegion().contains(rightRegion)) {
                        break;
                    }
                    leftChild = parent;
                    parent = parent.getParent();
                }
                if (parent != null) {
                    AbstractTraceRegion rightChild = right;
                    while (!parent.equals(rightChild.getParent())) {
                        rightChild = rightChild.getParent();
                        if (rightChild == null) {
                            return getMergedLocationInResource(leftChild);
                        }
                    }
                    SourceRelativeURI path = leftToPath;
                    if (path == null) {
                        path = leftChild.getAssociatedSrcRelativePath();
                    }
                    ITextRegionWithLineInformation merged = parent.getMergedAssociatedLocation();
                    if (merged != null) {
                        return createLocationInResource(merged, path);
                    }
                }
            }
        }
    }
    // TODO the remaining cases have yet to be implemented
    return null;
}
Also used : ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI)

Example 10 with AbstractTraceRegion

use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-core by eclipse.

the class AbstractTrace method getBestAssociatedLocation.

@Override
public ILocationInResource getBestAssociatedLocation(ITextRegion region) {
    AbstractTraceRegion right = findTraceRegionAtRightOffset(region.getOffset() + region.getLength());
    if (right != null && encloses(right, region.getOffset(), true)) {
        if (right.getMyOffset() + right.getMyLength() == region.getOffset() + region.getLength())
            return getMergedLocationInResource(right);
    }
    AbstractTraceRegion left = findTraceRegionAtLeftOffset(region.getOffset());
    return mergeRegions(left, right);
}
Also used : AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion)

Aggregations

AbstractTraceRegion (org.eclipse.xtext.generator.trace.AbstractTraceRegion)21 ITraceRegionProvider (org.eclipse.xtext.generator.trace.ITraceRegionProvider)10 ILocationData (org.eclipse.xtext.generator.trace.ILocationData)5 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)4 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)4 TraceNotFoundException (org.eclipse.xtext.generator.trace.TraceNotFoundException)4 EList (org.eclipse.emf.common.util.EList)3 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)3 InMemoryFileSystemAccess (org.eclipse.xtext.generator.InMemoryFileSystemAccess)3 IGeneratorNode (org.eclipse.xtext.generator.trace.node.IGeneratorNode)3 Model (org.eclipse.xtext.linking.lazy.lazyLinking.Model)3 Type (org.eclipse.xtext.linking.lazy.lazyLinking.Type)3 ITextRegionWithLineInformation (org.eclipse.xtext.util.ITextRegionWithLineInformation)3 Test (org.junit.Test)3 AbstractIterator (com.google.common.collect.AbstractIterator)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 List (java.util.List)2