Java Code Examples for org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext#getOffset()

The following examples show how to use org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext#getOffset() . 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: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void completeXBasicForLoopExpression_InitExpressions(EObject model, Assignment assignment,
		ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
	ICompositeNode node = NodeModelUtils.getNode(model);
	if (node.getOffset() >= context.getOffset()) {
		createLocalVariableAndImplicitProposals(model, IExpressionScope.Anchor.BEFORE, context, acceptor);
		return;
	}
	if (model instanceof XBasicForLoopExpression) {
		List<XExpression> children = ((XBasicForLoopExpression) model).getInitExpressions();
		if (!children.isEmpty()) {
			for(int i = children.size() - 1; i >= 0; i--) {
				XExpression child = children.get(i);
				ICompositeNode childNode = NodeModelUtils.getNode(child);
				if (childNode.getEndOffset() <= context.getOffset()) {
					createLocalVariableAndImplicitProposals(child, IExpressionScope.Anchor.AFTER, context, acceptor);
					return;
				}
			}
		}
	}
	createLocalVariableAndImplicitProposals(model, IExpressionScope.Anchor.BEFORE, context, acceptor);
}
 
Example 2
Source File: XtendJavaDocCompletionProposalComputer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isValidPositionForTypeProposal(ContentAssistContext contentAssistContext){
	INode currentNode = contentAssistContext.getCurrentNode();
	String content = currentNode.getText();
	int offsetInNode = contentAssistContext.getOffset() - currentNode.getOffset() - contentAssistContext.getPrefix().length();
	String textInFront = content.substring(0, offsetInNode);
	int lastIndexOfLink = textInFront.lastIndexOf(IJavaDocTypeReferenceProvider.LINK_TAG_WITH_SUFFIX);
	if(lastIndexOfLink != -1){
		if(textInFront.substring(lastIndexOfLink, offsetInNode).trim().equals(IJavaDocTypeReferenceProvider.LINK_TAG))
			return true;
	}
	int lastIndexOfSee = textInFront.lastIndexOf(IJavaDocTypeReferenceProvider.SEE_TAG_WITH_SUFFIX);
	if(lastIndexOfSee != -1){
		if(textInFront.substring(lastIndexOfSee, offsetInNode).trim().equals(IJavaDocTypeReferenceProvider.SEE_TAG))
			return true;
	}
	return false;
}
 
Example 3
Source File: XtendProposalProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public void completeInRichString(EObject model, RuleCall ruleCall, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	INode node = context.getCurrentNode();
	ITextRegion textRegion = node.getTextRegion();
	int offset = textRegion.getOffset();
	int length = textRegion.getLength();
	String currentNodeText = node.getText();
	if (currentNodeText.startsWith("\u00BB") && offset + 1 <= context.getOffset()
			|| currentNodeText.startsWith("'''") && offset + 3 <= context.getOffset()) {
		if (context.getOffset() > offset && context.getOffset() < offset + length)
			addGuillemotsProposal(context, acceptor);
	} else if (currentNodeText.startsWith("\u00AB\u00AB")) {
		try {
			IDocument document = context.getViewer().getDocument();
			int nodeLine = document.getLineOfOffset(offset);
			int completionLine = document.getLineOfOffset(context.getOffset());
			if (completionLine > nodeLine) {
				addGuillemotsProposal(context, acceptor);
			}
		} catch (BadLocationException e) {
			// ignore
		}
	}
}
 
Example 4
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 5
Source File: XtendProposalProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void completeMember_Type(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	if (announceProcessing(Lists.newArrayList("completeMember_Type", model, assignment.getFeature()))) {
		if (model instanceof XtendField) {
			XtendField field = (XtendField) model;
			if(!field.getModifiers().isEmpty()) {
				// don't propose types everywhere but only if there's already an indicator for fields, e.g. static, extension, var, val
				if (field.getName() != null) {
					List<INode> nameNodes = NodeModelUtils.findNodesForFeature(model, XtendPackage.Literals.XTEND_FIELD__NAME);
					if (nameNodes.size() == 1) {
						INode node = nameNodes.get(0);
						if (node.getOffset() < context.getOffset()) {
							return;
						}
					}
				}
				completeJavaTypes(context, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, true,
						getQualifiedNameValueConverter(), createVisibilityFilter(context), acceptor);
			}
		}
	}
}
 
Example 6
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean doNotProposeFeatureOfBinaryOperation(ContentAssistContext contentAssistContext,
		XBinaryOperation binaryOperation) {
	List<INode> nodesForFeature = NodeModelUtils.findNodesForFeature(binaryOperation, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
	if (!nodesForFeature.isEmpty()) {
		INode node = nodesForFeature.get(0);
		if (node.getOffset() < contentAssistContext.getOffset() - contentAssistContext.getPrefix().length()) {
			XExpression rightOperand = binaryOperation.getRightOperand();
			if (rightOperand == null)
				return true;
			ICompositeNode rightOperandNode = NodeModelUtils.findActualNodeFor(rightOperand);
			if (rightOperandNode != null) {
				if (rightOperandNode.getOffset() >= contentAssistContext.getOffset())
					return true;
				if (isParentOf(rightOperandNode, contentAssistContext.getLastCompleteNode()))
					return true;
			}
		}
	}
	return false;
}
 
Example 7
Source File: JSONProposalFactory.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private ICompletionProposal createProposal(ContentAssistContext context, String name, String value,
		String description, String rawTemplate, Image image, boolean isGenericProposal) {

	TemplateContextType contextType = getTemplateContextType();
	IXtextDocument document = context.getDocument();
	TemplateContext tContext = new DocumentTemplateContext(contextType, document, context.getOffset(), 0);
	Region replaceRegion = context.getReplaceRegion();

	// pre-populate ${name} and ${value} with given args
	if (isGenericProposal) {
		tContext.setVariable("name", name);
	}
	tContext.setVariable("value", value);

	return new StyledTemplateProposal(context, name, description, rawTemplate, isGenericProposal, tContext,
			replaceRegion, image);
}
 
Example 8
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isKeywordWorthyToPropose(Keyword keyword, ContentAssistContext context) {
	if (isKeywordWorthyToPropose(keyword)) {
		if ("as".equals(keyword.getValue()) || "instanceof".equals(keyword.getValue())) {
			EObject previousModel = context.getPreviousModel();
			if (previousModel instanceof XExpression) {
				if (context.getPrefix().length() == 0) {
					if (NodeModelUtils.getNode(previousModel).getEndOffset() > context.getOffset()) {
						return false;
					}
				}
				LightweightTypeReference type = typeResolver.resolveTypes(previousModel).getActualType((XExpression) previousModel);
				if (type == null || type.isPrimitiveVoid()) {
					return false;
				}
			}
		}
		return true;
	}
	return false;
}
 
Example 9
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void completeBinaryOperationFeature(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	if (model instanceof XBinaryOperation) {
		if (context.getPrefix().length() == 0) { // check for a cursor inbetween an operator
			INode currentNode = context.getCurrentNode();
			int offset = currentNode.getOffset();
			int endOffset = currentNode.getEndOffset();
			if (offset < context.getOffset() && endOffset >= context.getOffset()) {
				if (currentNode.getGrammarElement() instanceof CrossReference) {
					// don't propose another binary operator
					return;
				}
			}
		}
		if (NodeModelUtils.findActualNodeFor(model).getEndOffset() <= context.getOffset()) {
			createReceiverProposals((XExpression) model,
					(CrossReference) assignment.getTerminal(), context, acceptor);
		} else {
			createReceiverProposals(((XBinaryOperation) model).getLeftOperand(),
					(CrossReference) assignment.getTerminal(), context, acceptor);
		}
	} else {
		EObject previousModel = context.getPreviousModel();
		if (previousModel instanceof XExpression) {
			if (context.getPrefix().length() == 0) {
				if (NodeModelUtils.getNode(previousModel).getEndOffset() > context.getOffset()) {
					return;
				}
			}
			createReceiverProposals((XExpression) previousModel,
					(CrossReference) assignment.getTerminal(), context, acceptor);
		}
	}
}
 
Example 10
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 11
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void completeWithinBlock(EObject model, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
	ICompositeNode node = NodeModelUtils.getNode(model);
	if (node.getOffset() >= context.getOffset()) {
		createLocalVariableAndImplicitProposals(model, IExpressionScope.Anchor.BEFORE, context, acceptor);
		return;
	}
	if (model instanceof XBlockExpression) {
		List<XExpression> children = ((XBlockExpression) model).getExpressions();
		if (!children.isEmpty()) {
			for(int i = children.size() - 1; i >= 0; i--) {
				XExpression child = children.get(i);
				ICompositeNode childNode = NodeModelUtils.getNode(child);
				if (childNode.getEndOffset() <= context.getOffset()) {
					createLocalVariableAndImplicitProposals(child, IExpressionScope.Anchor.AFTER, context, acceptor);
					return;
				}
			}
		}
	}
	int endOffset = node.getEndOffset();
	if (endOffset <= context.getOffset()) {
		if (model instanceof XFeatureCall && model.eContainer() instanceof XClosure || endOffset == context.getOffset() && context.getPrefix().length() == 0)
			return;
		if (isInMemberFeatureCall(model, endOffset, context))
			return;
		createLocalVariableAndImplicitProposals(model, IExpressionScope.Anchor.AFTER, context, acceptor);
		return;
	} else if (isInMemberFeatureCall(model, endOffset, context)) {
		return;
	}
	if (model instanceof XClosure)
		return;
	createLocalVariableAndImplicitProposals(model, IExpressionScope.Anchor.BEFORE, context, acceptor);
}
 
Example 12
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 13
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 14
Source File: XtendProposalProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void completeWithinBlock(EObject model, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
	if (model instanceof AnonymousClass) {
		ICompositeNode node = NodeModelUtils.getNode(model);
		if (node.getOffset() >= context.getOffset()) {
			super.completeWithinBlock(model, context, acceptor);
		} else {
			return;
		}
	}
	if (model instanceof XtendMember && model.eContainer() instanceof AnonymousClass) {
		return;
	}
	super.completeWithinBlock(model, context, acceptor);
}
 
Example 15
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeTypeRef_Classifier(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	Grammar grammar = GrammarUtil.getGrammar(model);
	ContentAssistContext.Builder myContextBuilder = context.copy();
	myContextBuilder.setMatcher(new ClassifierPrefixMatcher(context.getMatcher(), classifierQualifiedNameConverter));
	if (model instanceof TypeRef) {
		ICompositeNode node = NodeModelUtils.getNode(model);
		if (node != null) {
			int offset = node.getOffset();
			Region replaceRegion = new Region(offset, context.getReplaceRegion().getLength()
					+ context.getReplaceRegion().getOffset() - offset);
			myContextBuilder.setReplaceRegion(replaceRegion);
			myContextBuilder.setLastCompleteNode(node);
			StringBuilder availablePrefix = new StringBuilder(4);
			for (ILeafNode leaf : node.getLeafNodes()) {
				if (leaf.getGrammarElement() != null && !leaf.isHidden()) {
					if ((leaf.getTotalLength() + leaf.getTotalOffset()) < context.getOffset())
						availablePrefix.append(leaf.getText());
					else
						availablePrefix.append(leaf.getText().substring(0,
								context.getOffset() - leaf.getTotalOffset()));
				}
				if (leaf.getTotalOffset() >= context.getOffset())
					break;
			}
			myContextBuilder.setPrefix(availablePrefix.toString());
		}
	}
	ContentAssistContext myContext = myContextBuilder.toContext();
	for (AbstractMetamodelDeclaration declaration : grammar.getMetamodelDeclarations()) {
		if (declaration.getEPackage() != null) {
			createClassifierProposals(declaration, model, myContext, acceptor);
		}
	}
}
 
Example 16
Source File: DotProposalProvider.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
private void proposeAttributeValues(String subgrammarName,
		ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	INode currentNode = context.getCurrentNode();
	String nodeText = currentNode.getText();
	String prefix = context.getPrefix();

	int offset = currentNode.getOffset();
	String text = nodeText.trim();

	if (!nodeText.contains(prefix)) {
		text = prefix;
		offset = context.getOffset() - prefix.length();
	} else {
		boolean quoted = text.startsWith("\"") //$NON-NLS-1$
				&& text.endsWith("\""); //$NON-NLS-1$
		boolean html = text.startsWith("<") && text.endsWith(">"); //$NON-NLS-1$ //$NON-NLS-2$

		if (quoted || html) {
			if (quoted) {
				text = ID.fromString(text, Type.QUOTED_STRING).toValue();
			} else {
				text = text.substring(1, text.length() - 1);
			}
			offset++;
		}
	}

	List<ConfigurableCompletionProposal> configurableCompletionProposals = new DotProposalProviderDelegator(
			subgrammarName).computeConfigurableCompletionProposals(text,
					context.getOffset() - offset);

	for (ConfigurableCompletionProposal configurableCompletionProposal : configurableCompletionProposals) {
		// adapt the replacement offset determined within the
		// sub-grammar context to be valid within the context of the
		// original text
		configurableCompletionProposal.setReplacementOffset(offset
				+ configurableCompletionProposal.getReplacementOffset());

		acceptor.accept(configurableCompletionProposal);
	}
}
 
Example 17
Source File: ParameterContextInformationProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void getContextInformation(ContentAssistContext context, IContextInformationAcceptor acceptor) {
	XExpression containerCall = getContainerCall(eObjectAtOffsetHelper.resolveContainedElementAt(context.getResource(), context.getOffset()));
	LightweightTypeReferenceFactory factory = proposalProvider.getTypeConverter(context.getResource());
	if (containerCall != null) {
		ICompositeNode containerCallNode = NodeModelUtils.findActualNodeFor(containerCall);
		ITextRegion containerCallRegion = containerCallNode.getTextRegion();
		if(containerCallRegion.getOffset() > context.getOffset()
				|| containerCallRegion.getOffset() + containerCallRegion.getLength() < context.getOffset()) 
			return;
		JvmIdentifiableElement calledFeature = getCalledFeature(containerCall);
		if (calledFeature instanceof JvmExecutable) {
			if(getParameterListOffset(containerCall) > context.getOffset()) 
				return;
			ParameterData parameterData = new ParameterData();
			IScope scope = getScope(containerCall);
			QualifiedName qualifiedName = QualifiedName.create(getCalledFeatureName(containerCall));
			boolean candidatesFound = false;
			for (IEObjectDescription element : scope.getElements(qualifiedName)) {
				if (element instanceof IIdentifiableElementDescription) {
					IIdentifiableElementDescription featureDescription = (IIdentifiableElementDescription) element;
					JvmIdentifiableElement featureCandidate = featureDescription.getElementOrProxy();
					if (featureCandidate instanceof JvmExecutable) {
						JvmExecutable executable = (JvmExecutable) featureCandidate;
						if(!executable.getParameters().isEmpty()) {
							StyledString styledString = new StyledString();
							proposalProvider.appendParameters(styledString, executable,
									featureDescription.getNumberOfIrrelevantParameters(), factory);
							parameterData.addOverloaded(styledString.toString(), executable.isVarArgs());
							candidatesFound = true;
						}
					}
				}
			}
			if (candidatesFound) {
				StyledString displayString = proposalProvider.getStyledDisplayString((JvmExecutable) calledFeature, true, 0, 
						qualifiedNameConverter.toString(qualifiedNameProvider.getFullyQualifiedName(calledFeature)), 
						calledFeature.getSimpleName(), factory);
				ParameterContextInformation parameterContextInformation = new ParameterContextInformation(
						parameterData, displayString.toString(), getParameterListOffset(containerCall), context.getOffset());
				acceptor.accept(parameterContextInformation);
			}
		}
	}
}