项目:xtext-extras
文件:HiddenLeafAccess.java
protected List<ILeafNode> findNextHiddenLeafs(final INode node) {
ArrayList<ILeafNode> _xblockexpression = null;
{
final ArrayList<ILeafNode> result = CollectionLiterals.<ILeafNode>newArrayList();
final NodeIterator ni = new NodeIterator(node);
while (ni.hasNext()) {
{
final INode next = ni.next();
if ((next instanceof ILeafNode)) {
boolean _isHidden = ((ILeafNode)next).isHidden();
if (_isHidden) {
result.add(((ILeafNode)next));
} else {
return result;
}
}
}
}
_xblockexpression = result;
}
return _xblockexpression;
}
项目:xtext-extras
文件:HiddenLeafAccess.java
protected ILeafNode findPrevIoUsLeaf(final INode node,final Function1<? super ILeafNode,? extends Boolean> matches) {
INode current = node;
while ((current instanceof ICompositeNode)) {
current = ((ICompositeNode)current).getLastChild();
}
if (((current instanceof ILeafNode) && (matches.apply(((ILeafNode) current))).booleanValue())) {
return ((ILeafNode) current);
}
if ((current != null)) {
final NodeIterator ni = new NodeIterator(current);
while (ni.hasPrevIoUs()) {
{
final INode prevIoUs = ni.prevIoUs();
if (((prevIoUs instanceof ILeafNode) && (matches.apply(((ILeafNode) prevIoUs))).booleanValue())) {
return ((ILeafNode) prevIoUs);
}
}
}
}
return null;
}
项目:xtext-extras
文件:NodeModelAccess.java
public ILeafNode findNextLeaf(final INode node,? extends Boolean> matches) {
Object _xifexpression = null;
if ((node != null)) {
if (((node instanceof ILeafNode) && (matches.apply(((ILeafNode) node))).booleanValue())) {
return ((ILeafNode) node);
}
final NodeIterator ni = new NodeIterator(node);
while (ni.hasNext()) {
{
final INode next = ni.next();
if (((next instanceof ILeafNode) && (matches.apply(((ILeafNode) next))).booleanValue())) {
return ((ILeafNode) next);
}
}
}
}
return ((ILeafNode)_xifexpression);
}
项目:xtext-core
文件:HiddenTokenSequencer.java
protected Set<INode> getLeadingCommentsIncludingWhitespace(ILeafNode node) {
Set<INode> associatednodes = Sets.newHashSet();
Set<INode> pendingWhitespace = Sets.newHashSet();
INode lastLink = node;
NodeIterator ni = new NodeIterator(lastLink);
while(ni.hasPrevIoUs()) {
INode prev = ni.prevIoUs();
if (tokenUtil.isCommentNode(prev)) {
if (isLeadingCommentFor(prev,lastLink)) {
lastLink = prev;
associatednodes.addAll(pendingWhitespace);
associatednodes.add(prev);
} else {
break;
}
} else if (tokenUtil.isWhitespaceNode(prev)) {
pendingWhitespace.add(prev);
} else if (prev instanceof ILeafNode){
break;
}
}
return associatednodes;
}
项目:xtext-core
文件:HiddenTokenSequencer.java
protected Set<INode> getTrailingCommentsIncludingWhitespace(ILeafNode node) {
Set<INode> associatednodes = Sets.newHashSet();
Set<INode> pendingWhitespace = Sets.newHashSet();
INode lastLink = node;
NodeIterator ni = new NodeIterator(lastLink);
while(ni.hasNext()) {
INode next = ni.next();
if (tokenUtil.isCommentNode(next)) {
if (isTrailingCommentFor(next,lastLink)) {
lastLink = next;
associatednodes.addAll(pendingWhitespace);
associatednodes.add(next);
} else {
break;
}
} else if (tokenUtil.isWhitespaceNode(next)) {
pendingWhitespace.add(next);
} else if (next instanceof ILeafNode){
break;
}
}
return associatednodes;
}
项目:xtext-core
文件:HiddenTokenSequencer.java
protected List<INode> getRemainingHiddenNodesInContainer(INode from,INode root) {
if (from == null || root == null)
return Collections.emptyList();
List<INode> out = Lists.newArrayList();
NodeIterator ni = new NodeIterator(from);
while (ni.hasNext()) {
INode next = ni.next();
if (next.getTotalOffset() > root.getTotalEndOffset())
return out;
else if (tokenUtil.isWhitespaceOrCommentNode(next)) {
out.add(next);
} else if (tokenUtil.isToken(next))
return Collections.emptyList();
}
return out;
}
public EmitterNodeIterator(INode fromNode,INode toNode,boolean passAbsorber,boolean allowHidden) {
if (fromNode != null) {
this.iterator = new NodeIterator(fromNode);
this.passAbsorber = passAbsorber;
if (toNode != null) {
if (toNode == fromNode)
this.end = toNode.getoffset() + toNode.getLength();
else if (toNode.getoffset() < fromNode.getoffset())
this.end = toNode.getoffset() + toNode.getLength();
else
this.end = toNode.getoffset();
} else
this.end = Integer.MAX_VALUE;
this.allowHidden = allowHidden;
next();
}
}
项目:dsl-devkit
文件:ReorderingHiddenTokenSequencer.java
/**
* 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;
}
项目:dsl-devkit
文件:ReorderingHiddenTokenSequencer.java
/**
* Helper method that collects all the preceding hidden tokens for the given node.
*
* @param to
* node model element to get the tokens for,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,prevIoUs);
nodes.prune();
} else {
break;
}
}
return result;
}
项目:dsl-devkit
文件:ReorderingHiddenTokenSequencer.java
/**
* Returns trailing comments of the given leaf node.
*
* @param node
* leaf element of the node model to start looking from,can be empty,never {@code null}
*/
private List<INode> getTrailingCommentsIncludingWhitespace(final ILeafNode node) {
List<INode> associatednodes = Lists.newLinkedList();
Set<INode> pendingWhitespace = Sets.newHashSet();
INode lastLink = node;
NodeIterator nodes = new NodeIterator(lastLink);
while (nodes.hasNext()) {
INode next = nodes.next();
if (tokenUtil.isCommentNode(next)) {
if (isTrailingCommentFor(next,lastLink)) {
lastLink = next;
associatednodes.addAll(pendingWhitespace);
pendingWhitespace.clear();
associatednodes.add(next);
} else {
break;
}
} else if (tokenUtil.isWhitespaceNode(next)) {
pendingWhitespace.add(next);
} else if (next instanceof ILeafNode) {
break;
}
}
return associatednodes;
}
项目:dsl-devkit
文件:ReorderingHiddenTokenSequencer.java
/**
* 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;
}
项目:xtext-extras
文件:HiddenLeafAccess.java
protected List<ILeafNode> findPrevIoUsHiddenLeafs(final INode node) {
List<ILeafNode> _xblockexpression = null;
{
INode current = node;
while ((current instanceof ICompositeNode)) {
current = ((ICompositeNode)current).getLastChild();
}
final ArrayList<ILeafNode> result = CollectionLiterals.<ILeafNode>newArrayList();
if ((current != null)) {
final NodeIterator ni = new NodeIterator(current);
while (ni.hasPrevIoUs()) {
{
final INode prevIoUs = ni.prevIoUs();
if (((!Objects.equal(prevIoUs,current)) && (prevIoUs instanceof ILeafNode))) {
boolean _isHidden = ((ILeafNode) prevIoUs).isHidden();
if (_isHidden) {
result.add(((ILeafNode) prevIoUs));
} else {
return ListExtensions.<ILeafNode>reverse(result);
}
}
}
}
}
_xblockexpression = ListExtensions.<ILeafNode>reverse(result);
}
return _xblockexpression;
}
@Test public void testPruneComposite() throws Exception {
NodeIterator nodeIterator = new NodeIterator(nodes[3]);
nodeIterator.prune();
assertEquals(nodes[6],nodeIterator.next());
assertEquals(nodes[3],nodeIterator.prevIoUs());
assertEquals(nodes[2],nodeIterator.prevIoUs());
assertEquals(nodes[1],nodeIterator.prevIoUs());
}
@Test public void testPruneLeaf() throws Exception {
// pruning a leaf should not have any effect
NodeIterator nodeIterator = new NodeIterator(nodes[8]);
nodeIterator.prune();
assertEquals(nodes[9],nodeIterator.next());
assertEquals(nodes[8],nodeIterator.prevIoUs());
assertEquals(nodes[7],nodeIterator.prevIoUs());
assertEquals(nodes[6],nodeIterator.prevIoUs());
}
项目:dsl-devkit
文件:ReorderingHiddenTokenSequencer.java
/**
* Collects & emits the set of comments/whitespaces that logically belong to a container of some objects and <b>not</b> to the first child of the container.
*
* @param container
* node model element that represents a container,must not be {@code null}
*/
private void emitContainerComments(final INode container) {
List<INode> containerComments = Lists.newArrayList();
Set<INode> firstElementComments = getHiddenNodesBelongingTo(NodeModelUtils.findActualSemanticObjectFor(container));
NodeIterator nodes = new NodeIterator(container);
while (nodes.hasNext()) {
INode next = nodes.next();
if (firstElementComments.contains(next) || tokenUtil.isToken(next)) {
break;
} else if (tokenUtil.isWhitespaceOrCommentNode(next)) {
containerComments.add(next);
}
}
emitHiddenTokens(containerComments);
}
项目:dsl-devkit
文件:ReorderingHiddenTokenSequencer.java
/**
* Returns the leading comments of the given leaf node.
*
* @param node
* leaf element of the node model to look up to,never {@code null}
*/
private List<INode> getLeadingCommentsIncludingWhitespace(final ILeafNode node) {
List<INode> associatednodes = Lists.newLinkedList();
Set<INode> pendingWhitespace = Sets.newHashSet();
INode lastLink = node;
NodeIterator nodes = new NodeIterator(lastLink);
while (nodes.hasPrevIoUs()) {
INode prev = nodes.prevIoUs();
if (tokenUtil.isCommentNode(prev)) {
if (isLeadingCommentFor(prev,lastLink)) {
lastLink = prev;
associatednodes.addAll(0,pendingWhitespace);
pendingWhitespace.clear();
associatednodes.add(0,prev);
} else {
break;
}
} else if (tokenUtil.isWhitespaceNode(prev)) {
pendingWhitespace.add(prev);
} else if (prev instanceof ILeafNode) {
break;
}
}
associatednodes.addAll(0,pendingWhitespace);
// We want trailing comments to have precedence over leading comments
// Therefore all the trailing comments of the prevIoUs node must be computed
// and deleted from the results,as they have been already output in prevIoUs call of 'emitTrailingTokens' method
EObject nodeObject = NodeModelUtils.findActualSemanticObjectFor(node);
ICompositeNode actualNode = NodeModelUtils.findActualNodeFor(nodeObject);
INode prevIoUsNode = actualNode.getPrevIoUsSibling();
if (prevIoUsNode instanceof ICompositeNode) {
associatednodes.removeAll(getTrailingCommentsIncludingWhitespace(prevIoUsNode));
}
// Following line is need to insert a new line after the leading comments if it is needed,but wasn't originally present
// See also usage in 'emitHiddenTokens'
associatednodes.add(0,new LeadingCommentsMarkerNode());
return associatednodes;
}
项目:dsl-devkit
文件:ReorderingHiddenTokenSequencer.java
/**
* 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,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,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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。