IDEA-141772 Debugger: Smart Step Into inside lambda suggests method calls outside...
authorEgor.Ushakov <egor.ushakov@jetbrains.com>
Wed, 24 Jun 2015 10:24:40 +0000 (13:24 +0300)
committerEgor.Ushakov <egor.ushakov@jetbrains.com>
Wed, 24 Jun 2015 10:30:19 +0000 (13:30 +0300)
java/debugger/impl/src/com/intellij/debugger/actions/JavaSmartStepIntoHandler.java

index c8ba3fa8bd2f5b16e3ca4342d4699b6a31bc0989..727e1b49aa82dd8718a08c477644386cc25d6f65 100644 (file)
@@ -24,9 +24,9 @@ import com.intellij.openapi.util.Ref;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.*;
+import com.intellij.util.DocumentUtil;
 import com.intellij.util.Range;
 import com.intellij.util.containers.OrderedSet;
-import com.intellij.util.text.CharArrayUtil;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
@@ -65,10 +65,15 @@ public class JavaSmartStepIntoHandler extends JvmSmartStepIntoHandler {
     if (line >= doc.getLineCount()) {
       return Collections.emptyList(); // the document has been changed
     }
-    final int startOffset = doc.getLineStartOffset(line);
-    final TextRange lineRange = new TextRange(startOffset, doc.getLineEndOffset(line));
-    final int offset = CharArrayUtil.shiftForward(doc.getCharsSequence(), startOffset, " \t");
-    PsiElement element = DebuggerUtilsEx.findElementAt(file, offset);
+    TextRange curLineRange = DocumentUtil.getLineTextRange(doc, line);
+    PsiElement element = position.getElementAt();
+    PsiElement method = getBody(DebuggerUtilsEx.getContainingMethod(element));
+    final TextRange lineRange = (method != null) ? curLineRange.intersection(method.getTextRange()) : curLineRange;
+
+    if (lineRange == null || lineRange.isEmpty()) {
+      return Collections.emptyList();
+    }
+
     if (element != null && !(element instanceof PsiCompiledElement)) {
       do {
         final PsiElement parent = element.getParent();
@@ -202,4 +207,13 @@ public class JavaSmartStepIntoHandler extends JvmSmartStepIntoHandler {
     return Collections.emptyList();
   }
 
+  private static PsiElement getBody(@Nullable PsiElement containingMethod) {
+    if (containingMethod instanceof PsiMethod) {
+      return ((PsiMethod)containingMethod).getBody();
+    }
+    else if (containingMethod instanceof PsiLambdaExpression) {
+      return ((PsiLambdaExpression)containingMethod).getBody();
+    }
+    return null;
+  }
 }