Java Code Examples for org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal#setReplacementString()

The following examples show how to use org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal#setReplacementString() . 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: StringProposalDelegate.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void accept(ICompletionProposal proposal) {
	if (proposal instanceof ConfigurableCompletionProposal) {
		ConfigurableCompletionProposal configurableCompletionProposal = (ConfigurableCompletionProposal) proposal;
		int endPos = configurableCompletionProposal.getReplacementOffset()
				+ configurableCompletionProposal.getReplacementLength();
		if (ctx.getDocument() != null && ctx.getDocument().getLength() > endPos) {
			// We are not at the end of the file
			try {
				if ("\"".equals(ctx.getDocument().get(endPos, 1))) {
					configurableCompletionProposal
							.setReplacementLength(configurableCompletionProposal.getReplacementLength() - 1);
					configurableCompletionProposal
							.setReplacementString(configurableCompletionProposal.getReplacementString().substring(0,
									configurableCompletionProposal.getReplacementString().length() - 1));
				}
			} catch (BadLocationException e) {
				LOG.debug("Skipped propsal adjustment.", e);
			}
		}
	}
	super.accept(proposal);
}
 
Example 2
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected ICompletionProposal createFeatureProposal(EStructuralFeature feature, int priorityFactor, Function<IEObjectDescription, ICompletionProposal> factory, 
		ContentAssistContext context) {
	IEObjectDescription description = EObjectDescription.create(QualifiedName.create(feature.getName()),
			feature);
	ConfigurableCompletionProposal proposal = (ConfigurableCompletionProposal) factory.apply(description);
	if (proposal != null) {
		proposal.setPriority(proposal.getPriority() * priorityFactor);
		if(SemanticHighlightingCalculator.SPECIAL_ATTRIBUTES.contains(feature.getName())) {
			StyledString displayString = stylerFactory.createFromXtextStyle(feature.getName(),
					semanticHighlightingConfiguration.specialAttribute())
					.append(" - Assignment of special attribute ")
					.append(stylerFactory.createFromXtextStyle(feature.getName(), 
							semanticHighlightingConfiguration.specialAttribute()));
			proposal.setDisplayString(displayString);
		} else {
			proposal.setDisplayString(new StyledString(feature.getName() + " - Assignment of feature " + feature.getName()));
		}
		if(feature.isMany()) {
			proposal.setReplacementString(feature.getName() + "+=");
			proposal.setCursorPosition(proposal.getCursorPosition() + 2);
		} else if(feature.getEType() == EcorePackage.Literals.EBOOLEAN) {
			proposal.setReplacementString(feature.getName() + "?=");
			proposal.setCursorPosition(proposal.getCursorPosition() + 2);
		} else {
			proposal.setReplacementString(feature.getName() + "=");
			proposal.setCursorPosition(proposal.getCursorPosition() + 1);
		}
	}
	return proposal;
}
 
Example 3
Source File: DotHtmlLabelProposalProvider.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeHtmlAttr_Name(EObject model, Assignment assignment,
		ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	super.completeHtmlAttr_Name(model, assignment, context, acceptor);

	if (model instanceof HtmlTag) {
		HtmlTag htmlTag = (HtmlTag) model;
		String htmlTagName = htmlTag.getName();
		Map<String, Set<String>> validAttributes = DotHtmlLabelHelper
				.getValidAttributes();
		if (validAttributes.containsKey(htmlTagName)) {
			Image image = imageHelper.getImage("attribute.png"); //$NON-NLS-1$
			Set<String> validAttributeNames = validAttributes
					.get(htmlTagName);
			for (String validAttributeName : validAttributeNames) {
				String proposal = validAttributeName;
				String format = "%s: Attribute"; //$NON-NLS-1$
				StyledString displayString = DotEditorUtils.style(format,
						proposal);

				ICompletionProposal completionProposal = createCompletionProposal(
						proposal, displayString, image, context);

				// insert the ="" symbols after the html attribute name and
				// place the cursor between the two "" symbols
				if (completionProposal instanceof ConfigurableCompletionProposal) {
					ConfigurableCompletionProposal configurableCompletionProposal = (ConfigurableCompletionProposal) completionProposal;
					String replacementString = validAttributeName + "=\"\""; //$NON-NLS-1$
					configurableCompletionProposal
							.setReplacementString(replacementString);
					configurableCompletionProposal.setCursorPosition(
							replacementString.length() - 1);
					acceptor.accept(configurableCompletionProposal);
				}

			}
		}
	}
}
 
Example 4
Source File: DotProposalProvider.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private void proposeAttributeNames(Context attributeContext,
		ContentAssistContext contentAssistContext,
		ICompletionProposalAcceptor acceptor) {

	Image attributeImage = imageHelper.getImage("attribute.png"); //$NON-NLS-1$
	String format = "%s: Attribute"; //$NON-NLS-1$

	for (String attributeName : dotAttributeNames.get(attributeContext)) {
		StyledString displayString = DotEditorUtils.style(format,
				attributeName);
		ICompletionProposal completionProposal = createCompletionProposal(
				attributeName, displayString, attributeImage,
				contentAssistContext);
		if (completionProposal instanceof ConfigurableCompletionProposal) {
			ConfigurableCompletionProposal configurableCompletionProposal = (ConfigurableCompletionProposal) completionProposal;

			// ensure that the '=' symbol is inserted after the attribute
			// name when applying the proposal
			configurableCompletionProposal.setReplacementString(
					configurableCompletionProposal.getReplacementString()
							+ "="); //$NON-NLS-1$
			configurableCompletionProposal.setCursorPosition(
					configurableCompletionProposal.getCursorPosition() + 1);
		}
		acceptor.accept(completionProposal);
	}
}