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

The following examples show how to use org.netbeans.api.lexer.Token#id() . 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: PHPBracesMatcher.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static boolean isBraceToken(Token<? extends PHPTokenId> token) {
    PHPTokenId id = token.id();
    return LexUtilities.textEquals(token.text(), '(') // NOI18N
            || LexUtilities.textEquals(token.text(), ')') // NOI18N
            || id == PHPTokenId.PHP_CURLY_OPEN
            || id == PHPTokenId.PHP_CURLY_CLOSE
            || LexUtilities.textEquals(token.text(), '[') // NOI18N
            || LexUtilities.textEquals(token.text(), ']') // NOI18N
            || LexUtilities.textEquals(token.text(), '$', '{') // NOI18N
            || LexUtilities.textEquals(token.text(), ':') // NOI18N
            || id == PHPTokenId.PHP_ENDFOR
            || id == PHPTokenId.PHP_ENDFOREACH
            || id == PHPTokenId.PHP_ENDIF
            || id == PHPTokenId.PHP_ENDSWITCH
            || id == PHPTokenId.PHP_ENDWHILE
            || id == PHPTokenId.PHP_ELSEIF
            || id == PHPTokenId.PHP_ELSE;
}
 
Example 2
Source File: RequireJsHtmlExtension.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private String getDataMainValue(TokenSequence<HTMLTokenId> ts, int offset) {
    ts.move(offset);
    if (ts.moveNext()) {
        Token<HTMLTokenId> token = ts.token();
        HTMLTokenId tokenId = token.id();
        if (tokenId == HTMLTokenId.VALUE) {
            String value = token.text().toString();
            token = LexerUtils.followsToken(ts, Arrays.asList(HTMLTokenId.ARGUMENT), true, false, HTMLTokenId.OPERATOR, HTMLTokenId.WS, HTMLTokenId.BLOCK_COMMENT);
            if (token != null && token.id() == HTMLTokenId.ARGUMENT && DATAMAIN.equals(token.text().toString())) {
                return value.substring(1, value.length() - 1);
            }

        }
    }
    return null;
}
 
Example 3
Source File: AttrImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Node getFirstChildLocked(TokenSequence ts) {
    while (ts.moveNext()) {
        Token<XMLTokenId> t = ts.token();
        if (t.id() == XMLTokenId.VALUE) {
            // fuzziness to relax minor tokenization changes
            CharSequence image = t.text();
            if (image.length() == 1) {
                char test = image.charAt(0);
                if (test == '"' || test == '\'') {
                    if (ts.moveNext()) {
                        t = ts.token();
                    } else {
                        return null;
                    }
                }
            }
            
            if (t.id() == XMLTokenId.VALUE) {
                return new TextImpl(syntax, t, ts.offset(), ts.offset() + t.length(), this);
            } else {
                return null;
            }
        }
    }
    return null;
}
 
Example 4
Source File: XMLBraceMatcher.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private int[] findMatchingTagForward(TokenSequence ts, String tagToMatch) {
    Stack<String> stack = new Stack<String>();
    while(ts.moveNext()) {
        Token t = ts.token();
        if(XMLTokenId.TAG != t.id())
            continue;
        String tag = t.text().toString();
        if(">".equals(tag))
            continue;
        if (tag.startsWith("</") || tag.equals("/>")) {
            if (stack.empty()) {
                if (tag.length() == 3 || tag.equals("</" + tagToMatch)) {
                    return findTagPosition(ts, false);
                } else {
                    return null;
                }
            }
            stack.pop();
        } else {
            stack.push(tag.substring(1));
        }
    }
    
    return null;
}
 
Example 5
Source File: LexUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Search forwards in the token sequence until a token of type <code>down</code> is found */
public static OffsetRange findFwd(Document doc, TokenSequence<? extends JsTokenId> ts, TokenId up,
    TokenId down) {
    int balance = 0;

    while (ts.moveNext()) {
        Token<? extends JsTokenId> token = ts.token();
        TokenId id = token.id();
        
        if (id == up) {
            balance++;
        } else if (id == down) {
            if (balance == 0) {
                return new OffsetRange(ts.offset(), ts.offset() + token.length());
            }

            balance--;
        }
    }

    return OffsetRange.NONE;
}
 
Example 6
Source File: TokenUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Token getPreviousNonEmptyToken(int offset) {
    ts.move(offset);

    if (!ts.moveNext() && !ts.movePrevious()) {
        return null;
    }

    Token ret = null;
    while (ts.movePrevious()) {
        Token token = ts.token();
        if ((token.id() != JsTokenId.BLOCK_COMMENT && token.id() != JsTokenId.DOC_COMMENT
            && token.id() != JsTokenId.LINE_COMMENT && token.id() != JsTokenId.EOL
            && token.id() != JsTokenId.WHITESPACE)) {
            ret = token;
            break;
        }
    }
    return ret;
}
 
Example 7
Source File: MakeClassAbstractHint.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private int getInsertPosition(BaseDocument doc) {
    TokenSequence<GroovyTokenId> ts = LexUtilities.getGroovyTokenSequence(doc, 1);

    while (ts.moveNext()) {
        Token t = ts.token();

        if (t.id() == GroovyTokenId.LITERAL_class) {
            int offset = ts.offset();
            if (isCorrectClassDefinition(ts)) {
                return offset;
            }
        }
    }

    return 0;
}
 
Example 8
Source File: XMLCompletionQuery.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isEndTagPrefix(Token token) {
    if (token == null) return false;

    TokenId tokenID = token.id();
    if (tokenID.equals(XMLTokenId.TAG) || tokenID.equals(XMLTokenId.TEXT)) {
        String tokenText = token.text().toString();
        if (tokenText.startsWith(END_TAG_PREFIX)) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: TestJoinMixTextTokenId.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public LanguageEmbedding<?> embedding(
Token<TestJoinMixTextTokenId> token, LanguagePath languagePath, InputAttributes inputAttributes) {
    // Test language embedding in the block comment
    switch (token.id()) {
        case WORD:
            return null;
        case WHITESPACE:
            return null;
    }
    return null; // No embedding
}
 
Example 10
Source File: TwigBracesMatcher.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int[] findOriginInSequence(TokenSequence<? extends TwigBlockTokenId> ts) {
    int[] result = null;
    ts.move(context.getSearchOffset());
    if (ts.moveNext()) {
        Token<? extends TwigBlockTokenId> currentToken = ts.token();
        if (currentToken != null) {
            TwigBlockTokenId currentTokenId = currentToken.id();
            if (currentTokenId == TwigBlockTokenId.T_TWIG_TAG) {
                result = new int[] {ts.offset(), ts.offset() + currentToken.length()};
            }
        }
    }
    return result;
}
 
Example 11
Source File: CompletionContextFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected static boolean isCallArgumentContext(TokenSequence<JsTokenId> ts) {
     if (ts.movePrevious()) {
        Token<? extends JsTokenId> token = LexUtilities.findPreviousNonWsNonComment(ts);
        if (token != null 
                && (token.id() == JsTokenId.BRACKET_LEFT_PAREN || token.id() == JsTokenId.OPERATOR_COMMA)) {
            int balanceParen = token.id() == JsTokenId.BRACKET_LEFT_PAREN ? 0 : 1;
            int balanceCurly = 0;
            int balanceBracket = 0;
            while (balanceParen != 0 && ts.movePrevious()) {
                token = ts.token();
                if (token.id() == JsTokenId.BRACKET_LEFT_PAREN) {
                    balanceParen--;
                } else if (token.id() == JsTokenId.BRACKET_RIGHT_PAREN) {
                    balanceParen++;
                } else if (token.id() == JsTokenId.BRACKET_LEFT_CURLY) {
                    balanceCurly--;
                } else if (token.id() == JsTokenId.BRACKET_RIGHT_CURLY) {
                    balanceCurly++;
                } else if (token.id() == JsTokenId.BRACKET_LEFT_BRACKET) {
                    balanceBracket--;
                } else if (token.id() == JsTokenId.BRACKET_RIGHT_BRACKET) {
                    balanceBracket++;
                }
            }
            if (balanceParen == 0 && balanceCurly == 0 && balanceBracket == 0) {
                if (ts.movePrevious() && token.id() == JsTokenId.BRACKET_LEFT_PAREN) {
                    token = LexUtilities.findPreviousNonWsNonComment(ts);
                    if (token.id() == JsTokenId.IDENTIFIER) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 
Example 12
Source File: XhtmlElEmbeddingProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public List<Embedding> getEmbeddings(Snapshot snapshot) {
    TokenHierarchy<?> th = snapshot.getTokenHierarchy();
    TokenSequence<XhtmlElTokenId> sequence = th.tokenSequence(XhtmlElTokenId.language());
    sequence.moveStart();
    List<Embedding> embeddings = new ArrayList<>();
    boolean lastEmbeddingIsVirtual = false;
    while (sequence.moveNext()) {
        Token t = sequence.token();
        if (t.id() == XhtmlElTokenId.HTML) {
            //lets suppose the text is always html :-(
            embeddings.add(snapshot.create(sequence.offset(), t.length(), "text/html")); //NOI18N
            lastEmbeddingIsVirtual = false;
        } else {
            //replace templating tokens by generated code marker
            if (!lastEmbeddingIsVirtual) {
                embeddings.add(snapshot.create(Constants.LANGUAGE_SNIPPET_SEPARATOR, "text/html"));
                lastEmbeddingIsVirtual = true;
            }
        }
    }
    if (embeddings.isEmpty()) {
        return Collections.emptyList();
    } else {
        return Collections.singletonList(Embedding.create(embeddings));
    }
}
 
Example 13
Source File: CompletionUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isEndTagSuffix(Token token) {
    if (token == null) return false;

    TokenId tokenID = token.id();
    if (tokenID.equals(XMLTokenId.TAG)) {
        String tokenText = token.text().toString();
        if (tokenText.equals(END_TAG_SUFFIX)) {
            return true;
        }
    }
    return false;
}
 
Example 14
Source File: JavadocCompletionUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isInlineTagStart(Token<JavadocTokenId> token) {
    if (token == null || token.id() != JavadocTokenId.OTHER_TEXT) {
        return false;
    }

    CharSequence text = token.text();
    boolean result = text.charAt(text.length() - 1) == '{';
    return result;
}
 
Example 15
Source File: AssignComments.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean alreadySeenJavadoc(Token<JavaTokenId> token, TokenSequence<JavaTokenId> seq) {
    return (token.id() == JavaTokenId.JAVADOC_COMMENT) &&
           (mixedJDocTokenIndexes.contains(seq.index()));
}
 
Example 16
Source File: FormatVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isKeyValueOperator(Token<PHPTokenId> token) {
    return token.id() == PHPTokenId.PHP_OPERATOR && TokenUtilities.textEquals("=>", token.text()); // NOI18N
}
 
Example 17
Source File: JspIndenter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isEndTagSymbol(Token<JspTokenId> token) {
    return token.id() == JspTokenId.SYMBOL &&
            token.text().toString().equals(">");
}
 
Example 18
Source File: JavadocImports.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isReferenceTag(Token<JavadocTokenId> tag) {
    String tagName = tag.text().toString().intern();
    return tag.id() == JavadocTokenId.TAG && ALL_REF_TAG_NAMES.contains(tagName);
}
 
Example 19
Source File: CompletionContext.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Reads the TokenSequence forward, and reach the end of the tag, or 
 * the processing instruction.
 * If the tag is not terminated, ie some TEXT will appear, or another tag token,
 * the search terminates, and the tag is recorded as unterminated
 */
@SuppressWarnings("fallthrough")
private void readTagContent(TokenSequence<XMLTokenId> seq) {
    attributes = new HashMap<String, ArgumentInfo>();
    String argName = null;
    int argStart = -1;
    Token<XMLTokenId> t;
    while (seq.moveNext()) {
        t = seq.token();
        XMLTokenId id = t.id();
        switch (id) {
            case TAG:
                CharSequence s = t.text();
                if (s.charAt(0) == '<') {
                    // unterminated tag
                    markUnclosed(seq.offset());
                    return;
                } else if (s.charAt(s.length() - 1) == '>') {
                    // end tag marker
                    finished = true;
                    tagEndOffset = seq.offset() + s.length();
                    selfClosed = s.length() >= 2 && s.charAt(s.length() - 2) == '/';
                    return;
                }

            // OK for tag content, not OK for PI
            case ARGUMENT:
                argName = t.text().toString();
                argStart = seq.offset();
                break;
            case VALUE:
                if (argName != null) {
                    int len = t.length();
                    CharSequence val = t.text();
                    StringBuilder compound = null;
                    while (seq.moveNext()) {
                        Token<XMLTokenId> nt = seq.token();
                        if (nt.id() != XMLTokenId.CHARACTER &&
                            nt.id() != XMLTokenId.VALUE) {
                            seq.movePrevious();
                            break;
                        }
                        if (compound == null) {
                            compound = new StringBuilder();
                            compound.append(val);
                        }
                        compound.append(nt.text());
                        len += nt.length();
                    }
                    attributes.put(argName, new ArgumentInfo(argStart, 
                            seq.offset(), seq.offset() + len, 
                            stripQuotes(
                                compound == null ?
                                    t.text() :
                                    compound
                            ).toString()));
                }
                break;
            case OPERATOR:
                break;

            // these are neither OK for tag or PI
            case CHARACTER:
            case BLOCK_COMMENT:
            case CDATA_SECTION:
            case DECLARATION:
            case TEXT:
            case ERROR:
                markUnclosed(seq.offset());
                return;
 
            // not OK for tag
            case PI_TARGET:
            case PI_CONTENT:
            case PI_END:
                markUnclosed(seq.offset());
                return;
                
            // this is OK for all
            case WS:
                break;
                
            case PI_START:
        }
    }
}
 
Example 20
Source File: IndentUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * This method count new indent ofr braces and parent
 *
 * @param doc
 * @param offset - the original offset, where is cursor
 * @param currentIndent - the indnet that should be modified
 * @param previousIndent - indent of the line abot
 * @return
 */
public static int countIndent(BaseDocument doc, int offset, int previousIndent) {
    int value = previousIndent;
    TokenSequence<? extends PHPTokenId> ts = LexUtilities.getPHPTokenSequence(doc, offset);
    if (ts != null) {
        ts.move(offset);
        if (!ts.movePrevious() || !ts.moveNext()) {
            return previousIndent;
        }
        Token<? extends PHPTokenId> token = ts.token();
        while (token.id() != PHPTokenId.PHP_CURLY_OPEN
                && token.id() != PHPTokenId.PHP_SEMICOLON
                && !(token.id() == PHPTokenId.PHP_TOKEN
                && (TokenUtilities.textEquals(token.text(), "(") // NOI18N
                || TokenUtilities.textEquals(token.text(), "["))) // NOI18N
                && ts.movePrevious()) {
            token = ts.token();
        }
        if (token.id() == PHPTokenId.PHP_CURLY_OPEN) {
            while (token.id() != PHPTokenId.PHP_CLASS
                    && token.id() != PHPTokenId.PHP_FUNCTION
                    && token.id() != PHPTokenId.PHP_IF
                    && token.id() != PHPTokenId.PHP_ELSE
                    && token.id() != PHPTokenId.PHP_ELSEIF
                    && token.id() != PHPTokenId.PHP_FOR
                    && token.id() != PHPTokenId.PHP_FOREACH
                    && token.id() != PHPTokenId.PHP_WHILE
                    && token.id() != PHPTokenId.PHP_DO
                    && token.id() != PHPTokenId.PHP_SWITCH
                    && ts.movePrevious()) {
                token = ts.token();
            }
            CodeStyle codeStyle = CodeStyle.get(doc);
            CodeStyle.BracePlacement bracePlacement = codeStyle.getOtherBracePlacement();
            if (token.id() == PHPTokenId.PHP_CLASS) {
                bracePlacement = codeStyle.getClassDeclBracePlacement();
            } else if (token.id() == PHPTokenId.PHP_FUNCTION) {
                bracePlacement = codeStyle.getMethodDeclBracePlacement();
            } else if (token.id() == PHPTokenId.PHP_IF || token.id() == PHPTokenId.PHP_ELSE || token.id() == PHPTokenId.PHP_ELSEIF) {
                bracePlacement = codeStyle.getIfBracePlacement();
            } else if (token.id() == PHPTokenId.PHP_FOR || token.id() == PHPTokenId.PHP_FOREACH) {
                bracePlacement = codeStyle.getForBracePlacement();
            } else if (token.id() == PHPTokenId.PHP_WHILE || token.id() == PHPTokenId.PHP_DO) {
                bracePlacement = codeStyle.getWhileBracePlacement();
            } else if (token.id() == PHPTokenId.PHP_SWITCH) {
                bracePlacement = codeStyle.getSwitchBracePlacement();
            }
            value = bracePlacement == CodeStyle.BracePlacement.NEW_LINE_INDENTED ? previousIndent + codeStyle.getIndentSize() : previousIndent;
        }
    }
    return value;
}