Search in sources :

Example 1 with Builder

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

the class CodetemplatesProposalProvider method createNestedProposals.

public void createNestedProposals(ContentAssistContext[] contexts, ITextViewer originalViewer, final ICompletionProposalAcceptor acceptor, TemplateData data) {
    for (ContentAssistContext context : contexts) {
        Builder builder = context.copy();
        builder.setViewer(originalViewer);
        ContentAssistContext myContext = builder.toContext();
        IFollowElementAcceptor selector = createNestedSelector(myContext, acceptor, data);
        for (AbstractElement element : myContext.getFirstSetGrammarElements()) {
            selector.accept(element);
        }
    }
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) Builder(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext.Builder) IFollowElementAcceptor(org.eclipse.xtext.ui.editor.contentassist.IFollowElementAcceptor)

Example 2 with Builder

use of org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext.Builder 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)

Example 3 with Builder

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

the class JdtTypesProposalProvider method searchAndCreateProposals.

protected void searchAndCreateProposals(IJavaSearchScope scope, final ICompletionProposalFactory proposalFactory, ContentAssistContext context, EReference typeReference, final Filter filter, final IValueConverter<String> valueConverter, final ICompletionProposalAcceptor acceptor) throws JavaModelException {
    String prefix = context.getPrefix();
    List<String> split = Strings.split(prefix, '.');
    char[] typeName = null;
    char[] packageName = null;
    if (prefix.length() > 0 && !split.isEmpty()) {
        CharMatcher dotMatcher = CharMatcher.is('.');
        if (Character.isUpperCase(split.get(split.size() - 1).charAt(0))) {
            typeName = split.get(split.size() - 1).toCharArray();
            if (split.size() > 1)
                packageName = ("*" + dotMatcher.replaceFrom(prefix.substring(0, prefix.length() - (typeName.length + 1)), "*.") + "*").toCharArray();
        } else {
            if (prefix.endsWith("."))
                prefix = prefix.substring(0, prefix.length() - 1);
            packageName = ("*" + dotMatcher.replaceFrom(prefix, "*.") + "*").toCharArray();
        }
    }
    IScope typeScope = null;
    if (context.getCurrentModel() != null) {
        typeScope = scopeProvider.getScope(context.getCurrentModel(), typeReference);
    }
    final IReplacementTextApplier textApplier = createTextApplier(context, typeScope, qualifiedNameConverter, valueConverter);
    final ICompletionProposalAcceptor scopeAware = textApplier != null ? new ICompletionProposalAcceptor.Delegate(acceptor) {

        @Override
        public void accept(ICompletionProposal proposal) {
            if (proposal instanceof ConfigurableCompletionProposal) {
                ((ConfigurableCompletionProposal) proposal).setTextApplier(textApplier);
            }
            super.accept(proposal);
        }
    } : acceptor;
    Builder contextBuilder = context.copy();
    final PrefixMatcher original = context.getMatcher();
    contextBuilder.setMatcher(new PrefixMatcher() {

        @Override
        public boolean isCandidateMatchingPrefix(String name, String prefix) {
            if (original.isCandidateMatchingPrefix(name, prefix))
                return true;
            String nameWithoutDollars = name.replace('$', '.');
            String prefixWithoutDollars = prefix.replace('$', '.');
            final boolean nameOrPrefixHasDollars = (nameWithoutDollars != name) || (prefixWithoutDollars != prefix);
            if (nameOrPrefixHasDollars && original.isCandidateMatchingPrefix(nameWithoutDollars, prefixWithoutDollars))
                return true;
            String sub = nameWithoutDollars;
            int delimiter = sub.indexOf('.');
            while (delimiter != -1) {
                sub = sub.substring(delimiter + 1);
                delimiter = sub.indexOf('.');
                if (delimiter == -1 || prefixWithoutDollars.length() > 0 && Character.isLowerCase(prefixWithoutDollars.charAt(0))) {
                    if (original.isCandidateMatchingPrefix(sub, prefixWithoutDollars))
                        return true;
                }
            }
            return false;
        }
    });
    final ContentAssistContext myContext = contextBuilder.toContext();
    final IJvmTypeProvider jvmTypeProvider = jdtTypeProviderFactory.findOrCreateTypeProvider(context.getResource().getResourceSet());
    final Set<String> filteredTypeNames = getDirtyTypeNames();
    final Filter dirtyTypenameFilter = new ITypesProposalProvider.Filter() {

        @Override
        public boolean accept(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
            if (path == null || path.endsWith(".class") || path.endsWith(".java")) {
                // Java index match
                String identifier = getIdentifier(packageName, simpleTypeName, enclosingTypeNames);
                if (filteredTypeNames.contains(identifier)) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public int getSearchFor() {
            return filter.getSearchFor();
        }
    };
    BasicSearchEngine searchEngine = new BasicSearchEngine();
    searchEngine.searchAllTypeNames(packageName, SearchPattern.R_PATTERN_MATCH, typeName, SearchPattern.R_PREFIX_MATCH | SearchPattern.R_CAMELCASE_MATCH, filter.getSearchFor(), scope, new IRestrictedAccessTypeRequestor() {

        @Override
        public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) {
            if (dirtyTypenameFilter.accept(modifiers, packageName, simpleTypeName, enclosingTypeNames, path) && filter.accept(modifiers, packageName, simpleTypeName, enclosingTypeNames, path) && (!checkAccessRestriction() || (access == null || access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter()))) {
                StringBuilder fqName = new StringBuilder(packageName.length + simpleTypeName.length + 1);
                if (packageName.length != 0) {
                    fqName.append(packageName);
                    fqName.append('.');
                }
                for (char[] enclosingType : enclosingTypeNames) {
                    /*
								 * the JDT index sometimes yields enclosingTypeNames in the form
								 * char[][] { { Outer$Middle } }
								 * rather than
								 * char[][] { { Outer }, { Middle } }
								 * thus we create the fqName as the binary name and post process the proposal later on
								 */
                    fqName.append(enclosingType);
                    fqName.append('$');
                }
                fqName.append(simpleTypeName);
                String fqNameAsString = fqName.toString();
                createTypeProposal(fqNameAsString, modifiers, enclosingTypeNames.length > 0, proposalFactory, myContext, scopeAware, jvmTypeProvider, valueConverter);
            }
        }
    }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor() {

        @Override
        public boolean isCanceled() {
            return !acceptor.canAcceptMoreProposals();
        }
    });
    if (acceptor.canAcceptMoreProposals()) {
        Iterable<IEObjectDescription> allDirtyTypes = dirtyStateManager.getExportedObjectsByType(TypesPackage.Literals.JVM_TYPE);
        for (IEObjectDescription description : allDirtyTypes) {
            QualifiedName qualifiedName = description.getQualifiedName();
            final int modifiers = getDirtyStateModifiers(context, description);
            if (filter.accept(modifiers, qualifiedName.skipLast(1).toString().toCharArray(), qualifiedName.getLastSegment().toCharArray(), new char[0][0], description.getEObjectURI().toPlatformString(true))) {
                String fqName = description.getQualifiedName().toString();
                createTypeProposal(fqName, modifiers, fqName.indexOf('$') > 0, proposalFactory, myContext, scopeAware, jvmTypeProvider, valueConverter);
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) AccessRestriction(org.eclipse.jdt.internal.compiler.env.AccessRestriction) IReplacementTextApplier(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal.IReplacementTextApplier) BasicSearchEngine(org.eclipse.jdt.internal.core.search.BasicSearchEngine) Builder(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext.Builder) StyledString(org.eclipse.jface.viewers.StyledString) ICompletionProposalAcceptor(org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor) IJvmTypeProvider(org.eclipse.xtext.common.types.access.IJvmTypeProvider) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IScope(org.eclipse.xtext.scoping.IScope) PrefixMatcher(org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher) IRestrictedAccessTypeRequestor(org.eclipse.jdt.internal.core.search.IRestrictedAccessTypeRequestor) CharMatcher(com.google.common.base.CharMatcher) QualifiedName(org.eclipse.xtext.naming.QualifiedName) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext)

Aggregations

ContentAssistContext (org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext)3 Builder (org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext.Builder)3 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 StyledString (org.eclipse.jface.viewers.StyledString)2 ConfigurableCompletionProposal (org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal)2 PrefixMatcher (org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher)2 CharMatcher (com.google.common.base.CharMatcher)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 AccessRestriction (org.eclipse.jdt.internal.compiler.env.AccessRestriction)1 BasicSearchEngine (org.eclipse.jdt.internal.core.search.BasicSearchEngine)1 IRestrictedAccessTypeRequestor (org.eclipse.jdt.internal.core.search.IRestrictedAccessTypeRequestor)1 ContextTypeRegistry (org.eclipse.jface.text.templates.ContextTypeRegistry)1 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)1 TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)1 AbstractElement (org.eclipse.xtext.AbstractElement)1 IJvmTypeProvider (org.eclipse.xtext.common.types.access.IJvmTypeProvider)1 QualifiedName (org.eclipse.xtext.naming.QualifiedName)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1 IScope (org.eclipse.xtext.scoping.IScope)1 Variable (org.eclipse.xtext.ui.codetemplates.templates.Variable)1