private static void removeJSBreakpointsInDartFiles(final Project project) {
    final XBreakpointManager breakpointManager =
        XDebuggerManager.getInstance(project).getBreakpointManager();
    final Collection<XBreakpoint<?>> toRemove = new ArrayList<XBreakpoint<?>>();

    for (XBreakpoint<?> breakpoint : breakpointManager.getAllBreakpoints()) {
      final XSourcePosition position = breakpoint.getSourcePosition();
      if (position != null
          && position.getFile().getFileType() == DartFileType.INSTANCE
          && !(breakpoint.getType() instanceof DartLineBreakpointType)) {
        toRemove.add(breakpoint);
      }
    }

    if (!toRemove.isEmpty()) {
      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                public void run() {
                  for (XBreakpoint<?> breakpoint : toRemove) {
                    breakpointManager.removeBreakpoint(breakpoint);
                  }
                }
              });
    }
  }
  public void init() {
    XBreakpointManager manager = XDebuggerManager.getInstance(myProject).getBreakpointManager();
    manager.addBreakpointListener(
        new XBreakpointListener() {
          @Override
          public void breakpointAdded(@NotNull XBreakpoint xBreakpoint) {
            if (isJavaType(xBreakpoint)) {
              onBreakpointAdded(xBreakpoint);
            }
          }

          @Override
          public void breakpointRemoved(@NotNull XBreakpoint xBreakpoint) {
            onBreakpointRemoved(xBreakpoint);
          }

          @Override
          public void breakpointChanged(@NotNull XBreakpoint xBreakpoint) {
            Breakpoint breakpoint = myBreakpoints.get(xBreakpoint);
            if (breakpoint != null) {
              fireBreakpointChanged(breakpoint);
            }
          }
        });
  }
  protected void stepOver() {
    XDebugSession currentSession = XDebuggerManager.getInstance(getProject()).getCurrentSession();

    Assert.assertTrue(currentSession.isSuspended());
    Assert.assertEquals(0, myPausedSemaphore.availablePermits());

    currentSession.stepOver(false);
  }
  protected void smartStepInto(String funcName) {
    XDebugSession currentSession = XDebuggerManager.getInstance(getProject()).getCurrentSession();

    Assert.assertTrue(currentSession.isSuspended());
    Assert.assertEquals(0, myPausedSemaphore.availablePermits());

    myDebugProcess.startSmartStepInto(funcName);
  }
 @Override
 public void initComponent() {
   super.init();
   myDebuggerManager = DebuggerManagerEx.getInstanceEx(myProject);
   XDebuggerManager.getInstance(myProject)
       .getBreakpointManager()
       .addBreakpointListener(myBreakpointListener);
 }
 @Override
 public void disposeComponent() {
   super.dispose();
   XDebuggerManager.getInstance(myProject)
       .getBreakpointManager()
       .removeBreakpointListener(myBreakpointListener);
   myDebuggerManager = null;
 }
  protected void runToLine(int line) throws InvocationTargetException, InterruptedException {
    XDebugSession currentSession = XDebuggerManager.getInstance(getProject()).getCurrentSession();
    XSourcePosition position = currentSession.getCurrentPosition();

    currentSession.runToPosition(
        XDebuggerUtil.getInstance().createPosition(position.getFile(), line), false);

    waitForPause();
  }
  protected void stepIntoMyCode() {
    XDebugSession currentSession = XDebuggerManager.getInstance(getProject()).getCurrentSession();

    Assert.assertTrue(currentSession.isSuspended());
    Assert.assertEquals(0, myPausedSemaphore.availablePermits());

    PyDebugProcess debugProcess = (PyDebugProcess) currentSession.getDebugProcess();
    debugProcess.startStepIntoMyCode();
  }
 @Nullable
 private static DebugProcessImpl getCurrentDebugProcess(@Nullable Project project) {
   if (project != null) {
     XDebugSession session = XDebuggerManager.getInstance(project).getCurrentSession();
     if (session != null) {
       XDebugProcess process = session.getDebugProcess();
       if (process instanceof JavaDebugProcess) {
         return ((JavaDebugProcess) process).getDebuggerSession().getProcess();
       }
     }
   }
   return null;
 }
  private XDebugSession connectToDebugger() throws ExecutionException {
    final ServerSocket serverSocket = PythonCommandLineState.createServerSocket();

    final XDebugSession session =
        XDebuggerManager.getInstance(getProject())
            .startSessionAndShowTab(
                "Python Console Debugger",
                PythonIcons.Python.Python,
                null,
                true,
                new XDebugProcessStarter() {
                  @NotNull
                  public XDebugProcess start(@NotNull final XDebugSession session) {
                    PythonDebugLanguageConsoleView debugConsoleView =
                        new PythonDebugLanguageConsoleView(getProject(), mySdk);

                    PyConsoleDebugProcessHandler consoleDebugProcessHandler =
                        new PyConsoleDebugProcessHandler(myProcessHandler);

                    PyConsoleDebugProcess consoleDebugProcess =
                        new PyConsoleDebugProcess(
                            session, serverSocket, debugConsoleView, consoleDebugProcessHandler);

                    PythonDebugConsoleCommunication communication =
                        PyDebugRunner.initDebugConsoleView(
                            getProject(),
                            consoleDebugProcess,
                            debugConsoleView,
                            consoleDebugProcessHandler);

                    myPydevConsoleCommunication.setDebugCommunication(communication);
                    debugConsoleView.attachToProcess(consoleDebugProcessHandler);

                    consoleDebugProcess.waitForNextConnection();

                    try {
                      consoleDebugProcess.connect(myPydevConsoleCommunication);
                    } catch (Exception e) {
                      LOG.error(e); // TODO
                    }

                    myProcessHandler.notifyTextAvailable(
                        "\nDebugger connected.\n", ProcessOutputTypes.STDERR);

                    return consoleDebugProcess;
                  }
                });

    return session;
  }
 public boolean isEnabled(@NotNull final Project project, final AnActionEvent event) {
   XLineBreakpointType<?>[] breakpointTypes = XDebuggerUtil.getInstance().getLineBreakpointTypes();
   final XBreakpointManager breakpointManager =
       XDebuggerManager.getInstance(project).getBreakpointManager();
   for (XSourcePosition position :
       XDebuggerUtilImpl.getAllCaretsPositions(project, event.getDataContext())) {
     for (XLineBreakpointType<?> breakpointType : breakpointTypes) {
       final VirtualFile file = position.getFile();
       final int line = position.getLine();
       if (breakpointType.canPutAt(file, line, project)
           || breakpointManager.findBreakpointAtLine(breakpointType, file, line) != null) {
         return true;
       }
     }
   }
   return false;
 }
 protected DebuggerSession attachVirtualMachine(
     RunProfileState state,
     ExecutionEnvironment environment,
     RemoteConnection remoteConnection,
     boolean pollConnection)
     throws ExecutionException {
   final DebuggerSession debuggerSession =
       DebuggerManagerEx.getInstanceEx(myProject)
           .attachVirtualMachine(
               new DefaultDebugEnvironment(environment, state, remoteConnection, pollConnection));
   XDebuggerManager.getInstance(myProject)
       .startSession(
           environment,
           new XDebugProcessStarter() {
             @Override
             @NotNull
             public XDebugProcess start(@NotNull XDebugSession session) {
               return JavaDebugProcess.create(session, debuggerSession);
             }
           });
   return debuggerSession;
 }
 private void updateGutter() {
   if (myVisible) {
     if (isValid()) {
       final XBreakpointManager breakpointManager =
           XDebuggerManager.getInstance(myProject).getBreakpointManager();
       breakpointManager.updateBreakpointPresentation(
           (XLineBreakpoint) myXBreakpoint, getIcon(), null);
     }
     // RangeHighlighter highlighter = myHighlighter;
     // if (highlighter != null && highlighter.isValid() && isValid()) {
     //  AppUIUtil.invokeLaterIfProjectAlive(myProject, new Runnable() {
     //    @Override
     //    public void run() {
     //      if (isValid()) {
     //        setupGutterRenderer(myHighlighter);
     //      }
     //    }
     //  });
     // }
     // else {
     //  DebuggerManagerEx.getInstanceEx(myProject).getBreakpointManager().removeBreakpoint(this);
     // }
   }
 }
  public void testXDebugger() throws Exception {
    final WorkingContextManager manager = getContextManager();
    final XBreakpointManager breakpointManager =
        XDebuggerManager.getInstance(getProject()).getBreakpointManager();

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                XLineBreakpointImpl<XBreakpointProperties> breakpoint =
                    (XLineBreakpointImpl<XBreakpointProperties>)
                        breakpointManager.addLineBreakpoint(
                            new XsltBreakpointType(), "foo", 0, null);

                final String name = "foo";
                manager.saveContext(name, null);
                breakpointManager.removeBreakpoint(breakpoint);
              }
            });
    manager.loadContext("foo");
    XBreakpoint<?>[] breakpoints = breakpointManager.getAllBreakpoints();
    assertEquals(1, breakpoints.length);
    manager.clearContext();
  }
 private XBreakpointManagerImpl getBreakpointManager() {
   return (XBreakpointManagerImpl) XDebuggerManager.getInstance(myProject).getBreakpointManager();
 }
 private XBreakpointManager getXBreakpointManager() {
   return XDebuggerManager.getInstance(myProject).getBreakpointManager();
 }
  /**
   * Toggle line breakpoint with editor support: - unfolds folded block on the line - if folded,
   * checks if line breakpoints could be toggled inside folded text
   */
  @NotNull
  public static Promise toggleLineBreakpoint(
      @NotNull Project project,
      @NotNull XSourcePosition position,
      @Nullable Editor editor,
      boolean temporary,
      boolean moveCarret) {
    int lineStart = position.getLine();
    VirtualFile file = position.getFile();
    // for folded text check each line and find out type with the biggest priority
    int linesEnd = lineStart;
    if (editor != null) {
      FoldRegion region = FoldingUtil.findFoldRegionStartingAtLine(editor, lineStart);
      if (region != null && !region.isExpanded()) {
        linesEnd = region.getDocument().getLineNumber(region.getEndOffset());
      }
    }

    final XBreakpointManager breakpointManager =
        XDebuggerManager.getInstance(project).getBreakpointManager();
    XLineBreakpointType<?>[] lineTypes = XDebuggerUtil.getInstance().getLineBreakpointTypes();
    XLineBreakpointType<?> typeWinner = null;
    int lineWinner = -1;
    for (int line = lineStart; line <= linesEnd; line++) {
      int maxPriority = 0;
      for (XLineBreakpointType<?> type : lineTypes) {
        maxPriority = Math.max(maxPriority, type.getPriority());
        final XLineBreakpoint<? extends XBreakpointProperties> breakpoint =
            breakpointManager.findBreakpointAtLine(type, file, line);
        if (breakpoint != null && temporary && !breakpoint.isTemporary()) {
          breakpoint.setTemporary(true);
        } else if (type.canPutAt(file, line, project) || breakpoint != null) {
          if (typeWinner == null || type.getPriority() > typeWinner.getPriority()) {
            typeWinner = type;
            lineWinner = line;
          }
        }
      }
      // already found max priority type - stop
      if (typeWinner != null && typeWinner.getPriority() == maxPriority) {
        break;
      }
    }

    if (typeWinner != null) {
      XSourcePosition winPosition =
          (lineStart == lineWinner) ? position : XSourcePositionImpl.create(file, lineWinner);
      if (winPosition != null) {
        Promise<XLineBreakpoint> res =
            XDebuggerUtilImpl.toggleAndReturnLineBreakpoint(
                project, typeWinner, winPosition, temporary, editor);

        if (editor != null && lineStart != lineWinner) {
          int offset = editor.getDocument().getLineStartOffset(lineWinner);
          ExpandRegionAction.expandRegionAtOffset(project, editor, offset);
          if (moveCarret) {
            editor.getCaretModel().moveToOffset(offset);
          }
        }
        return res;
      }
    }

    return PromiseKt.rejectedPromise();
  }
  @Nullable
  @Override
  public EditorNotificationPanel createNotificationPanel(
      @NotNull VirtualFile file, @NotNull FileEditor fileEditor) {
    XDebugSession session = XDebuggerManager.getInstance(myProject).getCurrentSession();
    if (session == null) {
      FILE_PROCESSED_KEY.set(file, null);
      return null;
    }

    XSourcePosition position = session.getCurrentPosition();
    if (position == null || !file.equals(position.getFile())) {
      FILE_PROCESSED_KEY.set(file, null);
      return null;
    }

    if (file.getFileType() == JavaClassFileType.INSTANCE) return null;

    final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
    if (psiFile == null) return null;

    if (!(psiFile instanceof PsiJavaFile)) return null;

    PsiClass[] classes = ((PsiJavaFile) psiFile).getClasses();
    if (classes.length == 0) return null;

    PsiClass baseClass = classes[0];
    String name = baseClass.getQualifiedName();

    if (name == null) return null;

    if (DumbService.getInstance(myProject).isDumb()) return null;

    ArrayList<PsiClass> alts =
        ContainerUtil.newArrayList(
            JavaPsiFacade.getInstance(myProject)
                .findClasses(name, GlobalSearchScope.allScope(myProject)));
    ContainerUtil.removeDuplicates(alts);

    FILE_PROCESSED_KEY.set(file, true);

    if (alts.size() > 1) {
      for (PsiClass cls : alts) {
        if (cls.equals(baseClass) || cls.getNavigationElement().equals(baseClass)) {
          alts.remove(cls);
          break;
        }
      }
      alts.add(0, baseClass);

      ComboBoxClassElement[] elems =
          ContainerUtil.map2Array(
              alts,
              ComboBoxClassElement.class,
              new Function<PsiClass, ComboBoxClassElement>() {
                @Override
                public ComboBoxClassElement fun(PsiClass psiClass) {
                  return new ComboBoxClassElement(psiClass);
                }
              });

      return new AlternativeSourceNotificationPanel(elems, baseClass, myProject, file);
    }
    return null;
  }
 @Override
 public AbstractDebuggerSession getCurrentSession(@NotNull Project project) {
   return XDebuggerManager.getInstance(project).getCurrentSession();
 }
  protected DebuggerSession createLocalSession(final JavaParameters javaParameters)
      throws ExecutionException, InterruptedException {
    createBreakpoints(javaParameters.getMainClass());
    DebuggerSettings.getInstance().DEBUGGER_TRANSPORT = DebuggerSettings.SOCKET_TRANSPORT;

    GenericDebuggerRunnerSettings debuggerRunnerSettings = new GenericDebuggerRunnerSettings();
    debuggerRunnerSettings.LOCAL = true;

    final RemoteConnection debugParameters =
        DebuggerManagerImpl.createDebugParameters(javaParameters, debuggerRunnerSettings, false);

    ExecutionEnvironment environment =
        new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance())
            .runnerSettings(debuggerRunnerSettings)
            .runProfile(new MockConfiguration())
            .build();
    final JavaCommandLineState javaCommandLineState =
        new JavaCommandLineState(environment) {
          @Override
          protected JavaParameters createJavaParameters() {
            return javaParameters;
          }

          @Override
          protected GeneralCommandLine createCommandLine() throws ExecutionException {
            return CommandLineBuilder.createFromJavaParameters(getJavaParameters());
          }
        };

    ApplicationManager.getApplication()
        .invokeAndWait(
            () -> {
              try {
                myDebuggerSession =
                    DebuggerManagerEx.getInstanceEx(myProject)
                        .attachVirtualMachine(
                            new DefaultDebugEnvironment(
                                new ExecutionEnvironmentBuilder(
                                        myProject, DefaultDebugExecutor.getDebugExecutorInstance())
                                    .runProfile(new MockConfiguration())
                                    .build(),
                                javaCommandLineState,
                                debugParameters,
                                false));
                XDebuggerManager.getInstance(myProject)
                    .startSession(
                        javaCommandLineState.getEnvironment(),
                        new XDebugProcessStarter() {
                          @Override
                          @NotNull
                          public XDebugProcess start(@NotNull XDebugSession session) {
                            return JavaDebugProcess.create(session, myDebuggerSession);
                          }
                        });
              } catch (ExecutionException e) {
                LOG.error(e);
              }
            });
    myDebugProcess = myDebuggerSession.getProcess();

    myDebugProcess.addProcessListener(
        new ProcessAdapter() {
          @Override
          public void onTextAvailable(ProcessEvent event, Key outputType) {
            print(event.getText(), outputType);
          }
        });

    assertNotNull(myDebuggerSession);
    assertNotNull(myDebugProcess);

    return myDebuggerSession;
  }