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

The following examples show how to use org.netbeans.api.lexer.TokenSequence#createEmbedding() . 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: LexerEmbeddingAdapter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void defineEmbeddings(TokenSequence seq, ConsoleModel model, ConsoleSection s) {
    F: for (Rng r : s.getPartRanges()) {
        seq.move(r.start);
        Token<JShellToken> tukac;
        
        W: while (seq.moveNext() && seq.offset() < r.end) {
            tukac = seq.token();
            switch (tukac.id()) {
                
                case JAVA:
                    seq.createEmbedding(JavaTokenId.language(), 0, 0);
                    
                    // fall through
                case WHITESPACE: 
                    break;
                    
                default:
                    break W;
            }
        }
    }
}
 
Example 2
Source File: JSEmbeddingProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void colorizeJSB(final CompilationInfo ci) {
    final CompilationUnitTree cu = ci.getCompilationUnit();
    final Trees trees = ci.getTrees();
    final SourcePositions sp = trees.getSourcePositions();
    final Finder f = new Finder(trees);
    final List<LiteralTree> result = new ArrayList<>();
    f.scan(cu, result);
    if (!result.isEmpty()) {
        try {
            final TokenHierarchy<Document> tk = TokenHierarchy.get(ci.getDocument());
            final Language<?> java = Language.find(JAVA_MIME_TYPE);
            final Language<?> javaScript = Language.find(JAVASCRIPT_MIME_TYPE);
            if (java != null && javaScript != null) {
                final TokenSequence<?> seq = tk.tokenSequence(java);
                if (seq != null) {
                    for (LiteralTree lt : result) {
                        final int start = (int) sp.getStartPosition(cu, lt);
                        final int end = (int) sp.getEndPosition(cu, lt);
                        seq.move(start);
                        while (seq.moveNext() && seq.offset() < end) {
                            if (
                                seq.embedded() != null &&
                                seq.embedded().language() != null &&
                                "text/x-java-string".equals(seq.embedded().language().mimeType())
                            ) {
                                seq.removeEmbedding(seq.embedded().language());
                            }
                            seq.createEmbedding(javaScript, 1, 1, true);
                        }
                    }
                }
            }
        } catch (IOException ioe) {
            LOG.log(Level.WARNING, null, ioe);
        }
    }
}