|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright 2014 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
| 7 | + * Other names may be trademarks of their respective owners. |
| 8 | + * |
| 9 | + * The contents of this file are subject to the terms of either the GNU |
| 10 | + * General Public License Version 2 only ("GPL") or the Common |
| 11 | + * Development and Distribution License("CDDL") (collectively, the |
| 12 | + * "License"). You may not use this file except in compliance with the |
| 13 | + * License. You can obtain a copy of the License at |
| 14 | + * http://www.netbeans.org/cddl-gplv2.html |
| 15 | + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
| 16 | + * specific language governing permissions and limitations under the |
| 17 | + * License. When distributing the software, include this License Header |
| 18 | + * Notice in each file and include the License file at |
| 19 | + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
| 20 | + * particular file as subject to the "Classpath" exception as provided |
| 21 | + * by Oracle in the GPL Version 2 section of the License file that |
| 22 | + * accompanied this code. If applicable, add the following below the |
| 23 | + * License Header, with the fields enclosed by brackets [] replaced by |
| 24 | + * your own identifying information: |
| 25 | + * "Portions Copyrighted [year] [name of copyright owner]" |
| 26 | + * |
| 27 | + * If you wish your version of this file to be governed by only the CDDL |
| 28 | + * or only the GPL Version 2, indicate your decision by adding |
| 29 | + * "[Contributor] elects to include this software in this distribution |
| 30 | + * under the [CDDL or GPL Version 2] license." If you do not indicate a |
| 31 | + * single choice of license, a recipient has the option to distribute |
| 32 | + * your version of this file under either the CDDL, the GPL Version 2 or |
| 33 | + * to extend the choice of license to its licensees as provided above. |
| 34 | + * However, if you add GPL Version 2 code and therefore, elected the GPL |
| 35 | + * Version 2 license, then the option applies only if the new code is |
| 36 | + * made subject to such option by the copyright holder. |
| 37 | + * |
| 38 | + * Contributor(s): |
| 39 | + * |
| 40 | + * Portions Copyrighted 2014 Sun Microsystems, Inc. |
| 41 | + * Portions Copyrighted 2014 [email protected] |
| 42 | + */ |
| 43 | +package de.markiewb.netbeans.plugins.hints.ternary; |
| 44 | + |
| 45 | +import com.sun.source.util.TreePath; |
| 46 | +import org.netbeans.spi.editor.hints.ErrorDescription; |
| 47 | +import org.netbeans.spi.editor.hints.Fix; |
| 48 | +import org.netbeans.spi.editor.hints.Severity; |
| 49 | +import org.netbeans.spi.java.hints.ErrorDescriptionFactory; |
| 50 | +import org.netbeans.spi.java.hints.Hint; |
| 51 | +import org.netbeans.spi.java.hints.HintContext; |
| 52 | +import org.netbeans.spi.java.hints.TriggerPattern; |
| 53 | +import org.netbeans.spi.java.hints.TriggerPatterns; |
| 54 | +import org.openide.util.NbBundle; |
| 55 | + |
| 56 | +/** |
| 57 | + * |
| 58 | + * @author markiewb |
| 59 | + */ |
| 60 | +@NbBundle.Messages({ |
| 61 | + "DN_ToTernaryReturn=Convert to ternary return", |
| 62 | + "DESC_ToTernaryReturn=Converts if statement to ternary return statement. <p>For example: <tt>if ($cond) {return $a;} else {return $b;}</tt> will be transformed to <tt>return ($cond) ? $a : $b;</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>", |
| 63 | + "DN_ToIfElseReturn=Convert to if/else return", |
| 64 | + "DESC_ToIfElseReturn=Converts ternary return statement to if/else statement. <p>For example: <tt>return ($cond) ? $a : $b;</tt> will be transformed to <tt>if ($cond) {return $a;} else {return $b;}</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>", |
| 65 | + "DN_ToTernaryAssign=Convert to ternary", |
| 66 | + "DESC_ToTernaryAssign=Converts if statement to ternary assignment statement. <p>For example: <tt>if ($cond) {$var = $a;} else {$var = $b;}</tt> will be transformed to <tt>$var = ($cond) ? $a : $b;</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>", |
| 67 | + "DN_ToIfElseAssign=Convert to if/else", |
| 68 | + "DESC_ToIfElseAssign=Converts ternary assignment statement to if/else statement. <p>For example: <tt>$var = ($cond) ? $a : $b;</tt> will be transformed to <tt>if ($cond) {$var = $a;} else {$var = $b;}</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",}) |
| 69 | +public class ToTernary { |
| 70 | + |
| 71 | + @TriggerPattern(value = "if ($cond) {return $a;} else {return $b;}") |
| 72 | + @Hint(displayName = "#DN_ToTernaryReturn", description = "#DESC_ToTernaryReturn", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) |
| 73 | + @NbBundle.Messages("ERR_ToTernaryReturn=Convert to ternary return") |
| 74 | + public static ErrorDescription toTernaryReturn(HintContext ctx) { |
| 75 | + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToTernaryReturn(), ctx.getPath(), "return ($cond)?$a:$b;"); |
| 76 | + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToTernaryReturn(), fix); |
| 77 | + } |
| 78 | + |
| 79 | + @TriggerPattern(value = "return ($cond)?$a:$b;") |
| 80 | + @Hint(displayName = "#DN_ToIfElseReturn", description = "#DESC_ToIfElseReturn", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) |
| 81 | + @NbBundle.Messages("ERR_ToIfElseReturn=Convert to if/else return") |
| 82 | + public static ErrorDescription toIfElseReturn(HintContext ctx) { |
| 83 | + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseReturn(), ctx.getPath(), "if ($cond) {return $a;} else {return $b;}"); |
| 84 | + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToIfElseReturn(), fix); |
| 85 | + } |
| 86 | + |
| 87 | + @TriggerPattern(value = "if ($cond) {$var = $a;}else {$var = $b;}") |
| 88 | + @Hint(displayName = "#DN_ToTernaryAssign", description = "#DESC_ToTernaryAssign", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) |
| 89 | + @NbBundle.Messages("ERR_ToTernaryAssign=Convert to ternary") |
| 90 | + public static ErrorDescription toTernaryAssign(HintContext ctx) { |
| 91 | + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToTernaryAssign(), ctx.getPath(), "$var=($cond)?$a:$b;"); |
| 92 | + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToTernaryAssign(), fix); |
| 93 | + } |
| 94 | + |
| 95 | + @TriggerPatterns( |
| 96 | + { |
| 97 | +// @TriggerPattern(value = "$var1=($cond)?$a:$b;"), |
| 98 | + @TriggerPattern(value = "$var2=$cond?$a:$b;"), |
| 99 | +// @TriggerPattern(value = "$type $var3=($cond)?$a:$b;"), |
| 100 | + @TriggerPattern(value = "$type $var4=$cond?$a:$b;") |
| 101 | + } |
| 102 | + ) |
| 103 | + @Hint(displayName = "#DN_ToIfElseAssign", description = "#DESC_ToIfElseAssign", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) |
| 104 | + @NbBundle.Messages("ERR_ToIfElseAssign=Convert to if/else") |
| 105 | + public static ErrorDescription toIfElseAssign(HintContext ctx) { |
| 106 | + |
| 107 | + Fix fix = null; |
| 108 | +// if (ctx.getVariables().containsKey("$var1")) { |
| 109 | +// fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "if (($cond)) {$var1 = $a;} else {$var1 = $b;}"); |
| 110 | +// } |
| 111 | + if (ctx.getVariables().containsKey("$var2")) { |
| 112 | + fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "if ($cond) {$var2 = $a;} else {$var2 = $b;}"); |
| 113 | + } |
| 114 | +// if (ctx.getVariables().containsKey("$var3")) { |
| 115 | +// fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "$type $var3; if (($cond)) {$var3 = $a;} else {$var3 = $b;}"); |
| 116 | +// } |
| 117 | + if (ctx.getVariables().containsKey("$var4")) { |
| 118 | + fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "$type $var4; if ($cond) {$var4 = $a;} else {$var4 = $b;}"); |
| 119 | + } |
| 120 | + if (null != fix) { |
| 121 | + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToIfElseAssign(), fix); |
| 122 | + } else { |
| 123 | + return null; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments