diff --git a/README.md b/README.md index dc4f181..5342ae1 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,126 @@ [![Build Status](https://travis-ci.org/markiewb/nb-additional-hints.svg?branch=master)](https://travis-ci.org/markiewb/nb-additional-hints) [![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=K4CMP92RZELE2) -Additional java hints for NetBeans IDE -=================== +Plugin page: http://plugins.netbeans.org/plugin/47589/ -Supported hints: -* "Replace '+' with 'MessageFormat.format()'" -* "Replace '+' with 'new StringBuilder().append()'" -* "Replace '+' with 'String.format()'" -* "Join literals" -* "Copy joined literals to clipboard" -* "Split at linebreaks" -* "Split at caret" (since 1.1) -* "Convert to assertTrue/assertFalse" (since 1.1) -* "Support transformation to BigDecimal constants" (since 1.1) -* "Remove "public abstract" modifiers from method declarations within interfaces" (since 1.1) -* ... and more see http://plugins.netbeans.org/plugin/47589/?show=true +
This plugin is orginally based on code from the "I18N Checker" plugin from Jan Lahoda. +The original sourcecode can be found at http://hg.netbeans.org/main/contrib/file/tip/editor.hints.i18n
+ +License remains CDDL-GPL-2-CP - http://www.netbeans.org/cddl-gplv2.html
+ ++Provide defects, request for enhancements and feedback at https://github.com/markiewb/nb-additional-hints/issues +
+Compatible to NetBeans 8.1+
+ \ No newline at end of file diff --git a/doc/screenshot.png b/doc/screenshot.png index 80c57c3..754683f 100644 Binary files a/doc/screenshot.png and b/doc/screenshot.png differ diff --git a/pom.xml b/pom.xml index 1ef7000..4f55be5 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@For example: if ($cond) {return $a;} else {return $b;} will be transformed to return ($cond) ? $a : $b;
Provided by nb-additional-hints plugin
", + "DN_ToIfElseReturn=Convert to if/else return", + "DESC_ToIfElseReturn=Converts ternary return statement to if/else statement.For example: return ($cond) ? $a : $b; will be transformed to if ($cond) {return $a;} else {return $b;}
Provided by nb-additional-hints plugin
", + "DN_ToTernaryAssign=Convert to ternary", + "DESC_ToTernaryAssign=Converts if statement to ternary assignment statement.For example: if ($cond) {$var = $a;} else {$var = $b;} will be transformed to $var = ($cond) ? $a : $b;
Provided by nb-additional-hints plugin
", + "DN_ToIfElseAssign=Convert to if/else", + "DESC_ToIfElseAssign=Converts ternary assignment statement to if/else statement.For example: $var = ($cond) ? $a : $b; will be transformed to if ($cond) {$var = $a;} else {$var = $b;}
Provided by nb-additional-hints plugin
",}) +public class ToTernary { + + @TriggerPattern(value = "if ($cond) {return $a;} else {return $b;}") + @Hint(displayName = "#DN_ToTernaryReturn", description = "#DESC_ToTernaryReturn", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) + @NbBundle.Messages("ERR_ToTernaryReturn=Convert to ternary return") + public static ErrorDescription toTernaryReturn(HintContext ctx) { + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToTernaryReturn(), ctx.getPath(), "return ($cond)?$a:$b;"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToTernaryReturn(), fix); + } + + @TriggerPattern(value = "return ($cond)?$a:$b;") + @Hint(displayName = "#DN_ToIfElseReturn", description = "#DESC_ToIfElseReturn", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) + @NbBundle.Messages("ERR_ToIfElseReturn=Convert to if/else return") + public static ErrorDescription toIfElseReturn(HintContext ctx) { + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseReturn(), ctx.getPath(), "if ($cond) {return $a;} else {return $b;}"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToIfElseReturn(), fix); + } + + @TriggerPattern(value = "if ($cond) {$var = $a;}else {$var = $b;}") + @Hint(displayName = "#DN_ToTernaryAssign", description = "#DESC_ToTernaryAssign", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) + @NbBundle.Messages("ERR_ToTernaryAssign=Convert to ternary") + public static ErrorDescription toTernaryAssign(HintContext ctx) { + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToTernaryAssign(), ctx.getPath(), "$var=($cond)?$a:$b;"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToTernaryAssign(), fix); + } + + @TriggerPatterns( + { +// @TriggerPattern(value = "$var1=($cond)?$a:$b;"), + @TriggerPattern(value = "$var2=$cond?$a:$b;"), +// @TriggerPattern(value = "$type $var3=($cond)?$a:$b;"), + @TriggerPattern(value = "$type $var4=$cond?$a:$b;") + } + ) + @Hint(displayName = "#DN_ToIfElseAssign", description = "#DESC_ToIfElseAssign", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) + @NbBundle.Messages("ERR_ToIfElseAssign=Convert to if/else") + public static ErrorDescription toIfElseAssign(HintContext ctx) { + + Fix fix = null; +// if (ctx.getVariables().containsKey("$var1")) { +// fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "if (($cond)) {$var1 = $a;} else {$var1 = $b;}"); +// } + if (ctx.getVariables().containsKey("$var2")) { + fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "if ($cond) {$var2 = $a;} else {$var2 = $b;}"); + } +// if (ctx.getVariables().containsKey("$var3")) { +// fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "$type $var3; if (($cond)) {$var3 = $a;} else {$var3 = $b;}"); +// } + if (ctx.getVariables().containsKey("$var4")) { + fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "$type $var4; if ($cond) {$var4 = $a;} else {$var4 = $b;}"); + } + if (null != fix) { + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToIfElseAssign(), fix); + } else { + return null; + } + } +} diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java new file mode 100644 index 0000000..01bcf51 --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java @@ -0,0 +1,192 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package de.markiewb.netbeans.plugins.hints.ternary; + +import org.junit.Ignore; +import org.junit.Test; +import org.netbeans.modules.java.hints.test.api.HintTest; + +/** + * + * @author markiewb + */ +public class ToTernaryTest { + + @Test + public void testToTernaryReturn() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static int main(String[] args) {\n" + + " if (tr|ue){return 1;}else{return 0;}\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("3:14-3:14:hint:" + Bundle.ERR_ToTernaryReturn()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static int main(String[] args) {\n" + + " return (true) ? 1 : 0;\n" + + " }\n" + + "}\n"); + + } + + @Test + public void testToIfElseReturn() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static int main(String[] args) {\n" + + " return (tr|ue) ? 1 : 0;\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("3:18-3:18:hint:" + Bundle.ERR_ToIfElseReturn()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static int main(String[] args) {\n" + + " if (true) { return 1; } else { return 0; }\n" + + " }\n" + + "}\n"); + + } + + @Ignore(value = "Since NB8.1: java.lang.IllegalArgumentException: No document available for MIME type: text/x-java\n" + +" at org.netbeans.api.editor.document.LineDocumentUtils.createDocument(LineDocumentUtils.java:554)") + @Test + public void testToTernaryAssign() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " if (t|rue){s = 1;}else{s = 0;}\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("4:13-4:13:hint:" + Bundle.ERR_ToTernaryAssign()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " s = (true) ? 1 : 0;\n" + + " }\n" + + "}\n"); + + } + + @Ignore(value = "Since NB8.1: java.lang.IllegalArgumentException: No document available for MIME type: text/x-java\n" + +" at org.netbeans.api.editor.document.LineDocumentUtils.createDocument(LineDocumentUtils.java:554)") + @Test + public void testToIfElseAssign() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " s = (t|rue) ? 1 : 0;\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("4:14-4:14:hint:" + Bundle.ERR_ToIfElseAssign()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " if (true) { s = 1; } else { s = 0; }\n" + + " }\n" + + "}\n"); + + } + + @Ignore(value = "Since NB8.1: java.lang.IllegalArgumentException: No document available for MIME type: text/x-java\n" + +" at org.netbeans.api.editor.document.LineDocumentUtils.createDocument(LineDocumentUtils.java:554)") + @Test + public void testToIfElseAssignWithoutBrackets() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " s = t|rue ? 1 : 0;\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("4:13-4:13:hint:" + Bundle.ERR_ToIfElseAssign()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " if (true) { s = 1; } else { s = 0; }\n" + + " }\n" + + "}\n"); + + } + + @Test + public void testToIfElseAssignNewVariable() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s = (t|rue) ? 1 : 0;\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("3:18-3:18:hint:" + Bundle.ERR_ToIfElseAssign()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " if (true) { s = 1; } else { s = 0; }\n" + + " }\n" + + "}\n"); + + } + + @Test + public void testToIfElseAssignNewVariableWithoutBrackets() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s = t|rue ? 1 : 0;\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("3:17-3:17:hint:" + Bundle.ERR_ToIfElseAssign()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " if (true) { s = 1; } else { s = 0; }\n" + + " }\n" + + "}\n"); + + } +}