<commit_before>{View, $$} = require 'space-pen'
Buffer = require 'buffer'
Point = require 'point'
Cursor = require 'cursor'
Selection = require 'selection'
Highlighter = require 'highlighter'
Range = require 'range'

$ = require 'jquery'
_ = require 'underscore'

module.exports =
class Editor extends View
  @content: ->
    @div class: 'editor', tabindex: -1, =>
      @div outlet: 'lines'
      @input class: 'hidden-input', outlet: 'hiddenInput'

  vScrollMargin: 2
  hScrollMargin: 10
  cursor: null
  buffer: null
  selection: null
  lineHeight: null
  charWidth: null

  initialize: () ->
    requireStylesheet 'editor.css'
    requireStylesheet 'theme/twilight.css'
    @bindKeys()
    @buildCursorAndSelection()
    @handleEvents()
    @setBuffer(new Buffer)

  bindKeys: ->
    atom.bindKeys '*',
      right: 'move-right'
      left: 'move-left'
      down: 'move-down'
      up: 'move-up'
      'shift-right': 'select-right'
      'shift-left': 'select-left'
      'shift-up': 'select-up'
      'shift-down': 'select-down'
      enter: 'newline'
      backspace: 'backspace'
      delete: 'delete'
      'meta-x': 'cut'
      'meta-c': 'copy'
      'meta-v': 'paste'

    @on 'move-right', => @moveCursorRight()
    @on 'move-left', => @moveCursorLeft()
    @on 'move-down', => @moveCursorDown()
    @on 'move-up', => @moveCursorUp()
    @on 'select-right', => @selectRight()
    @on 'select-left', => @selectLeft()
    @on 'select-up', => @selectUp()
    @on 'select-down', => @selectDown()
    @on 'newline', =>  @insertNewline()
    @on 'backspace', => @backspace()
    @on 'delete', => @delete()
    @on 'cut', => @cutSelection()
    @on 'copy', => @copySelection()
    @on 'paste', => @paste()

  buildCursorAndSelection: ->
    @cursor = new Cursor(this)
    @append(@cursor)

    @selection = new Selection(this)
    @append(@selection)

  handleEvents: ->
    @on 'focus', =>
      @hiddenInput.focus()
      false

    @on 'mousedown', (e) =>
      clickCount = e.originalEvent.detail

      if clickCount == 1
        @setCursorPosition @pointFromMouseEvent(e)
      else if clickCount == 2
        @selection.selectWord()
      else if clickCount >= 3
        @selection.selectLine(@getCursorRow())

      @selectTextOnMouseMovement()

    @hiddenInput.on "textInput", (e) =>
      @insertText(e.originalEvent.data)

    @on 'cursor:position-changed', =>
      @hiddenInput.css(@pixelPositionFromPoint(@cursor.getPosition()))

    @one 'attach', =>
      @calculateDimensions()
      @hiddenInput.width(@charWidth)
      @focus()

  selectTextOnMouseMovement: ->
    moveHandler = (e) => @selectToPosition(@pointFromMouseEvent(e))
    @on 'mousemove', moveHandler
    $(document).one 'mouseup', => @off 'mousemove', moveHandler


  buildLineElement: (row) ->
    tokens = @highlighter.tokensForRow(row)
    $$ ->
      @pre class: 'line', =>
        if tokens.length
          for token in tokens
            classes = token.type.split('.').map((c) -> "ace_#{c}").join(' ')
            @span { class: token.type.replace('.', ' ') }, token.value
        else
          @raw '&nbsp;'

  setBuffer: (@buffer) ->
    @highlighter = new Highlighter(@buffer)

    @lines.empty()
    for row in [0..@buffer.lastRow()]
      line = @buildLineElement(row)
      @lines.append line

    @setCursorPosition(row: 0, column: 0)

    @buffer.on 'change', (e) =>
      @cursor.bufferChanged(e)

    @highlighter.on 'change', (e) =>
      { preRange, postRange } = e
<commit_msg>Delete dead code<commit_after>{View, $$} = require 'space-pen'
Buffer = require 'buffer'
Point = require 'point'
Cursor = require 'cursor'
Selection = require 'selection'
Highlighter = require 'highlighter'
Range = require 'range'

$ = require 'jquery'
_ = require 'underscore'

module.exports =
class Editor extends View
  @content: ->
    @div class: 'editor', tabindex: -1, =>
      @div outlet: 'lines'
      @input class: 'hidden-input', outlet: 'hiddenInput'

  vScrollMargin: 2
  hScrollMargin: 10
  cursor: null
  buffer: null
  selection: null
  lineHeight: null
  charWidth: null

  initialize: () ->
    requireStylesheet 'editor.css'
    requireStylesheet 'theme/twilight.css'
    @bindKeys()
    @buildCursorAndSelection()
    @handleEvents()
    @setBuffer(new Buffer)

  bindKeys: ->
    atom.bindKeys '*',
      right: 'move-right'
      left: 'move-left'
      down: 'move-down'
      up: 'move-up'
      'shift-right': 'select-right'
      'shift-left': 'select-left'
      'shift-up': 'select-up'
      'shift-down': 'select-down'
      enter: 'newline'
      backspace: 'backspace'
      delete: 'delete'
      'meta-x': 'cut'
      'meta-c': 'copy'
      'meta-v': 'paste'

    @on 'move-right', => @moveCursorRight()
    @on 'move-left', => @moveCursorLeft()
    @on 'move-down', => @moveCursorDown()
    @on 'move-up', => @moveCursorUp()
    @on 'select-right', => @selectRight()
    @on 'select-left', => @selectLeft()
    @on 'select-up', => @selectUp()
    @on 'select-down', => @selectDown()
    @on 'newline', =>  @insertNewline()
    @on 'backspace', => @backspace()
    @on 'delete', => @delete()
    @on 'cut', => @cutSelection()
    @on 'copy', => @copySelection()
    @on 'paste', => @paste()

  buildCursorAndSelection: ->
    @cursor = new Cursor(this)
    @append(@cursor)

    @selection = new Selection(this)
    @append(@selection)

  handleEvents: ->
    @on 'focus', =>
      @hiddenInput.focus()
      false

    @on 'mousedown', (e) =>
      clickCount = e.originalEvent.detail

      if clickCount == 1
        @setCursorPosition @pointFromMouseEvent(e)
      else if clickCount == 2
        @selection.selectWord()
      else if clickCount >= 3
        @selection.selectLine(@getCursorRow())

      @selectTextOnMouseMovement()

    @hiddenInput.on "textInput", (e) =>
      @insertText(e.originalEvent.data)

    @on 'cursor:position-changed', =>
      @hiddenInput.css(@pixelPositionFromPoint(@cursor.getPosition()))

    @one 'attach', =>
      @calculateDimensions()
      @hiddenInput.width(@charWidth)
      @focus()

  selectTextOnMouseMovement: ->
    moveHandler = (e) => @selectToPosition(@pointFromMouseEvent(e))
    @on 'mousemove', moveHandler
    $(document).one 'mouseup', => @off 'mousemove', moveHandler


  buildLineElement: (row) ->
    tokens = @highlighter.tokensForRow(row)
    $$ ->
      @pre class: 'line', =>
        if tokens.length
          for token in tokens
            @span { class: token.type.replace('.', ' ') }, token.value
        else
          @raw '&nbsp;'

  setBuffer: (@buffer) ->
    @highlighter = new Highlighter(@buffer)

    @lines.empty()
    for row in [0..@buffer.lastRow()]
      line = @buildLineElement(row)
      @lines.append line

    @setCursorPosition(row: 0, column: 0)

    @buffer.on 'change', (e) =>
      @cursor.bufferChanged(e)

    @highlighter.on 'change', (e) =>
      { preRange, postRange } = e

{'fraction_non_alphanumeric': 0.1665700202957379, 'fraction_numerical': 0.0028993911278631487, 'mean_word_length': 2.891144952058658, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6850449', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2240, 'n_words': 499}