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

The following examples show how to use org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal#setReplacementOffset() . 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: DotHtmlLabelProposalProvider.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
private void proposeHtmlColorAttributeValues(ContentAssistContext context,
		ICompletionProposalAcceptor acceptor, String text,
		int beginReplacementOffset, int contextOffset) {
	String subgrammarName = DotActivator.ORG_ECLIPSE_GEF_DOT_INTERNAL_LANGUAGE_DOTCOLOR;

	List<ConfigurableCompletionProposal> configurableCompletionProposals = new DotProposalProviderDelegator(
			subgrammarName).computeConfigurableCompletionProposals(text,
					contextOffset - beginReplacementOffset);

	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(
				beginReplacementOffset + configurableCompletionProposal
						.getReplacementOffset());
		acceptor.accept(configurableCompletionProposal);
	}
}
 
Example 2
Source File: DotHtmlLabelProposalProvider.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
private void proposeHtmlAttributeValues(ContentAssistContext context,
		ICompletionProposalAcceptor acceptor, String... proposals) {
	for (String proposal : proposals) {
		ICompletionProposal completionProposal = createCompletionProposal(
				proposal, context);
		String text = context.getCurrentNode().getText();
		if ((text.startsWith("\"") || text.startsWith("'"))//$NON-NLS-1$ //$NON-NLS-2$
				&& completionProposal instanceof ConfigurableCompletionProposal) {
			// ensure that the single quote / double quote at the beginning
			// of an attribute
			// value is not overridden when applying the proposal
			ConfigurableCompletionProposal configurableCompletionProposal = (ConfigurableCompletionProposal) completionProposal;
			configurableCompletionProposal.setReplacementOffset(
					configurableCompletionProposal.getReplacementOffset()
							+ 1);
			configurableCompletionProposal.setReplacementLength(
					configurableCompletionProposal.getReplacementLength()
							- 1);
			configurableCompletionProposal.setReplaceContextLength(
					configurableCompletionProposal.getReplaceContextLength()
							- 1);
		}
		acceptor.accept(completionProposal);
	}
}
 
Example 3
Source File: ProjectionAwareProposalAcceptor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void accept(ICompletionProposal proposal) {
	if (proposal != null) {
		ConfigurableCompletionProposal configurableProposal = 
			(ConfigurableCompletionProposal) proposal;
		configurableProposal.setSelectionStart(evaluatedTemplate.getOriginalOffset(configurableProposal.getSelectionStart()));
		configurableProposal.setReplacementOffset(evaluatedTemplate.getOriginalOffset(configurableProposal.getReplacementOffset()));
		acceptor.accept(proposal);
	}
}
 
Example 4
Source File: N4JSReplacementTextApplier.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Really do apply the proposal. Assumed to be run within the prevent flickering mode.
 */
private int doApply(QualifiedName qualifiedName, String alias, IDocument document,
		ConfigurableCompletionProposal proposal) throws BadLocationException {
	String shortSemanticReplacementString = alias != null ? alias : lastSegmentOrDefaultHost(qualifiedName);
	String shortSyntacticReplacementString = valueConverter.toString(shortSemanticReplacementString);
	String longReplacementString = shortSyntacticReplacementString
			+ ConfigurableCompletionProposalExtensions.getReplacementSuffix(proposal);

	ImportRewriter importRewriter = importRewriterFactory.create(document, context);

	ReplaceEdit replaceEdit = new ReplaceEdit(
			proposal.getReplacementOffset(),
			proposal.getReplacementLength(),
			longReplacementString);
	MultiTextEdit compound = new MultiTextEdit();
	AliasLocation aliasLocation = null;
	if (alias != null) {
		aliasLocation = importRewriter.addSingleImport(qualifiedName, alias, compound);
	} else {
		importRewriter.addSingleImport(qualifiedName, compound);
	}
	compound.addChild(replaceEdit);

	Position caret = new Position(proposal.getReplacementOffset(), 0);
	document.addPosition(caret);
	compound.apply(document);
	document.removePosition(caret);

	int cursorPosition = caret.getOffset();
	proposal.setReplacementOffset(cursorPosition - longReplacementString.length());
	proposal.setReplacementLength(shortSyntacticReplacementString.length()); // do not include suffix!
	proposal.setCursorPosition(
			cursorPosition - proposal.getReplacementOffset()); // cursorPosition is relative to replacementOffset!

	if (aliasLocation != null) {
		final int aliasOffset = aliasLocation.getBaseOffset() + aliasLocation.getRelativeOffset();
		final int aliasLength = shortSyntacticReplacementString.length();
		N4JSCompletionProposal castedProposal = (N4JSCompletionProposal) proposal;
		castedProposal.setLinkedModeBuilder((appliedProposal, currentDocument) -> {
			if (viewer.getTextWidget() == null || viewer.getTextWidget().isDisposed()) {
				return; // do not attempt to set up linked mode in a disposed UI
			}
			try {
				LinkedPositionGroup group = new LinkedPositionGroup();
				group.addPosition(new LinkedPosition(
						currentDocument,
						aliasOffset,
						aliasLength,
						LinkedPositionGroup.NO_STOP));
				group.addPosition(new LinkedPosition(
						currentDocument,
						proposal.getReplacementOffset(),
						proposal.getReplacementLength(),
						LinkedPositionGroup.NO_STOP));
				proposal.setSelectionStart(proposal.getReplacementOffset());
				proposal.setSelectionLength(proposal.getReplacementLength());
				LinkedModeModel model = new LinkedModeModel();
				model.addGroup(group);
				model.forceInstall();

				LinkedModeUI ui = new LinkedModeUI(model, viewer);
				ui.setExitPolicy(new IdentifierExitPolicy('\n'));
				ui.setExitPosition(
						viewer,
						proposal.getReplacementOffset()
								+ proposal.getCursorPosition()
								+ ConfigurableCompletionProposalExtensions.getCursorOffset(proposal),
						0,
						Integer.MAX_VALUE);
				ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER);
				ui.enter();
			} catch (BadLocationException e) {
				logger.error(e.getMessage(), e);
			}
		});
	} else {
		adjustCursorPositionIfRequested(proposal);
	}
	return cursorPosition;
}
 
Example 5
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);
	}
}