<commit_before>_ = require 'underscore-plus'
{Point, Range, Emitter, CompositeDisposable} = require 'atom'
Delegator = require 'delegato'
Table = require './table'
DisplayColumn = require './display-column'

module.exports =
class DisplayTable
  Delegator.includeInto(this)

  @delegatesMethods(
    'changeColumnName', 'undo', 'redo', 'getRows', 'getColumns','getColumnCount', 'getRowCount', 'clearUndoStack', 'clearRedoStack', 'getValueAtPosition', 'setValueAtPosition', 'setValuesAtPositions', 'setValuesInRange', 'rowRangeFrom',
    toProperty: 'table'
  )

  rowOffsets: null
  columnOffsets: null

  constructor: (options={}) ->
    {@table} = options
    @table = new Table unless @table?
    @emitter = new Emitter
    @subscriptions = new CompositeDisposable
    @screenColumnsSubscriptions = new WeakMap

    @subscribeToConfig()
    @subscribeToTable()

    @screenColumns = @table.getColumns().map (column) =>
      screenColumn = new DisplayColumn({name: column})
      @subscribeToScreenColumn(screenColumn)
      screenColumn

    @rowHeights = @table.getColumns().map (column) => @getRowHeight()
    @computeScreenColumnOffsets()
    @updateScreenRows()

  destroy: ->
    @unsubscribeFromScreenColumn(column) for column in @screenColumns
    @rowOffsets = []
    @rowHeights = []
    @columnOffsets = []
    @screenColumns = []
    @screenRows = []
    @screenToModelRowsMap = {}
    @modelToScreenRowsMap = {}
    @destroyed = true
    @emitter.emit 'did-destroy', this
    @emitter.dispose()
    @emitter = null
    @subscriptions.dispose()
    @subscriptions = null
    @table = null

  onDidDestroy: (callback) ->
    @emitter.on 'did-destroy', callback

  onDidAddColumn: (callback) ->
    @emitter.on 'did-add-column', callback

  onDidRemoveColumn: (callback) ->
    @emitter.on 'did-remove-column', callback

  onDidRenameColumn: (callback) ->
    @emitter.on 'did-rename-column', callback

  onDidChangeColumnOption: (callback) ->
    @emitter.on 'did-change-column-options', callback

  onDidChangeCellValue: (callback) ->
    @emitter.on 'did-change-cell-value', callback

  onDidAddRow: (callback) ->
    @emitter.on 'did-add-row', callback

  onDidRemoveRow: (callback) ->
    @emitter.on 'did-remove-row', callback

  onDidChange: (callback) ->
    @emitter.on 'did-change', callback

  onDidChangeRowHeight: (callback) ->
    @emitter.on 'did-change-row-height', callback

  subscribeToTable: ->
    @subscriptions.add @table.onDidAddColumn ({column, index}) =>
      @addScreenColumn(index, {name: column})

    @subscriptions.add @table.onDidRemoveColumn ({column, index}) =>
      @removeScreenColumn(index, column)

    @subscriptions.add @table.onDidRenameColumn ({newName, oldName, index}) =>
      @screenColumns[index].setOption 'name', newName
      @emitter.emit('did-rename-column', {screenColumn: @screenColumns[index], oldName, newName, index})

    @subscriptions.add @table.onDidAddRow ({index}) =>
      @rowHeights.splice(index, 0, undefined)

    @subscriptions.add @table.onDidRemoveRow ({index}) =>
      @rowHeights.splice(index, 1)

    @subscriptions.add @table.onDidChange (event) =>
      @updateScreenRows()
      @emitter.emit 'did-change', event

    @subscriptions.add @table.onDidChangeCellValue (event) =>
      if event.positions?
        {positions, oldValues, newValues} = event
        newEvent = {
          positions, oldValues, newValues
          screenPositions: positions.map (p) => @screenPosition(p)
        }
      else if event.position?
        {position, oldValue, newValue} = event
        newEvent = {
          position, oldValue, newValue
          screenPosition: @screenPosition(position)
        }
      else
        newEvent = event

      @emitter.emit 'did-change-cell-value', newEvent

    @subscriptions.add @table.onDidDestroy (event) => @destroy()

  subscribeToConfig: ->
    @observeConfig
      'table-edit.undefinedDisplay': (@configUndefinedDisplay) =>
      'table-edit.rowHeight': (@configRowHeight) =>
        @computeRowOffsets() if @rowHeights?
      'table-edit.minimumRowHeight': (@configMinimumRowHeight) =><commit_msg>Convert range to screen in cell value change event<commit_after>_ = require 'underscore-plus'
{Point, Range, Emitter, CompositeDisposable} = require 'atom'
Delegator = require 'delegato'
Table = require './table'
DisplayColumn = require './display-column'

module.exports =
class DisplayTable
  Delegator.includeInto(this)

  @delegatesMethods(
    'changeColumnName', 'undo', 'redo', 'getRows', 'getColumns','getColumnCount', 'getRowCount', 'clearUndoStack', 'clearRedoStack', 'getValueAtPosition', 'setValueAtPosition', 'setValuesAtPositions', 'setValuesInRange', 'rowRangeFrom',
    toProperty: 'table'
  )

  rowOffsets: null
  columnOffsets: null

  constructor: (options={}) ->
    {@table} = options
    @table = new Table unless @table?
    @emitter = new Emitter
    @subscriptions = new CompositeDisposable
    @screenColumnsSubscriptions = new WeakMap

    @subscribeToConfig()
    @subscribeToTable()

    @screenColumns = @table.getColumns().map (column) =>
      screenColumn = new DisplayColumn({name: column})
      @subscribeToScreenColumn(screenColumn)
      screenColumn

    @rowHeights = @table.getColumns().map (column) => @getRowHeight()
    @computeScreenColumnOffsets()
    @updateScreenRows()

  destroy: ->
    @unsubscribeFromScreenColumn(column) for column in @screenColumns
    @rowOffsets = []
    @rowHeights = []
    @columnOffsets = []
    @screenColumns = []
    @screenRows = []
    @screenToModelRowsMap = {}
    @modelToScreenRowsMap = {}
    @destroyed = true
    @emitter.emit 'did-destroy', this
    @emitter.dispose()
    @emitter = null
    @subscriptions.dispose()
    @subscriptions = null
    @table = null

  onDidDestroy: (callback) ->
    @emitter.on 'did-destroy', callback

  onDidAddColumn: (callback) ->
    @emitter.on 'did-add-column', callback

  onDidRemoveColumn: (callback) ->
    @emitter.on 'did-remove-column', callback

  onDidRenameColumn: (callback) ->
    @emitter.on 'did-rename-column', callback

  onDidChangeColumnOption: (callback) ->
    @emitter.on 'did-change-column-options', callback

  onDidChangeCellValue: (callback) ->
    @emitter.on 'did-change-cell-value', callback

  onDidAddRow: (callback) ->
    @emitter.on 'did-add-row', callback

  onDidRemoveRow: (callback) ->
    @emitter.on 'did-remove-row', callback

  onDidChange: (callback) ->
    @emitter.on 'did-change', callback

  onDidChangeRowHeight: (callback) ->
    @emitter.on 'did-change-row-height', callback

  subscribeToTable: ->
    @subscriptions.add @table.onDidAddColumn ({column, index}) =>
      @addScreenColumn(index, {name: column})

    @subscriptions.add @table.onDidRemoveColumn ({column, index}) =>
      @removeScreenColumn(index, column)

    @subscriptions.add @table.onDidRenameColumn ({newName, oldName, index}) =>
      @screenColumns[index].setOption 'name', newName
      @emitter.emit('did-rename-column', {screenColumn: @screenColumns[index], oldName, newName, index})

    @subscriptions.add @table.onDidAddRow ({index}) =>
      @rowHeights.splice(index, 0, undefined)

    @subscriptions.add @table.onDidRemoveRow ({index}) =>
      @rowHeights.splice(index, 1)

    @subscriptions.add @table.onDidChange (event) =>
      @updateScreenRows()
      @emitter.emit 'did-change', event

    @subscriptions.add @table.onDidChangeCellValue (event) =>
      if event.positions?
        {positions, oldValues, newValues} = event
        newEvent = {
          positions, oldValues, newValues
          screenPositions: positions.map (p) => @screenPosition(p)
        }
      else if event.position?
        {position, oldValue, newValue} = event
        newEvent = {
          position, oldValue, newValue
          screenPosition: @screenPosition(position)
        }
      else if event.range?
        {range, oldValues, newValues} = event
        if @order?
          screenPositions = []
          range.each (row, column) =>
            screenPositions.push(@screenPosition([row, column]))
          newEvent = { range, oldValues, newValues, screenPositions }
        else
          newEvent = {
            range, oldValues, newValues
            screenRange: range.copy()
          }
      else
        newEvent = event

      @emitter.emit 'did-change-cell-value', newEvent

    @subscriptions.add @table.onDidDestroy (event) => @destroy()

  subscribeToConfig: ->
    @observeConfig
      'table-edit.undefinedDisplay': (@configUndefinedDisplay) =>
      'table-edit.rowHeight': (@configRowHeight) =>
        @computeRowOffsets() if @rowHeights?
      'table-edit.minimumRowHeight': (@configMinimumRowHeight) =>
{'fraction_non_alphanumeric': 0.132276954874268, 'fraction_numerical': 0.00045929498220231943, 'mean_word_length': 3.508281573498965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2725783', 'n_tokens_mistral': 2685, 'n_tokens_neox': 2580, 'n_words': 535}