ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSEmbeddingProvider.java
branchcanvas
changeset 1439 6c04b26f9a9a
parent 1438 03cd1b3e2e70
parent 1437 3ec3ae9699ef
child 1440 c943709738df
     1.1 --- a/ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSEmbeddingProvider.java	Tue Feb 11 10:48:24 2014 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,188 +0,0 @@
     1.4 -/**
     1.5 - * Back 2 Browser Bytecode Translator
     1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 - *
     1.8 - * This program is free software: you can redistribute it and/or modify
     1.9 - * it under the terms of the GNU General Public License as published by
    1.10 - * the Free Software Foundation, version 2 of the License.
    1.11 - *
    1.12 - * This program is distributed in the hope that it will be useful,
    1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 - * GNU General Public License for more details.
    1.16 - *
    1.17 - * You should have received a copy of the GNU General Public License
    1.18 - * along with this program. Look for COPYING file in the top folder.
    1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 - */
    1.21 -package org.apidesign.bck2brwsr.ide.editor;
    1.22 -
    1.23 -import com.sun.source.tree.AnnotationTree;
    1.24 -import com.sun.source.tree.AssignmentTree;
    1.25 -import com.sun.source.tree.CompilationUnitTree;
    1.26 -import com.sun.source.tree.ExpressionTree;
    1.27 -import com.sun.source.tree.LiteralTree;
    1.28 -import com.sun.source.tree.MethodTree;
    1.29 -import com.sun.source.util.SourcePositions;
    1.30 -import com.sun.source.util.TreePath;
    1.31 -import com.sun.source.util.TreePathScanner;
    1.32 -import com.sun.source.util.Trees;
    1.33 -import java.io.IOException;
    1.34 -import java.util.ArrayList;
    1.35 -import java.util.Collection;
    1.36 -import java.util.Collections;
    1.37 -import java.util.List;
    1.38 -import java.util.concurrent.atomic.AtomicBoolean;
    1.39 -import javax.lang.model.element.TypeElement;
    1.40 -import javax.swing.text.Document;
    1.41 -import org.netbeans.api.editor.mimelookup.MimeRegistration;
    1.42 -import org.netbeans.api.java.source.CompilationInfo;
    1.43 -import org.netbeans.api.java.source.JavaParserResultTask;
    1.44 -import org.netbeans.api.java.source.JavaSource;
    1.45 -import org.netbeans.api.lexer.Language;
    1.46 -import org.netbeans.api.lexer.TokenHierarchy;
    1.47 -import org.netbeans.api.lexer.TokenSequence;
    1.48 -import org.netbeans.modules.parsing.api.Snapshot;
    1.49 -import org.netbeans.modules.parsing.spi.Parser;
    1.50 -import org.netbeans.modules.parsing.spi.Scheduler;
    1.51 -import org.netbeans.modules.parsing.spi.SchedulerEvent;
    1.52 -import org.netbeans.modules.parsing.spi.SchedulerTask;
    1.53 -import org.netbeans.modules.parsing.spi.TaskFactory;
    1.54 -import org.openide.util.Exceptions;
    1.55 -
    1.56 -/**
    1.57 - *
    1.58 - * @author Tomas Zezula
    1.59 - */
    1.60 -public final class JSEmbeddingProvider extends JavaParserResultTask<Parser.Result> {
    1.61 -
    1.62 -    private static final int PRIORITY = 1000;
    1.63 -    private static final String JS_ANNOTATION = "org.apidesign.bck2brwsr.core.JavaScriptBody";  //NOI18N
    1.64 -    private static final String BODY = "body";                            //NOI18N
    1.65 -    private static final String JAVA_MIME_TYPE = "text/x-java";           //NOI18N
    1.66 -    private static final String JAVASCRIPT_MIME_TYPE = "text/javascript"; //NOI18N
    1.67 -    private final AtomicBoolean canceled = new AtomicBoolean();
    1.68 -
    1.69 -    private JSEmbeddingProvider() {
    1.70 -        super(JavaSource.Phase.ELEMENTS_RESOLVED);
    1.71 -    }
    1.72 -
    1.73 -    @Override
    1.74 -    public int getPriority() {
    1.75 -        return PRIORITY;
    1.76 -    }
    1.77 -
    1.78 -    @Override
    1.79 -    public void cancel() {
    1.80 -        canceled.set(true);
    1.81 -    }
    1.82 -
    1.83 -    @Override
    1.84 -    public Class<? extends Scheduler> getSchedulerClass() {
    1.85 -        return Scheduler.EDITOR_SENSITIVE_TASK_SCHEDULER;
    1.86 -    }
    1.87 -
    1.88 -    @Override
    1.89 -    public void run(Parser.Result t, SchedulerEvent se) {
    1.90 -        canceled.set(false);
    1.91 -        final CompilationInfo ci = CompilationInfo.get(t);
    1.92 -        final CompilationUnitTree cu = ci.getCompilationUnit();
    1.93 -        final Trees trees = ci.getTrees();
    1.94 -        final SourcePositions sp = trees.getSourcePositions();
    1.95 -        final Finder f = new Finder(trees);
    1.96 -        final List<LiteralTree> result = new ArrayList<LiteralTree>();
    1.97 -        f.scan(cu, result);
    1.98 -        if (!result.isEmpty()) {
    1.99 -            try {
   1.100 -                final TokenHierarchy<Document> tk = TokenHierarchy.get(ci.getDocument());
   1.101 -                final Language<?> java = Language.find(JAVA_MIME_TYPE);
   1.102 -                final Language<?> javaScript = Language.find(JAVASCRIPT_MIME_TYPE);
   1.103 -                if (java != null && javaScript != null) {
   1.104 -                    final TokenSequence<?> seq = tk.tokenSequence(java);
   1.105 -                    if (seq != null) {
   1.106 -                        for (LiteralTree lt : result) {
   1.107 -                            final int start = (int) sp.getStartPosition(cu, lt);
   1.108 -                            final int end = (int) sp.getEndPosition(cu, lt);
   1.109 -                            seq.move(start);
   1.110 -                            while (seq.moveNext() && seq.offset() < end) {
   1.111 -                                seq.createEmbedding(javaScript, 1, 1, true);
   1.112 -                            }
   1.113 -                        }
   1.114 -                    }
   1.115 -                }
   1.116 -            } catch (IOException ioe) {
   1.117 -                Exceptions.printStackTrace(ioe);
   1.118 -            }
   1.119 -        }
   1.120 -    }
   1.121 -
   1.122 -
   1.123 -
   1.124 -
   1.125 -    private static final class Finder extends TreePathScanner<Void, List<? super LiteralTree>> {
   1.126 -
   1.127 -        private final Trees trees;
   1.128 -        private CompilationUnitTree cu;
   1.129 -        private boolean inEmbedding;
   1.130 -
   1.131 -        Finder(final Trees trees) {
   1.132 -            this.trees = trees;
   1.133 -        }
   1.134 -
   1.135 -        @Override
   1.136 -        public Void visitCompilationUnit(
   1.137 -                final CompilationUnitTree unit,
   1.138 -                final List p) {
   1.139 -            this.cu = unit;
   1.140 -            return super.visitCompilationUnit(unit, p);
   1.141 -        }
   1.142 -
   1.143 -
   1.144 -
   1.145 -        @Override
   1.146 -        public Void visitMethod(
   1.147 -                final MethodTree m,
   1.148 -                final List<? super LiteralTree> p) {
   1.149 -            for (AnnotationTree a : m.getModifiers().getAnnotations()) {
   1.150 -                final TypeElement ae = (TypeElement) trees.getElement(TreePath.getPath(cu, a.getAnnotationType()));
   1.151 -                if (ae != null && JS_ANNOTATION.contentEquals(ae.getQualifiedName())) {
   1.152 -                    final List<? extends ExpressionTree> args =  a.getArguments();
   1.153 -                    for (ExpressionTree kvp : args) {
   1.154 -                        if (kvp instanceof AssignmentTree) {
   1.155 -                            final AssignmentTree assignemt = (AssignmentTree) kvp;
   1.156 -                            if (BODY.equals(assignemt.getVariable().toString())) {
   1.157 -                                inEmbedding = true;
   1.158 -                                try {
   1.159 -                                    scan(assignemt.getExpression(), p);
   1.160 -                                } finally {
   1.161 -                                    inEmbedding = false;
   1.162 -                                }
   1.163 -                            }
   1.164 -                        }
   1.165 -                    }
   1.166 -                }
   1.167 -            }
   1.168 -            return null;
   1.169 -        }
   1.170 -
   1.171 -        @Override
   1.172 -        public Void visitLiteral(LiteralTree node, List<? super LiteralTree> p) {
   1.173 -            if (inEmbedding) {
   1.174 -                p.add(node);
   1.175 -            }
   1.176 -            return super.visitLiteral(node, p);
   1.177 -        }
   1.178 -
   1.179 -    }
   1.180 -
   1.181 -    @MimeRegistration(
   1.182 -            service = TaskFactory.class,
   1.183 -            mimeType = JAVA_MIME_TYPE)
   1.184 -    public static final class Factory extends TaskFactory {
   1.185 -        @Override
   1.186 -        public Collection<? extends SchedulerTask> create(Snapshot snpsht) {
   1.187 -            return Collections.singleton(new JSEmbeddingProvider());
   1.188 -        }
   1.189 -    }
   1.190 -
   1.191 -}