Search in sources :

Example 1 with ICompletionProposalAcceptor

use of org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor in project dsl-devkit by dsldevkit.

the class CheckProposalProvider method completeContextVariable_Type.

/**
 * {@inheritDoc}
 */
@Override
public // CHECKSTYLE:OFF
void completeContextVariable_Type(final EObject model, final Assignment assignment, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    // CHECKSTYLE:ON
    completeJavaTypes(context, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, true, qualifiedNameValueConverter, new Filter() {

        // @Format-Off
        private final Set<String> resourceEClassNames = model.eResource().getResourceSet().getResources().stream().map(resource -> resource.getContents()).flatMap(List::stream).filter(obj -> obj instanceof EPackage).map(obj -> ((EPackage) obj).getEClassifiers()).flatMap(List::stream).map(eClass -> eClass.getEPackage().getName() + '.' + eClass.getName()).collect(Collectors.toSet());

        // @Format-On
        @Override
        public int getSearchFor() {
            return IJavaSearchConstants.INTERFACE;
        }

        @Override
        public boolean accept(final int modifiers, final char[] packageName, final char[] simpleTypeName, final char[][] enclosingTypeNames, final String path) {
            String fullPackageName = new String(packageName);
            String simpleClassName = new String(simpleTypeName);
            String ePackageName = fullPackageName.substring(fullPackageName.lastIndexOf('.') + 1);
            return REGISTERED_ECLASS_NAMES.contains(fullPackageName + '.' + simpleClassName) || resourceEClassNames.contains(ePackageName + '.' + simpleClassName);
        }
    }, acceptor);
    shortCircuit();
}
Also used : CheckImages(com.avaloq.tools.ddk.check.ui.labeling.CheckImages) Inject(com.google.inject.Inject) CheckResourceUtil(com.avaloq.tools.ddk.check.ui.util.CheckResourceUtil) XbaseQualifiedNameValueConverter(org.eclipse.xtext.xbase.conversion.XbaseQualifiedNameValueConverter) TypesPackage(org.eclipse.xtext.common.types.TypesPackage) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) TerminalsProposalProvider(org.eclipse.xtext.common.ui.contentassist.TerminalsProposalProvider) IJavaSearchConstants(org.eclipse.jdt.core.search.IJavaSearchConstants) ICompletionProposalAcceptor(org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor) Assignment(org.eclipse.xtext.Assignment) ImmutableSet(com.google.common.collect.ImmutableSet) NLS(org.eclipse.osgi.util.NLS) Strings(org.eclipse.xtext.util.Strings) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) Collectors(java.util.stream.Collectors) EPackage(org.eclipse.emf.ecore.EPackage) RuleCall(org.eclipse.xtext.RuleCall) Objects(java.util.Objects) Keyword(org.eclipse.xtext.Keyword) CheckPackage(com.avaloq.tools.ddk.check.check.CheckPackage) List(java.util.List) Stream(java.util.stream.Stream) Filter(org.eclipse.xtext.common.types.xtext.ui.ITypesProposalProvider.Filter) CheckConstants(com.avaloq.tools.ddk.check.CheckConstants) GrammarUtil(org.eclipse.xtext.GrammarUtil) ConcurrentModificationException(java.util.ConcurrentModificationException) Filter(org.eclipse.xtext.common.types.xtext.ui.ITypesProposalProvider.Filter) List(java.util.List) EPackage(org.eclipse.emf.ecore.EPackage)

Example 2 with ICompletionProposalAcceptor

use of org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor in project statecharts by Yakindu.

the class STextProposalProvider method complete_HEX.

@Override
public void complete_HEX(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    ICompletionProposalAcceptor priorityOptimizer = getCustomAcceptor(model, "integer", acceptor);
    String proposalText = "0x1";
    ICompletionProposal proposal = createCompletionProposal(proposalText, proposalText + " - " + ruleCall.getRule().getName(), null, context);
    if (proposal instanceof ConfigurableCompletionProposal) {
        ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
        configurable.setSelectionStart(configurable.getReplacementOffset() + 2);
        configurable.setSelectionLength(proposalText.length() - 2);
        configurable.setAutoInsertable(false);
        configurable.setSimpleLinkedMode(context.getViewer(), '\t', ' ');
    }
    priorityOptimizer.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) ICompletionProposalAcceptor(org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)

Example 3 with ICompletionProposalAcceptor

use of org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor in project statecharts by Yakindu.

the class STextProposalProvider method complete_DOUBLE.

@Override
public void complete_DOUBLE(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    ICompletionProposalAcceptor priorityOptimizer = getCustomAcceptor(model, "real", acceptor);
    String proposalText = "0.1";
    ICompletionProposal proposal = createCompletionProposal(proposalText, proposalText + " - " + ruleCall.getRule().getName(), null, context);
    priorityOptimizer.accept(proposal);
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) StyledString(org.eclipse.jface.viewers.StyledString) ICompletionProposalAcceptor(org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)

Example 4 with ICompletionProposalAcceptor

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

the class CodetemplatesProposalProvider method complete_Literal.

@Override
public void complete_Literal(EObject model, RuleCall ruleCall, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    if ((mode & NESTED) != 0) {
        final TemplateData data = new TemplateData(model);
        if (data.doCreateProposals()) {
            final EvaluatedTemplate evaluatedTemplate = new EvaluatedTemplate(data.template);
            temporaryResourceProvider.useTemporaryResource(data.template.eResource().getResourceSet(), data.language, data.rule, evaluatedTemplate.getMappedString(), new IUnitOfWork.Void<XtextResource>() {

                @Override
                public void process(XtextResource resource) throws Exception {
                    IPartialEditingContentAssistContextFactory delegateFactory = languageRegistry.getPartialContentAssistContextFactory(data.language);
                    delegateFactory.initializeFor(data.rule);
                    String mappedInput = evaluatedTemplate.getMappedString();
                    int mappedOffset = Math.min(mappedInput.length(), evaluatedTemplate.getMappedOffset(context.getOffset()));
                    DummyDocument document = new DummyDocument(mappedInput);
                    DummyTextViewer dummyViewer = new DummyTextViewer(TextSelection.emptySelection(), document);
                    ContentAssistContext[] contexts = delegateFactory.create(dummyViewer, mappedOffset, resource);
                    ICompletionProposalAcceptor mappingAcceptor = new ProjectionAwareProposalAcceptor(acceptor, evaluatedTemplate);
                    createNestedProposals(contexts, context.getViewer(), mappingAcceptor, data);
                }
            });
        }
    }
}
Also used : XtextResource(org.eclipse.xtext.resource.XtextResource) StyledString(org.eclipse.jface.viewers.StyledString) ICompletionProposalAcceptor(org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) EvaluatedTemplate(org.eclipse.xtext.ui.codetemplates.ui.evaluator.EvaluatedTemplate) IPartialEditingContentAssistContextFactory(org.eclipse.xtext.ui.codetemplates.ui.partialEditing.IPartialEditingContentAssistContextFactory) ProjectionAwareProposalAcceptor(org.eclipse.xtext.ui.codetemplates.ui.projectedEditing.ProjectionAwareProposalAcceptor)

Example 5 with ICompletionProposalAcceptor

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

ICompletionProposalAcceptor (org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)6 StyledString (org.eclipse.jface.viewers.StyledString)5 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)4 ConfigurableCompletionProposal (org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal)2 ContentAssistContext (org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext)2 CheckConstants (com.avaloq.tools.ddk.check.CheckConstants)1 CheckPackage (com.avaloq.tools.ddk.check.check.CheckPackage)1 CheckImages (com.avaloq.tools.ddk.check.ui.labeling.CheckImages)1 CheckResourceUtil (com.avaloq.tools.ddk.check.ui.util.CheckResourceUtil)1 CharMatcher (com.google.common.base.CharMatcher)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Inject (com.google.inject.Inject)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 EObject (org.eclipse.emf.ecore.EObject)1