<commit_before># TODO: autocomplete

{Emitter} = require 'atom'
ConsoleView = require './view'

module.exports =
class Console
  @activate: ->
    @evalCmd = atom.commands.add '.ink-console atom-text-editor',
      'console:evaluate': ->
        ed = @getModel()
        ed.inkConsole.eval ed
      'core:backspace': (e) ->
        @getModel().inkConsole.cancelMode e
      'console:previous-in-history': ->
        @getModel().inkConsole.previous()
      'console:next-in-history': ->
        @getModel().inkConsole.next()

    atom.commands.add '.ink-console',
      'core:copy': ->
        if (sel = document.getSelection().toString())
          atom.clipboard.write sel

  @deactivate: ->
    @evalCmd.dispose()

  constructor: ->
    @view = new ConsoleView
    @view.getModel = -> @
    @observeInput (cell) =>
      @watchModes cell
    @onEval => @logInput()

  isInput: false

  setGrammar: (g) ->
    @view.setGrammar g

  eval: (ed) ->
    if @isInput
      input = @view.getInputEd()
      if ed == input
        @emitter.emit 'eval', ed
      else
        input.setText ed.getText()
        @view.focusInput true
        @view.scroll()

  input: ->
    if not @isInput
      v = @view.inputView this
      @emitter.emit 'new-input', v
      @view.add v
      @isInput = true

  done: ->
    if @isInput
      @view.element.focus() # Defocus input
      @isInput = false

  @debounce: (t, f) ->
    timeout = null
    (args...) ->
      if timeout? then clearTimeout timeout
      timeout = setTimeout (=> f.call this, args...), t

  @buffer: (f) ->
    buffer = []
    flush = @debounce 10, ->
      f.call this, buffer.join('').trim()
      buffer = []
    (s) ->
      buffer.push(s)
      flush.call this

  out: @buffer (s) -> @view.add(@view.outView(s), @isInput)

  err: @buffer (s) -> @view.add(@view.errView(s), @isInput)

  info: @buffer (s) -> @view.add(@view.infoView(s), @isInput)

  result: (r, opts) -> @view.add(@view.resultView(r, opts), @isInput)

  clear: ->
    @done()
    @view.clear()

  reset: ->
    focus = @view.hasFocus()
    @clear()
    @input()
    @view.focusInput focus

  emitter: new Emitter

  onEval: (f) -> @emitter.on 'eval', f

  observeInput: (f) -> @emitter.on 'new-input', f

  openInTab: ->
    p = atom.workspace.getActivePane()
    if p.items.length > 0
      p = p.splitDown()
      p.setFlexScale 1/2
    p.activateItem @view
    p.onDidActivate => setTimeout =>
      if document.activeElement == @view[0]
          @view.focusInput(true)

  toggle: ->
    if atom.workspace.getPaneItems().indexOf(@view) > -1
      @view[0].parentElement.parentElement.getModel().removeItem @view
    else
      @openInTab()
      @view.focusInput()

  modes: -> {}

  cursorAtBeginning: (ed) ->
    ed.getCursors().length == 1 and
    ed.getCursors()[0].getBufferPosition().isEqual [0, 0]
<commit_msg>only focus input if visible<commit_after># TODO: autocomplete

{Emitter} = require 'atom'
ConsoleView = require './view'

module.exports =
class Console
  @activate: ->
    @evalCmd = atom.commands.add '.ink-console atom-text-editor',
      'console:evaluate': ->
        ed = @getModel()
        ed.inkConsole.eval ed
      'core:backspace': (e) ->
        @getModel().inkConsole.cancelMode e
      'console:previous-in-history': ->
        @getModel().inkConsole.previous()
      'console:next-in-history': ->
        @getModel().inkConsole.next()

    atom.commands.add '.ink-console',
      'core:copy': ->
        if (sel = document.getSelection().toString())
          atom.clipboard.write sel

  @deactivate: ->
    @evalCmd.dispose()

  constructor: ->
    @view = new ConsoleView
    @view.getModel = -> @
    @observeInput (cell) =>
      @watchModes cell
    @onEval => @logInput()

  isInput: false

  setGrammar: (g) ->
    @view.setGrammar g

  eval: (ed) ->
    if @isInput
      input = @view.getInputEd()
      if ed == input
        @emitter.emit 'eval', ed
      else
        input.setText ed.getText()
        @view.focusInput true
        @view.scroll()

  input: ->
    if not @isInput
      v = @view.inputView this
      @emitter.emit 'new-input', v
      @view.add v
      @isInput = true

  done: ->
    if @isInput
      @view.element.focus() # Defocus input
      @isInput = false

  @debounce: (t, f) ->
    timeout = null
    (args...) ->
      if timeout? then clearTimeout timeout
      timeout = setTimeout (=> f.call this, args...), t

  @buffer: (f) ->
    buffer = []
    flush = @debounce 10, ->
      f.call this, buffer.join('').trim()
      buffer = []
    (s) ->
      buffer.push(s)
      flush.call this

  out: @buffer (s) -> @view.add(@view.outView(s), @isInput)

  err: @buffer (s) -> @view.add(@view.errView(s), @isInput)

  info: @buffer (s) -> @view.add(@view.infoView(s), @isInput)

  result: (r, opts) -> @view.add(@view.resultView(r, opts), @isInput)

  clear: ->
    @done()
    @view.clear()

  reset: ->
    focus = @view.hasFocus()
    @clear()
    @input()
    @view.focusInput focus

  emitter: new Emitter

  onEval: (f) -> @emitter.on 'eval', f

  observeInput: (f) -> @emitter.on 'new-input', f

  openInTab: ->
    p = atom.workspace.getActivePane()
    if p.items.length > 0
      p = p.splitDown()
      p.setFlexScale 1/2
    p.activateItem @view
    p.onDidActivate => setTimeout =>
      if document.activeElement == @view[0] && @view.lastCellVisible()
          @view.focusInput(true)

  toggle: ->
    if atom.workspace.getPaneItems().indexOf(@view) > -1
      @view[0].parentElement.parentElement.getModel().removeItem @view
    else
      @openInTab()
      @view.focusInput()

  modes: -> {}

  cursorAtBeginning: (ed) ->
    ed.getCursors().length == 1 and
    ed.getCursors()[0].getBufferPosition().isEqual [0, 0]

{'fraction_non_alphanumeric': 0.16634699215344376, 'fraction_numerical': 0.004184829991281604, 'mean_word_length': 2.354385964912281, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 77, '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': '210382', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1940, 'n_words': 421}