Example usage for org.eclipse.jdt.internal.codeassist RelevanceConstants R_EXACT_NAME

List of usage examples for org.eclipse.jdt.internal.codeassist RelevanceConstants R_EXACT_NAME

Introduction

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

Prototype

int R_EXACT_NAME

To view the source code for org.eclipse.jdt.internal.codeassist RelevanceConstants R_EXACT_NAME.

Click Source Link

Usage

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

License:Open Source License

@Override
public void process(final IProcessableProposal proposal) {

    String completionIdentifier = computeCompletionIdentifier(proposal, proposal.getCoreProposal().orNull());
    final String matchingArea = CompletionContexts.getPrefixMatchingArea(completionIdentifier);

    proposal.getProposalProcessorManager().addProcessor(new ProposalProcessor() {

        int[] bestSequence = EMPTY_SEQUENCE;

        String prefix;/*from   www .j  av a 2 s  . c om*/

        @Override
        public boolean isPrefix(String prefix) {
            if (this.prefix != prefix) {
                this.prefix = prefix;
                CompletionProposal coreProposal = proposal.getCoreProposal().orNull();
                if (coreProposal != null && (coreProposal
                        .getKind() == CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER
                        || coreProposal.getKind() == CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER)) {
                    // This covers the case where the user starts with a prefix of "receiver.ge" and continues
                    // typing 't' from there: In this case, prefix == "receiver.get" rather than "get".
                    // I have only ever encountered this with proposal kinds of *_REF_WITH_CASTED_RECEIVER.
                    int lastIndexOfDot = prefix.lastIndexOf('.');
                    bestSequence = LCSS.bestSubsequence(matchingArea, prefix.substring(lastIndexOfDot + 1));
                } else {
                    int lastIndexOfHash = prefix.lastIndexOf('#');
                    if (lastIndexOfHash >= 0) {
                        // This covers the case where the user starts with a prefix of "Collections#" and continues
                        // from there.
                        bestSequence = LCSS.bestSubsequence(matchingArea,
                                prefix.substring(lastIndexOfHash + 1));
                    } else {
                        // Besides the obvious, this also covers the case where the user starts with a prefix of
                        // "Collections#e", which manifests itself as just "e".
                        bestSequence = LCSS.bestSubsequence(matchingArea, prefix);
                    }
                }
            }
            return prefix.isEmpty() || bestSequence.length > 0;
        }

        @Override
        public void modifyDisplayString(StyledString displayString) {
            final int highlightAdjustment;
            CompletionProposal coreProposal = proposal.getCoreProposal().orNull();
            if (coreProposal == null) {
                // HTML tag proposals are non-lazy(!) JavaCompletionProposals that don't have a core proposal.
                if (proposal instanceof JavaCompletionProposal && displayString.toString().startsWith("</")) { //$NON-NLS-1$
                    highlightAdjustment = 2;
                } else if (proposal instanceof JavaCompletionProposal
                        && displayString.toString().startsWith("<")) { //$NON-NLS-1$
                    highlightAdjustment = 1;
                } else {
                    highlightAdjustment = 0;
                }
            } else {
                switch (coreProposal.getKind()) {
                case CompletionProposal.JAVADOC_FIELD_REF:
                case CompletionProposal.JAVADOC_METHOD_REF:
                case CompletionProposal.JAVADOC_VALUE_REF:
                    highlightAdjustment = displayString.toString().lastIndexOf('#') + 1;
                    break;
                case CompletionProposal.JAVADOC_TYPE_REF:
                    highlightAdjustment = JAVADOC_TYPE_REF_HIGHLIGHT_ADJUSTMENT;
                    break;
                default:
                    highlightAdjustment = 0;
                }
            }

            for (int index : bestSequence) {
                displayString.setStyle(index + highlightAdjustment, 1, StyledString.COUNTER_STYLER);
            }
        }

        /**
         * Since we may simulate completion triggers at positions before the actual triggering, we don't get JDT's
         * additional relevance for exact prefix matches. So we add the additional relevance ourselves, if is not
         * already supplied by the JDT which it does, if the prefix is shorter than the configured minimum prefix
         * length.
         *
         * The boost is the same one as JDT adds at
         * {@link org.eclipse.jdt.internal.codeassist.CompletionEngine#computeRelevanceForCaseMatching}
         *
         * The boost is further multiplied by 16 which reflects the same thing happening in
         * {@link org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal#computeRelevance}
         */
        @Override
        public int modifyRelevance() {
            if (ArrayUtils.isEmpty(bestSequence)) {
                proposal.setTag(IS_PREFIX_MATCH, true);
                return 0;
            }

            int relevanceBoost = 0;

            if (minPrefixLengthForTypes < prefix.length()
                    && StringUtils.equalsIgnoreCase(matchingArea, prefix)) {
                relevanceBoost += 16 * RelevanceConstants.R_EXACT_NAME;
                proposal.setTag(IS_EXACT_MATCH, true);
            }

            // We only apply case matching to genuine Java proposals, i.e., proposals link HTML tags are ranked
            // together with the case in-sensitive matches.
            if (StringUtils.startsWith(matchingArea, prefix)
                    && isFromJavaCompletionProposalComputer(proposal)) {
                proposal.setTag(SUBWORDS_SCORE, null);
                proposal.setTag(IS_PREFIX_MATCH, true);
                // Don't adjust relevance.
            } else if (startsWithIgnoreCase(matchingArea, prefix)) {
                proposal.setTag(SUBWORDS_SCORE, null);
                proposal.setTag(IS_CASE_INSENSITIVE_PREFIX_MATCH, true);
                relevanceBoost = IGNORE_CASE_RANGE_START + relevanceBoost;
            } else if (CharOperation.camelCaseMatch(prefix.toCharArray(), matchingArea.toCharArray())
                    && isFromJavaCompletionProposalComputer(proposal)) {
                proposal.setTag(IS_PREFIX_MATCH, false);
                proposal.setTag(IS_CAMEL_CASE_MATCH, true);
                relevanceBoost = CAMEL_CASE_RANGE_START + relevanceBoost;
            } else {
                int score = LCSS.scoreSubsequence(bestSequence);
                proposal.setTag(IS_PREFIX_MATCH, false);
                proposal.setTag(SUBWORDS_SCORE, score);
                relevanceBoost = SUBWORDS_RANGE_START + relevanceBoost + score;
            }

            return relevanceBoost;
        }

        /**
         * Some {@link IProcessableProposal}s are not produced by the {@link JavaCompletionProposalComputer}, but by
         * some other {@link IJavaCompletionProposalComputer}, e.g., the {@link HTMLTagCompletionProposalComputer}.
         * These proposals do not have a core proposal.
         */
        private boolean isFromJavaCompletionProposalComputer(final IProcessableProposal proposal) {
            return proposal.getCoreProposal().isPresent();
        }
    });
}