org.eclipse.xtext.util.ITextRegionWithLineInformation Java Examples

The following examples show how to use org.eclipse.xtext.util.ITextRegionWithLineInformation. 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: XbaseStratumBreakpointSupport.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isValidLineForBreakpoint(ICompositeNode node, int line) {
	for (INode n : node.getChildren()) {
		ITextRegionWithLineInformation textRegion = n.getTextRegionWithLineInformation();
		if (textRegion.getLineNumber()<= line && textRegion.getEndLineNumber() >= line) {
			EObject eObject = n.getSemanticElement();
			if (eObject instanceof XExpression && !(eObject.eClass() == XbasePackage.Literals.XBLOCK_EXPRESSION)) {
				return true;
			}
			if (n instanceof ICompositeNode && isValidLineForBreakpoint((ICompositeNode) n, line)) {
				return true;
			}
		}
		if (textRegion.getLineNumber() > line) {
			return false;
		}
	}
	return false;
}
 
Example #2
Source File: DotFoldingRegionProvider.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void computeObjectFolding(EObject eObject,
		IFoldingRegionAcceptor<ITextRegion> foldingRegionAcceptor) {
	ILocationInFileProvider locationInFileProvider = getLocationInFileProvider();
	ITextRegion region = locationInFileProvider.getFullTextRegion(eObject);
	ITextRegionWithLineInformation regionWithLineInformation = (ITextRegionWithLineInformation) region;
	if (region != null) {
		ITextRegion significant = locationInFileProvider
				.getSignificantTextRegion(eObject);
		if (significant == null)
			throw new NullPointerException(
					"significant region may not be null"); //$NON-NLS-1$
		if (!isAlreadyAccepted(regionWithLineInformation)) {
			foldingRegionAcceptor.accept(region.getOffset(),
					region.getLength(), significant);
			acceptedRegions.add(regionWithLineInformation);
		}
	}
}
 
Example #3
Source File: LocationInFileProviderTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug353969_04() throws Exception {
	Bus b = (Bus) model.getComponents().get(1);
	EList<Mode> modes = b.getMode();
	assertEquals(2, modes.size());
	Mode mb2 = modes.get(1);
	ITextRegionWithLineInformation mb2FullRegion = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(mb2);
	assertEquals(modelAsString.indexOf("mode mb2;"), mb2FullRegion.getOffset());
	assertEquals("mode mb2;".length(), mb2FullRegion.getLength());
	assertEquals(9, mb2FullRegion.getLineNumber());
	assertEquals(9, mb2FullRegion.getEndLineNumber());
	ITextRegionWithLineInformation mb2SignificantRegion = (ITextRegionWithLineInformation) locationInFileProvider.getSignificantTextRegion(mb2);
	assertEquals(modelAsString.indexOf("mb2;"), mb2SignificantRegion.getOffset());
	assertEquals("mb2".length(), mb2SignificantRegion.getLength());
	assertEquals(9, mb2SignificantRegion.getLineNumber());
	assertEquals(9, mb2SignificantRegion.getEndLineNumber());
}
 
Example #4
Source File: LocationInFileProviderTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug353969_03() throws Exception {
	Bus b = (Bus) model.getComponents().get(1);
	EList<Mode> modes = b.getMode();
	assertEquals(2, modes.size());
	Mode mb1 = modes.get(0);
	ITextRegionWithLineInformation mb1FullRegion = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(mb1);
	assertEquals(modelAsString.indexOf("mode mb1;"), mb1FullRegion.getOffset());
	assertEquals("mode mb1;".length(), mb1FullRegion.getLength());
	assertEquals(8, mb1FullRegion.getLineNumber());
	assertEquals(8, mb1FullRegion.getEndLineNumber());
	ITextRegionWithLineInformation mb1SignificantRegion = (ITextRegionWithLineInformation) locationInFileProvider.getSignificantTextRegion(mb1);
	assertEquals(modelAsString.indexOf("mb1;"), mb1SignificantRegion.getOffset());
	assertEquals("mb1".length(), mb1SignificantRegion.getLength());
	assertEquals(8, mb1SignificantRegion.getLineNumber());
	assertEquals(8, mb1SignificantRegion.getEndLineNumber());
}
 
Example #5
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 #6
Source File: LocationInFileProviderTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug353969_02() throws Exception {
	Processor p = (Processor) model.getComponents().get(0);
	EList<Mode> modes = p.getMode();
	assertEquals(2, modes.size());
	Mode m2 = modes.get(1);
	ITextRegionWithLineInformation m2FullRegion = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(m2);
	assertEquals(modelAsString.indexOf("mode m2;"), m2FullRegion.getOffset());
	assertEquals("mode m2;".length(), m2FullRegion.getLength());
	assertEquals(4, m2FullRegion.getLineNumber());
	assertEquals(4, m2FullRegion.getEndLineNumber());
	ITextRegionWithLineInformation m2SignificantRegion = (ITextRegionWithLineInformation) locationInFileProvider.getSignificantTextRegion(m2);
	assertEquals(modelAsString.indexOf("m2;"), m2SignificantRegion.getOffset());
	assertEquals("m2".length(), m2SignificantRegion.getLength());
	assertEquals(4, m2SignificantRegion.getLineNumber());
	assertEquals(4, m2SignificantRegion.getEndLineNumber());
}
 
Example #7
Source File: LocationInFileProviderTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug353969_01() throws Exception {
	Processor p = (Processor) model.getComponents().get(0);
	EList<Mode> modes = p.getMode();
	assertEquals(2, modes.size());
	Mode m1 = modes.get(0);
	ITextRegionWithLineInformation m1FullRegion = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(m1);
	assertEquals(modelAsString.indexOf("mode m1;"), m1FullRegion.getOffset());
	assertEquals("mode m1;".length(), m1FullRegion.getLength());
	assertEquals(3, m1FullRegion.getLineNumber());
	assertEquals(3, m1FullRegion.getEndLineNumber());
	ITextRegionWithLineInformation m1SignificantRegion = (ITextRegionWithLineInformation) locationInFileProvider.getSignificantTextRegion(m1);
	assertEquals(modelAsString.indexOf("m1;"), m1SignificantRegion.getOffset());
	assertEquals("m1".length(), m1SignificantRegion.getLength());
	assertEquals(3, m1SignificantRegion.getLineNumber());
	assertEquals(3, m1SignificantRegion.getEndLineNumber());
}
 
Example #8
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 #9
Source File: DefaultLocationInFileProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.3
 */
protected ITextRegion createRegion(List<INode> nodes, RegionDescription query) {
	if (query == RegionDescription.FULL || query == RegionDescription.SIGNIFICANT)
		return createRegion(nodes);
	ITextRegion result = ITextRegion.EMPTY_REGION;
	for (INode node : nodes) {
		for(INode leafNode: node.getLeafNodes()) {
			if (!isHidden(leafNode, query)) {
				ITextRegionWithLineInformation region = leafNode.getTextRegionWithLineInformation();
				if (region.getLength() != 0) {
					result = result.merge(toZeroBasedRegion(region));
				}
			}
		}
	}
	return result;
}
 
Example #10
Source File: N4JSDiagnosticConverter.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected IssueLocation getLocationForNode(INode node) {
	ITextRegionWithLineInformation nodeRegion = node.getTextRegionWithLineInformation();
	N4JSIssueLocation result = new N4JSIssueLocation();
	result.offset = nodeRegion.getOffset();
	result.length = nodeRegion.getLength();

	LineAndColumn lineAndColumnStart = NodeModelUtils.getLineAndColumn(node, result.offset);
	result.lineNumber = lineAndColumnStart.getLine();
	result.column = lineAndColumnStart.getColumn();

	LineAndColumn lineAndColumnEnd = NodeModelUtils.getLineAndColumn(node, result.offset + result.length);
	result.lineNumberEnd = lineAndColumnEnd.getLine();
	result.columnEnd = lineAndColumnEnd.getColumn();
	return result;
}
 
Example #11
Source File: XbaseFormatter2.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isMultiline(final XExpression expression, final FormattableDocument doc) {
  final INode node = this._nodeModelAccess.nodeForEObject(expression);
  boolean _and = false;
  if (!(node != null)) {
    _and = false;
  } else {
    boolean _xblockexpression = false;
    {
      final ITextRegionWithLineInformation textRegion = node.getTextRegionWithLineInformation();
      int _lineNumber = textRegion.getLineNumber();
      int _endLineNumber = textRegion.getEndLineNumber();
      _xblockexpression = (_lineNumber != _endLineNumber);
    }
    _and = _xblockexpression;
  }
  return _and;
}
 
Example #12
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 #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: DefaultHierarchyNodeLocationProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ITextRegionWithLineInformation getTextRegion(EObject obj) {
	if (obj == null) {
		return ITextRegionWithLineInformation.EMPTY_REGION;
	}
	ITextRegion textRegion = locationInFileProvider.getSignificantTextRegion(obj);
	return toTextRegionWithLineInformation(obj, textRegion);
}
 
Example #15
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 #16
Source File: DerivedResourceMarkerCopier.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void copyProblemMarker(IFile javaFile, IEclipseTrace traceToSource, Set<IMarker> problemsInJava, IFile srcFile)
		throws CoreException {
	String sourceMarkerType = null;
	for (IMarker marker : problemsInJava) {
		String message = (String) marker.getAttribute(IMarker.MESSAGE);
		if (message == null) {
			continue;
		}
		Integer charStart = marker.getAttribute(IMarker.CHAR_START, 0);
		Integer charEnd = marker.getAttribute(IMarker.CHAR_END, 0);
		int severity = MarkerUtilities.getSeverity(marker);

		ILocationInEclipseResource associatedLocation = traceToSource.getBestAssociatedLocation(new TextRegion(charStart,
				charEnd - charStart));
		if (associatedLocation != null) {
			if (sourceMarkerType == null) {
				sourceMarkerType = determinateMarkerTypeByURI(associatedLocation.getSrcRelativeResourceURI());
			}
			if (!srcFile.equals(findIFile(associatedLocation, srcFile.getWorkspace()))) {
				LOG.error("File in associated location is not the same as main source file.");
			}
			IMarker xtendMarker = srcFile.createMarker(sourceMarkerType);
			xtendMarker.setAttribute(IMarker.MESSAGE, "Java problem: " + message);
			xtendMarker.setAttribute(IMarker.SEVERITY, severity);
			ITextRegionWithLineInformation region = associatedLocation.getTextRegion();
			xtendMarker.setAttribute(IMarker.LINE_NUMBER, region.getLineNumber());
			xtendMarker.setAttribute(IMarker.CHAR_START, region.getOffset());
			xtendMarker.setAttribute(IMarker.CHAR_END, region.getOffset() + region.getLength());
			xtendMarker.setAttribute(COPIED_FROM_FILE, javaFile.getFullPath().toString());
		}
	}

}
 
Example #17
Source File: DotFoldingRegionProvider.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isAlreadyAccepted(
		ITextRegionWithLineInformation regionWithLineInformation) {
	for (ITextRegionWithLineInformation acceptedRegion : acceptedRegions) {
		if (equals(regionWithLineInformation, acceptedRegion)) {
			return true;
		}
	}
	return false;
}
 
Example #18
Source File: DefaultCallHierarchyBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String getText(EObject obj, ITextRegionWithLineInformation textRegion) {
	if (obj == null || textRegion == ITextRegionWithLineInformation.EMPTY_REGION) {
		return "";
	}
	ICompositeNode node = NodeModelUtils.getNode(EcoreUtil.getRootContainer(obj));
	if (node == null) {
		return "";
	}
	int endOffset = textRegion.getOffset() + textRegion.getLength();
	return node.getRootNode().getText().substring(textRegion.getOffset(), endOffset);
}
 
Example #19
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 #20
Source File: DefaultCallHierarchyBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return a hierarchy node reference for the given reference; cannot be <code>null</code>
 */
protected IHierarchyNodeReference createNodeReference(IReferenceDescription reference) {
	return readOnly(reference.getSourceEObjectUri(), (EObject sourceObject) -> {
		ITextRegionWithLineInformation textRegion = getTextRegion(sourceObject, reference.getEReference(),
				reference.getIndexInList());
		String text = getText(sourceObject, textRegion);
		return new DefaultHierarchyNodeReference(text, textRegion, reference);
	});
}
 
Example #21
Source File: LocationInFileProviderTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testLineNumbers_02() throws Exception {
	Bus b = (Bus) model.getComponents().get(1);
	ITextRegionWithLineInformation fullRegion = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(b);
	int b1Index = modelAsString.indexOf("bus b1");
	assertEquals(b1Index, fullRegion.getOffset());
	assertEquals(modelAsString.length() - b1Index, fullRegion.getLength());
	assertEquals(6, fullRegion.getLineNumber());
	assertEquals(10, fullRegion.getEndLineNumber());
}
 
Example #22
Source File: AbstractNode.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.5
 */
@Override
public ITextRegionWithLineInformation getTextRegionWithLineInformation() {
	int offset = getOffset();
	int length = getEndOffset() - offset;
	return getTextRegionWithLineInformation(offset, length);
}
 
Example #23
Source File: AbstractNode.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.5
 */
@Override
public ITextRegionWithLineInformation getTotalTextRegionWithLineInformation() {
	int totalOffset = getTotalOffset();
	int totalLength = getTotalLength();
	return getTextRegionWithLineInformation(totalOffset, totalLength);
}
 
Example #24
Source File: LocationInFileProviderTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testLineNumbers_01() throws Exception {
	Processor p = (Processor) model.getComponents().get(0);
	ITextRegionWithLineInformation fullRegion = (ITextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(p);
	int b1Index = modelAsString.indexOf("processor p1");
	assertEquals(b1Index, fullRegion.getOffset());
	assertEquals(modelAsString.indexOf("end;") - b1Index + "end;".length(), fullRegion.getLength());
	assertEquals(1, fullRegion.getLineNumber());
	assertEquals(5, fullRegion.getEndLineNumber());
}
 
Example #25
Source File: GeneratorNodeProcessor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Delegate
public ITextRegionWithLineInformation getDelegate() {
	if (delegate == null) {
		throw new IllegalStateException("region not completed");
	}
	return delegate;
}
 
Example #26
Source File: AbstractStatefulTraceRegion.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected AbstractStatefulTraceRegion(ITextRegionWithLineInformation myRegion, boolean useForDebugging, List<ILocationData> associatedLocations, AbstractTraceRegion parent) {
	super(parent);
	this.myRegion = myRegion;
	this.useForDebugging = useForDebugging;
	this.associatedLocations = associatedLocations;
	if (!isConsistentWithParent()) {
		throw new IllegalArgumentException("Produced region is inconsistent with parent, this: " + this + ", parent: " + parent);
	}
}
 
Example #27
Source File: DiagnosticConverterImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Computes {@link IssueLocation} for the given node.
 */
protected IssueLocation getLocationForNode(INode node) {
	ITextRegionWithLineInformation nodeRegion = node.getTextRegionWithLineInformation();
	int offset = nodeRegion.getOffset();
	int length = nodeRegion.getLength();
	return getLocationForNode(node, offset, length);
}
 
Example #28
Source File: DefaultLocationInFileProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected ITextRegion createRegion(final List<INode> nodes) {
	ITextRegion result = ITextRegion.EMPTY_REGION;
	for (INode node : nodes) {
		if (!isHidden(node)) {
			ITextRegionWithLineInformation region = node.getTextRegionWithLineInformation();
			if (region.getLength() != 0) {
				result = result.merge(toZeroBasedRegion(region));
			}
		}
	}
	return result;
}
 
Example #29
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 #30
Source File: CharSequenceTraceWrapper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public CharSequence wrapWithTraceData(CharSequence sequence, EObject origin) {
	ITextRegionWithLineInformation location = (ITextRegionWithLineInformation) locationInFileProvider.getSignificantTextRegion(origin);
	AbsoluteURI absoluteURI = new AbsoluteURI(origin.eResource().getURI());
	IProjectConfig projectConfig = projectConfigProvider.getProjectConfig(EcoreUtil2.getResourceSet(origin));
	SourceRelativeURI sourceRelativeURI = absoluteURI.deresolve(projectConfig);
	return wrapWithTraceData(sequence, sourceRelativeURI, location.getOffset(), location.getLength(), location.getLineNumber(), location.getEndLineNumber());
}