Search in sources :

Example 1 with IReplacementTextApplier

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

the class XbaseProposalProvider method proposeFavoriteStaticFeatures.

/**
 * @since 2.17
 */
protected void proposeFavoriteStaticFeatures(EObject context, ContentAssistContext contentAssistContext, ICompletionProposalAcceptor acceptor, IScope scopedFeatures) {
    Function<IEObjectDescription, ICompletionProposal> proposalFactory = getProposalFactory(getFeatureCallRuleName(), contentAssistContext);
    IReplacementTextApplier textApplier = new FQNImporter(contentAssistContext.getResource(), contentAssistContext.getViewer(), scopedFeatures, qualifiedNameConverter, qualifiedNameValueConverter, importSectionFactory, replaceConverter);
    Function<IEObjectDescription, ICompletionProposal> importAddingProposalFactory = input -> {
        ICompletionProposal proposal = proposalFactory.apply(input);
        if (proposal instanceof ConfigurableCompletionProposal) {
            ConfigurableCompletionProposal castedProposal = (ConfigurableCompletionProposal) proposal;
            // Add textApplier to introduce imports if necessary
            ((ConfigurableCompletionProposal) proposal).setTextApplier(textApplier);
            return castedProposal;
        }
        return proposal;
    };
    getCrossReferenceProposalCreator().lookupCrossReference(scopedFeatures, context, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, acceptor, getFeatureDescriptionPredicate(contentAssistContext), importAddingProposalFactory);
}
Also used : XBasicForLoopExpression(org.eclipse.xtext.xbase.XBasicForLoopExpression) RewritableImportSection(org.eclipse.xtext.xbase.imports.RewritableImportSection) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StyledString(org.eclipse.jface.viewers.StyledString) IValueConverter(org.eclipse.xtext.conversion.IValueConverter) Inject(com.google.inject.Inject) XBinaryOperation(org.eclipse.xtext.xbase.XBinaryOperation) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) XbaseQualifiedNameValueConverter(org.eclipse.xtext.xbase.conversion.XbaseQualifiedNameValueConverter) Logger(org.apache.log4j.Logger) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) OperatorMapping(org.eclipse.xtext.xbase.scoping.featurecalls.OperatorMapping) SyntaxFilteredScopes(org.eclipse.xtext.xbase.scoping.SyntaxFilteredScopes) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) IContextInformation(org.eclipse.jface.text.contentassist.IContextInformation) INode(org.eclipse.xtext.nodemodel.INode) IQualifiedNameConverter(org.eclipse.xtext.naming.IQualifiedNameConverter) CrossReference(org.eclipse.xtext.CrossReference) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) ICompletionProposalAcceptor(org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor) IIdentifiableElementDescription(org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) Function(com.google.common.base.Function) Group(org.eclipse.xtext.Group) XbasePackage(org.eclipse.xtext.xbase.XbasePackage) Strings(org.eclipse.xtext.util.Strings) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) EObject(org.eclipse.emf.ecore.EObject) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XClosure(org.eclipse.xtext.xbase.XClosure) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) Keyword(org.eclipse.xtext.Keyword) List(java.util.List) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) Predicate(com.google.common.base.Predicate) StaticFeatureDescription(org.eclipse.xtext.xbase.scoping.batch.StaticFeatureDescription) GrammarUtil(org.eclipse.xtext.GrammarUtil) IBatchTypeResolver(org.eclipse.xtext.xbase.typesystem.IBatchTypeResolver) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XtypePackage(org.eclipse.xtext.xtype.XtypePackage) XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) QualifiedNameValueConverter(org.eclipse.xtext.conversion.impl.QualifiedNameValueConverter) TypeReferences(org.eclipse.xtext.common.types.util.TypeReferences) Iterables(com.google.common.collect.Iterables) JvmFeature(org.eclipse.xtext.common.types.JvmFeature) FQNImporter(org.eclipse.xtext.xbase.ui.contentassist.ImportingTypesProposalProvider.FQNImporter) AbstractElement(org.eclipse.xtext.AbstractElement) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) StaticExtensionFeatureDescriptionWithImplicitFirstArgument(org.eclipse.xtext.xbase.scoping.batch.StaticExtensionFeatureDescriptionWithImplicitFirstArgument) IReplacementTextApplier(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal.IReplacementTextApplier) JvmType(org.eclipse.xtext.common.types.JvmType) PrefixMatcher(org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) RepeatedContentAssistProcessor(org.eclipse.xtext.ui.editor.contentassist.RepeatedContentAssistProcessor) IDocument(org.eclipse.jface.text.IDocument) Lists(com.google.common.collect.Lists) TypesPackage(org.eclipse.xtext.common.types.TypesPackage) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) BadLocationException(org.eclipse.jface.text.BadLocationException) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) EReference(org.eclipse.emf.ecore.EReference) IJavaSearchConstants(org.eclipse.jdt.core.search.IJavaSearchConstants) XtextResource(org.eclipse.xtext.resource.XtextResource) XExpression(org.eclipse.xtext.xbase.XExpression) Assignment(org.eclipse.xtext.Assignment) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XAssignment(org.eclipse.xtext.xbase.XAssignment) LightweightTypeReferenceFactory(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReferenceFactory) IScope(org.eclipse.xtext.scoping.IScope) IExpressionScope(org.eclipse.xtext.xbase.typesystem.IExpressionScope) Maps(com.google.common.collect.Maps) TypeMatchFilters(org.eclipse.xtext.common.types.xtext.ui.TypeMatchFilters) RuleCall(org.eclipse.xtext.RuleCall) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ReplaceConverter(org.eclipse.xtext.xbase.ui.imports.ReplaceConverter) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) PreferenceConstants(org.eclipse.jdt.ui.PreferenceConstants) ITypesProposalProvider(org.eclipse.xtext.common.types.xtext.ui.ITypesProposalProvider) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) IReplacementTextApplier(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal.IReplacementTextApplier) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) FQNImporter(org.eclipse.xtext.xbase.ui.contentassist.ImportingTypesProposalProvider.FQNImporter) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 2 with IReplacementTextApplier

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

ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 StyledString (org.eclipse.jface.viewers.StyledString)2 QualifiedName (org.eclipse.xtext.naming.QualifiedName)2 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)2 IScope (org.eclipse.xtext.scoping.IScope)2 ConfigurableCompletionProposal (org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal)2 IReplacementTextApplier (org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal.IReplacementTextApplier)2 ContentAssistContext (org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext)2 ICompletionProposalAcceptor (org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)2 PrefixMatcher (org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher)2 CharMatcher (com.google.common.base.CharMatcher)1 Function (com.google.common.base.Function)1 Predicate (com.google.common.base.Predicate)1 FluentIterable (com.google.common.collect.FluentIterable)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Inject (com.google.inject.Inject)1 List (java.util.List)1 Map (java.util.Map)1