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

The following examples show how to use org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext#getDocument() . 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: 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 2
Source File: AbstractTemplateProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Positions created for template contexts have to be added to the document so they are updated when the document
 * is modified.
 * 
 * @since 2.8
 */
protected Position createPosition(ContentAssistContext context) {
	Position position = new Position(context.getReplaceRegion().getOffset(), context.getReplaceRegion().getLength());
	IDocument document = context.getDocument();
	if (document.containsPositionCategory(XTEXT_TEMPLATE_POS_CATEGORY)) {
		try {
			document.addPosition(XTEXT_TEMPLATE_POS_CATEGORY, position);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
	return position;
}
 
Example 3
Source File: AbstractTemplateProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected TemplateContext doCreateTemplateContext(TemplateContextType contextType, ContentAssistContext context) {
	return new XtextTemplateContext(contextType, context.getDocument(),
			createPosition(context),
			context, getScopeProvider());
}
 
Example 4
Source File: ImplementMemberFromSuperAssist.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected ICompletionProposal createOverrideMethodProposal(XtendTypeDeclaration model, IResolvedExecutable overrideable,
		final ContentAssistContext context, IProposalConflictHelper conflictHelper) {
	IXtextDocument document = context.getDocument();
	XtextResource resource = (XtextResource) model.eResource();
	int offset = context.getReplaceRegion().getOffset();
	int currentIndentation = appendableFactory.getIndentationLevelAtOffset(offset, document, resource);
	final int indentationLevel = currentIndentation == 0 ? 1 : currentIndentation;
	ReplacingAppendable appendable = appendableFactory.create(document, resource, offset, context.getReplaceRegion().getLength(), new OptionalParameters() {{ 
				ensureEmptyLinesAround = true;
				baseIndentationLevel = indentationLevel;	
			}});
	final String simpleName;
	JvmExecutable declaration = overrideable.getDeclaration();
	if (overrideable instanceof IResolvedOperation) {
		implementor.appendOverrideFunction(model, (IResolvedOperation) overrideable, appendable);
		simpleName = overrideable.getDeclaration().getSimpleName();
	} else if (model instanceof XtendClass) {
		implementor.appendConstructorFromSuper((XtendClass) model, (IResolvedConstructor) overrideable, appendable);
		simpleName = "new";
	} else {
		return null;
	}
	String code = appendable.getCode();
	if (!isValidProposal(code.trim(), context, conflictHelper) && !isValidProposal(simpleName, context, conflictHelper))
		return null;
	ImageDescriptor imageDescriptor = images.forOperation(declaration.getVisibility(), adornments.getOverrideAdornment(declaration));
	ImportOrganizingProposal completionProposal = createCompletionProposal(appendable, context.getReplaceRegion(),
			getLabel(overrideable), imageHelper.getImage(imageDescriptor));
	Matcher matcher = bodyExpressionPattern.matcher(code);
	if (matcher.find()) {
		int bodyExpressionLength = matcher.end(1) - matcher.start(1);
		int bodyExpressionStart = matcher.start(1) + appendable.getTotalOffset() - completionProposal.getReplacementOffset();
		if (bodyExpressionLength == 0) {
			completionProposal.setCursorPosition(bodyExpressionStart);
		} else {
			completionProposal.setSelectionStart(completionProposal.getReplacementOffset() + bodyExpressionStart);
			completionProposal.setSelectionLength(bodyExpressionLength);
			completionProposal.setAutoInsertable(false);
			completionProposal.setCursorPosition(bodyExpressionStart + bodyExpressionLength);
			completionProposal.setSimpleLinkedMode(context.getViewer(), '\t');
		}
	}
	completionProposal.setPriority(getPriority(model, declaration, context));
	completionProposal.setMatcher(new PrefixMatcher() {

		@Override
		public boolean isCandidateMatchingPrefix(String name, String prefix) {
			PrefixMatcher delegate = context.getMatcher();
			boolean result = delegate.isCandidateMatchingPrefix(simpleName, prefix);
			return result;
		}
		
	});
	return completionProposal;
}