Example usage for org.eclipse.jdt.internal.codeassist CompletionEngine CompletionEngine

List of usage examples for org.eclipse.jdt.internal.codeassist CompletionEngine CompletionEngine

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.codeassist CompletionEngine CompletionEngine.

Prototype

public CompletionEngine(SearchableEnvironment nameEnvironment, CompletionRequestor requestor, Map settings,
        IJavaProject javaProject, WorkingCopyOwner owner, IProgressMonitor monitor) 

Source Link

Document

The CompletionEngine is responsible for computing source completions.

Usage

From source file:org.codehaus.groovy.eclipse.codeassist.processors.GroovyProposalTypeSearchRequestor.java

License:Apache License

private ICompletionProposal proposeConstructor(char[] simpleTypeName, int parameterCount, char[] signature,
        char[][] parameterTypes, char[][] parameterNames, int modifiers, char[] packageName, int typeModifiers,
        int accessibility, char[] typeName, char[] fullyQualifiedName, boolean isQualified, int extraFlags) {

    // only show context information and only for methods
    // that exactly match the name. This happens when we are at the
    // start of an argument or an open paren
    String simpleTypeNameStr = String.valueOf(simpleTypeName);
    String fullyQualifiedNameStr = String.valueOf(fullyQualifiedName);
    if (contextOnly && !completionExpression.equals(simpleTypeNameStr)
            && !completionExpression.equals(fullyQualifiedNameStr)) {
        return null;
    }/*from  www.j av a 2  s .  c  om*/

    char[] typeCompletion;
    if (isQualified) {
        typeCompletion = fullyQualifiedName;
        if (packageName == null || packageName.length == 0) {
            typeCompletion = simpleTypeName;
        }
    } else {
        typeCompletion = simpleTypeName;
    }

    float relevanceMultiplier = 1;
    relevanceMultiplier += accessibility == IAccessRule.K_ACCESSIBLE ? 2 : -1;
    relevanceMultiplier += computeRelevanceForCaseMatching(this.completionExpression.toCharArray(),
            simpleTypeName);

    int augmentedModifiers = modifiers;
    if (Flags.isDeprecated(typeModifiers)) {
        augmentedModifiers |= Flags.AccDeprecated;
    }

    if (parameterCount == -1) {
        // default constructor
        parameterNames = CharOperation.NO_CHAR_CHAR;
        parameterTypes = CharOperation.NO_CHAR_CHAR;
    } else {
        int parameterNamesLength = parameterNames == null ? 0 : parameterNames.length;
        if (parameterCount != parameterNamesLength) {
            parameterNames = null;
        }
    }

    GroovyCompletionProposal proposal = createProposal(
            contextOnly ? CompletionProposal.METHOD_REF : CompletionProposal.CONSTRUCTOR_INVOCATION,
            offset - 1);
    char[] declarationSignature = CompletionEngine.createNonGenericTypeSignature(packageName, typeName);
    proposal.setDeclarationSignature(declarationSignature);

    if (contextOnly) {
        proposal.setReplaceRange(actualCompletionPosition, actualCompletionPosition);
        proposal.setTokenRange(actualCompletionPosition, actualCompletionPosition);
        proposal.setCompletion(CharOperation.NO_CHAR);
    } else {
        // otherwise this is a normal constructor proposal
        proposal.setCompletion(this.completionExpression.toCharArray());
        // looks strange, but this is just copying similar code in
        // CompletionEngine.java
        proposal.setReplaceRange(this.offset + this.replaceLength, this.offset + this.replaceLength);
        proposal.setTokenRange(this.offset, this.actualCompletionPosition);
        proposal.setCompletion(new char[] { '(', ')' });

        // provides the import statement
        GroovyCompletionProposal typeProposal = createTypeProposal(packageName, typeModifiers, accessibility,
                typeName, fullyQualifiedName, isQualified, typeCompletion, augmentedModifiers,
                declarationSignature);
        proposal.setRequiredProposals(new CompletionProposal[] { typeProposal });
    }

    if (signature == null) {
        proposal.setSignature(createConstructorSignature(parameterTypes, isQualified));
    } else {
        char[] copy = new char[signature.length];
        System.arraycopy(signature, 0, copy, 0, copy.length);
        CharOperation.replace(copy, '/', '.');
        proposal.setSignature(copy);
    }

    if (parameterNames != null) {
        proposal.setParameterNames(parameterNames);
    } else {
        proposal.setHasNoParameterNamesFromIndex(true);
        if (mockEngine == null) {
            // used for caching types only
            mockEngine = new CompletionEngine(null, new CompletionRequestor() {

                @Override
                public void accept(CompletionProposal proposal) {

                }
            }, null, null, null, null);
        }
        proposal.setCompletionEngine(mockEngine);
    }

    proposal.setDeclarationPackageName(packageName);
    proposal.setDeclarationTypeName(simpleTypeName);
    proposal.setParameterTypeNames(parameterTypes);
    // proposal.setParameterPackageNames(); not right
    proposal.setName(simpleTypeName);

    proposal.setIsContructor(true);

    proposal.setRelevance(Relevance.MEDIUM_HIGH.getRelevance(relevanceMultiplier));

    proposal.setFlags(augmentedModifiers);

    proposal.setTypeName(simpleTypeName);
    proposal.setAccessibility(typeModifiers);
    proposal.setPackageName(packageName);

    LazyJavaCompletionProposal lazyProposal = new GroovyJavaMethodCompletionProposal(proposal, javaContext,
            getProposalOptions());
    lazyProposal.setRelevance(proposal.getRelevance());
    if (proposal.hasParameters()) {
        lazyProposal.setTriggerCharacters(ProposalUtils.METHOD_WITH_ARGUMENTS_TRIGGERS);
    } else {
        lazyProposal.setTriggerCharacters(ProposalUtils.METHOD_TRIGGERS);
    }
    ImportRewrite r = groovyRewriter.getImportRewrite(monitor);
    if (r != null) {
        ReflectionUtils.setPrivateField(LazyJavaTypeCompletionProposal.class, "fImportRewrite", lazyProposal,
                r);
    }
    if (contextOnly) {
        ((GroovyJavaMethodCompletionProposal) lazyProposal).contextOnly();
    }

    return lazyProposal;
}

From source file:org.eclipse.ajdt.core.javaelements.AJCompilationUnit.java

License:Open Source License

/**
* this method is a copy of {@link Openable#codeComplete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit, org.eclipse.jdt.internal.compiler.env.ICompilationUnit, int, CompletionRequestor, WorkingCopyOwner, ITypeRoot)}
* The only change is that we need to create an {@link ITDAwareNameEnvironment}, not  standard {@link SearchableEnvironment}.
 * /*ww  w  .ja v a 2s .com*/
* @param cu
* @param unitToSkip
* @param position
* @param requestor
* @param owner
* @param typeRoot
* @throws JavaModelException
*/
private void internalCodeComplete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit cu,
        org.eclipse.jdt.internal.compiler.env.ICompilationUnit unitToSkip, int position,
        CompletionRequestor requestor, WorkingCopyOwner owner, ITypeRoot typeRoot,
        /* AJDT 1.7 */
        IProgressMonitor monitor) throws JavaModelException {

    if (requestor == null) {
        throw new IllegalArgumentException("Completion requestor cannot be null"); //$NON-NLS-1$
    }
    PerformanceStats performanceStats = CompletionEngine.PERF
            ? PerformanceStats.getStats(JavaModelManager.COMPLETION_PERF, this)
            : null;
    if (performanceStats != null) {
        performanceStats.startRun(new String(cu.getFileName()) + " at " + position); //$NON-NLS-1$
    }
    IBuffer buffer = getBuffer();
    if (buffer == null) {
        return;
    }
    if (position < -1 || position > buffer.getLength()) {
        throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS));
    }
    JavaProject project = (JavaProject) getJavaProject();
    /* AJDT 1.7 */
    ITDAwareNameEnvironment environment = new ITDAwareNameEnvironment(project, owner, monitor);

    environment.setUnitToSkip(unitToSkip);

    // code complete
    /* AJDT 1.7 */
    CompletionEngine engine = new CompletionEngine(environment, requestor, project.getOptions(true), project,
            owner, monitor);
    engine.complete(cu, position, 0, typeRoot);
    if (performanceStats != null) {
        performanceStats.endRun();
    }
    if (NameLookup.VERBOSE) {
        AJLog.log(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " //$NON-NLS-1$
                + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$
        AJLog.log(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " //$NON-NLS-1$
                + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$
    }

}

From source file:org.eclipse.ajdt.internal.core.contentassist.ContentAssistProvider.java

License:Open Source License

public boolean doContentAssist(ICompilationUnit cu, ICompilationUnit unitToSkip, int position,
        CompletionRequestor requestor, WorkingCopyOwner owner,
        /* AJDT 1.7 */
        ITypeRoot typeRoot, Openable target, IProgressMonitor monitor) throws Exception {
    JavaProject project = (JavaProject) target.getJavaProject();
    if (!AspectJPlugin.isAJProject(project.getProject())) {
        return false;
    }/* ww w  .  j  a v  a  2  s.c  o  m*/
    if (target instanceof AJCompilationUnit) {
        // already handled by the compilation unit
        return false;
    }
    if (!(target instanceof CompilationUnit)) {
        return false;
    }
    IBuffer buffer = target.getBuffer();
    if (buffer == null) {
        return false;
    }

    if (requestor == null) {
        throw new IllegalArgumentException("Completion requestor cannot be null"); //$NON-NLS-1$
    }

    MockCompilationUnit mcu = new MockCompilationUnit((CompilationUnit) target);
    ProposalRequestorWrapper wrapped = new ProposalRequestorWrapper(requestor, mcu, mcu.insertionTable);
    int transformedPos = mcu.translatePositionToFake(position);
    if (transformedPos < -1 || transformedPos > mcu.getContents().length) {
        throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS));
    }

    /* AJDT 1.7 */
    ITDAwareNameEnvironment environment = new ITDAwareNameEnvironment(project, owner, monitor);
    environment.setUnitToSkip(unitToSkip);

    // code complete
    /* AJDT 1.7 */
    CompletionEngine engine = new CompletionEngine(environment, wrapped, project.getOptions(true), project,
            owner, monitor);
    engine.lookupEnvironment = new ITDAwareLookupEnvironment(engine.lookupEnvironment, environment);
    engine.complete(mcu, transformedPos, 0, typeRoot);

    return true;
}