org.eclipse.xtext.util.TextRegionWithLineInformation Java Examples

The following examples show how to use org.eclipse.xtext.util.TextRegionWithLineInformation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultHierarchyNodeLocationProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected ITextRegionWithLineInformation toTextRegionWithLineInformation(EObject obj, ITextRegion textRegion) {
	if (textRegion == null) {
		return ITextRegionWithLineInformation.EMPTY_REGION;
	}
	if (textRegion instanceof ITextRegionWithLineInformation) {
		return (ITextRegionWithLineInformation) textRegion;
	}
	ICompositeNode node = NodeModelUtils.getNode(obj);
	if (node == null) {
		return new TextRegionWithLineInformation(textRegion.getOffset(), textRegion.getLength(), 0, 0);
	}
	int startLine = NodeModelUtils.getLineAndColumn(node, textRegion.getOffset()).getLine() - 1;
	int endLine = NodeModelUtils.getLineAndColumn(node, textRegion.getOffset() + textRegion.getLength()).getLine()
			- 1;
	return new TextRegionWithLineInformation(textRegion.getOffset(), textRegion.getLength(), startLine, endLine);
}
 
Example #2
Source File: TreeAppendableTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testNoRedundantRegions() {
	ITextRegionWithLineInformation redundant = new TextRegionWithLineInformation(47, 11, 12, 137);
	ITextRegionWithLineInformation second = new TextRegionWithLineInformation(8, 15, 12, 137);
	expectedRegions = Arrays.asList(redundant, redundant, second).iterator();
	TreeAppendable appendable = new TreeAppendable(new ImportManager(false), this, this, this, content, "  ", "\n");
	appendable.append("initial");
	appendable.trace(content).append("first");
	appendable.trace(content).append("second");
	assertEquals("initialfirstsecond", appendable.getContent());
	AbstractTraceRegion traceRegion = appendable.getTraceRegion();
	assertNotNull(traceRegion);
	assertEquals(47, traceRegion.getMergedAssociatedLocation().getOffset());
	assertEquals(11, traceRegion.getMergedAssociatedLocation().getLength());
	assertEquals(0, traceRegion.getMyOffset());
	assertEquals("initialfirstsecond".length(), traceRegion.getMyLength());
	List<AbstractTraceRegion> nestedRegions = traceRegion.getNestedRegions();
	assertEquals(1, nestedRegions.size());
	AbstractTraceRegion child = nestedRegions.get(0);
	assertEquals(8, child.getMergedAssociatedLocation().getOffset());
	assertEquals(15, child.getMergedAssociatedLocation().getLength());
	assertEquals("initialfirst".length(), child.getMyOffset());
	assertEquals("second".length(), child.getMyLength());
}
 
Example #3
Source File: TreeAppendableTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testIntermediatesMayBeEmpty() {
	ITextRegionWithLineInformation root = new TextRegionWithLineInformation(47, 11, 12, 137);
	ITextRegionWithLineInformation emptyChild = new TextRegionWithLineInformation(8, 15, 12, 137);
	ITextRegionWithLineInformation emptyGrandChild = new TextRegionWithLineInformation(123, 321, 12, 137);
	expectedRegions = Arrays.asList(root, emptyChild, emptyGrandChild).iterator();
	TreeAppendable appendable = new TreeAppendable(new ImportManager(false), this, this, this, content, "  ", "\n");
	appendable.trace(content).trace(content).append("text");
	assertEquals("text", appendable.getContent());
	AbstractTraceRegion traceRegion = appendable.getTraceRegion();
	assertEquals(1, traceRegion.getNestedRegions().size());
	AbstractTraceRegion child = traceRegion.getNestedRegions().get(0);
	assertEquals(1, child.getNestedRegions().size());
	AbstractTraceRegion grandChild = child.getNestedRegions().get(0);
	assertTrue(grandChild.getNestedRegions().isEmpty());
	assertEquals(0, grandChild.getMyOffset());
	assertEquals(4, grandChild.getMyLength());
	assertEquals(123, grandChild.getMergedAssociatedLocation().getOffset());
	assertEquals(321, grandChild.getMergedAssociatedLocation().getLength());
}
 
Example #4
Source File: TreeAppendableTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testInsertionIsProhibited() {
	ITextRegionWithLineInformation root = new TextRegionWithLineInformation(47, 11, 12, 137);
	ITextRegionWithLineInformation child = new TextRegionWithLineInformation(8, 15, 12, 137);
	expectedRegions = Arrays.asList(root, child).iterator();
	TreeAppendable appendable = new TreeAppendable(new ImportManager(false), this, this, this, content, "  ", "\n");
	TreeAppendable traced = appendable.trace(content);
	appendable.append("test");
	// don't use @Test(expected=..) since we want to be sure about the cause
	try {
		traced.append("insertion");
		fail("Expected IllegalStateException");
	} catch(IllegalStateException e) {
		// expected
	}
}
 
Example #5
Source File: TreeAppendableTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testEmpty() {
	expectedRegions = Collections.<ITextRegionWithLineInformation>singleton(new TextRegionWithLineInformation(47, 11, 12, 137)).iterator();
	TreeAppendable appendable = new TreeAppendable(new ImportManager(false), this, this, this, content, "  ", "\n");
	assertEquals("", appendable.getContent());
	AbstractTraceRegion traceRegion = appendable.getTraceRegion();
	assertNotNull(traceRegion);
	assertEquals(47, traceRegion.getMergedAssociatedLocation().getOffset());
	assertEquals(11, traceRegion.getMergedAssociatedLocation().getLength());
	assertEquals(getURIForTrace(resource), traceRegion.getAssociatedSrcRelativePath());
	assertTrue(traceRegion.getNestedRegions().isEmpty());
}
 
Example #6
Source File: TraceRegionTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testLeafIterator_TwoChildren_WithGaps() {
	final TraceRegion parent = new TraceRegion(0, 3, 0, 3, true, 2, 3, 0, 0, null, newURI());
	TraceRegion first = new TraceRegion(0, 1, 0, 1, true, 2, 3, 0, 0, parent, null);
	AbstractTraceRegion second = new AbstractStatefulTraceRegion(new TextRegionWithLineInformation(1, 1, 1, 2), true, new LocationData(2, 3, 0, 0, null), parent) {};
	AbstractTraceRegion third = new TraceRegion(2, 1, 2, 3, true, 3, 4, 0, 0, parent, null);
	Iterator<AbstractTraceRegion> iter = parent.leafIterator();
	assertEquals(Arrays.asList(first, second, third).iterator(), iter);
}
 
Example #7
Source File: TraceRegionTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testLeafIterator_OneGrandChild_RightGap() {
	final TraceRegion root = new TraceRegion(0, 2, 0, 2, true, 2, 3, 0, 0, null, newURI());
	TraceRegion parent = new TraceRegion(0, 1, 0, 1, true, 3, 4, 0, 0, root, null);
	TraceRegion first = new TraceRegion(0, 1, 0, 1, true, 3, 4, 0, 0, parent, null);
	AbstractTraceRegion second = new AbstractStatefulTraceRegion(new TextRegionWithLineInformation(1, 1, 1, 2), true, new LocationData(2, 3, 0, 0, null), root) {};
	Iterator<AbstractTraceRegion> iter = root.leafIterator();
	assertEquals(Arrays.asList(first, second).iterator(), iter);
}
 
Example #8
Source File: TraceRegionTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testLeafIterator_OneGrandChild_LeftGap() {
	final TraceRegion root = new TraceRegion(0, 2, 0, 2, true, 2, 3, 0, 0, null, newURI());
	AbstractTraceRegion first = new AbstractStatefulTraceRegion(new TextRegionWithLineInformation(0, 1, 0, 1), true, new LocationData(2, 3, 0, 0, null), root) {};
	TraceRegion parent = new TraceRegion(1, 1, 1, 2, true, 3, 4, 0, 0, root, null);
	TraceRegion second = new TraceRegion(1, 1, 1, 2, true, 3, 4, 0, 0, parent, null);
	Iterator<AbstractTraceRegion> iter = root.leafIterator();
	assertEquals(Arrays.asList(first, second).iterator(), iter);
}
 
Example #9
Source File: TraceRegionTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testLeafIterator_OneChild_RightGap() {
	final TraceRegion parent = new TraceRegion(0, 2, 0, 2, true, 2, 3, 0, 0, null, newURI());
	AbstractTraceRegion first = new TraceRegion(0, 1, 0, 1, true, 3, 4, 0, 0, parent, null);
	AbstractTraceRegion second = new AbstractStatefulTraceRegion(new TextRegionWithLineInformation(1, 1, 1, 2), true, new LocationData(2, 3, 0, 0, null), parent) {};
	Iterator<AbstractTraceRegion> iter = parent.leafIterator();
	assertEquals(Arrays.asList(first, second).iterator(), iter);
}
 
Example #10
Source File: TraceRegionTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testLeafIterator_OneChild_LeftGap() {
	final TraceRegion parent = new TraceRegion(0, 2, 0, 2, true, 2, 3, 0, 0, null, newURI());
	AbstractTraceRegion first = new AbstractStatefulTraceRegion(new TextRegionWithLineInformation(0, 1, 0, 1), true, new LocationData(2, 3, 0, 0, null), parent) {};
	TraceRegion second = new TraceRegion(1, 1, 1, 2, true, 3, 4, 0, 0, parent, null);
	Iterator<AbstractTraceRegion> iter = parent.leafIterator();
	assertEquals(Arrays.asList(first, second).iterator(), iter);
}
 
Example #11
Source File: TraceRegion.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public TraceRegion(int myOffset, int myLength, int myLineNumber, int myEndLineNumber, boolean useForDebugging, ILocationData locationData, AbstractTraceRegion parent) {
	super(new TextRegionWithLineInformation(myOffset, myLength, myLineNumber, myEndLineNumber), useForDebugging, locationData, parent);
	if (myOffset == myLength && myOffset == 0) {
		throw new IllegalArgumentException();
	}
	if (parent == null) {
		if (locationData.getSrcRelativePath() == null) {
			throw new IllegalArgumentException("associatedPath may not be null");
		}
		} else {
			if (parent.getAssociatedSrcRelativePath() == null && locationData.getSrcRelativePath() == null) {
				throw new IllegalArgumentException("associatedPath may not be null");
			}
		}
}
 
Example #12
Source File: AbstractTraceRegion.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the merged location of all associated locations if they belong to the same resource. Otherwise
 * <code>null</code> is returned.
 */
public ILocationData getMergedAssociatedLocation() {
	List<ILocationData> allData = getAssociatedLocations();
	if (allData.isEmpty()) {
		return null;
	}
	if (allData.size() == 1) {
		return allData.get(0);
	}
	boolean allNull = true;
	SourceRelativeURI path = null;
	ITextRegionWithLineInformation region = ITextRegionWithLineInformation.EMPTY_REGION;
	for (ILocationData data : allData) {
		if (path != null) {
			if (!path.equals(data.getSrcRelativePath())) {
				return null;
			}
		} else {
			if (data.getSrcRelativePath() == null) {
				if (!allNull)
					throw new IllegalStateException(
							"Iff multiple associated locations are present, the path has to be set");
			} else {
				allNull = false;
				path = data.getSrcRelativePath();
			}
		}
		region = region.merge(new TextRegionWithLineInformation(data.getOffset(), data.getLength(),
				data.getLineNumber(), data.getEndLineNumber()));
	}
	return new LocationData(region.getOffset(), region.getLength(), region.getLineNumber(),
			region.getEndLineNumber(), path);
}
 
Example #13
Source File: AbstractNode.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.5
 */
protected ITextRegionWithLineInformation getTextRegionWithLineInformation(int offset, int length) {
	INode rootNode = getRootNode();
	if (rootNode != null) {
		int startLine = basicGetLineOfOffset(rootNode, offset);
		int endLine = basicGetLineOfOffset(rootNode, offset + length);
		return new TextRegionWithLineInformation(offset, length, startLine, endLine); 
	}
	return new TextRegionWithLineInformation(offset, length, 1, 1);
}
 
Example #14
Source File: CompilerTraceTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public AbstractTraceRegion merge(final List<AbstractTraceRegion> regions) {
  boolean _isEmpty = regions.isEmpty();
  boolean _not = (!_isEmpty);
  Assert.assertTrue(_not);
  int _size = regions.size();
  boolean _greaterThan = (_size > 1);
  if (_greaterThan) {
    ITextRegionWithLineInformation rootLocation = ITextRegionWithLineInformation.EMPTY_REGION;
    ITextRegionWithLineInformation associated = ITextRegionWithLineInformation.EMPTY_REGION;
    for (final AbstractTraceRegion child : regions) {
      {
        int _myOffset = child.getMyOffset();
        int _myLength = child.getMyLength();
        int _myLineNumber = child.getMyLineNumber();
        int _myEndLineNumber = child.getMyEndLineNumber();
        TextRegionWithLineInformation _textRegionWithLineInformation = new TextRegionWithLineInformation(_myOffset, _myLength, _myLineNumber, _myEndLineNumber);
        rootLocation = rootLocation.merge(_textRegionWithLineInformation);
        ILocationData childAssociation = child.getMergedAssociatedLocation();
        if ((childAssociation != null)) {
          associated = associated.merge(childAssociation);
        }
      }
    }
    final RootTraceRegionForTesting root = new RootTraceRegionForTesting(rootLocation, associated);
    for (final AbstractTraceRegion child_1 : regions) {
      child_1.setParent(root);
    }
    return root;
  } else {
    return IterableExtensions.<AbstractTraceRegion>head(regions);
  }
}
 
Example #15
Source File: TreeAppendableTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testLineNumbers() {
	expectedRegions = new AbstractIterator<ITextRegionWithLineInformation>() {
		int start = 0;
		@Override
		protected ITextRegionWithLineInformation computeNext() {
			return new TextRegionWithLineInformation(start++, 1, 1, 1);
		}
	};
	TreeAppendable appendable = new TreeAppendable(new ImportManager(false), this, this, this, content, "  ", "\n");
	appendable.append("start line").increaseIndentation();
	appendable.newLine().trace(content).append("1");
	appendable.trace(content).newLine().append("2");
	appendable.newLine().trace(content).append("3\n4");
	appendable.decreaseIndentation().newLine().append("last line");
	assertEquals(
			"start line\n" +
			"  1\n" +
			"  2\n" +
			"  3\n" +
			"  4\n" +
			"last line", appendable.getContent());
	AbstractTraceRegion rootTraceRegion = appendable.getTraceRegion();
	assertEquals(0, rootTraceRegion.getMyLineNumber());
	assertEquals(5, rootTraceRegion.getMyEndLineNumber());
	AbstractTraceRegion firstChild = rootTraceRegion.getNestedRegions().get(0);
	assertEquals(1, firstChild.getMyLineNumber());
	assertEquals(1, firstChild.getMyEndLineNumber());
	AbstractTraceRegion secondChild = rootTraceRegion.getNestedRegions().get(1);
	assertEquals(2, secondChild.getMyLineNumber());
	assertEquals(2, secondChild.getMyEndLineNumber());
	AbstractTraceRegion thirdChild = rootTraceRegion.getNestedRegions().get(2);
	assertEquals(3, thirdChild.getMyLineNumber());
	assertEquals(4, thirdChild.getMyEndLineNumber());
	expectedRegions = null;
}
 
Example #16
Source File: TreeAppendableTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testUnsafeInsertionIsOk() {
	ITextRegionWithLineInformation root = new TextRegionWithLineInformation(47, 11, 12, 137);
	ITextRegionWithLineInformation child = new TextRegionWithLineInformation(8, 15, 12, 137);
	expectedRegions = Arrays.asList(root, child).iterator();
	TreeAppendable appendable = new TreeAppendable(new ImportManager(false), this, this, this, content, "  ", "\n");
	TreeAppendable traced = appendable.trace(content);
	appendable.append("test");
	traced.appendUnsafe("insertion");
	assertEquals("insertiontest", appendable.getContent());
}
 
Example #17
Source File: TreeAppendableTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testNoEmptyLeafs() {
	ITextRegionWithLineInformation root = new TextRegionWithLineInformation(47, 11, 12, 137);
	ITextRegionWithLineInformation emptyChild = new TextRegionWithLineInformation(8, 15, 12, 137);
	ITextRegionWithLineInformation emptyGrandChild = new TextRegionWithLineInformation(123, 321, 12, 137);
	expectedRegions = Arrays.asList(root, emptyChild, emptyGrandChild).iterator();
	TreeAppendable appendable = new TreeAppendable(new ImportManager(false), this, this, this, content, "  ", "\n");
	appendable.append("initial");
	appendable.trace(content).trace(content);
	appendable.append("end");
	assertEquals("initialend", appendable.getContent());
	AbstractTraceRegion traceRegion = appendable.getTraceRegion();
	assertTrue(traceRegion.getNestedRegions().isEmpty());
}
 
Example #18
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ILocationData toLocationData(List<INode> nodes) {
	ITextRegionWithLineInformation result = ITextRegionWithLineInformation.EMPTY_REGION;
	for (INode node : nodes) {
		if (!isHidden(node)) {
			ITextRegionWithLineInformation region = node.getTextRegionWithLineInformation();
			if (region.getLength() != 0) {
				result = result.merge(new TextRegionWithLineInformation(region.getOffset(), region.getLength(), region.getLineNumber() - 1, region.getEndLineNumber() - 1));
			}
		}
	}
	if (result.getLength() == 0)
		return null;
	return new LocationData(result.getOffset(), result.getLength(), result.getLineNumber(),
			result.getEndLineNumber(), null);
}
 
Example #19
Source File: AbstractTraceRegion.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public ITextRegionWithLineInformation getMyRegion() {
	return new TextRegionWithLineInformation(getMyOffset(), getMyLength(), getMyLineNumber(), getMyEndLineNumber());
}
 
Example #20
Source File: AbstractLocationInResource.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ITextRegionWithLineInformation getTextRegion() {
	ITextRegionWithLineInformation result = new TextRegionWithLineInformation(getOffset(), getLength(), getLineNumber(), getEndLineNumber());
	return result;
}
 
Example #21
Source File: TemporaryTraceRegion.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public TemporaryTraceRegion(int myOffset, int myLength, int myLineNumber, int myEndLineNumber, boolean useForDebugging, List<ILocationData> locations,
		AbstractTraceRegion parent) {
	super(new TextRegionWithLineInformation(myOffset, myLength, myLineNumber, myEndLineNumber), useForDebugging, locations, parent);
}
 
Example #22
Source File: XtendLocationInFileProvider.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ITextRegion getSignificantTextRegion(EObject element) {
	if (element instanceof RichStringLiteral) {
		ICompositeNode elementNode = findNodeFor(element);
		if (elementNode == null) {
			return ITextRegion.EMPTY_REGION;
		}
		ITextRegion result = ITextRegion.EMPTY_REGION;
		for (INode node : elementNode.getLeafNodes()) {
			if (isHidden(node)) {
				continue;
			}
			EObject grammarElement = node.getGrammarElement();
			if (!(grammarElement instanceof RuleCall)) {
				continue;
			}
			RuleCall ruleCall = (RuleCall) grammarElement;
			
			ITextRegionWithLineInformation region = node.getTextRegionWithLineInformation();
			int offset = region.getOffset();
			int length = region.getLength();
			if (grammarAccess.getRICH_TEXTRule() == ruleCall.getRule()) {
				offset += 3;
				length -= 6;
			} else if (grammarAccess.getRICH_TEXT_STARTRule() == ruleCall.getRule()) {
				offset += 3;
				length -= 4;
			} else if (grammarAccess.getRICH_TEXT_ENDRule() == ruleCall.getRule()) {
				offset += 1;
				length -= 4;
			} else if (grammarAccess.getRICH_TEXT_INBETWEENRule() == ruleCall.getRule()) {
				offset += 1;
				length -= 2;
			} else if (grammarAccess.getCOMMENT_RICH_TEXT_ENDRule() == ruleCall.getRule()) {
				offset += 2;
				length -= 5;
			} else if (grammarAccess.getCOMMENT_RICH_TEXT_INBETWEENRule() == ruleCall.getRule()) {
				offset += 2;
				length -= 3;
			} else {
				continue;
			}
			result = result.merge(toZeroBasedRegion(new TextRegionWithLineInformation(offset, length, region.getLineNumber(), region.getEndLineNumber())));
		}
		return result;
	}
	return super.getSignificantTextRegion(element);
}
 
Example #23
Source File: GeneratorNodeProcessor.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public void complete(int offset, int length, int startLine, int endLine) {
	region.delegate = new TextRegionWithLineInformation(offset,
			length, startLine, endLine);
}
 
Example #24
Source File: DefaultLocationInFileProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.5
 */
protected TextRegionWithLineInformation toZeroBasedRegion(ITextRegionWithLineInformation region) {
	return new TextRegionWithLineInformation(region.getOffset(), region.getLength(), region.getLineNumber() - 1, region.getEndLineNumber() - 1);
}
 
Example #25
Source File: UnicodeAwarePostProcessorTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
private ITextRegion getDummyTextRegion() {
	return new TextRegionWithLineInformation(1, 1, 1, 1);
}