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

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

Introduction

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

Prototype

public int getOffset() 

Source Link

Document

Returns the offset position in the source file buffer after which code assist is requested.

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) {/*w  w  w.ja  v  a2  s . c  om*/

    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.jpt.jpa.ui.internal.jpql.JpaJpqlJavaCompletionProposalComputer.java

License:Open Source License

private List<ICompletionProposal> computeCompletionProposals(JavaContentAssistInvocationContext context,
        IProgressMonitor monitor) throws Exception {

    CompletionContext completionContext = context.getCoreContext();
    if (completionContext == null)
        return Collections.emptyList();

    // The token "start" is the offset of the token's first character within the document.
    // A token start of -1 can means:
    // - It is inside the string representation of a unicode character, \\u0|0E9 where | is the
    //   cursor, then -1 is returned;
    // - The string is not valid (it has some invalid characters)
    int tokenStart[] = { completionContext.getTokenStart() };
    int tokenEnd[] = { completionContext.getTokenEnd() };
    if (tokenStart[0] == -1)
        return Collections.emptyList();

    int[] position = { completionContext.getOffset() - tokenStart[0] - 1 };
    if (position[0] < 0)
        return Collections.emptyList();

    ICompilationUnit compilationUnit = context.getCompilationUnit();
    if (compilationUnit == null)
        return Collections.emptyList();
    CompilationUnit astRoot = ASTTools.buildASTRoot(compilationUnit);

    IFile file = getCorrespondingResource(compilationUnit);
    if (file == null)
        return Collections.emptyList();

    JpaFile jpaFile = (JpaFile) file.getAdapter(JpaFile.class);
    if (jpaFile == null)
        return Collections.emptyList();

    monitor.worked(80);/*  w  ww. j  av  a  2  s  .com*/
    checkCanceled(monitor);

    // Retrieve the JPA's model object
    NamedQuery namedQuery = namedQuery(jpaFile, tokenStart[0]);
    if (namedQuery == null)
        return Collections.emptyList();

    // Retrieve the actual value of the element "query" since the content assist can be
    // invoked before the model received the new content
    String jpqlQuery = retrieveQuery(astRoot, tokenStart, tokenEnd, position);

    // Now create the proposals
    ResourceManager resourceManager = this.getResourceManager(context.getViewer().getTextWidget());
    return buildProposals(namedQuery, jpqlQuery, tokenStart[0], tokenEnd[0], position[0], resourceManager);
}