Skip to content

Commit

Permalink
issue #31: draft
Browse files Browse the repository at this point in the history
  • Loading branch information
markiewb committed May 12, 2014
1 parent f1da1f8 commit 49c3051
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
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);
}
}
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");

}

}

0 comments on commit 49c3051

Please sign in to comment.