Introducing 'otherwise' condition (currently meaningfull only on fixes), which will be true if and only if no other fix is available
authorJan Lahoda <jlahoda@netbeans.org>
Sun, 16 Jan 2011 13:56:54 +0100
changeset 5147c501d1f2ad8
parent 513 4e4e3643aa73
child 515 367b58d3bdc9
Introducing 'otherwise' condition (currently meaningfull only on fixes), which will be true if and only if no other fix is available
file/src/org/netbeans/modules/jackpot30/file/Condition.java
file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintLexer.java
file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintTokenId.java
file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintsParser.java
file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintsWorker.java
file/test/unit/src/org/netbeans/modules/jackpot30/file/DeclarativeHintLexerTest.java
file/test/unit/src/org/netbeans/modules/jackpot30/file/otherwise-test.hint
file/test/unit/src/org/netbeans/modules/jackpot30/file/otherwise-test.test
     1.1 --- a/file/src/org/netbeans/modules/jackpot30/file/Condition.java	Sun Jan 16 13:56:54 2011 +0100
     1.2 +++ b/file/src/org/netbeans/modules/jackpot30/file/Condition.java	Sun Jan 16 13:56:54 2011 +0100
     1.3 @@ -163,4 +163,22 @@
     1.4              return "(FALSE)";
     1.5          }
     1.6      }
     1.7 +
     1.8 +    public static final class Otherwise extends Condition {
     1.9 +
    1.10 +        public Otherwise() {
    1.11 +            super(false);
    1.12 +        }
    1.13 +
    1.14 +        @Override
    1.15 +        public boolean holds(Context ctx, boolean global) {
    1.16 +            return false;
    1.17 +        }
    1.18 +
    1.19 +        @Override
    1.20 +        public String toString() {
    1.21 +            return "(OTHERWISE)";
    1.22 +        }
    1.23 +    }
    1.24 +    
    1.25  }
     2.1 --- a/file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintLexer.java	Sun Jan 16 13:56:54 2011 +0100
     2.2 +++ b/file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintLexer.java	Sun Jan 16 13:56:54 2011 +0100
     2.3 @@ -257,6 +257,7 @@
     2.4          map.put(";;", DeclarativeHintTokenId.DOUBLE_SEMICOLON);
     2.5          map.put("%%", DeclarativeHintTokenId.DOUBLE_PERCENT);
     2.6          map.put("instanceof", DeclarativeHintTokenId.INSTANCEOF);
     2.7 +        map.put("otherwise", DeclarativeHintTokenId.OTHERWISE);
     2.8          map.put(":", DeclarativeHintTokenId.COLON);
     2.9  
    2.10          TOKENS = Collections.unmodifiableMap(map);
     3.1 --- a/file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintTokenId.java	Sun Jan 16 13:56:54 2011 +0100
     3.2 +++ b/file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintTokenId.java	Sun Jan 16 13:56:54 2011 +0100
     3.3 @@ -64,6 +64,7 @@
     3.4      LEADS_TO("operator"),
     3.5      AND("operator"),
     3.6      INSTANCEOF("keyword"),
     3.7 +    OTHERWISE("keyword"),
     3.8      NOT("operator"),
     3.9      DOUBLE_SEMICOLON("operator"),
    3.10      DOUBLE_PERCENT("operator"),
     4.1 --- a/file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintsParser.java	Sun Jan 16 13:56:54 2011 +0100
     4.2 +++ b/file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintsParser.java	Sun Jan 16 13:56:54 2011 +0100
     4.3 @@ -40,9 +40,6 @@
     4.4  package org.netbeans.modules.jackpot30.file;
     4.5  
     4.6  import java.io.File;
     4.7 -import java.net.MalformedURLException;
     4.8 -import java.util.ArrayList;
     4.9 -import java.util.StringTokenizer;
    4.10  import java.util.logging.Level;
    4.11  import java.util.logging.Logger;
    4.12  import java.security.CodeSource;
    4.13 @@ -72,7 +69,6 @@
    4.14  import org.netbeans.api.annotations.common.NonNull;
    4.15  import org.netbeans.api.annotations.common.NullAllowed;
    4.16  import org.netbeans.api.java.classpath.ClassPath;
    4.17 -import org.netbeans.api.java.platform.JavaPlatform;
    4.18  import org.netbeans.api.java.source.ClasspathInfo;
    4.19  import org.netbeans.api.java.source.CompilationController;
    4.20  import org.netbeans.api.java.source.JavaSource;
    4.21 @@ -83,6 +79,7 @@
    4.22  import org.netbeans.modules.jackpot30.file.Condition.Instanceof;
    4.23  import org.netbeans.modules.jackpot30.file.Condition.MethodInvocation;
    4.24  import org.netbeans.modules.jackpot30.file.Condition.MethodInvocation.ParameterKind;
    4.25 +import org.netbeans.modules.jackpot30.file.Condition.Otherwise;
    4.26  import org.netbeans.modules.jackpot30.spi.Hacks;
    4.27  import org.netbeans.spi.editor.hints.ErrorDescription;
    4.28  import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
    4.29 @@ -269,6 +266,14 @@
    4.30  
    4.31      private void parseCondition(List<Condition> conditions, List<int[]> spans) {
    4.32          int conditionStart = input.offset();
    4.33 +
    4.34 +        if (id() == OTHERWISE) {
    4.35 +            nextToken();
    4.36 +            conditions.add(new Otherwise());
    4.37 +            spans.add(new int[] {conditionStart, input.offset()});
    4.38 +            return ;
    4.39 +        }
    4.40 +
    4.41          boolean not = false;
    4.42  
    4.43          if (id() == NOT) {
     5.1 --- a/file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintsWorker.java	Sun Jan 16 13:56:54 2011 +0100
     5.2 +++ b/file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintsWorker.java	Sun Jan 16 13:56:54 2011 +0100
     5.3 @@ -46,6 +46,7 @@
     5.4  import java.util.List;
     5.5  import java.util.Map;
     5.6  import javax.lang.model.type.TypeMirror;
     5.7 +import org.netbeans.modules.jackpot30.file.Condition.Otherwise;
     5.8  import org.netbeans.modules.jackpot30.file.conditionapi.Context;
     5.9  import org.netbeans.modules.jackpot30.spi.HintContext;
    5.10  import org.netbeans.modules.jackpot30.spi.HintContext.MessageKind;
    5.11 @@ -106,6 +107,13 @@
    5.12  
    5.13              try {
    5.14                  for (Condition c : fix.getConditions()) {
    5.15 +                    if (c instanceof Otherwise) {
    5.16 +                        if (editorFixes.isEmpty()) {
    5.17 +                            continue;
    5.18 +                        } else {
    5.19 +                            continue OUTER;
    5.20 +                        }
    5.21 +                    }
    5.22                      if (!c.holds(context, false)) {
    5.23                          continue OUTER;
    5.24                      }
     6.1 --- a/file/test/unit/src/org/netbeans/modules/jackpot30/file/DeclarativeHintLexerTest.java	Sun Jan 16 13:56:54 2011 +0100
     6.2 +++ b/file/test/unit/src/org/netbeans/modules/jackpot30/file/DeclarativeHintLexerTest.java	Sun Jan 16 13:56:54 2011 +0100
     6.3 @@ -275,7 +275,7 @@
     6.4  
     6.5      @Test
     6.6      public void testImportsAndPredicates() {
     6.7 -        String text = "<?import java.util.List;?>'test': 1 + 1 => 1 + 1;;<?private boolean doTest() {return false;}?>'test2': 1 + 1 => 1 + 1;;";
     6.8 +        String text = "<?import java.util.List;?>'test': 1 + 1 => 1 + 1;;<?private boolean doTest() {return false;}?>'test2': 1 + 1 => 1 + 1 :: otherwise;;";
     6.9          TokenHierarchy<?> hi = TokenHierarchy.create(text, language());
    6.10          TokenSequence<?> ts = hi.tokenSequence();
    6.11          assertNextTokenEquals(ts, JAVA_BLOCK, "<?import java.util.List;?>");
    6.12 @@ -290,7 +290,10 @@
    6.13          assertNextTokenEquals(ts, COLON, ":");
    6.14          assertNextTokenEquals(ts, JAVA_SNIPPET, " 1 + 1 ");
    6.15          assertNextTokenEquals(ts, LEADS_TO, "=>");
    6.16 -        assertNextTokenEquals(ts, JAVA_SNIPPET, " 1 + 1");
    6.17 +        assertNextTokenEquals(ts, JAVA_SNIPPET, " 1 + 1 ");
    6.18 +        assertNextTokenEquals(ts, DOUBLE_COLON, "::");
    6.19 +        assertNextTokenEquals(ts, WHITESPACE, " ");
    6.20 +        assertNextTokenEquals(ts, OTHERWISE, "otherwise");
    6.21          assertNextTokenEquals(ts, DOUBLE_SEMICOLON, ";;");
    6.22  
    6.23          assertFalse(ts.moveNext());
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/file/test/unit/src/org/netbeans/modules/jackpot30/file/otherwise-test.hint	Sun Jan 16 13:56:54 2011 +0100
     7.3 @@ -0,0 +1,5 @@
     7.4 +   assert $cond;
     7.5 +=> assert $left != $right; :: matchesWithBind($cond, "$left == $right")
     7.6 +=> assert $left == $right; :: matchesWithBind($cond, "$left != $right")
     7.7 +=> assert !$cond           :: otherwise
     7.8 +;;
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/file/test/unit/src/org/netbeans/modules/jackpot30/file/otherwise-test.test	Sun Jan 16 13:56:54 2011 +0100
     8.3 @@ -0,0 +1,28 @@
     8.4 +%%TestCase pos-1
     8.5 +package test;
     8.6 +public class Test {
     8.7 +    private void t(int v1, int v2) {
     8.8 +        assert v1 == v2;
     8.9 +    }
    8.10 +}
    8.11 +%%=>
    8.12 +package test;
    8.13 +public class Test {
    8.14 +    private void t(int v1, int v2) {
    8.15 +        assert v1 != v2;
    8.16 +    }
    8.17 +}
    8.18 +%%TestCase pos-2
    8.19 +package test;
    8.20 +public class Test {
    8.21 +    private void t(boolean b1, boolean b2) {
    8.22 +        assert b1 && b2;
    8.23 +    }
    8.24 +}
    8.25 +%%=>
    8.26 +package test;
    8.27 +public class Test {
    8.28 +    private void t(boolean b1, boolean b2) {
    8.29 +        assert !(b1 && b2);
    8.30 +    }
    8.31 +}