ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBody.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 13 Feb 2013 12:45:02 +0100
branchide
changeset 718 b93760cedf02
parent 716 8d7f8719d29c
child 719 72a100b59e88
permissions -rw-r--r--
Using Igor's tokenizer in Lahvac's hint
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.ide.editor;
    19 
    20 import com.sun.source.tree.AnnotationTree;
    21 import com.sun.source.tree.ExpressionTree;
    22 import com.sun.source.tree.LiteralTree;
    23 import com.sun.source.tree.MethodTree;
    24 import com.sun.source.tree.Tree.Kind;
    25 import com.sun.source.tree.VariableTree;
    26 import com.sun.source.util.TreePath;
    27 import java.util.ArrayList;
    28 import java.util.Arrays;
    29 import java.util.Collections;
    30 import java.util.List;
    31 import java.util.regex.Matcher;
    32 import java.util.regex.Pattern;
    33 import org.netbeans.api.java.lexer.JavaTokenId;
    34 import static org.netbeans.api.java.lexer.JavaTokenId.BLOCK_COMMENT;
    35 import static org.netbeans.api.java.lexer.JavaTokenId.JAVADOC_COMMENT;
    36 import static org.netbeans.api.java.lexer.JavaTokenId.LINE_COMMENT;
    37 import static org.netbeans.api.java.lexer.JavaTokenId.WHITESPACE;
    38 import org.netbeans.api.java.source.CompilationInfo;
    39 import org.netbeans.api.java.source.TreeMaker;
    40 import org.netbeans.api.lexer.Token;
    41 import org.netbeans.api.lexer.TokenSequence;
    42 import org.netbeans.spi.editor.hints.ErrorDescription;
    43 import org.netbeans.spi.editor.hints.Fix;
    44 import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
    45 import org.netbeans.spi.java.hints.Hint;
    46 import org.netbeans.spi.java.hints.HintContext;
    47 import org.netbeans.spi.java.hints.JavaFix;
    48 import org.netbeans.spi.java.hints.TriggerTreeKind;
    49 import org.openide.util.NbBundle.Messages;
    50 
    51 @Hint(displayName = "#DN_JSNI2JavaScriptBody", description = "#DESC_JSNI2JavaScriptBody", category = "general")
    52 @Messages({
    53     "DN_JSNI2JavaScriptBody=JSNI to @JavaScriptBody",
    54     "DESC_JSNI2JavaScriptBody=JSNI to @JavaScriptBody"
    55 })
    56 public class JSNI2JavaScriptBody {
    57 
    58     @TriggerTreeKind(Kind.METHOD)
    59     @Messages("ERR_JSNI2JavaScriptBody=Can convert JSNI to @JavaScriptBody")
    60     public static ErrorDescription computeWarning(final HintContext ctx) {
    61         Token<JavaTokenId> token = findBlockToken(ctx.getInfo(), ctx.getPath(), ctx);
    62 
    63         if (token == null) {
    64             return null;
    65         }
    66 
    67         Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
    68         return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_JSNI2JavaScriptBody(), fix);
    69     }
    70 
    71     private static Token<JavaTokenId> findBlockToken(CompilationInfo info, TreePath path, HintContext ctx) {
    72         int end = (int) info.getTrees().getSourcePositions().getEndPosition(path.getCompilationUnit(), path.getLeaf());
    73         TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
    74 
    75         if (ts == null) return null;
    76 
    77         ts.move(end);
    78 
    79         if ((ctx != null && ctx.isCanceled()) || !ts.movePrevious() || ts.token().id() != JavaTokenId.SEMICOLON) return null;
    80 
    81         OUTER: while (ts.movePrevious()) {
    82             if (ctx != null && ctx.isCanceled()) return null;
    83 
    84             switch (ts.token().id()) {
    85                 case WHITESPACE: break;
    86                 case LINE_COMMENT: break;
    87                 case JAVADOC_COMMENT: break;
    88                 case BLOCK_COMMENT:
    89                     final CharSequence tok = ts.token().text();
    90                     final int l = tok.length(); 
    91                     if (l > 4 
    92                         && tok.subSequence(0, 4).toString().equals("/*-{") // NOI18N
    93                         && tok.subSequence(l - 4, l).toString().equals("}-*/") // NOI18N
    94                     ) {
    95                         return ts.offsetToken();
    96                     }
    97                     break;
    98                 default:
    99                     break OUTER;
   100             }
   101         }
   102 
   103         return null;
   104     }
   105 
   106     private static final class FixImpl extends JavaFix {
   107 
   108         public FixImpl(CompilationInfo info, TreePath tp) {
   109             super(info, tp);
   110         }
   111 
   112         @Override
   113         @Messages("FIX_JSNI2JavaScriptBody=Convert JSNI to @JavaScriptBody")
   114         protected String getText() {
   115             return Bundle.FIX_JSNI2JavaScriptBody();
   116         }
   117 
   118         @Override
   119         protected void performRewrite(TransformationContext ctx) {
   120             Token<JavaTokenId> jsniComment = findBlockToken(ctx.getWorkingCopy(), ctx.getPath(), null);
   121 
   122             if (jsniComment == null) {
   123                 //XXX: warn?
   124                 return ;
   125             }
   126             
   127             JsniCommentTokenizer tok = new JsniCommentTokenizer();
   128             ManglingSink ms = new ManglingSink();
   129             final CharSequence cmnt = jsniComment.text();
   130             tok.process(cmnt.subSequence(4, cmnt.length() - 4), ms);
   131 
   132             TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
   133             MethodTree mt = (MethodTree) ctx.getPath().getLeaf();
   134             List<LiteralTree> params = new ArrayList<LiteralTree>();
   135 
   136             for (VariableTree p : mt.getParameters()) {
   137                 params.add(make.Literal(p.getName().toString()));
   138             }
   139 
   140             AnnotationTree jsBody = make.Annotation(make.QualIdent("org.apidesign.bck2brwsr.core.JavaScriptBody"),
   141                 Arrays.<ExpressionTree>asList(
   142                     make.Assignment(make.Identifier("args"), make.NewArray(null, Collections.<ExpressionTree>emptyList(), params)),
   143                     make.Assignment(make.Identifier("body"), make.Literal(ms.out.toString()))
   144                 )
   145             );
   146 
   147 
   148             ctx.getWorkingCopy().rewrite(mt.getModifiers(), make.addModifiersAnnotation(mt.getModifiers(), jsBody));
   149         }
   150     }
   151 }