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

The following examples show how to use org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext#getCurrentNode() . 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: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void createAnnotationProposals(ICompletionProposalAcceptor acceptor, ContentAssistContext context, boolean withPrefix) {
	final INode node = context.getCurrentNode();
	if (node == null) {
		return;
	}
	
	final Object o = context.getCurrentNode().getGrammarElement();
	if (o == grammarAccess.getGrammarAccess().getNameGrammarIDParserRuleCall_1_0()
			|| o == grammarAccess.getGrammarAccess().getUsedGrammarsGrammarCrossReference_2_1_0()
			|| o == grammarAccess.getGrammarAccess().getUsedGrammarsAssignment_2_1()) {
		// don't propose annotations within grammarIds
		return;
	}
	
	for (String name : AnnotationNames.VALID_ANNOTATIONS_NAMES) {
		final String proposal = withPrefix ? "@" + name : name;
		acceptor.accept(createCompletionProposal(proposal, "@" + name, null, context));
	}
}
 
Example 2
Source File: DotHtmlLabelProposalProvider.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
private void proposeHtmlBgColorAttributeValues(ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	INode currentNode = context.getCurrentNode();
	String fullText = currentNode.getText();
	String text = fullText;
	int beginReplacementOffset = currentNode.getOffset();

	if (context.getPrefix().contains(":")) { //$NON-NLS-1$
		int colonOffset = fullText.indexOf(':') + 1;
		text = fullText.substring(colonOffset);
		beginReplacementOffset += colonOffset;
	} else {
		beginReplacementOffset += beginsWithQuote(text) ? 1 : 0;
	}
	proposeHtmlColorAttributeValues(context, acceptor,
			text.replaceAll("['\"]", ""), //$NON-NLS-1$ //$NON-NLS-2$
			beginReplacementOffset, context.getOffset());
	if (!fullText.contains(":")) { //$NON-NLS-1$
		acceptor.accept(new ConfigurableCompletionProposal(":", //$NON-NLS-1$
				context.getOffset(), 0, 1));
	}
}
 
Example 3
Source File: XtendProposalProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void completeType_Members(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	if (isValidTypeForOverriding(model)) {
		INode node = context.getCurrentNode();
		EObject eObject = NodeModelUtils.findActualSemanticObjectFor(node);
		if (!(eObject instanceof AnonymousClass)) {
			// due to some optimizations in the CA parser, we get some bogus context here and have to
			// double check that an override proposal would be valid at this location
			// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=370955
			EObject prevModel = context.getPreviousModel();
			if (prevModel instanceof XExpression) {
				XtendMember containingMember = EcoreUtil2.getContainerOfType(prevModel, XtendMember.class);
				XBlockExpression blockExpression = EcoreUtil2.getContainerOfType(prevModel, XBlockExpression.class);
				if (blockExpression != null && blockExpression != prevModel) {
					if (EcoreUtil.isAncestor(containingMember, blockExpression)) { // still inside block
						return;
					}
				}
			}
			overrideAssist.createOverrideProposals((XtendTypeDeclaration) model, context, acceptor, getConflictHelper());
		}
	}
	super.completeType_Members(model, assignment, context, acceptor);
}
 
Example 4
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 5
Source File: XtendJavaDocProposalFactory.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected int getReplacementContextLength(ContentAssistContext context) {
	int replacementOffset = context.getReplaceRegion().getOffset();
	INode currentNode = context.getCurrentNode();
	ITextRegion currentRegion = currentNode.getTextRegion();
	String text = currentNode.getText();
	int index = text.indexOf('}', replacementOffset - currentRegion.getOffset());
	if (index == -1) {
		index = text.indexOf('\n', replacementOffset - currentRegion.getOffset());
	}
	if (index != -1) {
		int indexFromStart = index + currentNode.getOffset();
		return indexFromStart - replacementOffset;
	} else {
		return context.getReplaceContextLength();
	}
}
 
Example 6
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 7
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 8
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 9
Source File: DotHtmlLabelProposalProvider.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private void proposeHtmlColorAttributeValues(ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	INode currentNode = context.getCurrentNode();
	String text = currentNode.getText();
	proposeHtmlColorAttributeValues(context, acceptor,
			text.replaceAll("['\"]", ""), //$NON-NLS-1$ //$NON-NLS-2$
			currentNode.getOffset() + (beginsWithQuote(text) ? 1 : 0),
			context.getOffset());
}
 
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: 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);
	}
}