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

The following examples show how to use org.netbeans.api.lexer.Token#getProperty() . 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: SLanguageProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public LanguageEmbedding<?> findLanguageEmbedding (
    Token token, 
    LanguagePath languagePath, 
    InputAttributes inputAttributes
) {
    String mimeType = languagePath.innerLanguage ().mimeType ();
    if (!LanguagesManager.getDefault ().isSupported (mimeType)) return null;
    Language<STokenId> language = getTokenImport (mimeType, token);
    if (language == null) 
        language = getPreprocessorImport (languagePath, token);
    if (language == null) return null;
    Integer i = (Integer) token.getProperty ("startSkipLength");
    int startSkipLength = i == null ? 0 : i.intValue ();
    i = (Integer) token.getProperty ("endSkipLength");
    int endSkipLength = i == null ? 0 : i.intValue ();
    return LanguageEmbedding.create (
        language, 
        startSkipLength, 
        endSkipLength
    );
}
 
Example 2
Source File: HtmlLexerTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testScriptType_value() {
    TokenHierarchy th = TokenHierarchy.create("<script type=\"text/plain\">plain</script>", HTMLTokenId.language());
    TokenSequence ts = th.tokenSequence();
    ts.moveStart();

    while(ts.moveNext()) {
        Token t = ts.token();
        if(t.id() == HTMLTokenId.SCRIPT) {
            String scriptType = (String)t.getProperty(HTMLTokenId.SCRIPT_TYPE_TOKEN_PROPERTY);
            assertNotNull(scriptType);
            assertEquals("text/plain", scriptType);
            return ;
        }
    }
    
    assertTrue("Couldn't find any SCRIPT token!", false);
}
 
Example 3
Source File: HtmlLexerTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testScriptType_empty() {
    TokenHierarchy th = TokenHierarchy.create("<script type=\"\">plain</script>", HTMLTokenId.language());
    TokenSequence ts = th.tokenSequence();
    ts.moveStart();

    while(ts.moveNext()) {
        Token t = ts.token();
        if(t.id() == HTMLTokenId.SCRIPT) {
            String scriptType = (String)t.getProperty(HTMLTokenId.SCRIPT_TYPE_TOKEN_PROPERTY);
            assertNotNull(scriptType);
            assertEquals("", scriptType);
            return ;
        }
    }
    
    assertTrue("Couldn't find any SCRIPT token!", false);
}
 
Example 4
Source File: HtmlLexerTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testScriptType_missing() {
    TokenHierarchy th = TokenHierarchy.create("<script>plain</script>", HTMLTokenId.language());
    TokenSequence ts = th.tokenSequence();
    ts.moveStart();

    while(ts.moveNext()) {
        Token t = ts.token();
        if(t.id() == HTMLTokenId.SCRIPT) {
            String scriptType = (String)t.getProperty(HTMLTokenId.SCRIPT_TYPE_TOKEN_PROPERTY);
            assertNull(scriptType);
            return ;
        }
    }
    
    assertTrue("Couldn't find any SCRIPT token!", false);
}
 
Example 5
Source File: IncludedJSPFileProcessor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void renderProcess() throws BadLocationException {
    processIncludes(false, null);

    TokenHierarchy tokenHierarchy = TokenHierarchy.get(doc);
    TokenSequence tokenSequence = tokenHierarchy.tokenSequence(); //get top level token sequence
    if (!tokenSequence.moveNext()) {
        return; //no tokens in token sequence
    }

    /**
     * process java code blocks one by one
     * note: We count on the fact the scripting language in JSP is Java
     */
    do {
        Token token = tokenSequence.token();

        if (token.id() == JspTokenId.SCRIPTLET) {
            JavaCodeType blockType = (JavaCodeType) token.getProperty(JspTokenId.SCRIPTLET_TOKEN_TYPE_PROPERTY);
            StringBuilder buff = blockType == JavaCodeType.DECLARATION ? declarations : scriptlets;

            if (blockType != JavaCodeType.EXPRESSION) {
                buff.append(token.text() + "\n"); //NOI18N
            }
        }
    } while (tokenSequence.moveNext());


    importsDeclarations.append(createImplicitImportStatements(Collections.<String>emptyList()));
    // no need to do it, the JSP parser will return the beans for the including page
    //declarations.append(createBeanVarDeclarations());
}
 
Example 6
Source File: SyntaxHighlighting.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * @param token non-null token.
 * @return attributes for tokenId or null if none found.
 */
synchronized AttributeSet findAttrs(Token token) {
    if (token.id() != TextmateTokenId.TEXTMATE) {
        return null;
    }
    
    List<AttributeSet> attrs = new ArrayList<>();
    List<String> categories = (List<String>) token.getProperty("categories");
    
    for (String category : categories) {
        if (category.startsWith("meta.embedded")) {
            attrs.clear();
        } else {
            attrs.add(scopeName2Coloring.computeIfAbsent(category, c -> {
                String cat = category;

                while (true) {
                    AttributeSet currentAttrs = fcs.getTokenFontColors(cat);

                    if (currentAttrs != null) {
                        return currentAttrs;
                    }

                    int dot = cat.lastIndexOf('.');

                    if (dot == (-1))
                        break;

                    cat = cat.substring(0, dot);
                }

                return SimpleAttributeSet.EMPTY;
            }));
        }
    }

    return AttributesUtilities.createComposite(attrs.toArray(new AttributeSet[0]));
}
 
Example 7
Source File: SimplifiedJspServlet.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected void renderProcess() throws BadLocationException{
    //check servlet API on classpath
    if (!isServletAPIOnClasspath()){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                displayServletAPIMissingWarning();
            }
        });
        processingSuccessful = false;
        return;
    }

    processIncludes(true, null);

    //XXX The InputAttribute from the document are not copied to the following TokenHierarchy,
    //the JspLexer behaviour may seem to be inaccurate in some cases!
    TokenHierarchy<CharSequence> tokenHierarchy = TokenHierarchy.create(charSequence, JspTokenId.language());

    TokenSequence<JspTokenId> tokenSequence = tokenHierarchy.tokenSequence(JspTokenId.language()); //get top level token sequence
    if (!tokenSequence.moveNext()) {
        return; //no tokens in token sequence
    }

    /**
     * process java code blocks one by one
     * note: We count on the fact the scripting language in JSP is Java
     */
    do {
        Token<JspTokenId> token = tokenSequence.token();
        String tokenText = token.text() == null ? "" : CharSequenceUtilities.toString(token.text()).trim(); //NOI18N
        if (token.id() == JspTokenId.SCRIPTLET) {
            int blockStart = token.offset(tokenHierarchy);
            // workaround for #172594
            int blockLength = Math.min(token.length(), snapshot.getText().length() - blockStart);

            JavaCodeType blockType = (JavaCodeType) token.getProperty(JspTokenId.SCRIPTLET_TOKEN_TYPE_PROPERTY);
            List<Embedding> buff = blockType == JavaCodeType.DECLARATION ? declarations : scriptlets;

            if (blockType == JavaCodeType.EXPRESSION) {
                // the "" + (...) construction is used to preserve compatibility with pre-autoboxing java
                // see issue #116598
                buff.add(snapshot.create(String.format("\n\t\tObject expr%1$d = \"\" + (", expressionIndex++), "text/x-java")); //NOI18N
                buff.add(snapshot.create(blockStart, blockLength, "text/x-java"));
                buff.add(snapshot.create(");", "text/x-java")); //NOI18N
            } else {
                boolean unfinishedScriptlet = false;
                if (isUnfinishedScriptletInQueue(tokenSequence)) {
                    // see issue #213963 - we are trying to cut rest of the tag after the caret position
                    int caretOffset = GsfUtilities.getLastKnownCaretOffset(snapshot, null);
                    if (caretOffset - blockStart > 0) {
                        blockLength = Math.min(blockLength, caretOffset - blockStart);
                        unfinishedScriptlet = true;
                    }
                }
                buff.add(snapshot.create(blockStart, blockLength, "text/x-java"));
                //https://netbeans.org/bugzilla/show_bug.cgi?id=231452
                if(unfinishedScriptlet) {
                    buff.add(snapshot.create(" ; ", "text/x-java"));
                }
            }
        } else if (token.id() == JspTokenId.TAG && "include".equals(tokenText)) {
            processIncludes(false, getIncludedPath(tokenSequence));
        }
    } while (tokenSequence.moveNext());

    processJavaInTagValues(tokenSequence); //repositions the tokenSequence


    String extendsClass = null; //NOI18N
    PageInfo pageInfo = getPageInfo();

    if (pageInfo != null) {
        extendsClass = pageInfo.getExtends();
    }

    if (extendsClass == null ||
            // workaround for issue #116314
            "org.apache.jasper.runtime.HttpJspBase".equals(extendsClass)){ //NOI18N
        extendsClass = "HttpServlet"; //NOI18N
    }

    header.add(snapshot.create("\nclass SimplifiedJSPServlet extends ", "text/x-java")); //NOI18N

    if (pageExtends != null){
        header.add(pageExtends);
    } else {
        header.add(snapshot.create(extendsClass, "text/x-java")); //NOI18N
    }

    header.add(snapshot.create(" {\n\tprivate static final long serialVersionUID = 1L;\n", "text/x-java")); //NOI18N

    implicitImports.add(snapshot.create(createImplicitImportStatements(localImportsFound), "text/x-java"));
    beanDeclarations.add(snapshot.create("\n" + createBeanVarDeclarations(localBeansFound), "text/x-java"));
}
 
Example 8
Source File: SyntaxHighlighting.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if the token should be treated as a block. Returns true if the token
 * does NOT representa a block. Blocks are highlighted as a whole, including
 * free space after the trailing whitespaces on the line.
 * 
 * @param t token to check
 * @return true, if token is normal text; false if block.
 */
private static boolean noBlock(Token t) {
    return t.getProperty("highlight.block") != Boolean.TRUE; // NOI18N
}
 
Example 9
Source File: SyntaxHighlighting.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if the token should be treated as a block. Returns true if the token
 * does NOT representa a block. Blocks are highlighted as a whole, including
 * free space after the trailing whitespaces on the line.
 * 
 * @param t token to check
 * @return true, if token is normal text; false if block.
 */
private static boolean noBlock(Token t) {
    return t.getProperty("highlight.block") != Boolean.TRUE; // NOI18N
}