onModelChange()

in src/overlay/gather-markers.ts [120:245]


  onModelChange(eventType: GatherModelEvent, eventData: GatherEventData, model: GatherModel) {
    // When a cell is executed, search for definitions and output.
    if (eventType == GatherModelEvent.CELL_EXECUTION_LOGGED) {
      let cell = eventData as py.Cell;
      this.clearSelectablesForCell(cell);
      let editor = this._elementFinder.getEditor(cell);
      if (editor) {
        this.highlightDefs(editor, cell);
      }
      let outputElements = this._elementFinder.getOutputs(cell);
      this.highlightOutputs(cell, outputElements);
    }

    // When a cell is deleted or edited, delete all of its def markers.
    if (eventType == GatherModelEvent.CELL_DELETED || eventType == GatherModelEvent.CELL_EDITED) {
      let cell = eventData as py.Cell;
      this._updateDependenceHighlightsForCell(cell);
      this.clearSelectablesForCell(cell);
    }

    // When definitions are found, highlight them.
    if (eventType == GatherModelEvent.EDITOR_DEF_FOUND) {
      let editorDef = eventData as EditorDef;
      this.highlightDef(editorDef);
    }

    // When definitions are removed from the model, deselect and remove their markers.
    if (eventType == GatherModelEvent.EDITOR_DEF_REMOVED) {
      let editorDef = eventData as EditorDef;
      for (let i = this._defMarkers.length - 1; i >= 0; i--) {
        let defMarker = this._defMarkers[i];
        if (defMarker.def == editorDef.def) {
          let defsToDeselect = this._model.selectedDefs.filter(d => d.editorDef == editorDef);
          for (let defToDeselect of defsToDeselect) {
            this._model.deselectDef(defToDeselect);
          }
          defMarker.marker.clear();
          this._defMarkers.splice(i, 1);
        }
      }
    }

    // When outputs are found, highlight them.
    if (eventType == GatherModelEvent.OUTPUT_FOUND) {
      let output = eventData as CellOutput;
      this.highlightOutput(output);
    }

    // When outputs are removed from the model, deselect and remove their markers.
    if (eventType == GatherModelEvent.OUTPUT_REMOVED) {
      let output = eventData as CellOutput;
      for (let i = this._outputMarkers.length - 1; i >= 0; i--) {
        let outputMarker = this._outputMarkers[i];
        if (outputMarker.cell == output.cell && outputMarker.outputIndex == output.outputIndex) {
          this._model.deselectOutput({
            cell: output.cell,
            outputIndex: output.outputIndex
          });
          outputMarker.destroy();
          this._outputMarkers.splice(i, 1);
        }
      }
    }

    // Whenever a definition is selected, add a marker to its line.
    if (eventType == GatherModelEvent.DEF_SELECTED) {
      let defSelection = eventData as DefSelection;
      let editor = defSelection.editorDef.editor;
      let def = defSelection.editorDef.def;
      let lineHandle = editor.addLineClass(
        def.location.first_line - 1,
        "background",
        DEFINITION_LINE_SELECTED_CLASS
      );
      this._defLineHandles.push({ def: def, lineHandle: lineHandle });
    }

    // Whenever a definition is deselected from outside, unhighlight it.
    if (eventType == GatherModelEvent.DEF_DESELECTED) {
      let defSelection = eventData as DefSelection;
      this._defMarkers
        .filter(marker => {
          return (
            defSelection.editorDef.def.location == marker.location &&
            defSelection.cell.executionEventId == marker.cell.executionEventId
          );
        })
        .forEach(marker => marker.deselect());

      let editorDef = defSelection.editorDef;
      for (let i = this._defLineHandles.length - 1; i >= 0; i--) {
        let defLineHandle = this._defLineHandles[i];
        if (defLineHandle.def == editorDef.def) {
          editorDef.editor.removeLineClass(
            defLineHandle.lineHandle,
            "background",
            DEFINITION_LINE_SELECTED_CLASS
          );
        }
      }
    }

    // Whenever an output is deselected from outside, unhighlight it.
    if (eventType == GatherModelEvent.OUTPUT_DESELECTED) {
      let outputSelection = eventData as OutputSelection;
      this._outputMarkers
        .filter(marker => {
          return (
            marker.outputIndex == outputSelection.outputIndex &&
            marker.cell.executionEventId == outputSelection.cell.executionEventId
          );
        })
        .forEach(marker => marker.deselect());
    }

    // When the chosen slices change, update which lines are highlighted in the document.
    if (
      eventType == GatherModelEvent.SLICE_SELECTED ||
      eventType == GatherModelEvent.SLICE_DESELECTED
    ) {
      this._clearDependencyLineMarkers();
      model.selectedSlices.forEach(sliceSelection => {
        this.highlightDependencies(sliceSelection.slice);
      });
    }
  }