<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { INotebookTracker, NotebookTracker } from '@jupyterlab/notebook';

import { CodeCell } from '@jupyterlab/cells';

import { IDisposable } from '@phosphor/disposable';

import { Signal } from '@phosphor/signaling';

import { Breakpoints } from '../breakpoints';

import { Debugger } from '../debugger';

import { IDebugger } from '../tokens';

import { Callstack } from '../callstack';

import { CellManager } from './cell';

export class NotebookHandler implements IDisposable {
  constructor(options: NotebookHandler.IOptions) {
    this.debuggerModel = options.debuggerService.model;
    this.debuggerService = options.debuggerService;
    this.notebookTracker = options.tracker;
    this.id = options.id;
    this.breakpoints = this.debuggerModel.breakpointsModel;

    this.cellManager = new CellManager({
      breakpointsModel: this.breakpoints,
      debuggerModel: this.debuggerModel,
      debuggerService: this.debuggerService,
      type: 'notebook',
      activeCell: this.notebookTracker.activeCell as CodeCell
    });

    this.notebookTracker.activeCellChanged.connect(
      this.onActiveCellChanged,
      this
    );

    this.debuggerModel.callstackModel.currentFrameChanged.connect(
      (_, frame) => {
        this.showCurrentLine(frame);
      }
    );
  }

  isDisposed: boolean;

  dispose(): void {
    if (this.isDisposed) {
      return;
    }
    this.isDisposed = true;
    this.cellManager.dispose();
    Signal.clearData(this);
  }

  protected onActiveCellChanged(
    notebookTracker: NotebookTracker,
    codeCell: CodeCell
  ) {
    if (notebookTracker.currentWidget.id !== this.id) {
      return;
    }
    // TODO: do we need this requestAnimationFrame?
    requestAnimationFrame(() => {
      this.cellManager.activeCell = codeCell;
    });
  }

  private showCurrentLine(frame: Callstack.IFrame) {
    if (!frame) {
      return;
    }

    const notebook = this.notebookTracker.currentWidget;
    if (!notebook) {
      return;
    }

    const cells = notebook.content.widgets;

    cells.forEach(cell => {
      // check the event is for the correct cell
      const code = cell.model.value.text;<commit_msg>Clear cells in notebook handler<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { INotebookTracker, NotebookTracker } from '@jupyterlab/notebook';

import { CodeCell } from '@jupyterlab/cells';

import { IDisposable } from '@phosphor/disposable';

import { Signal } from '@phosphor/signaling';

import { Breakpoints } from '../breakpoints';

import { Debugger } from '../debugger';

import { IDebugger } from '../tokens';

import { Callstack } from '../callstack';

import { CellManager } from './cell';

export class NotebookHandler implements IDisposable {
  constructor(options: NotebookHandler.IOptions) {
    this.debuggerModel = options.debuggerService.model;
    this.debuggerService = options.debuggerService;
    this.notebookTracker = options.tracker;
    this.id = options.id;
    this.breakpoints = this.debuggerModel.breakpointsModel;

    this.cellManager = new CellManager({
      breakpointsModel: this.breakpoints,
      debuggerModel: this.debuggerModel,
      debuggerService: this.debuggerService,
      type: 'notebook',
      activeCell: this.notebookTracker.activeCell as CodeCell
    });

    this.notebookTracker.activeCellChanged.connect(
      this.onActiveCellChanged,
      this
    );

    this.debuggerModel.callstackModel.currentFrameChanged.connect(
      (_, frame) => {
        this.showCurrentLine(frame);
      }
    );
  }

  isDisposed: boolean;

  dispose(): void {
    if (this.isDisposed) {
      return;
    }
    this.isDisposed = true;
    this.cellManager.dispose();
    Signal.clearData(this);
  }

  protected onActiveCellChanged(
    notebookTracker: NotebookTracker,
    codeCell: CodeCell
  ) {
    if (notebookTracker.currentWidget.id !== this.id) {
      return;
    }
    // TODO: do we need this requestAnimationFrame?
    requestAnimationFrame(() => {
      this.cellManager.activeCell = codeCell;
    });
  }

  private showCurrentLine(frame: Callstack.IFrame) {
    const notebook = this.notebookTracker.currentWidget;
    if (!notebook) {
      return;
    }

    const cells = notebook.content.widgets;
    cells.forEach(cell => CellManager.cleanupHighlight(cell));

    if (!frame) {
      return;
    }

    cells.forEach(cell => {
      // check the event is for the correct cell
      const code = cell.model.value.text;
{'fraction_non_alphanumeric': 0.11144643242078034, 'fraction_numerical': 0.0, 'mean_word_length': 3.2028985507246377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '323823', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1355, 'n_words': 299}