Java Code Examples for org.netbeans.api.lexer.Token#partType()

The following examples show how to use org.netbeans.api.lexer.Token#partType() . 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: SQLLexerTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static CharSequence dumpTokens(TokenSequence<?> seq) {
    seq.moveStart();
    StringBuilder builder = new StringBuilder();
    Token<?> token = null;
    while (seq.moveNext()) {
        if (token != null) {
            builder.append('\n');
        }
        token = seq.token();
        builder.append(token.id());
        PartType part = token.partType();
        if (part != PartType.COMPLETE) {
            builder.append(' ');
            builder.append(token.partType());
        }
        builder.append(' ');
        builder.append('\'');
        builder.append(token.text());
        builder.append('\'');
    }
    return builder;
}
 
Example 2
Source File: SemanticHighlighterBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitLiteral(LiteralTree node, Void p) {
    int startPos = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), node);
    tl.moveToOffset(startPos);
    Token t = tl.currentToken();
    if (t != null && t.id() == JavaTokenId.MULTILINE_STRING_LITERAL && t.partType() == PartType.COMPLETE) {
        String tokenText = t.text().toString();
        String[] lines = tokenText.split("\n");
        int indent = Arrays.stream(lines, 1, lines.length)
                           .mapToInt(this::leadingIndent)
                           .min()
                           .orElse(0);
        int pos = startPos + lines[0].length() + 1;
        for (int i = 1; i < lines.length; i++) {
            String line = lines[i];
            if (i == lines.length - 1) {
                line = line.substring(0, line.length() - 3);
            }
            String strippendLine = line.replaceAll("[\t ]+$", "");
            int indentedStart = pos + indent;
            int indentedEnd = pos + strippendLine.length();
            if (indentedEnd > indentedStart)
                extraColoring.add(Pair.of(new int[] {indentedStart, indentedEnd}, UNINDENTED_TEXT_BLOCK));
            pos += line.length() + 1;
        }
    }

    addParameterInlineHint(node);
    return super.visitLiteral(node, p);
}
 
Example 3
Source File: JavadocCompletionUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Finds javadoc token sequence.
 * @param javac compilation info
 * @param e element for which the tokens are queried
 * @return javadoc token sequence or null.
 */
@SuppressWarnings("fallthrough")
public static TokenSequence<JavadocTokenId> findJavadocTokenSequence(CompilationInfo javac, Tree tree, Element e) {
    if (e == null || javac.getElementUtilities().isSynthetic(e))
        return null;

    if (tree == null)
        tree = javac.getTrees().getTree(e);
    if (tree == null)
        return null;

    int elementStartOffset = (int) javac.getTrees().getSourcePositions().getStartPosition(javac.getCompilationUnit(), tree);
    TokenSequence<JavaTokenId> s = SourceUtils.getJavaTokenSequence(javac.getTokenHierarchy(), elementStartOffset);
    if (s == null) {
        return null;
    }
    s.move(elementStartOffset);
    Token<JavaTokenId> token = null;
    while (s.movePrevious()) {
        token = s.token();
        switch (token.id()) {
            case BLOCK_COMMENT:
                // see #147533
                if (!"/**/".contentEquals(token.text())) { // NOI18N
                    break;
                }
            case JAVADOC_COMMENT:
                if (token.partType() == PartType.COMPLETE) {
                    return javac.getElements().getDocComment(e) == null
                            ? null : s.embedded(JavadocTokenId.language());
                }
                break;
            case WHITESPACE:
            case LINE_COMMENT:
                break;
            default:
                return null;
        }
    }
    return null;
}
 
Example 4
Source File: DTDTokenIdTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private String lexDocument(String resourceName) throws Exception {
    javax.swing.text.Document document = getDocument("resources/" + resourceName + ".dtd");
    String content = document.getText(0, document.getLength());
    String[] lines = content.split("\n");
    int line = 0;
    int offset = 0;
    int lineEnd = offset + lines[line].length() + 1;
    boolean first = true;
    StringBuilder sb = new StringBuilder();
    
    ((AbstractDocument)document).readLock();
    try {
        TokenHierarchy th = TokenHierarchy.get(document);
        TokenSequence ts = th.tokenSequence();
        //assert(ts.tokenCount() == expectedIds.length);
        int overLine = 0;
        while(ts.moveNext()) {
            if (first) {
                lineEnd = ts.offset()  - overLine + lines[line].length() + 1;
            }
            Token<DTDTokenId> t = ts.token();
            if (!first) {
                sb.append(", ");
            }
            first = false;
            if (PartType.MIDDLE == t.partType()) {
                sb.append("{");
            }
            sb.append(t.id().name());
            if (PartType.MIDDLE == t.partType()) {
                sb.append("}");
            }
            offset += t.length();
            if (offset >= lineEnd) {
                sb.append("\n");
                first = true;
                String s = t.text().toString();
                int pos = 0;
                do {
                    int nl = s.indexOf('\n', pos);
                    if (nl == -1) {
                        break;
                    }
                    line++;
                    pos = nl + 1;
                } while (true);
                overLine = s.length() - pos;
            }
        }
        return sb.toString();
    } finally {
        ((AbstractDocument)document).readUnlock();
    }
}