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

The following examples show how to use org.eclipse.xtext.nodemodel.INode#equals() . 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: IndentationAwareCompletionPrefixProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private PeekingIterator<ILeafNode> createReversedLeafIterator(INode root, INode candidate, LinkedList<ILeafNode> sameGrammarElement) {
	EObject grammarElement = null;
	PeekingIterator<ILeafNode> iterator = Iterators.peekingIterator(Iterators.filter(root.getAsTreeIterable().reverse().iterator(), ILeafNode.class));
	// traverse until we find the current candidate
	while(iterator.hasNext()) {
		ILeafNode next = iterator.next();
		if (candidate.equals(next)) {
			break;
		} else if (next.getTotalLength() == 0) {
			EObject otherGrammarElement = tryGetGrammarElementAsRule(next);
			if (grammarElement == null) {
				grammarElement = otherGrammarElement;
			}
			if (otherGrammarElement.equals(grammarElement)) {
				sameGrammarElement.add(next);
			} else {
				sameGrammarElement.removeLast();
			}
		}
	}
	return iterator;
}
 
Example 2
Source File: ReorderingHiddenTokenSequencer.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Helper method that collects all the hidden tokens that follow the given node.
 *
 * @param from
 *          starting point in the node model, must not be {@code null}
 * @param deletedSemanticElements
 *          set of semantic elements that have been deleted from the model, must not be {@code null}
 * @return list of hidden tokens, never {@code null}, can be empty
 */
private List<INode> getFollowingHiddenTokens(final INode from, final Set<EObject> deletedSemanticElements) {
  List<INode> result = Lists.newArrayList();
  NodeIterator nodes = new NodeIterator(from);
  while (nodes.hasNext()) {
    INode next = nodes.next();
    if (next.getTotalOffset() > rootEndOffset || next.equals(lastEmittedNode)) {
      break;
    } else if (tokenUtil.isWhitespaceOrCommentNode(next)) {
      if (!emittedComments.contains(next)) {
        result.add(next);
      }
    } else if (belongsToDeletedElement(next)) {
      handleDeletedElement(result, deletedSemanticElements, next);
      nodes.prune();
    } else {
      break;
    }
  }
  return result;
}
 
Example 3
Source File: ReorderingHiddenTokenSequencer.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Helper method that collects all the preceding hidden tokens for the given node.
 *
 * @param to
 *          node model element to get the tokens for, must not be {@code null}
 * @param deletedSemanticElements
 *          set of semantic elements that have been deleted from the model, must not be {@code null}
 * @return list of hidden tokens, never {@code null}, can be empty
 */
private List<INode> getPrecedingHiddenTokens(final INode to, final Set<EObject> deletedSemanticElements) {
  List<INode> result = Lists.newLinkedList();
  NodeIterator nodes = new NodeIterator(to);
  while (nodes.hasPrevious()) {
    INode previous = nodes.previous();
    if (previous.getTotalEndOffset() < rootOffset || previous.equals(lastEmittedNode)) {
      break;
    } else if (tokenUtil.isWhitespaceOrCommentNode(previous)) {
      if (!emittedComments.contains(previous)) {
        result.add(0, previous);
      }
    } else if (belongsToDeletedElement(previous)) {
      handleDeletedElement(result, deletedSemanticElements, previous);
      nodes.prune();
    } else {
      break;
    }
  }
  return result;
}
 
Example 4
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isParentOf(INode node, INode child) {
	if (node == null)
		return false;
	while(child != null && node.equals(child)) {
		child = child.getParent();
	}
	return node.equals(child);
}
 
Example 5
Source File: AbstractParseTreeConstructor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void writeWhitespacesSince(INode node) throws IOException {
	if (node == null) {
		nodeIterator = null;
		return;
	}
	if (nodeIterator == null || !nodeIterator.hasNext()) {
		nodeIterator = new HiddenAndTokenNodeIterator(node, tokenUtil);
		return;
	}
	List<ILeafNode> whitespaces = Lists.newArrayList();
	while (nodeIterator.hasNext()) {
		INode nextNode = nodeIterator.next();
		if (tokenUtil.isWhitespaceNode(nextNode)) {
			whitespaces.add((ILeafNode) nextNode);
		} else if (node.equals(nextNode)) {
			if (whitespaces.isEmpty()) {
				// signal to the formatter not to insert a whitespace
				out.writeHidden(hiddenTokenHelper.getWhitespaceRuleFor(null, ""), "");
				//						System.out.println("WS: -nothing-");
			}
			for (ILeafNode whitespace : whitespaces) {
				//						System.out.println("WS: '" + whitespace.getText() + "'");
				out.writeHidden(whitespace.getGrammarElement(), whitespace.getText());
			}
			return;
		} else {
			// unmatched semantic token node
			nodeIterator = new HiddenAndTokenNodeIterator(node, tokenUtil);
			return;
		}
	}
}
 
Example 6
Source File: DefaultEcoreElementFactoryTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testNullInput() throws Exception {
	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	final ILeafNode node = new LeafNode();
	
	Function2<String, INode, Object> toValueImpl = new Function2<String, INode, Object>() {

		@Override
		public Object apply(String lexerRule, INode nodeParam) {
			if ("foo".equals(lexerRule) && nodeParam.equals(node)) {
				return null;
			}
			fail();
			return null;
		}
	};

	DefaultEcoreElementFactory factory = new DefaultEcoreElementFactory();
	factory.setConverterService(new MockedConverterService(toValueImpl));
	try {
		factory.set(eClass, "abstract", null, "foo", node);
		fail("Expected ValueConverterException");
	} catch (ValueConverterException ex) {
		assertNull(ex.getCause());
		assertTrue(ex.getMessage().indexOf("ValueConverter returned null for") >= 0);
		assertSame(node, ex.getNode());
	}
}
 
Example 7
Source File: SARLDiagnosticLabelDecorator.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static boolean inside(INode node, ICompositeNode parentCandidate) {
	INode current = node;
	while (current != null) {
		if (current.equals(parentCandidate)) {
			return true;
		}
		current = current.getParent();
	}
	return false;
}
 
Example 8
Source File: ReorderingHiddenTokenSequencer.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Collects all the hidden tokens between two given nodes of the node model.
 *
 * @param from
 *          node that serves as a start point, must not be {@code null}
 * @param to
 *          search end point, must not be {@code null}
 * @param deletedSemanticElements
 *          set of the deleted semantic elements, must not be {@code null}
 * @return list of hidden tokens, never {@code null}, but can be empty
 */
private List<INode> getHiddenTokensBetween(final INode from, final INode to, final Set<EObject> deletedSemanticElements) {
  EObject fromElement = NodeModelUtils.findActualSemanticObjectFor(from);
  if (from.equals(NodeModelUtils.getNode(fromElement))) {
    // If the starting node represents some container, emit the comments that belong to it
    // This is needed to correctly handle some edge cases like ImportList in AvqScript
    // Logic for distinguishing between container's comments and the ones of first element is expected to be implemented in 'isLeadingCommentFor'
    emitContainerComments(from);
  }
  List<INode> result = Lists.newArrayList();
  boolean handleReordering = from.getTotalOffset() > to.getTotalOffset();
  if (!handleReordering) {
    // Elements are not reordered
    // Just going through the interval and collecting comments, unless they have already been emitted
    NodeIterator nodes = new NodeIterator(from);
    while (nodes.hasNext()) {
      INode next = nodes.next();
      if (tokenUtil.isWhitespaceOrCommentNode(next)) {
        if (!emittedComments.contains(next)) {
          result.add(next);
        }
      } else if (next.equals(to)) {
        // We have hit the 'to' node
        // If it is a composite one, we have to iterate through its children
        // and collect whitespaces/comments until we encounter first token (keyword, identifier...)
        if (next instanceof ICompositeNode && (GrammarUtil.isDatatypeRuleCall(next.getGrammarElement())
            || GrammarUtil.isEnumRuleCall(next.getGrammarElement()) || next.getGrammarElement() instanceof CrossReference)) {
          while (nodes.hasNext()) {
            INode lastNodeChild = nodes.next();
            if (tokenUtil.isWhitespaceOrCommentNode(lastNodeChild)) {
              if (!emittedComments.contains(lastNodeChild)) {
                result.add(lastNodeChild);
              }
            } else if (lastNodeChild instanceof ILeafNode) {
              break;
            }
          }
          break;
        } else {
          // 'to' node is not a composite one, nothing to do here, just exit the loop
          break;
        }
      } else if (belongsToDeletedElement(next)) {
        handleDeletedElement(result, deletedSemanticElements, next);
        nodes.prune();
      } else if (tokenUtil.isToken(next)) {
        // We have encountered some token, but not the one we expected
        // Will be handled by invoking 'getLeadingCommentsIncludingWhitespace' method later
        handleReordering = true;
        break;
      }
    }
  }
  if (handleReordering) {
    return getLeadingCommentsIncludingWhitespace(to);
  }
  return result;
}