Example usage for org.eclipse.jdt.core CompletionContext TOKEN_KIND_NAME

List of usage examples for org.eclipse.jdt.core CompletionContext TOKEN_KIND_NAME

Introduction

In this page you can find the example usage for org.eclipse.jdt.core CompletionContext TOKEN_KIND_NAME.

Prototype

int TOKEN_KIND_NAME

To view the source code for org.eclipse.jdt.core CompletionContext TOKEN_KIND_NAME.

Click Source Link

Document

The completion token is a name.

Usage

From source file:org.eclipse.babel.tapiji.tools.java.ui.MessageCompletionProposalComputer.java

License:Open Source License

@Override
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context,
        IProgressMonitor monitor) {//from   w w  w .  jav  a  2  s.  co m

    List<ICompletionProposal> completions = new ArrayList<ICompletionProposal>();

    if (!InternationalizationNature.hasNature(
            ((JavaContentAssistInvocationContext) context).getCompilationUnit().getResource().getProject())) {
        return completions;
    }

    try {
        JavaContentAssistInvocationContext javaContext = ((JavaContentAssistInvocationContext) context);
        CompletionContext coreContext = javaContext.getCoreContext();

        int tokenStart = coreContext.getTokenStart();
        int tokenEnd = coreContext.getTokenEnd();
        int tokenOffset = coreContext.getOffset();
        boolean isStringLiteral = coreContext.getTokenKind() == CompletionContext.TOKEN_KIND_STRING_LITERAL;

        if (cu == null) {
            manager = ResourceBundleManager
                    .getManager(javaContext.getCompilationUnit().getResource().getProject());

            resource = javaContext.getCompilationUnit().getResource();

            csav = new ResourceAuditVisitor(null, manager.getProject().getName());

            cu = ASTutilsUI.getAstRoot(ASTutilsUI.getCompilationUnit(resource));

            cu.accept(csav);
        }

        if (coreContext.getTokenKind() == CompletionContext.TOKEN_KIND_NAME
                && (tokenEnd + 1) - tokenStart > 0) {

            // Cal10n extension
            String[] metaData = ASTutils.getCal10nEnumLiteralDataAtPos(manager.getProject().getName(), cu,
                    tokenOffset - 1);

            if (metaData != null) {
                completions.add(new KeyRefactoringProposal(tokenOffset, metaData[1],
                        manager.getProject().getName(), metaData[0], metaData[2]));
            }

            return completions;
        }

        if (tokenStart < 0) {
            // is string literal in front of cursor?
            StringLiteral strLit = ASTutils.getStringLiteralAtPos(cu, tokenOffset - 1);
            if (strLit != null) {
                tokenStart = strLit.getStartPosition();
                tokenEnd = tokenStart + strLit.getLength() - 1;
                tokenOffset = tokenOffset - 1;
                isStringLiteral = true;
            } else {
                tokenStart = tokenOffset;
                tokenEnd = tokenOffset;
            }
        }

        if (isStringLiteral) {
            tokenStart++;
        }

        tokenEnd = Math.max(tokenEnd, tokenStart);

        String fullToken = "";

        if (tokenStart < tokenEnd) {
            fullToken = context.getDocument().get(tokenStart, tokenEnd - tokenStart);
        }

        // Check if the string literal is up to be written within the
        // context of a resource-bundle accessor method
        if (csav.getKeyAt(new Long(tokenOffset)) != null && isStringLiteral) {
            completions.addAll(getResourceBundleCompletionProposals(tokenStart, tokenEnd, tokenOffset,
                    isStringLiteral, fullToken, manager, csav, resource));
        } else if (csav.getRBReferenceAt(new Long(tokenOffset)) != null && isStringLiteral) {
            completions.addAll(getRBReferenceCompletionProposals(tokenStart, tokenEnd, fullToken,
                    isStringLiteral, manager, resource));
        } else {
            completions.addAll(getBasicJavaCompletionProposals(tokenStart, tokenEnd, tokenOffset, fullToken,
                    isStringLiteral, manager, csav, resource));
        }
        if (completions.size() == 1) {
            completions.add(new NoActionProposal());
        }

    } catch (Exception e) {
        Logger.logError(e);
    }
    return completions;
}

From source file:org.eclipse.objectteams.otdt.tests.model.CompletionTestsRequestor2.java

License:Open Source License

public String getContext() {
    if (this.context == null)
        return "";

    StringBuffer buffer = new StringBuffer();

    if (!this.shortContext) {
        buffer.append("completion offset=");
        buffer.append(context.getOffset());
        buffer.append('\n');

        buffer.append("completion range=[");
        buffer.append(context.getTokenStart());
        buffer.append(", ");
        buffer.append(context.getTokenEnd());
        buffer.append("]\n");

        char[] token = context.getToken();
        buffer.append("completion token=");
        if (token == null) {
            buffer.append("null");
        } else {/*w  w w.  j  av a  2  s.c  o m*/
            buffer.append('\"');
            buffer.append(token);
            buffer.append('\"');
        }
        buffer.append('\n');

        buffer.append("completion token kind=");
        int tokenKind = context.getTokenKind();
        if (tokenKind == CompletionContext.TOKEN_KIND_STRING_LITERAL) {
            buffer.append("TOKEN_KIND_STRING_LITERAL");
        } else if (tokenKind == CompletionContext.TOKEN_KIND_NAME) {
            buffer.append("TOKEN_KIND_NAME");
        } else {
            buffer.append("TOKEN_KIND_UNKNOWN");
        }
        buffer.append('\n');
    }
    char[][] expectedTypesSignatures = this.context.getExpectedTypesSignatures();
    buffer.append("expectedTypesSignatures=");
    if (expectedTypesSignatures == null) {
        buffer.append(NULL_LITERAL);
    } else {
        buffer.append('{');
        for (int i = 0; i < expectedTypesSignatures.length; i++) {
            if (i > 0)
                buffer.append(',');
            buffer.append(expectedTypesSignatures[i]);

        }
        buffer.append('}');
    }
    buffer.append('\n');

    char[][] expectedTypesKeys = this.context.getExpectedTypesKeys();
    buffer.append("expectedTypesKeys=");
    if (expectedTypesSignatures == null) {
        buffer.append(NULL_LITERAL);
    } else {
        buffer.append('{');
        for (int i = 0; i < expectedTypesKeys.length; i++) {
            if (i > 0)
                buffer.append(',');
            buffer.append(expectedTypesKeys[i]);

        }
        buffer.append('}');
    }

    if (!this.shortContext) {
        buffer.append('\n');

        int locationType = this.context.getTokenLocation();
        if (locationType == 0) {
            buffer.append("completion token location=UNKNOWN"); //$NON-NLS-1$
        } else {
            buffer.append("completion token location={"); //$NON-NLS-1$
            boolean first = true;
            if ((locationType & CompletionContext.TL_MEMBER_START) != 0) {
                if (!first)
                    buffer.append(',');
                buffer.append("MEMBER_START"); //$NON-NLS-1$
                first = false;
            }
            if ((locationType & CompletionContext.TL_STATEMENT_START) != 0) {
                if (!first)
                    buffer.append(',');
                buffer.append("STATEMENT_START"); //$NON-NLS-1$
                first = false;
            }
            buffer.append('}');
        }
    }

    if (this.computeEnclosingElement) {
        buffer.append('\n');
        buffer.append("enclosingElement="); //$NON-NLS-1$
        JavaElement enclosingElement = (JavaElement) this.context.getEnclosingElement();
        if (enclosingElement == null) {
            buffer.append("null"); //$NON-NLS-1$
        } else {
            buffer.append(enclosingElement.toStringWithAncestors(true /*show resolved info*/));
        }
    }

    if (this.computeVisibleElements) {
        buffer.append('\n');

        IJavaElement[] visibleElements = this.context.getVisibleElements(this.assignableType);
        buffer.append("visibleElements="); //$NON-NLS-1$
        if (visibleElements == null) {
            buffer.append("null"); //$NON-NLS-1$
        } else if (visibleElements.length == 0) {
            buffer.append("{}"); //$NON-NLS-1$
        } else {
            buffer.append('{');
            buffer.append('\n');
            for (int i = 0; i < visibleElements.length; i++) {
                JavaElement element = (JavaElement) visibleElements[i];
                buffer.append('\t');
                buffer.append(element.toStringWithAncestors(true /*show resolved info*/));
                buffer.append(",\n"); //$NON-NLS-1$
            }
            buffer.append('}');
        }
    }

    //buffer.append('\n');

    return buffer.toString();
}