Search in sources :

Example 16 with ConfigurableCompletionProposal

use of org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal in project xtext-eclipse by eclipse.

the class CodetemplatesProposalProvider method createStringProposal.

private void createStringProposal(ContentAssistContext context, ICompletionProposalAcceptor acceptor, String feature, RuleCall ruleCall) {
    String proposalText = feature != null ? feature : Strings.toFirstUpper(ruleCall.getRule().getName().toLowerCase());
    proposalText = getValueConverter().toString(proposalText, ruleCall.getRule().getName());
    String displayText = proposalText;
    if (feature != null)
        displayText = displayText + " - " + ruleCall.getRule().getName();
    ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
    if (proposal instanceof ConfigurableCompletionProposal) {
        ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
        configurable.setSelectionStart(configurable.getReplacementOffset() + 1);
        configurable.setSelectionLength(proposalText.length() - 2);
        configurable.setAutoInsertable(false);
        configurable.setSimpleLinkedMode(context.getViewer(), proposalText.charAt(0), '\t');
    }
    acceptor.accept(proposal);
}
Also used : ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) StyledString(org.eclipse.jface.viewers.StyledString)

Example 17 with ConfigurableCompletionProposal

use of org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal in project xtext-eclipse by eclipse.

the class CodetemplatesProposalProvider method complete_Variable.

@Override
public void complete_Variable(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    if ((mode & NORMAL) != 0) {
        String proposalText = "${}";
        StyledString displayText = new StyledString(proposalText, StyledString.DECORATIONS_STYLER).append(" - Create a new template variable", StyledString.QUALIFIER_STYLER);
        ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
        if (proposal instanceof ConfigurableCompletionProposal) {
            ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
            configurable.setSelectionStart(configurable.getReplacementOffset() + 2);
            configurable.setSelectionLength(0);
            configurable.setAutoInsertable(false);
            configurable.setSimpleLinkedMode(context.getViewer(), '\t', ' ');
        }
        acceptor.accept(proposal);
    }
}
Also used : ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString)

Example 18 with ConfigurableCompletionProposal

use of org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal in project xtext-eclipse by eclipse.

the class CodetemplatesProposalProvider method completeCodetemplate_KeywordContext.

@Override
public void completeCodetemplate_KeywordContext(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    Codetemplate template = EcoreUtil2.getContainerOfType(model, Codetemplate.class);
    if (template != null) {
        Codetemplates templates = EcoreUtil2.getContainerOfType(template, Codetemplates.class);
        if (templates != null) {
            Grammar language = templates.getLanguage();
            if (language != null && !language.eIsProxy()) {
                Set<String> keywords = GrammarUtil.getAllKeywords(language);
                for (String keyword : keywords) {
                    String proposalText = keyword;
                    proposalText = getValueConverter().toString(proposalText, ((RuleCall) assignment.getTerminal()).getRule().getName());
                    StyledString displayText = new StyledString(proposalText).append(" - Keyword", StyledString.QUALIFIER_STYLER);
                    ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
                    getPriorityHelper().adjustCrossReferencePriority(proposal, context.getPrefix());
                    if (proposal instanceof ConfigurableCompletionProposal) {
                        ((ConfigurableCompletionProposal) proposal).setPriority(((ConfigurableCompletionProposal) proposal).getPriority() - 1);
                    }
                    acceptor.accept(proposal);
                }
            }
        }
    }
}
Also used : Codetemplates(org.eclipse.xtext.ui.codetemplates.templates.Codetemplates) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) Codetemplate(org.eclipse.xtext.ui.codetemplates.templates.Codetemplate) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Grammar(org.eclipse.xtext.Grammar) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString)

Example 19 with ConfigurableCompletionProposal

use of org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal in project xtext-eclipse by eclipse.

the class CodetemplatesProposalProvider method completeNestedCrossReference.

public void completeNestedCrossReference(CrossReference crossReference, ContentAssistContext context, ICompletionProposalAcceptor acceptor, TemplateData data) {
    if (data.doCreateProposals()) {
        ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(data.language);
        if (helper != null) {
            String contextTypeId = helper.getId(data.rule);
            ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(data.language);
            TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
            TemplateVariableResolver crossRefResolver = getResolver(contextType, "CrossReference");
            if (crossRefResolver != null) {
                Assignment assignment = (Assignment) crossReference.eContainer();
                EReference reference = GrammarUtil.getReference(crossReference);
                if (reference != null) {
                    String proposalText = "${" + assignment.getFeature() + ":CrossReference(" + reference.getEContainingClass().getName() + "." + reference.getName() + ")}";
                    StyledString displayText = new StyledString("${", StyledString.DECORATIONS_STYLER).append(assignment.getFeature()).append(":CrossReference(", StyledString.DECORATIONS_STYLER).append(reference.getEContainingClass().getName() + "." + reference.getName(), StyledString.COUNTER_STYLER).append(")}", StyledString.DECORATIONS_STYLER).append(" - Create a new template variable", StyledString.QUALIFIER_STYLER);
                    ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
                    if (proposal instanceof ConfigurableCompletionProposal) {
                        ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
                        configurable.setSelectionStart(configurable.getReplacementOffset() + 2);
                        configurable.setSelectionLength(assignment.getFeature().length());
                        configurable.setAutoInsertable(false);
                        configurable.setSimpleLinkedMode(context.getViewer(), '\t');
                        configurable.setPriority(configurable.getPriority() * 2);
                    }
                    acceptor.accept(proposal);
                }
            }
        }
    }
}
Also used : Assignment(org.eclipse.xtext.Assignment) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ContextTypeRegistry(org.eclipse.jface.text.templates.ContextTypeRegistry) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) ContextTypeIdHelper(org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper) EReference(org.eclipse.emf.ecore.EReference) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 20 with ConfigurableCompletionProposal

use of org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal in project xtext-eclipse by eclipse.

the class CodetemplatesProposalProvider method completeVariable_Name.

@Override
public void completeVariable_Name(EObject model, Assignment assignment, final ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    if ((mode & NORMAL) != 0) {
        {
            String proposalText = "variable";
            StyledString displayText = new StyledString(proposalText).append(" - Create a new template variable", StyledString.QUALIFIER_STYLER);
            ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
            if (proposal instanceof ConfigurableCompletionProposal) {
                ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
                configurable.setSelectionStart(configurable.getReplacementOffset());
                configurable.setSelectionLength(proposalText.length());
                configurable.setAutoInsertable(false);
                configurable.setSimpleLinkedMode(context.getViewer(), '\t', ' ');
            }
            acceptor.accept(proposal);
        }
        TemplateData data = new TemplateData(model);
        if (data.doCreateProposals()) {
            ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(data.language);
            if (helper != null) {
                String contextTypeId = helper.getId(data.rule);
                ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(data.language);
                TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
                if (contextType != null) {
                    Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
                    while (resolvers.hasNext()) {
                        final TemplateVariableResolver resolver = resolvers.next();
                        IInspectableTemplateVariableResolver inspectableResolver = templateVariableResolverRegistry.toInspectableResolver(resolver);
                        if (inspectableResolver != null) {
                            if (!inspectableResolver.hasMandatoryParameters()) {
                                String type = resolver.getType();
                                StyledString displayString = new StyledString(type, StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
                                acceptor.accept(createCompletionProposal(type, displayString, null, context));
                            }
                            String proposalText = "variable:" + resolver.getType();
                            StyledString displayText = new StyledString("variable").append(":" + resolver.getType(), StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
                            if (inspectableResolver.hasMandatoryParameters())
                                proposalText = proposalText + "()";
                            Builder builder = context.copy();
                            PrefixMatcher newMatcher = new PrefixMatcher() {

                                @Override
                                public boolean isCandidateMatchingPrefix(String name, String prefix) {
                                    return context.getMatcher().isCandidateMatchingPrefix(name, prefix) || context.getMatcher().isCandidateMatchingPrefix("variable:" + resolver.getType(), prefix) || context.getMatcher().isCandidateMatchingPrefix(resolver.getType(), prefix);
                                }
                            };
                            builder.setMatcher(newMatcher);
                            ContentAssistContext myContext = builder.toContext();
                            ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, myContext);
                            if (proposal instanceof ConfigurableCompletionProposal) {
                                ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
                                configurable.setSelectionStart(configurable.getReplacementOffset());
                                configurable.setSelectionLength("variable".length());
                                configurable.setAutoInsertable(false);
                                if (inspectableResolver.hasMandatoryParameters()) {
                                    configurable.setCursorPosition(proposalText.length() - 1);
                                }
                                configurable.setSimpleLinkedMode(myContext.getViewer(), '\t');
                            }
                            acceptor.accept(proposal);
                        } else {
                            String type = resolver.getType();
                            StyledString displayString = new StyledString(type, StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
                            acceptor.accept(createCompletionProposal(type, displayString, null, context));
                        }
                    }
                }
            }
            if (data.template.getBody() != null) {
                for (Variable variable : Iterables.filter(data.template.getBody().getParts(), Variable.class)) {
                    if (variable != model && variable.getName() != null) {
                        String proposalText = variable.getName();
                        StyledString displayText = new StyledString(proposalText).append(" - existing variable", StyledString.QUALIFIER_STYLER);
                        if (variable.getType() != null)
                            displayText = displayText.append(" of type " + variable.getType(), StyledString.QUALIFIER_STYLER);
                        ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
                        acceptor.accept(proposal);
                    }
                }
            }
        }
    }
}
Also used : PrefixMatcher(org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher) Variable(org.eclipse.xtext.ui.codetemplates.templates.Variable) Builder(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext.Builder) ContextTypeRegistry(org.eclipse.jface.text.templates.ContextTypeRegistry) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) ContextTypeIdHelper(org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Aggregations

ConfigurableCompletionProposal (org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal)35 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)21 StyledString (org.eclipse.jface.viewers.StyledString)16 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)8 PrefixMatcher (org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher)5 ContentAssistContext (org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext)4 EObject (org.eclipse.emf.ecore.EObject)3 QualifiedName (org.eclipse.xtext.naming.QualifiedName)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 EClass (org.eclipse.emf.ecore.EClass)2 EClassifier (org.eclipse.emf.ecore.EClassifier)2 EPackage (org.eclipse.emf.ecore.EPackage)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 ContextTypeRegistry (org.eclipse.jface.text.templates.ContextTypeRegistry)2 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)2 TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)2 AbstractRule (org.eclipse.xtext.AbstractRule)2 EnumRule (org.eclipse.xtext.EnumRule)2 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)2 IScope (org.eclipse.xtext.scoping.IScope)2