org.eclipse.xtext.util.LineAndColumn Java Examples

The following examples show how to use org.eclipse.xtext.util.LineAndColumn. 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: 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 #2
Source File: Bug437669Test.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testUnresolved_02() {
	Type type = getContext();
	INode nameNode = NodeModelUtils.findNodesForFeature(type, ImportedURIPackage.Literals.TYPE__NAME).get(0);
	resolve(type, "BlaBlaBla", nameNode.getOffset(), nameNode.getLength());
	Resource resource = type.eResource();
	assertEquals(resource.getErrors().toString(), 1, resource.getErrors().size());
	
	LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(nameNode, nameNode.getOffset());

	Diagnostic diagnostic = (Diagnostic) resource.getErrors().get(0);
	assertEquals(nameNode.getOffset(), diagnostic.getOffset());
	assertEquals(nameNode.getLength(), diagnostic.getLength());
	assertEquals(lineAndColumn.getLine(), diagnostic.getLine());
	assertEquals(lineAndColumn.getColumn(), diagnostic.getColumn());
	assertEquals("Couldn't resolve reference to Type 'BlaBlaBla'.", diagnostic.getMessage());
}
 
Example #3
Source File: InternalNodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected static LineAndColumn getLineAndColumn(String text, int[] lineBreaks, int offset) {
	if (offset > text.length() || offset < 0) {
		throw new IndexOutOfBoundsException();
	}
	int idx = Arrays.binarySearch(lineBreaks, offset);
	if (idx >= 0) {
		/*
		 * We found an entry in the array. The offset is a line break.
		 * The line number is the idx in the array, the column needs to be
		 * adjusted.
		 */
		return getLineAndColumnOfLineBreak(text, lineBreaks, idx, offset);
	} else {
		// if not found, the result of a binary search `-(insertion point) - 1`
		int insertionPoint = -(idx + 1);
		return getLineAndColumnNoExactLineBreak(text, lineBreaks, insertionPoint, offset);
	}
}
 
Example #4
Source File: InternalNodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private static LineAndColumn getLineAndColumnOfLineBreak(String text, int[] lineBreaks, int lineBreakIdx, int offset) {
	int line, column;
	line = lineBreakIdx + 1;
	if (lineBreakIdx == 0) {
		column = offset + 1;
	} else {
		int lineBreak = lineBreaks[lineBreakIdx];
		int prevLineBreak = lineBreaks[lineBreakIdx - 1];
		if (prevLineBreak < lineBreak - 1 && text.charAt(prevLineBreak) == '\r' && text.charAt(prevLineBreak + 1) == '\n') {
			column = lineBreak - prevLineBreak - 1;
		} else {
			// we found two subsequent line breaks, e.g. \n\n
			// and the offset is the second line break.
			// the column number is 1 for that case.
			column = lineBreak - prevLineBreak;
		}
	}
	return LineAndColumn.from(line, column);
}
 
Example #5
Source File: N4JSDiagnosticConverter.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void convertResourceDiagnostic(Diagnostic diagnostic, Severity severity, IAcceptor<Issue> acceptor) {
	// START: Following copied from super method
	LSPIssue issue = new LSPIssue(); // Changed
	issue.setSyntaxError(diagnostic instanceof XtextSyntaxDiagnostic);
	issue.setSeverity(severity);
	issue.setLineNumber(diagnostic.getLine());
	issue.setColumn(diagnostic.getColumn());
	issue.setMessage(diagnostic.getMessage());

	if (diagnostic instanceof org.eclipse.xtext.diagnostics.Diagnostic) {
		org.eclipse.xtext.diagnostics.Diagnostic xtextDiagnostic = (org.eclipse.xtext.diagnostics.Diagnostic) diagnostic;
		issue.setOffset(xtextDiagnostic.getOffset());
		issue.setLength(xtextDiagnostic.getLength());
	}
	if (diagnostic instanceof AbstractDiagnostic) {
		AbstractDiagnostic castedDiagnostic = (AbstractDiagnostic) diagnostic;
		issue.setUriToProblem(castedDiagnostic.getUriToProblem());
		issue.setCode(castedDiagnostic.getCode());
		issue.setData(castedDiagnostic.getData());

		// START: Changes here
		INode node = ReflectionUtils.getMethodReturn(AbstractDiagnostic.class, "getNode", diagnostic);
		int posEnd = castedDiagnostic.getOffset() + castedDiagnostic.getLength();
		LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(node, posEnd);
		issue.setLineNumberEnd(lineAndColumn.getLine());
		issue.setColumnEnd(lineAndColumn.getColumn());
		// END: Changes here
	}
	issue.setType(CheckType.FAST);
	acceptor.accept(issue);
	// END: Following copied from super method
}
 
Example #6
Source File: DocumentExtensions.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public Position newPosition(Resource resource, int offset) {
	if (resource instanceof XtextResource) {
		ICompositeNode rootNode = ((XtextResource) resource).getParseResult().getRootNode();
		LineAndColumn lineAndColumn = getLineAndColumn(rootNode, offset);
		return new Position(lineAndColumn.getLine() - 1, lineAndColumn.getColumn() - 1);
	}
	return null;
}
 
Example #7
Source File: LineAndColumnTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void doAssertLineAndColumn(String text, int offset, int line, int column) {
	int[] lineBreaks = AccessibleNodeModelUtils.computeLineBreaks(text);
	LineAndColumn lineAndColumn = AccessibleNodeModelUtils.getLineAndColumn(text, lineBreaks, offset);
	Assert.assertEquals("line", line, lineAndColumn.getLine());
	Assert.assertEquals("column", column, lineAndColumn.getColumn());
	
	LineAndColumn offsetZero = AccessibleNodeModelUtils.getLineAndColumn(text, lineBreaks, 0);
	Assert.assertEquals("line", 1, offsetZero.getLine());
	Assert.assertEquals("column", 1, offsetZero.getColumn());
}
 
Example #8
Source File: AbstractDiagnostic.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public int getColumnEnd() {
	INode node = getNode();
	if (node != null) {
		LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(node, getOffset() + getLength());
		return lineAndColumn.getColumn();
	}
	return 0;
}
 
Example #9
Source File: AbstractDiagnostic.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public int getColumn() {
	INode node = getNode();
	if (node != null) {
		LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(node, getOffset());
		return lineAndColumn.getColumn();
	}
	return 0;
}
 
Example #10
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 offset and length in the given node.
 * 
 * @since 2.21
 */
protected IssueLocation getLocationForNode(INode node, int offset, int length) {
	IssueLocation result = new IssueLocation();
	result.offset = offset;
	result.length = length;
	
	LineAndColumn lineAndColumnStart = NodeModelUtils.getLineAndColumn(node, offset);
	result.lineNumber = lineAndColumnStart.getLine();
	result.column = lineAndColumnStart.getColumn();
	
	LineAndColumn lineAndColumnEnd = NodeModelUtils.getLineAndColumn(node, offset + length);
	result.lineNumberEnd = lineAndColumnEnd.getLine();
	result.columnEnd = lineAndColumnEnd.getColumn();
	return result;
}
 
Example #11
Source File: InternalNodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Obtain the line breaks from the document and search / compute the line number
 * and column number at the given document offset.
 */
protected static LineAndColumn getLineAndColumn(INode anyNode, int documentOffset) {
	INode rootNode = anyNode.getRootNode();
	int[] lineBreaks = getLineBreakOffsets(rootNode);
	String document = rootNode.getText();
	return getLineAndColumn(document, lineBreaks, documentOffset);
}
 
Example #12
Source File: PreparationStep.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Here we use the target of the original cross reference "id", "property", or "declaredType" to set the rewired
 * target (i.e. symbol table entry) of the corresponding IM-model reference "id_IM", "property_IM", or
 * "declaredType_IM".
 */
private void rewire(EObject eObject, ReferencingElement_IM copyEObject) {
	final IdentifiableElement originalTarget = getOriginalTargetOfNodeToRewire(eObject);
	final SymbolTableEntry rewiredTarget;
	if (!originalTarget.eIsProxy()) {
		// standard case: original target is a valid IdentifiableElement
		rewiredTarget = getSymbolTableEntry(originalTarget, true);
		copyEObject.setRewiredTarget(rewiredTarget);
	} else {
		// special case: unresolved proxy
		// -> this is usually an error, except in the following special cases:
		if (eObject instanceof ParameterizedPropertyAccessExpression) {
			// property access to an any+ type
			// -> because we know the transpiler is never invoked for resources that contain errors, we can
			// simply assume that we have the any+ case without actually checking the type of the receiver
			final String propName = getPropertyAsString((ParameterizedPropertyAccessExpression) eObject);
			((ParameterizedPropertyAccessExpression_IM) copyEObject).setAnyPlusAccess(true);
			((ParameterizedPropertyAccessExpression_IM) copyEObject).setNameOfAnyPlusProperty(propName);
		} else if (isDynamicNamespaceReference(eObject)) {
			// type references via dynamic namespace imports can still be transpiled
			// (no additional properties to set in ParameterizedTypeRef_IM)
		} else if (eObject instanceof IdentifierRef && eObject.eContainer() instanceof JSXElementName) {
			// name of a JSX element, e.g. the "div" in something like: <div prop='value'></div>
			String tagName = ((IdentifierRef) eObject).getIdAsText();
			((IdentifierRef_IM) copyEObject).setIdAsText(tagName);
		} else if (MigrationUtils.isMigrateCall(eObject.eContainer())) {
			// unresolved migrate-calls can still be transpiled
		} else {
			final ICompositeNode node = NodeModelUtils.findActualNodeFor(eObject);
			final LineAndColumn pos = NodeModelUtils.getLineAndColumn(node, node.getOffset());
			throw new UnresolvedProxyInSubGeneratorException(
					eObject.eResource(), pos.getLine(), pos.getColumn());
		}
	}
}
 
Example #13
Source File: InternalNodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private static LineAndColumn getLineAndColumnNoExactLineBreak(String text, int[] lineBreaks, int insertionPoint, int offset) {
	int line, column;
	if (insertionPoint == 0) {
		// the insertionPoint would be before the first line break
		// so we are in
		line = 1;
		// at 
		column = offset + 1;
	} else {
		int lineBreakIdx = insertionPoint - 1;
		int leftLineBreak = lineBreaks[lineBreakIdx];
		if (leftLineBreak + 1 == offset) {
			// offset is immediately after an existing line break
			if (text.charAt(leftLineBreak) == '\r' && text.charAt(offset) == '\n') {
				// windows line break, the offset belongs to the prev line
				line = insertionPoint;
				if (lineBreakIdx > 0) {
					int prevLineBreak = lineBreaks[lineBreakIdx - 1];
					// check for windows line breaks here, too
					if (text.charAt(prevLineBreak) == '\r' && text.charAt(prevLineBreak + 1) == '\n') {
						column = offset - prevLineBreak - 1;
					} else {
						column = offset - prevLineBreak;
					}
				} else {
					column = offset + 1;
				}
			} else {
				line = insertionPoint + 1;
				column = offset - leftLineBreak;
			}
		} else {
			line = insertionPoint + 1;
			// check for windows line breaks here, too
			if (text.charAt(leftLineBreak) == '\r' && text.charAt(leftLineBreak + 1) == '\n') {
				column = offset - leftLineBreak - 1;
			} else {
				column = offset - leftLineBreak;
			}
		}
	}
	return LineAndColumn.from(line, column);
}
 
Example #14
Source File: LineAndColumnTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public static LineAndColumn getLineAndColumn(String text, int[] lineBreaks, int offset) {
	return InternalNodeModelUtils.getLineAndColumn(text, lineBreaks, offset);
}
 
Example #15
Source File: IndentationAwareCompletionPrefixProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected INode findBestEndToken(INode root, INode candidate, int completionColumn, boolean candidateIsEndToken) {
	LinkedList<ILeafNode> sameGrammarElement = Lists.newLinkedList();
	PeekingIterator<ILeafNode> iterator = createReversedLeafIterator(root, candidate, sameGrammarElement);
	if (!iterator.hasNext()) {
		return candidate;
	}
	// collect all candidates that belong to the same offset
	LinkedList<ILeafNode> sameOffset = candidateIsEndToken ? collectLeafsWithSameOffset((ILeafNode)candidate, iterator) : Lists.newLinkedList();
	// continue until we find a paired leaf with length 0 that is at the correct offset
	EObject grammarElement = tryGetGrammarElementAsRule(candidateIsEndToken || sameGrammarElement.isEmpty() ? candidate : sameGrammarElement.getLast()); 
	ILeafNode result = candidateIsEndToken ? null : (ILeafNode) candidate;
	int sameOffsetSize = sameOffset.size();
	while(iterator.hasNext()) {
		ILeafNode next = iterator.next();
		if (result == null || result.isHidden()) {
			result = next;
		}
		if (next.getTotalLength() == 0) {
			// potential indentation token
			EObject rule = tryGetGrammarElementAsRule(next);
			if (rule != grammarElement) {
				LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(root, next.getTotalOffset());
				if (lineAndColumn.getColumn() <= completionColumn) {
					return result;
				} else {
					if (sameOffset.isEmpty()) {
						if (sameGrammarElement.isEmpty()) {
							result = null;	
						} else {
							result = sameGrammarElement.removeLast();
						}
						
					} else {
						if (sameOffsetSize >= sameOffset.size()) {
							result = sameOffset.removeLast();	
						} else {
							sameOffset.removeLast();
						}
					}
				}
			} else {
				sameOffset.add(next);
			}
		}
	}
	return candidate;
}
 
Example #16
Source File: NodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Compute the line and column information at the given offset from any node that belongs the the document. The line is one-based, e.g.
 * the first line has the line number '1'. The line break belongs the line that it breaks. In other words, the first line break in the
 * document also has the line number '1'. The column number starts at '1', too. In effect, the document offset '0' will always return
 * line '1' and column '1'.
 * 
 * If the given documentOffset points exactly to {@code anyNode.root.text.length}, it's assumed to be a virtual character thus
 * the offset is valid and the column and line information is returned as if it was there.
 * 
 * This contract is in sync with {@link org.eclipse.emf.ecore.resource.Resource.Diagnostic}.
 * 
 * @throws IndexOutOfBoundsException
 *             if the document offset does not belong to the document, 
 *             {@code documentOffset < 0 || documentOffset > anyNode.rootNode.text.length}
 */
public static LineAndColumn getLineAndColumn(INode anyNode, int documentOffset) {
	// special treatment for inconsistent nodes such as SyntheticLinkingLeafNode
	if (anyNode.getParent() == null && !(anyNode instanceof RootNode)) {
		return LineAndColumn.from(1,1);
	}
	return InternalNodeModelUtils.getLineAndColumn(anyNode, documentOffset);
}
 
Example #17
Source File: ScriptError.java    From openhab-core with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Creates new ScriptError.
 *
 * This constructor uses the given EObject instance to calculate the exact position.
 *
 * @param message Error Message
 * @param atEObject the EObject instance to use for calculating the position
 *
 */
public ScriptError(final String message, final EObject atEObject) {
    this.message = message;
    INode node = NodeModelUtils.getNode(atEObject);
    LineAndColumn lac = NodeModelUtils.getLineAndColumn(node, node.getOffset());
    this.line = lac.getLine();
    this.column = lac.getColumn();
    this.length = node.getEndOffset() - node.getOffset();
}
 
Example #18
Source File: ScriptError.java    From smarthome with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Creates new ScriptError.
 *
 * This constructor uses the given EObject instance to calculate the exact position.
 *
 * @param message Error Message
 * @param atEObject the EObject instance to use for calculating the position
 *
 */
public ScriptError(final String message, final EObject atEObject) {
    this.message = message;
    INode node = NodeModelUtils.getNode(atEObject);
    LineAndColumn lac = NodeModelUtils.getLineAndColumn(node, node.getOffset());
    this.line = lac.getLine();
    this.column = lac.getColumn();
    this.length = node.getEndOffset() - node.getOffset();
}