Example usage for org.eclipse.jdt.internal.codeassist InternalCompletionContext getCompletionNodeParent

List of usage examples for org.eclipse.jdt.internal.codeassist InternalCompletionContext getCompletionNodeParent

Introduction

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

Prototype

public ASTNode getCompletionNodeParent() 

Source Link

Document

Return the parent AST node of the completion node associated with the current completion.

Usage

From source file:org.eclipse.recommenders.completion.rcp.RecommendersCompletionContext.java

License:Open Source License

@Override
public Optional<ASTNode> getCompletionNodeParent() {
    InternalCompletionContext ctx = doGetCoreContext();
    ASTNode res = ctx != null ? ctx.getCompletionNodeParent() : null;
    return Optional.fromNullable(res);
}

From source file:org.eclipse.recommenders.internal.chain.rcp.TypeBindingAnalyzer.java

License:Open Source License

public static List<Optional<TypeBinding>> resolveBindingsForExpectedTypes(
        final IRecommendersCompletionContext ctx, final Scope scope) {
    final InternalCompletionContext context = (InternalCompletionContext) ctx.getJavaContext().getCoreContext();
    final ASTNode parent = context.getCompletionNodeParent();
    final List<Optional<TypeBinding>> bindings = Lists.newLinkedList();
    if (parent instanceof LocalDeclaration) {
        bindings.add(Optional.fromNullable(((LocalDeclaration) parent).type.resolvedType));
    } else if (parent instanceof ReturnStatement) {
        bindings.add(resolveReturnStatement(context));
    } else if (parent instanceof FieldDeclaration) {
        bindings.add(Optional.fromNullable(((FieldDeclaration) parent).type.resolvedType));
    } else if (parent instanceof Assignment) {
        bindings.add(Optional.fromNullable(((Assignment) parent).resolvedType));
    } else if (isCompletionOnMethodParameter(context)) {
        for (final ITypeName type : ctx.getExpectedTypeNames()) {
            bindings.add(Optional.of(scope.getType(type.getClassName().toCharArray())));
        }/*from ww  w.  j a va 2s . c  o m*/
    } else {
        log(WARNING_CANNOT_USE_AS_PARENT_OF_COMPLETION_LOCATION, parent.getClass());
    }
    return bindings;
}

From source file:org.eclipse.recommenders.internal.chain.rcp.TypeBindingAnalyzer.java

License:Open Source License

private static boolean isCompletionOnMethodParameter(final InternalCompletionContext context) {
    return context.getCompletionNode() instanceof CompletionOnQualifiedAllocationExpression
            || context.getCompletionNode() instanceof CompletionOnMessageSend
            || context.getCompletionNodeParent() instanceof MessageSend;
}

From source file:org.eclipse.recommenders.internal.subwords.rcp.SubwordsSessionProcessor.java

License:Open Source License

@Override
public void initializeContext(IRecommendersCompletionContext recContext) {
    try {// w  w  w  .  j  av a  2 s  .  co m
        minPrefixLengthForTypes = prefs.minPrefixLengthForTypes;
        JavaContentAssistInvocationContext jdtContext = recContext.getJavaContext();
        ICompilationUnit cu = jdtContext.getCompilationUnit();
        int offset = jdtContext.getInvocationOffset();

        // Tricky: we bypass the normal code completion request (triggered at the actual cursor position) by
        // replacing all required keys manually and triggering content assist where we need it:

        // TODO maybe we can get rid of that call by simply using the 'right' collector for the first time? This
        // would save ~5 ms I guess.
        NoProposalCollectingCompletionRequestor collector = new NoProposalCollectingCompletionRequestor();
        cu.codeComplete(offset, collector, new TimeDelimitedProgressMonitor(COMPLETION_TIME_OUT, MILLISECONDS));
        InternalCompletionContext compContext = collector.getCoreContext();
        if (compContext == null) {
            Logs.log(LogMessages.ERROR_COMPLETION_CONTEXT_NOT_COLLECTED, cu.getPath());
            return;
        }

        CORE_CONTEXT.set(jdtContext, compContext);
        recContext.set(CompletionContextKey.INTERNAL_COMPLETIONCONTEXT, compContext);

        String prefix = getPrefix(jdtContext);
        int length = prefix.length();
        recContext.set(CompletionContextKey.COMPLETION_PREFIX, prefix);

        Map<IJavaCompletionProposal, CompletionProposal> baseProposals = Maps.newHashMap();

        recContext.set(JAVA_PROPOSALS, baseProposals);

        ASTNode completionNode = compContext.getCompletionNode();
        ASTNode completionNodeParent = compContext.getCompletionNodeParent();
        SortedSet<Integer> triggerlocations = computeTriggerLocations(offset, completionNode,
                completionNodeParent, length);

        Set<String> sortkeys = Sets.newHashSet();
        for (int trigger : triggerlocations) {
            Map<IJavaCompletionProposal, CompletionProposal> newProposals = getNewProposals(jdtContext,
                    trigger);
            testAndInsertNewProposals(recContext, baseProposals, sortkeys, newProposals);
        }

        if (jdtContext instanceof JavadocContentAssistInvocationContext) {
            ITextViewer viewer = jdtContext.getViewer();
            IEditorPart editor = lookupEditor(cu);
            JavadocContentAssistInvocationContext newJdtContext = new JavadocContentAssistInvocationContext(
                    viewer, offset - length, editor, 0);
            testAndInsertNewProposals(recContext, baseProposals, sortkeys,
                    HtmlTagProposals.computeHtmlTagProposals(htmlTagProposalComputer, newJdtContext));
        }
    } catch (Exception e) {
        Logs.log(LogMessages.ERROR_EXCEPTION_DURING_CODE_COMPLETION, e);
    }
}