do not propose lambdas if body is not on the line - pt2
authorEgor.Ushakov <egor.ushakov@jetbrains.com>
Wed, 17 Jun 2015 18:21:07 +0000 (21:21 +0300)
committerEgor.Ushakov <egor.ushakov@jetbrains.com>
Wed, 17 Jun 2015 18:26:49 +0000 (21:26 +0300)
java/debugger/impl/src/com/intellij/debugger/impl/DebuggerUtilsEx.java

index 7bf87785592371a10b140baf24c7e64e6070abdf..ce8a1dfe9a3da1f1f3bdce6565150e69e8987776 100644 (file)
@@ -791,8 +791,8 @@ public abstract class DebuggerUtilsEx extends DebuggerUtils {
   public static List<PsiLambdaExpression> collectLambdas(@NotNull SourcePosition position, final boolean onlyOnTheLine) {
     ApplicationManager.getApplication().assertReadAccessAllowed();
     PsiFile file = position.getFile();
-    int line = position.getLine();
-    Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
+    final int line = position.getLine();
+    final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
     if (document == null || line >= document.getLineCount()) {
       return Collections.emptyList();
     }
@@ -812,8 +812,7 @@ public abstract class DebuggerUtilsEx extends DebuggerUtils {
       @Override
       public void visitLambdaExpression(PsiLambdaExpression expression) {
         super.visitLambdaExpression(expression);
-        PsiElement body = expression.getBody();
-        if (!onlyOnTheLine || (body != null && intersects(lineRange, body))) {
+        if (!onlyOnTheLine || getFirstElementOnTheLine(expression, document, line) != null) {
           lambdas.add(expression);
         }
       }
@@ -841,15 +840,17 @@ public abstract class DebuggerUtilsEx extends DebuggerUtils {
   @Nullable
   public static PsiElement getFirstElementOnTheLine(PsiLambdaExpression lambda, Document document, int line) {
     ApplicationManager.getApplication().assertReadAccessAllowed();
-    TextRange lineRange = new TextRange(document.getLineStartOffset(line), document.getLineEndOffset(line));
+    TextRange lineRange = DocumentUtil.getLineTextRange(document, line);
     if (!intersects(lineRange, lambda)) return null;
     PsiElement body = lambda.getBody();
+    if (body == null || !intersects(lineRange, body)) return null;
     if (body instanceof PsiCodeBlock) {
       for (PsiStatement statement : ((PsiCodeBlock)body).getStatements()) {
         if (intersects(lineRange, statement)) {
           return statement;
         }
       }
+      return null;
     }
     return body;
   }