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

The following examples show how to use org.eclipse.xtext.nodemodel.INode#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: XtendProposalProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void completeMember_Exceptions(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	if (getXbaseCrossReferenceProposalCreator().isShowSmartProposals()) {
		INode lastCompleteNode = context.getLastCompleteNode();
		if (lastCompleteNode instanceof ILeafNode && !((ILeafNode) lastCompleteNode).isHidden()) {
			if (lastCompleteNode.getLength() > 0 && lastCompleteNode.getTotalEndOffset() == context.getOffset()) {
				String text = lastCompleteNode.getText();
				char lastChar = text.charAt(text.length() - 1);
				if (Character.isJavaIdentifierPart(lastChar)) {
					return;
				}
			}
		}
		getTypesProposalProvider().createSubTypeProposals(
				typeReferences.findDeclaredType(Throwable.class, model),
				this,
				context,
				TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE,
				createVisibilityFilter(context, IJavaSearchConstants.CLASS),
				getQualifiedNameValueConverter(),
				acceptor);
	} else {
		super.completeJvmParameterizedTypeReference_Type(model, assignment, context, acceptor);
	}
}
 
Example 2
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 3
Source File: EObjectAtOffsetHelper.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected EObject resolveCrossReferencedElement(INode node) {
	EObject referenceOwner = NodeModelUtils.findActualSemanticObjectFor(node);
	if (referenceOwner != null) {
		EReference crossReference = GrammarUtil.getReference((CrossReference) node.getGrammarElement(),
				referenceOwner.eClass());
		if (!crossReference.isMany()) {
			return (EObject) referenceOwner.eGet(crossReference);
		} else {
			List<?> listValue = (List<?>) referenceOwner.eGet(crossReference);
			List<INode> nodesForFeature = NodeModelUtils.findNodesForFeature(referenceOwner, crossReference);
			int currentIndex = 0;
			for (INode nodeForFeature : nodesForFeature) {
				if (currentIndex >= listValue.size())
					return null;
				if (nodeForFeature.getTotalOffset() <= node.getTotalOffset()
						&& nodeForFeature.getTotalEndOffset() >= node.getTotalEndOffset())
					return (EObject) listValue.get(currentIndex);
				currentIndex++;
			}
		}
	}
	return null;
}
 
Example 4
Source File: HiddenTokenSequencer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example 5
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 6
Source File: DefaultImportsConfiguration.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public int getImportSectionOffset(XtextResource resource) {
	if(resource.getParseResult() != null && resource.getParseResult().getRootNode() != null) {
		List<EObject> pathToImportSection = findPathToImportSection();
		if(pathToImportSection != null) {
			INode node = findPreviousNode(resource.getParseResult().getRootNode(), pathToImportSection);
			if(node != null)
				return node.getTotalEndOffset();
		}
	}
	return 0;
}
 
Example 7
Source File: XbaseIdeContentProposalProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isInMemberFeatureCall(final EObject model, final int endOffset, final ContentAssistContext context) {
  if (((model instanceof XMemberFeatureCall) && (endOffset >= context.getOffset()))) {
    final List<INode> featureNodes = NodeModelUtils.findNodesForFeature(model, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
    boolean _isEmpty = featureNodes.isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
      final INode featureNode = IterableExtensions.<INode>head(featureNodes);
      if (((featureNode.getTotalOffset() < context.getOffset()) && 
        (featureNode.getTotalEndOffset() >= context.getOffset()))) {
        return true;
      }
    }
  }
  return false;
}
 
Example 8
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void completeJavaTypes(ContentAssistContext context, EReference reference, boolean forced,
		IValueConverter<String> valueConverter, ITypesProposalProvider.Filter filter,
		ICompletionProposalAcceptor acceptor) {
	String prefix = context.getPrefix();
	if (prefix.length() > 0) {
		if (Character.isJavaIdentifierStart(context.getPrefix().charAt(0))) {
			if (!forced && getXbaseCrossReferenceProposalCreator().isShowSmartProposals()) {
				if (!prefix.contains(".") && !prefix.contains("::") && !Character.isUpperCase(prefix.charAt(0)))
					return;
			}
			typeProposalProvider.createTypeProposals(this, context, reference, filter, valueConverter, acceptor);
		}
	} else {
		if (forced || !getXbaseCrossReferenceProposalCreator().isShowSmartProposals()) {
			INode lastCompleteNode = context.getLastCompleteNode();
			if (lastCompleteNode instanceof ILeafNode && !((ILeafNode) lastCompleteNode).isHidden()) {
				if (lastCompleteNode.getLength() > 0 && lastCompleteNode.getTotalEndOffset() == context.getOffset()) {
					String text = lastCompleteNode.getText();
					char lastChar = text.charAt(text.length() - 1);
					if (Character.isJavaIdentifierPart(lastChar)) {
						return;
					}
				}
			}
			typeProposalProvider.createTypeProposals(this, context, reference, filter, valueConverter, acceptor);
		}
	}
}
 
Example 9
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isInMemberFeatureCall(EObject model, int endOffset, ContentAssistContext context) {
	if (model instanceof XMemberFeatureCall && endOffset >= context.getOffset()) {
		List<INode> featureNodes = NodeModelUtils.findNodesForFeature(model, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
		if (!featureNodes.isEmpty()) {
			INode featureNode = featureNodes.get(0);
			if (featureNode.getTotalOffset() < context.getOffset() && featureNode.getTotalEndOffset() >= context.getOffset()) {
				return true;
			}
		}
	}
	return false;
}
 
Example 10
Source File: XtendProposalProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeXConstructorCall_Members(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	if (model instanceof AnonymousClass) {
		overrideAssist.createOverrideProposals((AnonymousClass) model, context, acceptor, getConflictHelper());
		return;
	} else if (model instanceof XtendField) {
		/*
		 * class C {
		 *   val x = new Object() {
		 *     toS<|>
		 *   }
		 * }
		 *
		 * At this cursor position, we get a field without a name and the type 'toS' as the context.
		 * If there's a field decl preceding the cursor position, the field will have a name.
		 */
		XtendField field = (XtendField) model;
		if (field.eContainer() instanceof AnonymousClass) {
			overrideAssist.createOverrideProposals((AnonymousClass) field.eContainer(), context, acceptor, getConflictHelper());
			return;
		}
	} else if (model instanceof XtendExecutable && context.getPrefix().length() == 0 && model.eContainer() instanceof AnonymousClass) {
		overrideAssist.createOverrideProposals((AnonymousClass) model.eContainer(), context, acceptor, getConflictHelper());
		return;
	} else if (model instanceof XExpression) {
		XtendMember member = EcoreUtil2.getContainerOfType(model, XtendMember.class);
		INode memberNode = NodeModelUtils.findActualNodeFor(member);
		if (memberNode.getTotalEndOffset() <= context.getOffset()) {
			if (member.eContainer() instanceof AnonymousClass) {
				overrideAssist.createOverrideProposals((AnonymousClass) member.eContainer(), context, acceptor, getConflictHelper());
				return;
			}
		}
	}
	INode node = context.getCurrentNode();
	EObject eObject = NodeModelUtils.findActualSemanticObjectFor(node);
	if (eObject instanceof AnonymousClass)
		overrideAssist.createOverrideProposals((XtendTypeDeclaration) eObject, context, acceptor, getConflictHelper());
}
 
Example 11
Source File: AbstractNode.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public int getLength() {
	BidiIterator<AbstractNode> iter = basicIterator();
	while(iter.hasPrevious()) {
		INode prev = iter.previous();
		if (prev instanceof ILeafNode && !((ILeafNode) prev).isHidden()) {
			int offset = getOffset();
			return prev.getTotalEndOffset() - offset;
		}
	}
	return getTotalLength();
}
 
Example 12
Source File: AbstractNode.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.5
 */
@Override
public int getEndOffset() {
	BidiIterator<AbstractNode> iter = basicIterator();
	while(iter.hasPrevious()) {
		INode prev = iter.previous();
		if (prev instanceof ILeafNode && !((ILeafNode) prev).isHidden()) {
			return prev.getTotalEndOffset();
		}
	}
	return getTotalEndOffset();
}
 
Example 13
Source File: BaseContentAssistParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean appendTextToParse(ICompositeNode node, int offset, boolean skipOptional, StringBuilder result) {
	for (INode child : node.getChildren()) {
		if (child instanceof ILeafNode) {
			String text = child.getText();
			if (child.getTotalEndOffset() >= offset) {
				String sub = text.substring(0, offset - child.getTotalOffset());
				result.append(sub);
				return true;
			} else {
				result.append(text);
			}
		} else {
			if (!skipOptional) {
				if (appendTextToParse((ICompositeNode) child, offset, child.getTotalEndOffset() < offset, result)) {
					return true;
				}
			} else {
				String skippedAs = getReplacement((ICompositeNode) child);
				if (skippedAs != null) {
					result.append(skippedAs);
				} else {
					if (appendTextToParse((ICompositeNode) child, offset, true, result)) {
						return true;
					}
				}
			}
		}
	}
	return false;
}
 
Example 14
Source File: Range.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Range(INode node) {
	this(node.getTotalOffset(), node.getTotalEndOffset());
}
 
Example 15
Source File: SARLCodeMiningProvider.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Add an annotation when the action's return type is implicit and inferred by the SARL compiler.
 *
 * @param resource the resource to parse.
 * @param acceptor the code mining acceptor.
 */
@SuppressWarnings("checkstyle:npathcomplexity")
private void createImplicitActionReturnType(XtextResource resource, IAcceptor<? super ICodeMining> acceptor) {
	final List<XtendFunction> actions = EcoreUtil2.eAllOfType(resource.getContents().get(0), XtendFunction.class);

	for (final XtendFunction action : actions) {
		// inline annotation only for methods with no return type
		if (action.getReturnType() != null) {
			continue;
		}
		// get return type name from operation
		final JvmOperation inferredOperation = (JvmOperation) this.jvmModelAssocitions.getPrimaryJvmElement(action);
		if (inferredOperation == null || inferredOperation.getReturnType() == null) {
			continue;
		}
		// find document offset for inline annotationn
		final ICompositeNode node = NodeModelUtils.findActualNodeFor(action);
		final Keyword parenthesis = this.grammar.getAOPMemberAccess().getRightParenthesisKeyword_2_5_6_2();
		final Assignment fctname = this.grammar.getAOPMemberAccess().getNameAssignment_2_5_5();
		int offsetFctname = -1;
		int offsetParenthesis = -1;
		for (Iterator<INode> it = node.getAsTreeIterable().iterator(); it.hasNext();) {
			final INode child = it.next();
			if (child != node) {
				final EObject grammarElement = child.getGrammarElement();
				if (grammarElement instanceof RuleCall) {
					if (fctname.equals(grammarElement.eContainer())) {
						offsetFctname = child.getTotalEndOffset();
					}
				} else if (parenthesis.equals(grammarElement)) {
					offsetParenthesis = child.getTotalEndOffset();
					break;
				}
			}
		}
		int offset = -1;
		if (offsetParenthesis >= 0) {
			offset = offsetParenthesis;
		} else if (offsetFctname >= 0) {
			offset = offsetFctname;
		}
		if (offset >= 0) {
			final String returnType = inferredOperation.getReturnType().getSimpleName();
			final String text = " " + this.keywords.getColonKeyword() + " " + returnType; //$NON-NLS-1$ //$NON-NLS-2$
			acceptor.accept(createNewLineContentCodeMining(offset, text));
		}
	}
}