Search in sources :

Example 6 with ITextRegionWithLineInformation

use of org.eclipse.xtext.util.ITextRegionWithLineInformation 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 7 with ITextRegionWithLineInformation

use of org.eclipse.xtext.util.ITextRegionWithLineInformation in project xtext-core by eclipse.

the class LocationInFileProviderTest method testEObjectSignificantLocation.

@Test
public void testEObjectSignificantLocation() throws Exception {
    assertEquals(2, elements.size());
    ITextRegionWithLineInformation location = (ITextRegionWithLineInformation) locationInFileProvider.getSignificantTextRegion(elements.get(0));
    assertEquals(modelAsString.indexOf("x"), location.getOffset());
    assertEquals(1, location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
    location = (ITextRegionWithLineInformation) locationInFileProvider.getSignificantTextRegion(elements.get(1));
    assertEquals(modelAsString.lastIndexOf("y"), location.getOffset());
    assertEquals(1, location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
}
Also used : ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) Test(org.junit.Test)

Example 8 with ITextRegionWithLineInformation

use of org.eclipse.xtext.util.ITextRegionWithLineInformation in project xtext-core by eclipse.

the class LocationInFileProviderTest method testContainmentRefSignificantLocation.

@Test
public void testContainmentRefSignificantLocation() throws Exception {
    assertEquals(2, elements.size());
    ITextRegionWithLineInformation location = (ITextRegionWithLineInformation) locationInFileProvider.getSignificantTextRegion(model, LocationprovidertestPackage.Literals.MODEL__ELEMENTS, 0);
    assertEquals(modelAsString.indexOf("x"), location.getOffset());
    assertEquals(1, location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
    location = (ITextRegionWithLineInformation) locationInFileProvider.getSignificantTextRegion(model, LocationprovidertestPackage.Literals.MODEL__ELEMENTS, 1);
    assertEquals(modelAsString.lastIndexOf("y"), location.getOffset());
    assertEquals(1, location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
    // multi feature index out of bounds -> return owner's significant region
    location = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(model, LocationprovidertestPackage.Literals.MODEL__ELEMENTS, 2);
    assertEquals(0, location.getOffset());
    assertEquals(modelAsString.length(), location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(10, location.getEndLineNumber());
}
Also used : ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) Test(org.junit.Test)

Example 9 with ITextRegionWithLineInformation

use of org.eclipse.xtext.util.ITextRegionWithLineInformation in project xtext-core by eclipse.

the class LocationInFileProviderTest method testAttributeFullLocation.

@Test
public void testAttributeFullLocation() {
    assertEquals(2, elements.size());
    ITextRegionWithLineInformation location = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(elements.get(0), LocationprovidertestPackage.Literals.ELEMENT__NAME, 0);
    assertEquals(modelAsString.indexOf("x"), location.getOffset());
    assertEquals(1, location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
    // single feature index out of bounds -> ignore index
    location = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(elements.get(1), LocationprovidertestPackage.Literals.ELEMENT__NAME, 1);
    assertEquals(modelAsString.lastIndexOf("y"), location.getOffset());
    assertEquals("y".length(), location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
}
Also used : ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) Test(org.junit.Test)

Example 10 with ITextRegionWithLineInformation

use of org.eclipse.xtext.util.ITextRegionWithLineInformation in project xtext-core by eclipse.

the class LocationInFileProviderTest method testCrossRefFullLocation.

@Test
public void testCrossRefFullLocation() throws Exception {
    assertEquals(2, elements.size());
    int indexOfFirstY = modelAsString.indexOf("y");
    int indexOfSecondY = modelAsString.indexOf("y", indexOfFirstY + 1);
    ITextRegionWithLineInformation location = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(elements.get(0), LocationprovidertestPackage.Literals.ELEMENT__SINGLEREF, 0);
    assertEquals(indexOfFirstY, location.getOffset());
    assertEquals(1, location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
    // single feature index out of bounds -> ignore index
    location = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(elements.get(0), LocationprovidertestPackage.Literals.ELEMENT__SINGLEREF, 1);
    assertEquals(indexOfFirstY, location.getOffset());
    assertEquals(1, location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
    location = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(elements.get(0), LocationprovidertestPackage.Literals.ELEMENT__MULTIREFS, 0);
    assertEquals(indexOfSecondY, location.getOffset());
    assertEquals(1, location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
    // multi feature index out of bounds -> return owner's full region
    location = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(elements.get(0), LocationprovidertestPackage.Literals.ELEMENT__MULTIREFS, 1);
    assertEquals(0, location.getOffset());
    assertEquals(modelAsString.indexOf(" element y"), location.getLength());
    assertEquals(0, location.getLineNumber());
    assertEquals(0, location.getEndLineNumber());
}
Also used : ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) Test(org.junit.Test)

Aggregations

ITextRegionWithLineInformation (org.eclipse.xtext.util.ITextRegionWithLineInformation)26 Test (org.junit.Test)14 TextRegionWithLineInformation (org.eclipse.xtext.util.TextRegionWithLineInformation)5 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)4 Mode (org.eclipse.xtext.resource.locationprovidertest.Mode)4 AbstractTraceRegion (org.eclipse.xtext.generator.trace.AbstractTraceRegion)3 ILocationData (org.eclipse.xtext.generator.trace.ILocationData)3 INode (org.eclipse.xtext.nodemodel.INode)3 Bus (org.eclipse.xtext.resource.locationprovidertest.Bus)3 Processor (org.eclipse.xtext.resource.locationprovidertest.Processor)3 ITextRegion (org.eclipse.xtext.util.ITextRegion)3 EObject (org.eclipse.emf.ecore.EObject)2 LocationData (org.eclipse.xtext.generator.trace.LocationData)2 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 URI (org.eclipse.emf.common.util.URI)1 RootTraceRegionForTesting (org.eclipse.xtend.core.tests.compiler.RootTraceRegionForTesting)1 SimpleTrace (org.eclipse.xtend.core.tests.compiler.SimpleTrace)1