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

The following examples show how to use org.eclipse.xtext.nodemodel.INode#getNextSibling() . 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: XtendImportsConfiguration.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public int getImportSectionOffset(XtextResource resource) {
	XtendFile xtendFile = getXtendFile(resource);
	if(xtendFile != null) {
		if(!isEmpty(xtendFile.getPackage())) {
			List<INode> nodes = NodeModelUtils.findNodesForFeature(xtendFile, XtendPackage.Literals.XTEND_FILE__PACKAGE);
			if(!nodes.isEmpty()) {
				INode lastNode = nodes.get(nodes.size()-1);
				INode nextSibling = lastNode.getNextSibling();
				while(nextSibling instanceof ILeafNode && ((ILeafNode)nextSibling).isHidden())
					nextSibling = nextSibling.getNextSibling();
				if(nextSibling != null && ";".equals(nextSibling.getText()))
					return nextSibling.getOffset() + 1;
				else
					return lastNode.getTotalEndOffset();
			}
		}
	}
	return 0;
}
 
Example 2
Source File: LazyURIEncoder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * ONLY public to be testable
 * 
 * @noreference This method is not intended to be referenced by clients.
 */
public void getRelativePath(StringBuilder result, INode parserNode, INode node) {
	if (parserNode == node)
		return;
	if (isAncestor(parserNode, node)) {
		ICompositeNode parent = node.getParent();
		getRelativePath(result, parserNode, parent);
		int idx = 0;
		INode child = parent.getFirstChild();
		while(child != node && child.hasNextSibling()) {
			idx++;
			child = child.getNextSibling();
		}
		result.append("/").append(idx);
	} else {
		result.append("/..");
		getRelativePath(result, parserNode.getParent(), node);
	}
}
 
Example 3
Source File: LazyURIEncoder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * ONLY public to be testable
 */
public INode getNode(final INode node, String path) {
	final List<String> split = Strings.split(path, '/');
	INode result = node;
	for (String string : split) {
		String trimmed = string.trim();
		if (trimmed.length() > 0) {
			if ("..".equals(trimmed)) {
				if (result.getParent() == null)
					throw new IllegalStateException("node has no parent");
				result = result.getParent();
			} else {
				int index = Integer.parseInt(string);
				if (index >= 0) {
					INode child = ((ICompositeNode) result).getFirstChild();
					while(index > 0) {
						child = child.getNextSibling();
						index--;
					}
					result = child;
				}
			}
		}
	}
	return result;
}
 
Example 4
Source File: SingleLineCommentDocumentationProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Finds trailing comment for a given context object. I.e. the comment after / on the same line as the context object.
 *
 * @param context
 *          the object
 * @return the documentation string
 */
protected String findTrailingComment(final EObject context) {
  StringBuilder returnValue = new StringBuilder();
  ICompositeNode node = NodeModelUtils.getNode(context);
  if (node != null) {
    final int contextEndLine = node.getEndLine();
    // process all leaf nodes first
    for (ILeafNode leave : node.getLeafNodes()) {
      addComment(returnValue, leave, contextEndLine);
    }
    // we also need to process siblings (leave nodes only) due to the fact that the last comment after
    // a given element is not a leaf node of that element anymore.
    INode sibling = node.getNextSibling();
    while (sibling instanceof ILeafNode) {
      addComment(returnValue, (ILeafNode) sibling, contextEndLine);
      sibling = sibling.getNextSibling();
    }
  }
  return returnValue.toString();
}
 
Example 5
Source File: SiblingIterable.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Iterator<INode> iterator() {
	return new Iterator<>() {
		private INode currNode = skipHidden(firstSibling);

		@Override
		public boolean hasNext() {
			return currNode != null;
		}

		@Override
		public INode next() {
			if (!hasNext()) {
				throw new NoSuchElementException();
			}
			final INode result = currNode;
			currNode = skipHidden(currNode.getNextSibling());
			return result;
		}

		private INode skipHidden(INode node) {
			if (skipHidden) {
				while (node instanceof HiddenLeafNode) {
					node = node.getNextSibling();
				}
			}
			return node;
		}
	};
}
 
Example 6
Source File: StyledTemplateProposal.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
static private boolean hasTrailingComma(ContentAssistContext context) {
	boolean trailingComma = false;

	// add trailing comma, if the name-value pair is inserted in the middle of a
	// list of existing pairs.
	final INode currentNode = context.getCurrentNode();
	if (currentNode.hasNextSibling()) {
		final INode nextSibling = currentNode.getNextSibling();
		if (nextSibling.getSemanticElement() instanceof NameValuePair) {
			trailingComma = true;
		}
	}
	return trailingComma;
}
 
Example 7
Source File: StaticMethodImporter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Attempts to find the next dot (.) or ::
 * 
 * @return the dot/:: node or null
 */
private INode findDot(final INode node) {
  INode sibling = node;
  while (sibling.hasNextSibling()) {
    {
      if ((Objects.equal(sibling.getGrammarElement(), this.grammar.getXMemberFeatureCallAccess().getFullStopKeyword_1_1_0_0_1_0()) || 
        Objects.equal(sibling.getGrammarElement(), 
          this.grammar.getXMemberFeatureCallAccess().getExplicitStaticColonColonKeyword_1_1_0_0_1_2_0()))) {
        return sibling;
      }
      sibling = sibling.getNextSibling();
    }
  }
  return null;
}
 
Example 8
Source File: NodeIterator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected INode findNextSibling(INode node) {
	ICompositeNode parent = node.getParent();
	if (parent == null) {
		return null;
	}
	INode successor = node.getNextSibling();
	if (successor != null) {
		return successor;
	}
	return findNextSibling(parent);
}
 
Example 9
Source File: SemanticNodeIterator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected INode next(INode node, boolean prune) {
	if (!prune && node instanceof ICompositeNode) {
		INode child = ((ICompositeNode) node).getFirstChild();
		if (child != null)
			return child;
	}
	INode n = node.getNextSibling();
	while (n == null) {
		node = node.getParent();
		if (node == null || isEObjectNode(node))
			return null;
		n = node.getNextSibling();
	}
	return n;
}
 
Example 10
Source File: EmitterNodeUtil.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private static INode findNextSibling(INode node, INode to) {
	INode successor = node.getNextSibling();
	if (successor != null) {
		if (successor == to) {
			return null;
		}
		return successor;
	}
	ICompositeNode parent = node.getParent();
	if (parent == null || parent == to) {
		return null;
	}
	return findNextSibling(parent, to);
}
 
Example 11
Source File: FastLazyURIEncoder.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public INode getNode(final INode node, final String path) {
  INode result = node;
  List<String> segments = Strings.split(path, '/');
  for (int i = 0; i < segments.size(); i++) {
    String seg = segments.get(i);
    if (seg.length() > 0) {
      if (PARENT_SEG.equals(seg)) {
        if (result.getParent() == null) {
          throw new IllegalStateException("node has no parent"); //$NON-NLS-1$
        }
        result = result.getParent();
      } else {
        int index = Integer.parseInt(seg);
        if (index >= 0) {
          INode child = ((ICompositeNode) result).getFirstChild();
          while (index > 0) {
            child = child.getNextSibling();
            index--;
          }
          result = child;
        }
      }
    }
  }
  return result;
}