Java Code Examples for org.netbeans.api.lexer.TokenSequence#offsetToken()

The following examples show how to use org.netbeans.api.lexer.TokenSequence#offsetToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSNI2JavaScriptBody.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Token<JavaTokenId> findBlockToken(CompilationInfo info, TreePath path, HintContext ctx) {
    int end = (int) info.getTrees().getSourcePositions().getEndPosition(path.getCompilationUnit(), path.getLeaf());
    TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());

    if (ts == null) return null;

    ts.move(end);

    if ((ctx != null && ctx.isCanceled()) || !ts.movePrevious() || ts.token().id() != JavaTokenId.SEMICOLON) return null;

    OUTER: while (ts.movePrevious()) {
        if (ctx != null && ctx.isCanceled()) return null;

        switch (ts.token().id()) {
            case WHITESPACE: break;
            case LINE_COMMENT: break;
            case JAVADOC_COMMENT: break;
            case BLOCK_COMMENT:
                final CharSequence tok = ts.token().text();
                final int l = tok.length();
                if (l > 4
                    && tok.subSequence(0, 4).toString().equals("/*-{") // NOI18N
                    && tok.subSequence(l - 4, l).toString().equals("}-*/") // NOI18N
                ) {
                    return ts.offsetToken();
                }
                break;
            default:
                break OUTER;
        }
    }

    return null;
}
 
Example 2
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static Token<JavaTokenId> createHighlightImpl(CompilationInfo info, Document doc, TreePath tree) {
    Tree leaf = tree.getLeaf();
    SourcePositions positions = info.getTrees().getSourcePositions();
    CompilationUnitTree cu = info.getCompilationUnit();
    
    //XXX: do not use instanceof:
    if (leaf instanceof MethodTree || leaf instanceof VariableTree || leaf instanceof ClassTree
            || leaf instanceof MemberSelectTree || leaf instanceof AnnotatedTypeTree || leaf instanceof MemberReferenceTree
            || "BINDING_PATTERN".equals(leaf.getKind().name())) {
        return findIdentifierSpan(info, doc, tree);
    }
    
    int start = (int) positions.getStartPosition(cu, leaf);
    int end = (int) positions.getEndPosition(cu, leaf);
    
    if (start == Diagnostic.NOPOS || end == Diagnostic.NOPOS) {
        return null;
    }
    
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
    
    if (ts.move(start) == Integer.MAX_VALUE) {
        return null;
    }
    
    if (ts.moveNext()) {
        Token<JavaTokenId> token = ts.token();
        if (ts.offset() == start && token != null) {
            final JavaTokenId id = token.id();
            if (id == JavaTokenId.IDENTIFIER) {
                return token;
            }
            if (id == JavaTokenId.THIS || id == JavaTokenId.SUPER) {
                return ts.offsetToken();
            }
        }
    }
    
    return null;
}
 
Example 3
Source File: ErrorHintsProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static Token findUnresolvedElementToken(CompilationInfo info, int offset) throws IOException {
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
    
    if (ts == null) {
        return null;
    }
    
    ts.move(offset);
    if (ts.moveNext()) {
        Token<JavaTokenId> t = ts.token();

        if (t.id() == JavaTokenId.DOT) {
            ts.moveNext();
            t = ts.token();
        } else {
            if (t.id() == JavaTokenId.LT) {
                ts.moveNext();
                t = ts.token();
            } else {
                if (t.id() == JavaTokenId.NEW || t.id() == JavaTokenId.WHITESPACE) {
                    t = skipWhitespaces(ts);

                    if (t == null) return null;
                } else if (t.id() == JavaTokenId.IMPORT) {
                    t = skipWhitespaces(ts);

                    if (t == null) return null;
                }
            }
        }

        while (t.id() == JavaTokenId.WHITESPACE) {
            ts.moveNext();
            t = ts.token();
        }
        
        switch (t.id()) {
            case IDENTIFIER:
            case INT_LITERAL:
            case LONG_LITERAL:
            case FLOAT_LITERAL:
            case DOUBLE_LITERAL:
            case CHAR_LITERAL:
            case STRING_LITERAL:
            case TRUE:
            case FALSE:
            case NULL:
                return ts.offsetToken();
        }
    }
    return null;
}
 
Example 4
Source File: SearchClassDependencyInRepo.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static Token findUnresolvedElementToken(CompilationInfo info, int offset) throws IOException {
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());

    if (ts == null) {
        return null;
    }

    ts.move(offset);
    if (ts.moveNext()) {
        Token t = ts.token();

        if (t.id() == JavaTokenId.DOT) {
            ts.moveNext();
            t = ts.token();
        } else {
            if (t.id() == JavaTokenId.LT) {
                ts.moveNext();
                t = ts.token();
            } else {
                if (t.id() == JavaTokenId.NEW) {
                    boolean cont = ts.moveNext();

                    while (cont && ts.token().id() == JavaTokenId.WHITESPACE) {
                        cont = ts.moveNext();
                    }

                    if (!cont) {
                        return null;
                    }
                    t = ts.token();
                }
            }
        }

        if (t.id() == JavaTokenId.IDENTIFIER) {
            return ts.offsetToken();
        }
    }
    return null;
}
 
Example 5
Source File: SearchModuleDependency.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static Token findUnresolvedElementToken(CompilationInfo info, int offset) throws IOException {
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());

    if (ts == null) {
        return null;
    }

    ts.move(offset);
    if (ts.moveNext()) {
        Token t = ts.token();

        if (t.id() == JavaTokenId.DOT) {
            ts.moveNext();
            t = ts.token();
        } else {
            if (t.id() == JavaTokenId.LT) {
                ts.moveNext();
                t = ts.token();
            } else {
                if (t.id() == JavaTokenId.NEW) {
                    boolean cont = ts.moveNext();

                    while (cont && ts.token().id() == JavaTokenId.WHITESPACE) {
                        cont = ts.moveNext();
                    }

                    if (!cont) {
                        return null;
                    }
                    t = ts.token();
                }
            }
        }

        if (t.id() == JavaTokenId.IDENTIFIER) {
            return ts.offsetToken();
        }
    }
    return null;
}
 
Example 6
Source File: CreateQualifier.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Token<?> findUnresolvedElementToken(CompilationInfo info, int offset) 
    throws IOException 
{
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
    
    if (ts == null) {
        return null;
    }
    
    ts.move(offset);
    if (ts.moveNext()) {
        Token<?> t = ts.token();

        if (t.id() == JavaTokenId.DOT) {
            ts.moveNext();
            t = ts.token();
        } else {
            if (t.id() == JavaTokenId.LT) {
                ts.moveNext();
                t = ts.token();
            } else {
                if (t.id() == JavaTokenId.NEW || t.id() == JavaTokenId.WHITESPACE) {
                    boolean cont = ts.moveNext();
                    
                    while (cont && ts.token().id() == JavaTokenId.WHITESPACE) {
                        cont = ts.moveNext();
                    }
                    
                    if (!cont)
                        return null;
                    
                    t = ts.token();
                }
            }
        }

        if (t.id() == JavaTokenId.IDENTIFIER) {
            return ts.offsetToken();
        }
    }
    return null;
}