Java Code Examples for org.eclipse.xtext.nodemodel.ICompositeNode#getTotalEndOffset()

The following examples show how to use org.eclipse.xtext.nodemodel.ICompositeNode#getTotalEndOffset() . 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: ImportsCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public EObject findActualSemanticObjectFor(final ICompositeNode rootNode, final ITextRegion textRegion) {
  ILeafNode leafNodeAtOffset = NodeModelUtils.findLeafNodeAtOffset(rootNode, textRegion.getOffset());
  EObject semanticElementOffset = leafNodeAtOffset.getSemanticElement();
  ICompositeNode actualOffsetNode = NodeModelUtils.findActualNodeFor(semanticElementOffset);
  int _offset = textRegion.getOffset();
  int _length = textRegion.getLength();
  final int endOffset = (_offset + _length);
  while (((actualOffsetNode.getParent() != null) && (actualOffsetNode.getTotalEndOffset() < endOffset))) {
    actualOffsetNode = actualOffsetNode.getParent();
  }
  final EObject actualSemanticObj = actualOffsetNode.getSemanticElement();
  return actualSemanticObj;
}
 
Example 2
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isInvalidRootNode(ICompositeNode rootNode, ICompositeNode candidate) {
	int endOffset = candidate.getTotalEndOffset();
	if (candidate instanceof SyntheticCompositeNode)
		return true;
	if (candidate.getGrammarElement() instanceof RuleCall) {
		AbstractRule rule = ((RuleCall) candidate.getGrammarElement()).getRule();
		if (!(rule instanceof ParserRule))
			return true;
		ParserRule casted = (ParserRule) rule;
		if (GrammarUtil.isDatatypeRule(casted) || casted.isFragment() || !casted.getParameters().isEmpty()) {
			return true;
		}
		if (isInvalidDueToPredicates((AbstractElement) candidate.getGrammarElement()))
			return true;
	}
	if (candidate.getGrammarElement() instanceof Action) {
		return true;
	}
	if (endOffset == rootNode.getTotalEndOffset()) {
		INode lastChild = getLastChild(candidate);
		if (lastChild instanceof ICompositeNode) {
			INode lastLeaf = getLastLeaf(candidate);
			if (isInvalidLastChildNode(candidate, lastLeaf)) {
				return true;
			}
		}
		if (isInvalidLastChildNode(candidate, lastChild)) {
			return true;
		}
	}
	return false;
}
 
Example 3
Source File: FixedPartialParsingHelper.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
protected boolean isInvalidRootNode(final ICompositeNode rootNode, final ICompositeNode candidate) {
  int endOffset = candidate.getTotalEndOffset();
  if (candidate instanceof SyntheticCompositeNode) {
    return true;
  }
  if (candidate.getGrammarElement() instanceof RuleCall) {
    AbstractRule rule = ((RuleCall) candidate.getGrammarElement()).getRule();
    if (!(rule instanceof ParserRule) || GrammarUtil.isDatatypeRule((ParserRule) rule)) {
      return true;
    } else if (isInvalidDueToPredicates((AbstractElement) candidate.getGrammarElement())) {
      return true;
    }
  }
  if (candidate.getGrammarElement() instanceof Action) {
    return true;
  }
  if (endOffset == rootNode.getTotalEndOffset()) {
    INode lastChild = getLastChild(candidate);
    if (lastChild instanceof ICompositeNode) {
      INode lastLeaf = getLastLeaf(candidate);
      if (isInvalidLastChildNode(candidate, lastLeaf)) {
        return true;
      }
    }
    if (isInvalidLastChildNode(candidate, lastChild)) {
      return true;
    }
  }
  return false;
}
 
Example 4
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Filter the nodes from the iterator that do not have any impact on the parse result.
 *
 * For now we filter mostly regions that do have lookahead 1 and are closed before the requested region starts.
 */
private Iterator<INode> filterIterator(TreeIterator<AbstractNode> iterator) {
	return new AbstractIterator<>() {
		@Override
		protected INode computeNext() {
			if (iterator.hasNext()) {
				INode result = iterator.next();
				if (result instanceof ICompositeNode) {
					ICompositeNode casted = (ICompositeNode) result;
					if (casted.getTotalEndOffset() < endOffset - 1) {
						if (casted.hasChildren() && casted.getLookAhead() == 1) {
							AbstractElement grammarElement = (AbstractElement) casted.getGrammarElement();
							// Filter script elements and member declarations to the left of the cursor position.
							if (grammarElement == scriptElementCall || grammarElement == memberDeclarationCall) {
								INode sibling = casted.getNextSibling();
								while (sibling instanceof ILeafNode) {
									ILeafNode siblingLeaf = (ILeafNode) sibling;
									if (siblingLeaf.isHidden()) {
										if (siblingLeaf.getTotalEndOffset() >= endOffset) {
											return result;
										}
									} else {
										break;
									}
									sibling = siblingLeaf.getNextSibling();
								}
								iterator.prune();

								// filter statements that are completed before the cursor position and are not
								// part of the lookahead
							} else if (grammarElement == statementsCall) {
								// check if this is in the parents lookAhead to disambiguate block from object
								// literal
								ICompositeNode parent = casted.getParent();
								if (parent.getLookAhead() > 1) {
									ILeafNode firstLeaf = Iterables.get(casted.getLeafNodes(), 0);
									int remainingLA = parent.getLookAhead();
									Iterator<ILeafNode> parentLeafs = parent.getLeafNodes().iterator();
									while (parentLeafs.hasNext() && remainingLA > 0) {
										ILeafNode leafNode = parentLeafs.next();
										if (leafNode == firstLeaf) {
											break;
										}
										if (!leafNode.isHidden()) {
											remainingLA--;
											if (remainingLA == 0) {
												iterator.prune();
											}
										}
									}
								}

								// Reduce the size of object literals.
							} else if (grammarElement == propertyAssignmentCall1
									|| grammarElement == propertyAssignmentCall2) {
								iterator.prune();
								Iterator<ILeafNode> localLeafs = casted.getLeafNodes().iterator();
								while (localLeafs.hasNext()) {
									ILeafNode leaf = localLeafs.next();
									if (!leaf.isHidden()) {
										return leaf;
									}
								}
							}
						}
					}
				}
				return result;
			}
			return endOfData();
		}
	};

}
 
Example 5
Source File: AstSelectionProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected int getSelectionOffset(ICompositeNode rootNode, ITextRegion region) {
	boolean selectionAtEndOfText = rootNode.getTotalEndOffset() == getEndOffset(region);
	return selectionAtEndOfText && region.getOffset() > 0 ? region.getOffset() - 1 : region.getOffset();
}
 
Example 6
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean nodeEnclosesRegion(ICompositeNode node, Range range) {
	boolean result = node.getTotalOffset() <= range.getOffset() && node.getTotalEndOffset() >= range.getEndOffset();
	return result;
}
 
Example 7
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Investigates the composite nodes containing the changed region and collects a list of nodes which could possibly
 * replaced by a partial parse. Such a node has a parent that consumes all his current lookahead tokens and all of
 * these tokens are located before the changed region.
 */
private List<ICompositeNode> internalFindValidReplaceRootNodeForChangeRegion(
		List<ICompositeNode> nodesEnclosingRegion, Range range) {
	List<ICompositeNode> result = new ArrayList<ICompositeNode>();
	boolean mustSkipNext = false;
	ICompositeNode previous = null;
	/*
	 * set to 'true' as soon as the lookahead of an enclosing
	 * exceeds the given range
	 */
	boolean done = false;  
	for (int i = 0; i < nodesEnclosingRegion.size() && !done; i++) {
		ICompositeNode node = nodesEnclosingRegion.get(i);
		if (node.getGrammarElement() != null) {
			if (!mustSkipNext) {
				boolean process = true;
				if (previous != null && !node.hasNextSibling()) {
					if (previous.getLookAhead() == node.getLookAhead() && previous.getLookAhead() == 0) {
						process = false;
					}
				}
				EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(node);
				if (semanticElement != null) {
					ICompositeNode actualNode = NodeModelUtils.findActualNodeFor(semanticElement);
					if (actualNode != null && (actualNode.getTotalOffset() < node.getTotalOffset() || actualNode.getTotalEndOffset() > node.getTotalEndOffset()))
						process = false;
				}
				if (process) {
					int remainingLookAhead = node.getLookAhead();
					if (remainingLookAhead != 0) {
						Iterator<ILeafNode> iterator = node.getLeafNodes().iterator();
						while(iterator.hasNext() && remainingLookAhead > 0) {
							ILeafNode leaf = iterator.next();
							if (!leaf.isHidden()) {
								if (remainingLookAhead > 0)
									remainingLookAhead--;
								if (remainingLookAhead == 0) {
									if (leaf.getTotalEndOffset() <= range.getOffset()) {
										result.add(node);
										previous = node;
										if (isActionNode(node)) {
											mustSkipNext = true;
										}
										break;
									} else {
										// lookahead ends left of the range, don't dive into child nodes
										done = true;
									}
								}
							}
						}
						if (remainingLookAhead != 0) {
							done = true;
						}
					} else {
						result.add(node);
						previous = node;
						if (isActionNode(node)) {
							mustSkipNext = true;
						}
					}
				}
			} else { // !mustSkipNext
				mustSkipNext = isActionNode(node);
			}
		}
	}
	return result;
}
 
Example 8
Source File: FixedPartialParsingHelper.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
protected boolean nodeEnclosesRegion(final ICompositeNode node, final Range range) {
  boolean result = node.getTotalOffset() <= range.getOffset() && node.getTotalEndOffset() >= range.getEndOffset();
  return result;
}
 
Example 9
Source File: FixedPartialParsingHelper.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Investigates the composite nodes containing the changed region and collects a list of nodes which could possibly
 * replaced by a partial parse. Such a node has a parent that consumes all his current lookahead tokens and all of
 * these tokens are located before the changed region.
 */
private List<ICompositeNode> internalFindValidReplaceRootNodeForChangeRegion(final List<ICompositeNode> nodesEnclosingRegion, final Range range) {
  List<ICompositeNode> result = new ArrayList<ICompositeNode>();
  boolean mustSkipNext = false;
  ICompositeNode previous = null;
  /*
   * set to 'true' as soon as the lookahead of an enclosing
   * exceeds the given range
   */
  boolean done = false;
  for (int i = 0; i < nodesEnclosingRegion.size() && !done; i++) {
    ICompositeNode node = nodesEnclosingRegion.get(i);
    if (node.getGrammarElement() != null) {
      if (!mustSkipNext) {
        boolean process = true;
        if (previous != null && !node.hasNextSibling()) {
          if (previous.getLookAhead() == node.getLookAhead() && previous.getLookAhead() == 0) {
            process = false;
          }
        }
        EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(node);
        if (semanticElement != null) {
          ICompositeNode actualNode = NodeModelUtils.findActualNodeFor(semanticElement);
          if (actualNode != null && (actualNode.getTotalOffset() < node.getTotalOffset() || actualNode.getTotalEndOffset() > node.getTotalEndOffset())) {
            mustSkipNext = isActionNode(node);
            process = false;
          }
        }
        if (process) {
          int remainingLookAhead = node.getLookAhead();
          if (remainingLookAhead != 0) {
            Iterator<ILeafNode> iterator = node.getLeafNodes().iterator();
            while (iterator.hasNext() && remainingLookAhead > 0) {
              ILeafNode leaf = iterator.next();
              if (!leaf.isHidden()) {
                if (remainingLookAhead > 0) {
                  remainingLookAhead--;
                }
                if (remainingLookAhead == 0) {
                  if (leaf.getTotalEndOffset() <= range.getOffset()) {
                    result.add(node);
                    previous = node;
                    if (isActionNode(node)) {
                      mustSkipNext = true;
                    }
                    break;
                  } else {
                    // lookahead ends left of the range, don't dive into child nodes
                    done = true;
                  }
                }
              }
            }
            if (remainingLookAhead != 0) {
              done = true;
            }
          } else {
            result.add(node);
            previous = node;
            if (isActionNode(node)) {
              mustSkipNext = true;
            }
          }
        }
      } else { // !mustSkipNext
        mustSkipNext = isActionNode(node);
      }
    }
  }
  return result;
}