ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBody.java
changeset 1432 b5d60677fec7
parent 1431 6ceb7c457073
child 1433 3d696782eab9
     1.1 --- a/ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBody.java	Mon Jan 13 12:37:03 2014 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,149 +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.ExpressionTree;
    1.25 -import com.sun.source.tree.LiteralTree;
    1.26 -import com.sun.source.tree.MethodTree;
    1.27 -import com.sun.source.tree.Tree.Kind;
    1.28 -import com.sun.source.tree.VariableTree;
    1.29 -import com.sun.source.util.TreePath;
    1.30 -import java.util.ArrayList;
    1.31 -import java.util.Arrays;
    1.32 -import java.util.Collections;
    1.33 -import java.util.List;
    1.34 -import org.netbeans.api.java.lexer.JavaTokenId;
    1.35 -import static org.netbeans.api.java.lexer.JavaTokenId.BLOCK_COMMENT;
    1.36 -import static org.netbeans.api.java.lexer.JavaTokenId.JAVADOC_COMMENT;
    1.37 -import static org.netbeans.api.java.lexer.JavaTokenId.LINE_COMMENT;
    1.38 -import static org.netbeans.api.java.lexer.JavaTokenId.WHITESPACE;
    1.39 -import org.netbeans.api.java.source.CompilationInfo;
    1.40 -import org.netbeans.api.java.source.TreeMaker;
    1.41 -import org.netbeans.api.lexer.Token;
    1.42 -import org.netbeans.api.lexer.TokenSequence;
    1.43 -import org.netbeans.spi.editor.hints.ErrorDescription;
    1.44 -import org.netbeans.spi.editor.hints.Fix;
    1.45 -import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
    1.46 -import org.netbeans.spi.java.hints.Hint;
    1.47 -import org.netbeans.spi.java.hints.HintContext;
    1.48 -import org.netbeans.spi.java.hints.JavaFix;
    1.49 -import org.netbeans.spi.java.hints.TriggerTreeKind;
    1.50 -import org.openide.util.NbBundle.Messages;
    1.51 -
    1.52 -@Hint(displayName = "#DN_JSNI2JavaScriptBody", description = "#DESC_JSNI2JavaScriptBody", category = "general")
    1.53 -@Messages({
    1.54 -    "DN_JSNI2JavaScriptBody=JSNI to @JavaScriptBody",
    1.55 -    "DESC_JSNI2JavaScriptBody=JSNI to @JavaScriptBody"
    1.56 -})
    1.57 -public class JSNI2JavaScriptBody {
    1.58 -
    1.59 -    @TriggerTreeKind(Kind.METHOD)
    1.60 -    @Messages("ERR_JSNI2JavaScriptBody=Can convert JSNI to @JavaScriptBody")
    1.61 -    public static ErrorDescription computeWarning(final HintContext ctx) {
    1.62 -        Token<JavaTokenId> token = findBlockToken(ctx.getInfo(), ctx.getPath(), ctx);
    1.63 -
    1.64 -        if (token == null) {
    1.65 -            return null;
    1.66 -        }
    1.67 -
    1.68 -        Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
    1.69 -        return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_JSNI2JavaScriptBody(), fix);
    1.70 -    }
    1.71 -
    1.72 -    private static Token<JavaTokenId> findBlockToken(CompilationInfo info, TreePath path, HintContext ctx) {
    1.73 -        int end = (int) info.getTrees().getSourcePositions().getEndPosition(path.getCompilationUnit(), path.getLeaf());
    1.74 -        TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
    1.75 -
    1.76 -        if (ts == null) return null;
    1.77 -
    1.78 -        ts.move(end);
    1.79 -
    1.80 -        if ((ctx != null && ctx.isCanceled()) || !ts.movePrevious() || ts.token().id() != JavaTokenId.SEMICOLON) return null;
    1.81 -
    1.82 -        OUTER: while (ts.movePrevious()) {
    1.83 -            if (ctx != null && ctx.isCanceled()) return null;
    1.84 -
    1.85 -            switch (ts.token().id()) {
    1.86 -                case WHITESPACE: break;
    1.87 -                case LINE_COMMENT: break;
    1.88 -                case JAVADOC_COMMENT: break;
    1.89 -                case BLOCK_COMMENT:
    1.90 -                    final CharSequence tok = ts.token().text();
    1.91 -                    final int l = tok.length(); 
    1.92 -                    if (l > 4 
    1.93 -                        && tok.subSequence(0, 4).toString().equals("/*-{") // NOI18N
    1.94 -                        && tok.subSequence(l - 4, l).toString().equals("}-*/") // NOI18N
    1.95 -                    ) {
    1.96 -                        return ts.offsetToken();
    1.97 -                    }
    1.98 -                    break;
    1.99 -                default:
   1.100 -                    break OUTER;
   1.101 -            }
   1.102 -        }
   1.103 -
   1.104 -        return null;
   1.105 -    }
   1.106 -
   1.107 -    private static final class FixImpl extends JavaFix {
   1.108 -
   1.109 -        public FixImpl(CompilationInfo info, TreePath tp) {
   1.110 -            super(info, tp);
   1.111 -        }
   1.112 -
   1.113 -        @Override
   1.114 -        @Messages("FIX_JSNI2JavaScriptBody=Convert JSNI to @JavaScriptBody")
   1.115 -        protected String getText() {
   1.116 -            return Bundle.FIX_JSNI2JavaScriptBody();
   1.117 -        }
   1.118 -
   1.119 -        @Override
   1.120 -        protected void performRewrite(TransformationContext ctx) {
   1.121 -            Token<JavaTokenId> jsniComment = findBlockToken(ctx.getWorkingCopy(), ctx.getPath(), null);
   1.122 -
   1.123 -            if (jsniComment == null) {
   1.124 -                //XXX: warn?
   1.125 -                return ;
   1.126 -            }
   1.127 -            
   1.128 -            JsniCommentTokenizer tok = new JsniCommentTokenizer();
   1.129 -            ManglingSink ms = new ManglingSink();
   1.130 -            final CharSequence cmnt = jsniComment.text();
   1.131 -            tok.process(cmnt.subSequence(4, cmnt.length() - 4), ms);
   1.132 -
   1.133 -            TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
   1.134 -            MethodTree mt = (MethodTree) ctx.getPath().getLeaf();
   1.135 -            List<LiteralTree> params = new ArrayList<LiteralTree>();
   1.136 -
   1.137 -            for (VariableTree p : mt.getParameters()) {
   1.138 -                params.add(make.Literal(p.getName().toString()));
   1.139 -            }
   1.140 -
   1.141 -            AnnotationTree jsBody = make.Annotation(make.QualIdent("org.apidesign.bck2brwsr.core.JavaScriptBody"),
   1.142 -                Arrays.<ExpressionTree>asList(
   1.143 -                    make.Assignment(make.Identifier("args"), make.NewArray(null, Collections.<ExpressionTree>emptyList(), params)),
   1.144 -                    make.Assignment(make.Identifier("body"), make.Literal(ms.out.toString()))
   1.145 -                )
   1.146 -            );
   1.147 -
   1.148 -
   1.149 -            ctx.getWorkingCopy().rewrite(mt.getModifiers(), make.addModifiersAnnotation(mt.getModifiers(), jsBody));
   1.150 -        }
   1.151 -    }
   1.152 -}