Merge remote-tracking branch 'origin/master'
authorDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Thu, 10 Nov 2016 17:06:22 +0000 (18:06 +0100)
committerDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Thu, 10 Nov 2016 17:06:22 +0000 (18:06 +0100)
15 files changed:
java/debugger/impl/src/com/intellij/debugger/actions/JavaSmartStepIntoHandler.java
java/java-analysis-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationInspectionBase.java
java/java-analysis-impl/src/com/intellij/codeInspection/nullable/ChangeNullableDefaultsFix.java
java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/EnableOptimizeImportsOnTheFlyFix.java
java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java
java/java-impl/src/com/intellij/codeInspection/util/SpecialAnnotationsUtil.java
java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java
java/java-tests/testData/refactoring/introduceVariable/DistinguishLambdaParams.after.java [new file with mode: 0644]
java/java-tests/testData/refactoring/introduceVariable/DistinguishLambdaParams.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java
java/jdkAnnotations/java/lang/annotations.xml
platform/lang-api/src/com/intellij/codeInsight/intention/impl/PriorityLocalQuickFixWrapper.java [deleted file]
platform/platform-impl/src/com/intellij/execution/impl/AsyncFilterRunner.java
platform/platform-impl/src/com/intellij/execution/impl/EditorHyperlinkSupport.java
plugins/ant/src/com/intellij/lang/ant/quickfix/AntChangeContextFix.java

index a6f570cbbf7843f0da0e5ce588e247b9170b060d..af67bc2e641f4adc94b68008f4a0b617d80667ea 100644 (file)
@@ -215,14 +215,14 @@ public class JavaSmartStepIntoHandler extends JvmSmartStepIntoHandler {
         private void visitConditional(@Nullable PsiElement condition,
                                       @Nullable PsiElement thenBranch,
                                       @Nullable PsiElement elseBranch) {
-          if (condition != null) {
+          if (condition != null && checkTextRange(condition, true)) {
             condition.accept(this);
           }
           ThreeState conditionRes = evaluateCondition(condition);
-          if (conditionRes != ThreeState.NO && thenBranch != null) {
+          if (conditionRes != ThreeState.NO && thenBranch != null && checkTextRange(thenBranch, true)) {
             thenBranch.accept(this);
           }
-          if (conditionRes != ThreeState.YES && elseBranch != null) {
+          if (conditionRes != ThreeState.YES && elseBranch != null && checkTextRange(elseBranch, true)) {
             elseBranch.accept(this);
           }
         }
@@ -246,7 +246,7 @@ public class JavaSmartStepIntoHandler extends JvmSmartStepIntoHandler {
           super.visitExpression(expression);
         }
 
-        boolean checkTextRange(PsiElement expression, boolean expand) {
+        boolean checkTextRange(@NotNull PsiElement expression, boolean expand) {
           TextRange range = expression.getTextRange();
           if (lineRange.intersects(range)) {
             if (expand) {
index 3ed5933928636775affdf296be92d2132c1a4e58..8d2b4ede119e4f2a38a0e9c0fa42935d5390ce78 100644 (file)
@@ -722,11 +722,6 @@ public class UnusedDeclarationInspectionBase extends GlobalInspectionTool {
     }
   }
 
-  @Override
-  public void initialize(@NotNull GlobalInspectionContext context) {
-    super.initialize(context);
-  }
-
   @TestOnly
   public List<EntryPoint> getExtensions() {
     return myExtensions;
index 237f227c4aa3aee648fc986a30374192233c4af9..c3549dffc93bda27047ca081cd7ba3b20247677b 100644 (file)
@@ -49,6 +49,11 @@ class ChangeNullableDefaultsFix implements LocalQuickFix {
     return "Make \"" + (myNotNullName != null ? myNotNullName : myNullableName) + "\" default annotation";
   }
 
+  @Override
+  public boolean startInWriteAction() {
+    return false;
+  }
+
   @Override
   public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
     if (myNotNullName != null) {
index 91e9910e146224c4a65fe68efc0f3790be83777d..f3ac84cf05bb8affd4ae4b478e92e4d593ddcafb 100644 (file)
@@ -55,6 +55,6 @@ public class EnableOptimizeImportsOnTheFlyFix implements IntentionAction, LowPri
 
   @Override
   public boolean startInWriteAction() {
-    return true;
+    return false;
   }
 }
index 1fe7c3b046dbb9aaf37458afd53b82d5f8ee8ada..a654116fb20d7e220450a677474f730251090a0f 100644 (file)
@@ -619,7 +619,7 @@ public class UnusedDeclarationPresentation extends DefaultInspectionToolPresenta
 
     @Override
     public boolean startInWriteAction() {
-      return true;
+      return false;
     }
   }
 
index ccc0845ec42f66703a1fdde7463b8a0c7e200715..65ab2dd9c422a7a30c96d5f2a1aeb202cd26f5aa 100644 (file)
@@ -29,13 +29,13 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.project.ProjectManager;
 import com.intellij.openapi.ui.Messages;
 import com.intellij.openapi.util.Condition;
-import com.intellij.psi.*;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiFile;
 import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.ui.*;
 import com.intellij.ui.components.JBList;
 import com.intellij.util.IconUtil;
 import com.intellij.util.IncorrectOperationException;
-import com.intellij.util.containers.Predicate;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
@@ -170,7 +170,7 @@ public class SpecialAnnotationsUtil {
 
       @Override
       public boolean startInWriteAction() {
-        return true;
+        return false;
       }
     };
   }
index 3b67150d9a5e1ea954980c3160bdccff3795a4af..9d2fd531c7865a8f94ac4d2c6636b8c1fff10711 100644 (file)
@@ -750,29 +750,33 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
   }
 
   private static ExpressionOccurrenceManager createOccurrenceManager(PsiExpression expr, PsiElement tempContainer) {
-    boolean skipForStatement = true;
-    final PsiForStatement forStatement = PsiTreeUtil.getParentOfType(expr, PsiForStatement.class);
-    if (forStatement != null) {
-      Set<PsiVariable> vars = new HashSet<>();
-      SyntaxTraverser.psiTraverser().withRoot(expr)
-        .filter(element -> element instanceof PsiReferenceExpression)
-        .forEach(element -> {
-          final PsiElement resolve = ((PsiReferenceExpression)element).resolve();
-          if (resolve instanceof PsiVariable) {
-            vars.add((PsiVariable)resolve);
-          }
-        });
-      skipForStatement = vars.stream().noneMatch(variable -> PsiTreeUtil.isAncestor(forStatement.getInitialization(), variable, true));
-    }
+    Set<PsiVariable> vars = new HashSet<>();
+    SyntaxTraverser.psiTraverser().withRoot(expr)
+      .filter(element -> element instanceof PsiReferenceExpression)
+      .forEach(element -> {
+        final PsiElement resolve = ((PsiReferenceExpression)element).resolve();
+        if (resolve instanceof PsiVariable) {
+          vars.add((PsiVariable)resolve);
+        }
+      });
 
     PsiElement containerParent = tempContainer;
     PsiElement lastScope = tempContainer;
     while (true) {
       if (containerParent instanceof PsiFile) break;
       if (containerParent instanceof PsiMethod) break;
-      // allow to find occurrences outside lambda as we allow this for loops, ifs, etc
-      // if (containerParent instanceof PsiLambdaExpression) break;
-      if (!skipForStatement && containerParent instanceof PsiForStatement) break;
+      if (containerParent instanceof PsiLambdaExpression) {
+        PsiParameter[] parameters = ((PsiLambdaExpression)containerParent).getParameterList().getParameters();
+        if (Arrays.stream(parameters).anyMatch(parameter -> vars.contains(parameter))) {
+          break;
+        }
+      }
+      if (containerParent instanceof PsiForStatement) {
+        PsiForStatement forStatement = (PsiForStatement)containerParent;
+        if (vars.stream().anyMatch(variable -> PsiTreeUtil.isAncestor(forStatement.getInitialization(), variable, true))) {
+          break;
+        }
+      }
       containerParent = containerParent.getParent();
       if (containerParent instanceof PsiCodeBlock) {
         lastScope = containerParent;
diff --git a/java/java-tests/testData/refactoring/introduceVariable/DistinguishLambdaParams.after.java b/java/java-tests/testData/refactoring/introduceVariable/DistinguishLambdaParams.after.java
new file mode 100644 (file)
index 0000000..fb37564
--- /dev/null
@@ -0,0 +1,15 @@
+import java.util.function.UnaryOperator;
+
+class Main {
+  public static void main(String[] args) {
+    UnaryOperator<String> f1 = s -> {
+      System.out.println("foo");
+        String temp = s.trim();
+        return temp;
+    };
+    UnaryOperator<String> f2 = s -> {
+      System.out.println("foo");
+      return s.trim();
+    };
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/introduceVariable/DistinguishLambdaParams.java b/java/java-tests/testData/refactoring/introduceVariable/DistinguishLambdaParams.java
new file mode 100644 (file)
index 0000000..0307800
--- /dev/null
@@ -0,0 +1,14 @@
+import java.util.function.UnaryOperator;
+
+class Main {
+  public static void main(String[] args) {
+    UnaryOperator<String> f1 = s -> {
+      System.out.println("foo");
+      return <selection>s.trim()</selection>;
+    };
+    UnaryOperator<String> f2 = s -> {
+      System.out.println("foo");
+      return s.trim();
+    };
+  }
+}
\ No newline at end of file
index f7da07c33723b4fff328dec674d911e29a566299..67f74b3d377d9b7da9c91329151db4a451881d1d 100644 (file)
@@ -211,6 +211,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
     doTest(new MockIntroduceVariableHandler("temp", true, false, false, "int"));
   }
 
+  public void testDistinguishLambdaParams() {
+    doTest(new MockIntroduceVariableHandler("temp", true, false, false, CommonClassNames.JAVA_LANG_STRING));
+  }
+
   public void testDuplicateGenericExpressions() {
     doTest(new MockIntroduceVariableHandler("temp", true, false, false, "Foo2<? extends java.lang.Runnable>"));
   }
index 098ef28db6a43d8bc2176952ad93b370fb58e745..7ce0469d41b846db9ccf37c4701681e7c3949504 100644 (file)
@@ -64,9 +64,6 @@
   <item name='java.lang.Enum T valueOf(java.lang.Class&lt;T&gt;, java.lang.String) 1'>
     <annotation name='org.jetbrains.annotations.NotNull'/>
   </item>
-  <item name="java.lang.Iterable Iterator&lt;T&gt; iterator()">
-    <annotation name="org.jetbrains.annotations.NotNull" />
-  </item>
   <item name='java.lang.Iterable java.util.Iterator&lt;T&gt; iterator()'>
     <annotation name='org.jetbrains.annotations.NotNull'/>
   </item>
diff --git a/platform/lang-api/src/com/intellij/codeInsight/intention/impl/PriorityLocalQuickFixWrapper.java b/platform/lang-api/src/com/intellij/codeInsight/intention/impl/PriorityLocalQuickFixWrapper.java
deleted file mode 100644 (file)
index 67b14a8..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Copyright 2000-2014 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.intention.impl;
-
-import com.intellij.codeInsight.intention.HighPriorityAction;
-import com.intellij.codeInsight.intention.LowPriorityAction;
-import com.intellij.codeInspection.LocalQuickFix;
-import com.intellij.codeInspection.ProblemDescriptor;
-import com.intellij.openapi.project.Project;
-import org.jetbrains.annotations.NotNull;
-
-/**
- * @author Danila Ponomarenko
- */
-public abstract class PriorityLocalQuickFixWrapper implements LocalQuickFix {
-  private final LocalQuickFix fix;
-
-  private PriorityLocalQuickFixWrapper(@NotNull LocalQuickFix fix) {
-    this.fix = fix;
-  }
-
-  @NotNull
-  @Override
-  public String getName() {
-    return fix.getName();
-  }
-
-  @NotNull
-  @Override
-  public String getFamilyName() {
-    return fix.getFamilyName();
-  }
-
-  @Override
-  public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
-    fix.applyFix(project, descriptor);
-  }
-
-  private static class HighPriorityLocalQuickFixWrapper extends PriorityLocalQuickFixWrapper implements HighPriorityAction {
-    protected HighPriorityLocalQuickFixWrapper(@NotNull LocalQuickFix fix) {
-      super(fix);
-    }
-  }
-
-  private static class NormalPriorityLocalQuickFixWrapper extends PriorityLocalQuickFixWrapper {
-    protected NormalPriorityLocalQuickFixWrapper(@NotNull LocalQuickFix fix) {
-      super(fix);
-    }
-  }
-
-
-  private static class LowPriorityLocalQuickFixWrapper extends PriorityLocalQuickFixWrapper implements LowPriorityAction {
-    protected LowPriorityLocalQuickFixWrapper(@NotNull LocalQuickFix fix) {
-      super(fix);
-    }
-  }
-
-  @NotNull
-  public static LocalQuickFix highPriority(@NotNull LocalQuickFix fix) {
-    return new HighPriorityLocalQuickFixWrapper(fix);
-  }
-
-  @NotNull
-  public static LocalQuickFix normalPriority(@NotNull LocalQuickFix fix) {
-    return new NormalPriorityLocalQuickFixWrapper(fix);
-  }
-
-  @NotNull
-  public static LocalQuickFix lowPriority(@NotNull LocalQuickFix fix) {
-    return new LowPriorityLocalQuickFixWrapper(fix);
-  }
-}
index 53fa4cd40fc0abaa93e801d111b71a21dbae7cf1..fd67e3707f31c43e192b67359b8434e0d356322f 100644 (file)
@@ -135,7 +135,7 @@ class AsyncFilterRunner {
       List<Filter.Result> results = new ArrayList<>();
       for (LineHighlighter task : tasks) {
         ProgressManager.checkCanceled();
-        if (!marker.isValid()) return FilterResults.EMPTY;
+        if (!marker.isValid() || marker.getEndOffset() == 0) return FilterResults.EMPTY;
         ContainerUtil.addIfNotNull(results, task.compute());
       }
       return new FilterResults(markerOffset, marker, results);
@@ -146,8 +146,8 @@ class AsyncFilterRunner {
   private static LineHighlighter processLine(Document document, Filter filter, int line) {
     int lineEnd = document.getLineEndOffset(line);
     int endOffset = lineEnd + (lineEnd < document.getTextLength() ? 1 /* for \n */ : 0);
-    String text = EditorHyperlinkSupport.getLineText(document, line, true);
-    return () -> checkRange(filter, endOffset, filter.applyFilter(text, endOffset));
+    CharSequence text = EditorHyperlinkSupport.getLineSequence(document, line, true);
+    return () -> checkRange(filter, endOffset, filter.applyFilter(text.toString(), endOffset));
   }
 
   private static Filter.Result checkRange(Filter filter, int endOffset, Filter.Result result) {
index 2af4e82824e1997cadf70ba417bfc257841968c3..ef17f54006869093b1ce8e0022fcca934bdbd070 100644 (file)
@@ -371,11 +371,15 @@ public class EditorHyperlinkSupport {
 
   @NotNull
   public static String getLineText(@NotNull Document document, int lineNumber, boolean includeEol) {
+    return getLineSequence(document, lineNumber, includeEol).toString();
+  }
+
+  static CharSequence getLineSequence(@NotNull Document document, int lineNumber, boolean includeEol) {
     int endOffset = document.getLineEndOffset(lineNumber);
     if (includeEol && endOffset < document.getTextLength()) {
       endOffset++;
     }
-    return document.getImmutableCharSequence().subSequence(document.getLineStartOffset(lineNumber), endOffset).toString();
+    return document.getImmutableCharSequence().subSequence(document.getLineStartOffset(lineNumber), endOffset);
   }
 
   private static class HyperlinkInfoTextAttributes extends TextAttributes {
index 21945f4943d83edb701def0f6add3fa31635d8f0..415d8faf834c41af9bdb7242c5420567fe74349e 100644 (file)
@@ -54,6 +54,11 @@ public class AntChangeContextFix extends BaseIntentionAction {
     return true;
   }
 
+  @Override
+  public boolean startInWriteAction() {
+    return false;
+  }
+
   public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
     final HectorComponent component = new HectorComponent(file);
     //final JComponent focusComponent = findComponentToFocus(component);