 
   inspect: ->
     @vimState.developer?.inspectOperationStack()
+
+module.exports = OperationStack

# Refactoring status: 100%
_ = require 'underscore-plus'
{CurrentSelection} = require './motion'
{Select} = require './operator'
{debug} = require './utils'
settings = require './settings'

module.exports =
class OperationStack
  constructor: (@vimState) ->
    @stack = []

  push: (op) ->
    if @isEmpty() and settings.get('debug')

      else
        debug "-> @process(): return: not <#{@peekTop().getKind()}>.isComplete()"
        @inspect()

  cancel: ->
    debug "Cancelled stack size: #{@stack.length}"
    debug(op.getKind()) for op in @pop()
    unless @vimState.isMode('visual') or @vimState.isMode('insert')
      @vimState.activate('reset')
    @finish()
    debug "#=== Canceled at #{new Date().toISOString()}\n"

  finish: ->
    if @vimState.isMode('normal')
      if @editor.getLastSelection().isEmpty()
        @dontPutCursorsAtEndOfLine()
    if @vimState.isMode('visual', 'blockwise')
      @vimState.showCursors()
    @vimState.reset()

  dontPutCursorsAtEndOfLine: ->
    for c in @editor.getCursors() when c.isAtEndOfLine() and not c.isAtBeginningOfLine()
      {goalColumn} = c
      c.moveLeft()
      c.goalColumn = goalColumn

  peekTop: ->
    _.last @stack

  pushToStack: (operation, {message}={}) ->
    debug message if message?
    @stack.push(operation)

  pop: ->
    @stack.pop()
