Пример #1
0
  public static TextWithImports getEditorText(final Editor editor) {
    if (editor == null) {
      return null;
    }
    final Project project = editor.getProject();
    if (project == null) return null;

    String defaultExpression = editor.getSelectionModel().getSelectedText();
    if (defaultExpression == null) {
      int offset = editor.getCaretModel().getOffset();
      PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
      if (psiFile != null) {
        PsiElement elementAtCursor = psiFile.findElementAt(offset);
        if (elementAtCursor != null) {
          final EditorTextProvider textProvider =
              EditorTextProvider.EP.forLanguage(elementAtCursor.getLanguage());
          if (textProvider != null) {
            final TextWithImports editorText = textProvider.getEditorText(elementAtCursor);
            if (editorText != null) return editorText;
          }
        }
      }
    } else {
      return new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, defaultExpression);
    }
    return null;
  }
 public boolean canMoveTo(SourcePosition position) {
   if (!super.canMoveTo(position)) {
     return false;
   }
   final Document document =
       PsiDocumentManager.getInstance(getProject()).getDocument(position.getFile());
   return canAddLineBreakpoint(myProject, document, position.getLine());
 }
Пример #3
0
  public static List<PsiLambdaExpression> collectLambdas(
      @NotNull SourcePosition position, final boolean onlyOnTheLine) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    PsiFile file = position.getFile();
    final int line = position.getLine();
    final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
    if (document == null || line >= document.getLineCount()) {
      return Collections.emptyList();
    }
    PsiElement element = position.getElementAt();
    final TextRange lineRange = DocumentUtil.getLineTextRange(document, line);
    do {
      PsiElement parent = element.getParent();
      if (parent == null || (parent.getTextOffset() < lineRange.getStartOffset())) {
        break;
      }
      element = parent;
    } while (true);

    final List<PsiLambdaExpression> lambdas = new ArrayList<PsiLambdaExpression>(3);
    final PsiElementVisitor lambdaCollector =
        new JavaRecursiveElementVisitor() {
          @Override
          public void visitLambdaExpression(PsiLambdaExpression expression) {
            super.visitLambdaExpression(expression);
            if (!onlyOnTheLine || getFirstElementOnTheLine(expression, document, line) != null) {
              lambdas.add(expression);
            }
          }
        };
    element.accept(lambdaCollector);
    // add initial lambda if we're inside already
    PsiElement method = getContainingMethod(element);
    if (method instanceof PsiLambdaExpression) {
      lambdas.add((PsiLambdaExpression) method);
    }
    for (PsiElement sibling = getNextElement(element);
        sibling != null;
        sibling = getNextElement(sibling)) {
      if (!intersects(lineRange, sibling)) {
        break;
      }
      sibling.accept(lambdaCollector);
    }
    return lambdas;
  }
  public static boolean canAddLineBreakpoint(
      Project project, final Document document, final int lineIndex) {
    if (lineIndex < 0 || lineIndex >= document.getLineCount()) {
      return false;
    }
    final BreakpointManager breakpointManager =
        DebuggerManagerEx.getInstanceEx(project).getBreakpointManager();
    final LineBreakpoint breakpointAtLine =
        breakpointManager.findBreakpoint(
            document, document.getLineStartOffset(lineIndex), CATEGORY);
    if (breakpointAtLine != null) {
      // there already exists a line breakpoint at this line
      return false;
    }
    PsiDocumentManager.getInstance(project).commitDocument(document);

    final boolean[] canAdd = new boolean[] {false};
    XDebuggerUtil.getInstance()
        .iterateLine(
            project,
            document,
            lineIndex,
            new Processor<PsiElement>() {
              @Override
              public boolean process(PsiElement element) {
                if ((element instanceof PsiWhiteSpace)
                    || (PsiTreeUtil.getParentOfType(element, PsiComment.class, false) != null)) {
                  return true;
                }
                PsiElement child = element;
                while (element != null) {

                  final int offset = element.getTextOffset();
                  if (offset >= 0) {
                    if (document.getLineNumber(offset) != lineIndex) {
                      break;
                    }
                  }
                  child = element;
                  element = element.getParent();
                }

                if (child instanceof PsiMethod
                    && child.getTextRange().getEndOffset()
                        >= document.getLineEndOffset(lineIndex)) {
                  PsiCodeBlock body = ((PsiMethod) child).getBody();
                  if (body == null) {
                    canAdd[0] = false;
                  } else {
                    PsiStatement[] statements = body.getStatements();
                    canAdd[0] =
                        statements.length > 0
                            && document.getLineNumber(statements[0].getTextOffset()) == lineIndex;
                  }
                } else {
                  canAdd[0] = true;
                }
                return false;
              }
            });

    return canAdd[0];
  }