Java Code Examples for org.eclipse.xtext.nodemodel.util.NodeModelUtils#getLineAndColumn()

The following examples show how to use org.eclipse.xtext.nodemodel.util.NodeModelUtils#getLineAndColumn() . 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: 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 4
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 5
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 6
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 7
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 8
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 9
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 10
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();
}