org.eclipse.xtext.nodemodel.util
Class NodeModelUtils

java.lang.Object
  extended by org.eclipse.xtext.nodemodel.util.NodeModelUtils

public class NodeModelUtils
extends java.lang.Object

The NodeModelUtils are a collection of useful methods when dealing with the node model directly. They encapsulate the default construction semantics of the node model as it is created by the parser. This API is quite low level and internal functionality of the framework relies on the implemened contracts. Clients should rather use the language specific APIs that provide almost the same functionality, e.g. ILocationInFileProvider and EObjectAtOffsetHelper if they want to to access the region of a semantic object.

Author:
Sebastian Zarnekow - Initial contribution and API

Constructor Summary
NodeModelUtils()
           
 
Method Summary
static java.lang.String compactDump(INode node, boolean showHidden)
          Creates a string representation of the given node.
static ICompositeNode findActualNodeFor(org.eclipse.emf.ecore.EObject semanticObject)
          Returns the node that covers all assigned values of the given object.
static org.eclipse.emf.ecore.EObject findActualSemanticObjectFor(INode node)
          Returns the semantic object that is really associated with the actual container node of the given node.
static ILeafNode findLeafNodeAtOffset(INode node, int leafNodeOffset)
          Find the leaf node at the given offset.
static java.util.List<INode> findNodesForFeature(org.eclipse.emf.ecore.EObject semanticObject, org.eclipse.emf.ecore.EStructuralFeature structuralFeature)
          Returns the list of nodes that were used to assign values to the given feature for the given object.
static ICompositeNode getNode(org.eclipse.emf.ecore.EObject object)
          Returns the node that is directly associated with the given object by means of an EMF-Adapter.
static java.lang.String getTokenText(INode node)
          This method converts a node to text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NodeModelUtils

public NodeModelUtils()
Method Detail

findLeafNodeAtOffset

@Nullable
public static ILeafNode findLeafNodeAtOffset(@NonNull
                                                      INode node,
                                                      int leafNodeOffset)
Find the leaf node at the given offset. May return null if the given offset is not valid for the node (sub-)tree. A node matches the leafNodeOffset if it fulfills the following condition:
  node.totalOffset <= leafNodeOffset &&
  node.totalEndOffset > leafNodeOffset 
 

Parameters:
node - the container node. May not be null.
leafNodeOffset - the offset that is covered by the searched node.
Returns:
the leaf node at the given offset or null.

getNode

@Nullable
public static ICompositeNode getNode(@Nullable
                                              org.eclipse.emf.ecore.EObject object)
Returns the node that is directly associated with the given object by means of an EMF-Adapter.

Parameters:
object - the semantic object whose direct node should be provided.
Returns:
the node that is directly associated with the given object.
See Also:
findActualNodeFor(EObject)

findNodesForFeature

@NonNull
public static java.util.List<INode> findNodesForFeature(org.eclipse.emf.ecore.EObject semanticObject,
                                                                org.eclipse.emf.ecore.EStructuralFeature structuralFeature)
Returns the list of nodes that were used to assign values to the given feature for the given object.

Returns:
the list of nodes that were used to assign values to the given feature for the given object.

findActualNodeFor

@Nullable
public static ICompositeNode findActualNodeFor(@Nullable
                                                        org.eclipse.emf.ecore.EObject semanticObject)

Returns the node that covers all assigned values of the given object. It handles the semantics of actions and unassigned rule calls. The returned node will include unassigned surrounding leafs, e.g. if you use something like Parenthesized expressions redundant parentheses will be part of the returned node.

Consider the following simple expression (a number literal):

   ((1))
 
Assuming it was parsed from a grammar like this:
 Expression: Number | Parentheses;
 Parentheses: '(' Expression ')';
 Number: value=INT
 
The actual node for the only semantic object that was produced from the input ((1)) is the root node even though the minimal node would be the one with the text 1.

Parameters:
semanticObject - the semantic object whose node should be provided.
Returns:
the node that covers all assigned values of the given object.

findActualSemanticObjectFor

@Nullable
public static org.eclipse.emf.ecore.EObject findActualSemanticObjectFor(@Nullable
                                                                                 INode node)
Returns the semantic object that is really associated with the actual container node of the given node. It handles the structural semantics that results from Actions and unassigned rule calls.

Returns:
the semantic object that is really associated with the actual container node of the given node.

compactDump

public static java.lang.String compactDump(INode node,
                                           boolean showHidden)
Creates a string representation of the given node. Useful for debugging.

Returns:
a debug string for the given node.

getTokenText

public static java.lang.String getTokenText(INode node)
This method converts a node to text. Leading and trailing text from hidden tokens (whitespace/comments) is removed. Text from hidden tokens that is surrounded by text from non-hidden tokens is summarized to a single whitespace. The preferred use case of this method is to convert the ICompositeNode that has been created for a data type rule to text. This is also the recommended way to convert a node to text if you want to invoke IValueConverterService.toValue(String, String, INode)