public boolean canMoveTo(@Nullable final SourcePosition position) { if (position == null || !position.getFile().isValid()) { return false; } final PsiFile psiFile = position.getFile(); final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(psiFile); if (document == null) { return false; } final int spOffset = position.getOffset(); if (spOffset < 0) { return false; } final BreakpointManager breakpointManager = DebuggerManagerEx.getInstanceEx(getProject()).getBreakpointManager(); return breakpointManager.findBreakpoint(document, spOffset, getCategory()) == null; }
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]; }