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

The following examples show how to use org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal#setCursorPosition() . 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: N4JSReplacementTextApplier.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Just insert the string at the replacement offset. Everything else is fine.
 */
private void simpleApply(IDocument document, String string, ConfigurableCompletionProposal proposal)
		throws BadLocationException {
	final String replacement = string + ConfigurableCompletionProposalExtensions.getReplacementSuffix(proposal);
	proposal.setCursorPosition(replacement.length()); // cursorPosition is relative to replacementOffset!
	document.replace(proposal.getReplacementOffset(), proposal.getReplacementLength(),
			replacement);
	adjustCursorPositionIfRequested(proposal);
}
 
Example 2
Source File: N4JSReplacementTextApplier.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private void adjustCursorPositionIfRequested(ConfigurableCompletionProposal proposal) {
	final int offset = ConfigurableCompletionProposalExtensions.getCursorOffset(proposal);
	if (offset != 0) {
		proposal.setCursorPosition(proposal.getCursorPosition() + offset);

		// do not attempt to set up linked mode in a disposed UI
		if (viewer.getTextWidget() != null && !viewer.getTextWidget().isDisposed()) {
			final int pos = proposal.getReplacementOffset() + proposal.getCursorPosition();
			proposal.setSelectionStart(pos);
			proposal.setSelectionLength(0);
			proposal.setSimpleLinkedMode(viewer, '\t', '\n', '\r');
		}
	}
}
 
Example 3
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 4
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void completeParserRule(EObject model, final ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	final Grammar grammar = GrammarUtil.getGrammar(model);
	for (AbstractMetamodelDeclaration metamodelDeclaration : grammar.getMetamodelDeclarations()) {
		if (metamodelDeclaration instanceof ReferencedMetamodel) {
			ReferencedMetamodel referencedMetamodel = (ReferencedMetamodel) metamodelDeclaration;
			EPackage ePackage = referencedMetamodel.getEPackage();
			if (ePackage != null) {
				for (EClassifier eClassifier : ePackage.getEClassifiers()) {
					if (isProposeParserRule(eClassifier, grammar)) {
						String proposal = eClassifier.getName();
						String metamodelAlias = referencedMetamodel.getAlias();
						if (metamodelAlias != null) {
							proposal = proposal + " returns " + metamodelAlias + "::" + eClassifier.getName();
						}
						proposal = proposal + ": \n;\n";
						ConfigurableCompletionProposal completionProposal = (ConfigurableCompletionProposal) createCompletionProposal(
								proposal, eClassifier.getName() + " - parser rule",
								getImage(XtextFactory.eINSTANCE.createParserRule()), context);
						if (completionProposal != null) {
							completionProposal.setCursorPosition(proposal.length() - 3);
							acceptor.accept(completionProposal);
						}
					}
				}
			}
		}
	}
}
 
Example 5
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 6
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);
	}
}
 
Example 7
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 8
Source File: DotHtmlLabelProposalProvider.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void complete_HtmlTag(EObject model, RuleCall ruleCall,
		ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {

	String parentName = null;
	List<HtmlContent> siblings = new ArrayList<>();

	if (model instanceof HtmlTag) {
		HtmlTag tag = (HtmlTag) model;
		parentName = tag.getName();
		siblings = tag.getChildren();
	} else {
		parentName = DotHtmlLabelHelper.getRootTagKey();
		if (model instanceof HtmlLabel) {
			siblings = ((HtmlLabel) model).getParts();
		}
		if (model instanceof HtmlContent) {
			siblings.add((HtmlContent) model);
		}
	}

	Image image = imageHelper.getImage("html_tag.png"); //$NON-NLS-1$
	for (String tagName : DotHtmlLabelHelper.getValidTags()
			.get(parentName)) {
		if (isValidSibling(tagName, siblings)) {

			String proposal = calculateProposalString(tagName);
			String format = "%s: Tag"; //$NON-NLS-1$
			StyledString displayString = DotEditorUtils.style(format,
					proposal);

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

			if (completionProposal instanceof ConfigurableCompletionProposal) {
				ConfigurableCompletionProposal configurableCompletionProposal = (ConfigurableCompletionProposal) completionProposal;
				int cursorPosition = calculateCursorPosition(proposal);
				String tagDescription = DotHtmlLabelHelper
						.getTagDescription(tagName);
				// place the cursor between the opening and the closing html
				// tag
				// after the proposal has been applied
				configurableCompletionProposal
						.setCursorPosition(cursorPosition);
				// add tag description to the proposal
				configurableCompletionProposal
						.setAdditionalProposalInfo(tagDescription);
				acceptor.accept(configurableCompletionProposal);
			}
		}
	}
}