-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package de.markiewb.netbeans.plugins.hints.ternary; | ||
|
||
import org.netbeans.spi.editor.hints.ErrorDescription; | ||
import org.netbeans.spi.editor.hints.Fix; | ||
import org.netbeans.spi.editor.hints.Severity; | ||
import org.netbeans.spi.java.hints.ErrorDescriptionFactory; | ||
import org.netbeans.spi.java.hints.Hint; | ||
import org.netbeans.spi.java.hints.HintContext; | ||
import org.netbeans.spi.java.hints.TriggerPattern; | ||
import org.openide.util.NbBundle; | ||
|
||
/** | ||
* | ||
* @author markiewb | ||
*/ | ||
@NbBundle.Messages({ | ||
"DN_ToTernaryReturn=Convert to XXX", | ||
"DESC_ToTernaryReturn=Converts xxxx. <br>For example: <tt>xxxx</tt> will be transformed to <tt>xxx</tt><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>", | ||
"DN_ToTernaryAssign=Convert to XXX", | ||
"DESC_ToTernaryAssign=Converts xxxx. <br>For example: <tt>xxxx</tt> will be transformed to <tt>xxx</tt><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",}) | ||
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=XXX") | ||
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 = "$type $var;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=XXX") | ||
public static ErrorDescription toTernaryAssign(HintContext ctx) { | ||
Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToTernaryAssign(), ctx.getPath(), "$type $var = ($cond$)?$a:$b;"); | ||
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToTernaryAssign(), fix); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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 de.markiewb.netbeans.plugins.hints.apache.commons.ToBlankFix; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
import org.netbeans.modules.java.hints.test.api.HintTest; | ||
import org.netbeans.spi.editor.hints.ErrorDescription; | ||
import org.netbeans.spi.java.hints.HintContext; | ||
import org.openide.filesystems.FileUtil; | ||
|
||
/** | ||
* | ||
* @author markiewb | ||
*/ | ||
public class ToTernaryTest { | ||
|
||
@Test | ||
public void testToTernaryAssign() throws Exception { | ||
HintTest.create() | ||
.input("package test;\n" | ||
+ "public class Test {\n" | ||
+ " public static void main(String[] args) {\n" | ||
+ " int s; if (true){s = 1;}else{s = 0;};\n" | ||
+ " }\n" | ||
+ "}\n") | ||
.run(ToTernary.class) | ||
.findWarning("4:18-4:53:hint:" + Bundle.ERR_ToTernaryAssign()) | ||
.applyFix() | ||
.assertCompilable() | ||
.assertOutput("package test;\n" | ||
+ "public class Test {\n" | ||
+ " public static void main(String[] args) {\n" | ||
+ " String s=null;\n" | ||
+ " int s = (true)?1:0;\n" | ||
+ " }\n" | ||
+ "}\n"); | ||
|
||
} | ||
|
||
} |