Java Code Examples for org.eclipse.xtext.nodemodel.INode#getStartLine()

The following examples show how to use org.eclipse.xtext.nodemodel.INode#getStartLine() . 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: ReorderingHiddenTokenSequencer.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the list of nodes that belong to 'root' node and follow the 'from' node.
 *
 * @param from
 *          node model element to start from, must not be {@code null}
 * @param root
 *          top-level node model element, must not be {@code null}
 * @return list of hiddens nodes, possibly empty, but never {@code null}
 */
private List<INode> getRemainingHiddenNodesInContainer(final INode from, final INode root) {
  if (from == null || root == null) {
    return Collections.emptyList();
  }
  List<INode> out = Lists.newArrayList();
  NodeIterator nodes = new NodeIterator(from);
  while (nodes.hasNext()) {
    INode next = nodes.next();
    if (next.getStartLine() > root.getEndLine()) {
      return out;
    } else if (tokenUtil.isWhitespaceOrCommentNode(next)) {
      out.add(next);
    } else if (tokenUtil.isToken(next)) {
      return out;
    }
  }
  return out;
}
 
Example 2
Source File: XbaseFormatter2.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isEachExpressionInOwnLine(final Iterable<? extends XExpression> expressions) {
  int lastLine = (-1);
  for (final XExpression e : expressions) {
    {
      final INode node = this._nodeModelAccess.nodeForEObject(e);
      int _startLine = node.getStartLine();
      boolean _equals = (lastLine == _startLine);
      if (_equals) {
        return false;
      }
      lastLine = node.getEndLine();
    }
  }
  return true;
}
 
Example 3
Source File: HiddenTokenSequencer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isLeadingCommentFor(INode comment, INode node) {
	if (!tokenUtil.isCommentNode(comment)) 
		return false;
	if (comment.getText().endsWith("\n")) {
		return node.getStartLine() == comment.getEndLine();
	} else {
		return node.getStartLine() - comment.getEndLine() <= 1;
	}
}
 
Example 4
Source File: HiddenTokenSequencer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isTrailingCommentFor(INode comment, INode node) {
	if (!tokenUtil.isCommentNode(comment)) 
		return false;
	if (node.getText().endsWith("\n")) 
		return false;
	return comment.getStartLine() == node.getEndLine();
}
 
Example 5
Source File: AbstractDiagnostic.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public int getLine() {
	INode node = getNode();
	if (node != null)
		return node.getStartLine();
	return -1;
}
 
Example 6
Source File: ReorderingHiddenTokenSequencer.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks whether the given node is the trailing comment of another one.
 *
 * @param comment
 *          node to check, can be {@code null}
 * @param node
 *          node that is expected to own the comment, must not be {@code null}
 * @return true if 'comment' node represents trailing comment, false otherwise
 */
protected boolean isTrailingCommentFor(final INode comment, final INode node) {
  if (!tokenUtil.isCommentNode(comment)) {
    return false;
  }
  if (node.getText().endsWith(NEW_LINE)) {
    return false;
  }
  return comment.getStartLine() == node.getEndLine();
}
 
Example 7
Source File: EObjectDescriptionToNameWithPositionMapper.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns a string with name and position of the described object. The position is specified by line number (if
 * possible, otherwise the uri fragment of the proxy is used). If the object is a {@link SyntaxRelatedTElement}, a
 * "T" is used as a prefix of the line number.
 *
 * The following examples shows different mappings, depending on the described object:
 * <table>
 * <tr>
 * <th>Mapping</th>
 * <th>Described Object</th>
 * </tr>
 * <tr>
 * <td><code>bar - 42</code></td>
 * <td>Some element "bar", located in same resource on line 42</td>
 * </tr>
 * <tr>
 * <td><code>foo - T23</code></td>
 * <td>A type "foo" (or other syntax related element, a function is a type) which syntax related element (from which
 * the type is build) is located in same file on line 23</td>
 * </tr>
 * <tr>
 * <td><code>Infinity - global.n4ts:3</code></td>
 * <td>An element "Infinity", located in another resource "global.n4ts" on line 3.</td>
 * </tr>
 * <tr>
 * <td><code>decodeURI - global.n4ts:11</code></td>
 * <td>An element "decodeURI", located in another resource "global.n4ts" on line 11. Although the element may be a
 * type, there is no syntax related element because "n4ts" directly describes types.</td>
 * </tr>
 * </table>
 *
 * @param currentURI
 *            the current resource's URI, if described object is in same resource, resource name is omitted
 * @param desc
 *            the object descriptor
 */
public static String descriptionToNameWithPosition(URI currentURI, boolean withLineNumber,
		IEObjectDescription desc) {
	String name = desc.getName().toString();

	EObject eobj = desc.getEObjectOrProxy();

	if (eobj == null) {
		return "No EObject or proxy for " + name + " at URI " + desc.getEObjectURI();
	}

	String location = "";

	if (eobj instanceof SyntaxRelatedTElement) {
		EObject syntaxElement = ((SyntaxRelatedTElement) eobj).getAstElement();
		if (syntaxElement != null) {
			location += "T";
			eobj = syntaxElement;
		}
	}

	Resource eobjRes = eobj.eResource();
	URI uri = eobjRes == null ? null : eobjRes.getURI();
	if (uri != currentURI && uri != null) {
		location = uri.lastSegment();
		if (eobj.eIsProxy() || withLineNumber) {
			location += ":";
		}
	}
	if (eobj.eIsProxy()) {
		URI proxyUri = desc.getEObjectURI();
		location += "proxy:" + simpleURIString(proxyUri);
	} else if (withLineNumber) {
		INode node = NodeModelUtils.findActualNodeFor(eobj);
		if (node == null) {
			location += "no node:" + simpleURIString(desc.getEObjectURI());
		} else {
			location += node.getStartLine();
		}
	}

	return name + SEPARATOR + location;
}
 
Example 8
Source File: ReorderingHiddenTokenSequencer.java    From dsl-devkit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Checks whether the given node is the leading comment of another one.
 *
 * @param comment
 *          node to check, can be {@code null}
 * @param node
 *          node that is expected to own the comment, must not be {@code null}
 * @return true if 'comment' node represents the leading comment, false otherwise
 */
protected boolean isLeadingCommentFor(final INode comment, final INode node) {
  return tokenUtil.isCommentNode(comment) && (node.getStartLine() >= comment.getEndLine());
}