Search in sources :

Example 11 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project lombok by rzwitserloot.

the class PatchExtensionMethodCompletionProposal method getJavaCompletionProposals.

public static IJavaCompletionProposal[] getJavaCompletionProposals(IJavaCompletionProposal[] javaCompletionProposals, CompletionProposalCollector completionProposalCollector) {
    List<IJavaCompletionProposal> proposals = new ArrayList<IJavaCompletionProposal>(Arrays.asList(javaCompletionProposals));
    if (canExtendCodeAssist()) {
        for (Extension extension : getExtensionMethods(completionProposalCollector)) {
            for (MethodBinding method : extension.extensionMethods) {
                if (!isMatchingProposal(method, completionProposalCollector)) {
                    continue;
                }
                ExtensionMethodCompletionProposal newProposal = new ExtensionMethodCompletionProposal(0);
                copyNameLookupAndCompletionEngine(completionProposalCollector, newProposal);
                ASTNode node = getAssistNode(completionProposalCollector);
                newProposal.setMethodBinding(method, node);
                createAndAddJavaCompletionProposal(completionProposalCollector, newProposal, proposals);
            }
        }
    }
    return proposals.toArray(new IJavaCompletionProposal[0]);
}
Also used : Extension(lombok.eclipse.agent.PatchExtensionMethod.Extension) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)

Example 12 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project jbosstools-base by jbosstools.

the class MarkerAnnotationInfo method getProposals.

private List<ICompletionProposal> getProposals(TemporaryAnnotation annotation, Position position) {
    List<ICompletionProposal> allProposals = new ArrayList<ICompletionProposal>();
    List<IQuickAssistProcessor> processors = new ArrayList<IQuickAssistProcessor>();
    boolean isJBTAnnotation = isJBTAnnotation(annotation);
    // get all relevant quick fixes for this annotation
    if (QuickFixManager.getInstance().hasProposals(annotation, position)) {
        if (annotation.getText().startsWith(UNKNOWN_TAG) || annotation.getText().startsWith(MISSING_ATTRIBUTE)) {
            annotation.setAdditionalFixInfo(viewer.getDocument());
        }
        List<IJavaCompletionProposal> proposals = QuickFixManager.getInstance().getProposals(annotation, position);
        allProposals.addAll(proposals);
    }
    if (!isJBTAnnotation) {
        Object o = annotation.getAdditionalFixInfo();
        if (o instanceof IQuickAssistProcessor) {
            processors.add((IQuickAssistProcessor) o);
        }
        QuickFixRegistry registry = QuickFixRegistry.getInstance();
        processors.addAll(Arrays.asList(registry.getQuickFixProcessors(annotation)));
        // set up context
        Map attributes = annotation.getAttributes();
        StructuredTextInvocationContext sseContext = new StructuredTextInvocationContext(viewer, position.getOffset(), position.getLength(), attributes);
        ICompletionProposal[] compoundQuickAssistProcessorProposals = fCompoundQuickAssistProcessor.computeQuickAssistProposals(sseContext);
        if (compoundQuickAssistProcessorProposals != null) {
            for (ICompletionProposal proposal : compoundQuickAssistProcessorProposals) {
                allProposals.add(proposal);
            }
        }
        // call each processor
        for (int i = 0; i < processors.size(); ++i) {
            List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
            collectProposals((IQuickAssistProcessor) processors.get(i), annotation, sseContext, proposals);
            allProposals.addAll(proposals);
        }
    }
    return allProposals;
}
Also used : QuickFixRegistry(org.eclipse.wst.sse.ui.internal.correction.QuickFixRegistry) ArrayList(java.util.ArrayList) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) IQuickAssistProcessor(org.eclipse.jface.text.quickassist.IQuickAssistProcessor) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Map(java.util.Map) StructuredTextInvocationContext(org.eclipse.wst.sse.ui.StructuredTextInvocationContext)

Example 13 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project gap-eclipse-plugins by gayanper.

the class StringFormatAsistProcessor method createProposalForStringFormat.

private IJavaCompletionProposal[] createProposalForStringFormat(IInvocationContext context, AST ast, InfixExpression oldInfixExpression) {
    final ICompilationUnit cu = context.getCompilationUnit();
    final ASTRewrite rewrite = ASTRewrite.create(ast);
    List<Expression> operands = new ArrayList<>();
    collectInfixPlusOperands(oldInfixExpression, operands);
    List<Expression> formatArguments = new ArrayList<>();
    StringBuilder formatString = new StringBuilder();
    for (Iterator<Expression> iterator = operands.iterator(); iterator.hasNext(); ) {
        Expression operand = iterator.next();
        if (operand instanceof StringLiteral) {
            String value = ((StringLiteral) operand).getEscapedValue();
            value = value.substring(1, value.length() - 1);
            value = value.replaceAll("'", "''");
            formatString.append(value);
        } else {
            formatString.append("%s");
            formatArguments.add((Expression) rewrite.createCopyTarget(operand));
        }
    }
    if (formatArguments.size() == 0) {
        return null;
    }
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal("Use 'String.format' for string concatenation", cu, rewrite, 0, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
    proposal.setCommandId(CONVERT_TO_STRING_FORMAT_ID);
    MethodInvocation formatInvocation = ast.newMethodInvocation();
    formatInvocation.setExpression(ast.newSimpleName("String"));
    formatInvocation.setName(ast.newSimpleName("format"));
    @SuppressWarnings("unchecked") List<Expression> arguments = formatInvocation.arguments();
    StringLiteral formatStringArgument = ast.newStringLiteral();
    // $NON-NLS-1$ //$NON-NLS-2$
    formatStringArgument.setEscapedValue("\"" + formatString + "\"");
    arguments.add(formatStringArgument);
    for (Iterator<Expression> iterator = formatArguments.iterator(); iterator.hasNext(); ) {
        arguments.add(iterator.next());
    }
    rewrite.replace(oldInfixExpression, formatInvocation, null);
    return new IJavaCompletionProposal[] { proposal };
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 14 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project grails-ide by spring-attic.

the class BeansJavaCompletionUtils method addClassValueProposals.

/**
 * Add class and package content assist proposals that match the given
 * <code>prefix</code>.
 */
public static void addClassValueProposals(IContentAssistContext context, IContentAssistProposalRecorder recorder, int flags) {
    String prefix = context.getMatchString();
    if (prefix == null || prefix.length() == 0) {
        return;
    }
    try {
        ICompilationUnit unit = createSourceCompilationUnit(context.getFile(), prefix);
        char enclosingChar = (prefix.lastIndexOf('$') > 0 ? '$' : '.');
        prefix = prefix.replace('$', '.');
        // we manually add the private inner classes if possible
        if (prefix.lastIndexOf('.') > 0) {
            String rootClass = prefix.substring(0, prefix.lastIndexOf('.'));
            IType type = JdtUtils.getJavaType(context.getFile().getProject(), rootClass);
            if (type != null) {
                for (IType innerType : type.getTypes()) {
                    if (Flags.isPrivate(innerType.getFlags()) && innerType.getFullyQualifiedName('.').startsWith(prefix)) {
                        recorder.recordProposal(JAVA_LABEL_PROVIDER.getImage(innerType), 10, JAVA_LABEL_PROVIDER.getText(innerType), innerType.getFullyQualifiedName(enclosingChar), innerType);
                    }
                }
            }
        }
        String sourceStart = CLASS_SOURCE_START + prefix;
        String packageName = null;
        int dot = prefix.lastIndexOf('.');
        if (dot > -1) {
            packageName = prefix.substring(0, dot);
            sourceStart = "package " + packageName + ";\n" + sourceStart;
        }
        String source = sourceStart + CLASS_SOURCE_END;
        setContents(unit, source);
        BeansJavaCompletionProposalCollector collector = new BeansJavaCompletionProposalCollector(unit, flags);
        unit.codeComplete(sourceStart.length(), collector, DefaultWorkingCopyOwner.PRIMARY);
        IJavaCompletionProposal[] props = collector.getJavaCompletionProposals();
        ICompletionProposal[] proposals = order(props);
        for (ICompletionProposal comProposal : proposals) {
            processJavaCompletionProposal(recorder, comProposal, packageName, enclosingChar);
        }
    } catch (Exception e) {
    // do nothing
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) IType(org.eclipse.jdt.core.IType)

Example 15 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project che by eclipse.

the class JavaTypeCompletionProposalComputer method createTypeProposal.

private IJavaCompletionProposal createTypeProposal(int relevance, String fullyQualifiedType, JavaContentAssistInvocationContext context) throws JavaModelException {
    IType type = context.getCompilationUnit().getJavaProject().findType(fullyQualifiedType);
    if (type == null)
        return null;
    CompletionProposal proposal = CompletionProposal.create(CompletionProposal.TYPE_REF, context.getInvocationOffset());
    proposal.setCompletion(fullyQualifiedType.toCharArray());
    proposal.setDeclarationSignature(type.getPackageFragment().getElementName().toCharArray());
    proposal.setFlags(type.getFlags());
    proposal.setRelevance(relevance);
    proposal.setReplaceRange(context.getInvocationOffset(), context.getInvocationOffset());
    proposal.setSignature(Signature.createTypeSignature(fullyQualifiedType, true).toCharArray());
    if (shouldProposeGenerics(context.getProject()))
        return new LazyGenericTypeProposal(proposal, context);
    else
        return new LazyJavaTypeCompletionProposal(proposal, context);
}
Also used : ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) IType(org.eclipse.jdt.core.IType)

Aggregations

IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)30 ArrayList (java.util.ArrayList)13 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)5 CoreException (org.eclipse.core.runtime.CoreException)4 CompletionContext (org.eclipse.jdt.core.CompletionContext)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IType (org.eclipse.jdt.core.IType)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 IProblemLocation (org.eclipse.jdt.ui.text.java.IProblemLocation)4 IDocument (org.eclipse.jface.text.IDocument)4 IContextInformation (org.eclipse.jface.text.contentassist.IContextInformation)4 Image (org.eclipse.swt.graphics.Image)4 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 ServiceContainer (com.liferay.ide.project.core.modules.ServiceContainer)2 List (java.util.List)2 Map (java.util.Map)2 IFile (org.eclipse.core.resources.IFile)2