<commit_before>{View, $$$} = require 'space-pen'
CommandInterpreter = require 'command-panel/command-interpreter'
RegexAddress = require 'command-panel/commands/regex-address'
CompositeCommand = require 'command-panel/commands/composite-command'
PreviewList = require 'command-panel/preview-list'
Editor = require 'editor'
{SyntaxError} = require('pegjs').parser

_ = require 'underscore'

module.exports =
class CommandPanel extends View
  @activate: (rootView, state) ->
    requireStylesheet 'command-panel.css'
    if state?
      @instance = CommandPanel.deserialize(state, rootView)
    else
      @instance = new CommandPanel(rootView)

  @deactivate: ->
    @instance.destroy()

  @serialize: ->
    text: @instance.miniEditor.getText()
    visible: @instance.hasParent()
    miniEditorFocused: @instance.miniEditor.isFocused
    history: @instance.history[-@instance.maxSerializedHistorySize..]

  @deserialize: (state, rootView) ->
    commandPanel = new CommandPanel(rootView, state.history)
    commandPanel.attach(state.text, focus: false) if state.visible
    commandPanel.miniEditor.focus() if state.miniEditorFocused
    commandPanel

  @content: (rootView) ->
    @div class: 'command-panel', =>
      @subview 'previewList', new PreviewList(rootView)
      @div class: 'prompt-and-editor', =>
        @div ':', class: 'prompt', outlet: 'prompt'
        @subview 'miniEditor', new Editor(mini: true)

  commandInterpreter: null
  history: null
  historyIndex: 0
  maxSerializedHistorySize: 100

  initialize: (@rootView, @history) ->
    @commandInterpreter = new CommandInterpreter(@rootView.project)

    @history ?= []
    @historyIndex = @history.length

    @on 'command-panel:unfocus', => @rootView.focus()
    @on 'command-panel:close', => @detach()

    @rootView.on 'command-panel:toggle', => @toggle()
    @rootView.on 'command-panel:toggle-preview', => @togglePreview()
    @rootView.on 'command-panel:execute', => @execute()
    @rootView.on 'command-panel:find-in-file', => @attach("/")
    @rootView.on 'command-panel:find-in-project', => @attach("Xx/")
    @rootView.on 'command-panel:repeat-relative-address', => @repeatRelativeAddress()
    @rootView.on 'command-panel:repeat-relative-address-in-reverse', => @repeatRelativeAddressInReverse()
    @rootView.on 'command-panel:set-selection-as-regex-address', => @setSelectionAsLastRelativeAddress()

    @on 'core:move-up', => @navigateBackwardInHistory()
    @on 'core:move-down', => @navigateForwardInHistory()

    @previewList.hide()

  destroy: ->
    @previewList.destroy()

  toggle: ->
    if @miniEditor.isFocused
      @detach()
      @rootView.focus()
    else
      @attach() unless @hasParent()
      @miniEditor.focus()

  togglePreview: ->
    if @previewList.is(':focus')
      @previewList.hide()
      @detach()
      @rootView.focus()
    else
      @attach() unless @hasParent()
      if @previewList.hasOperations()
        @previewList.show().focus()
      else
        @miniEditor.focus()

  attach: (text='', options={}) ->
    focus = options.focus ? true
    @rootView.vertical.append(this)
    @miniEditor.focus() if focus
    @miniEditor.setText(text)
    @miniEditor.setCursorBufferPosition([0, Infinity])

  detach: ->
    @rootView.focus()
    @previewList.hide()
    super

  execute: (command = @miniEditor.getText()) ->
    try
      @commandInterpreter.eval(command, @rootView.getActiveEditSession()).done (operationsToPreview) =>
        @history.push(command)
        @historyIndex = @history.length
        if operationsToPreview?.length
          @previewList.populate(operationsToPreview)
          @previewList.focus()
        else
          @detach()
    catch error
      if error.name is "SyntaxError"
        @flashError()
        return
      else
        throw error

  navigateBackwardInHistory: ->
    return if @historyIndex == 0
    @historyIndex--
    @miniEditor.setText(@history[@historyIndex])

  navigateForwardInHistory: ->
    return if @historyIndex == @history.length
    @historyIndex++
    @miniEditor.setText(@history[@historyIndex] or '')

  repeatRelativeAddress: ->
    @commandInterpreter.repeatRelativeAddress(@rootView.getActiveEditSession())

  repeatRelativeAddressInReverse: ->
    @commandInterpreter.repeatRelativeAddressInReverse(@rootView.getActiveEditSession())

  setSelectionAsLastRelativeAddress: ->
    selection = @rootView.getActiveEditor().getSelectedText()
    regex = _.escapeRegExp(selection)
    @commandInterpreter.lastRelativeAddress = new CompositeCommand([new RegexAddress(regex)])
<commit_msg>Use `@command` to handle CommandPanel commands<commit_after>{View, $$$} = require 'space-pen'
CommandInterpreter = require 'command-panel/command-interpreter'
RegexAddress = require 'command-panel/commands/regex-address'
CompositeCommand = require 'command-panel/commands/composite-command'
PreviewList = require 'command-panel/preview-list'
Editor = require 'editor'
{SyntaxError} = require('pegjs').parser

_ = require 'underscore'

module.exports =
class CommandPanel extends View
  @activate: (rootView, state) ->
    requireStylesheet 'command-panel.css'
    if state?
      @instance = CommandPanel.deserialize(state, rootView)
    else
      @instance = new CommandPanel(rootView)

  @deactivate: ->
    @instance.destroy()

  @serialize: ->
    text: @instance.miniEditor.getText()
    visible: @instance.hasParent()
    miniEditorFocused: @instance.miniEditor.isFocused
    history: @instance.history[-@instance.maxSerializedHistorySize..]

  @deserialize: (state, rootView) ->
    commandPanel = new CommandPanel(rootView, state.history)
    commandPanel.attach(state.text, focus: false) if state.visible
    commandPanel.miniEditor.focus() if state.miniEditorFocused
    commandPanel

  @content: (rootView) ->
    @div class: 'command-panel', =>
      @subview 'previewList', new PreviewList(rootView)
      @div class: 'prompt-and-editor', =>
        @div ':', class: 'prompt', outlet: 'prompt'
        @subview 'miniEditor', new Editor(mini: true)

  commandInterpreter: null
  history: null
  historyIndex: 0
  maxSerializedHistorySize: 100

  initialize: (@rootView, @history) ->
    @commandInterpreter = new CommandInterpreter(@rootView.project)

    @history ?= []
    @historyIndex = @history.length

    @command 'command-panel:unfocus', => @rootView.focus()
    @command 'command-panel:close', => @detach()
    @command 'command-panel:execute', => @execute()

    @rootView.command 'command-panel:toggle', => @toggle()
    @rootView.command 'command-panel:toggle-preview', => @togglePreview()
    @rootView.command 'command-panel:find-in-file', => @attach("/")
    @rootView.command 'command-panel:find-in-project', => @attach("Xx/")
    @rootView.command 'command-panel:repeat-relative-address', => @repeatRelativeAddress()
    @rootView.command 'command-panel:repeat-relative-address-in-reverse', => @repeatRelativeAddressInReverse()
    @rootView.command 'command-panel:set-selection-as-regex-address', => @setSelectionAsLastRelativeAddress()

    @command 'core:move-up', => @navigateBackwardInHistory()
    @command 'core:move-down', => @navigateForwardInHistory()

    @previewList.hide()

  destroy: ->
    @previewList.destroy()

  toggle: ->
    if @miniEditor.isFocused
      @detach()
      @rootView.focus()
    else
      @attach() unless @hasParent()
      @miniEditor.focus()

  togglePreview: ->
    if @previewList.is(':focus')
      @previewList.hide()
      @detach()
      @rootView.focus()
    else
      @attach() unless @hasParent()
      if @previewList.hasOperations()
        @previewList.show().focus()
      else
        @miniEditor.focus()

  attach: (text='', options={}) ->
    focus = options.focus ? true
    @rootView.vertical.append(this)
    @miniEditor.focus() if focus
    @miniEditor.setText(text)
    @miniEditor.setCursorBufferPosition([0, Infinity])

  detach: ->
    @rootView.focus()
    @previewList.hide()
    super

  execute: (command = @miniEditor.getText()) ->
    try
      @commandInterpreter.eval(command, @rootView.getActiveEditSession()).done (operationsToPreview) =>
        @history.push(command)
        @historyIndex = @history.length
        if operationsToPreview?.length
          @previewList.populate(operationsToPreview)
          @previewList.focus()
        else
          @detach()
    catch error
      if error.name is "SyntaxError"
        @flashError()
        return
      else
        throw error

  navigateBackwardInHistory: ->
    return if @historyIndex == 0
    @historyIndex--
    @miniEditor.setText(@history[@historyIndex])

  navigateForwardInHistory: ->
    return if @historyIndex == @history.length
    @historyIndex++
    @miniEditor.setText(@history[@historyIndex] or '')

  repeatRelativeAddress: ->
    @commandInterpreter.repeatRelativeAddress(@rootView.getActiveEditSession())

  repeatRelativeAddressInReverse: ->
    @commandInterpreter.repeatRelativeAddressInReverse(@rootView.getActiveEditSession())

  setSelectionAsLastRelativeAddress: ->
    selection = @rootView.getActiveEditor().getSelectedText()
    regex = _.escapeRegExp(selection)
    @commandInterpreter.lastRelativeAddress = new CompositeCommand([new RegexAddress(regex)])

{'fraction_non_alphanumeric': 0.1370635436998482, 'fraction_numerical': 0.0013012361743656475, 'mean_word_length': 3.8363922391190353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 67, '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': '1685294', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2698, 'n_words': 491}