     [open, close] = pair
     @pattern = ///(#{_.escapeRegExp(open)})|(#{_.escapeRegExp(close)})///g
 
-  getPairState: ({match}) ->
-    switch
+  getEventState: ({match, range}) ->
+    state = switch
       when match[1] then 'open'
       when match[2] then 'close'
+    {state, range}
 
 class QuoteFinder extends PairFinder
   setPatternForPair: (pair) ->

      range: event.range
    }

  findTagState: (stack, state, name) ->
    for tagState in stack by -1 when (tagState.state is state) and (tagState.name is name)
      return tagState

  findPair: (which, direction, from, fn) ->
    stack = []
    range = null
    findingState = which
    oppositeState = switch findingState
      when 'open' then 'close'
      when 'close' then 'open'

    @scanPair which, direction, from, (event) =>
      tagState = @getPairState(event)
      if tagState.state isnt which
        stack.push(tagState)
      else
        if oppositeTagState = @findTagState(stack, oppositeState, tagState.name)
          stack = stack[0...stack.indexOf(oppositeTagState)]

        if fn(stack, oppositeTagState)
          range = event.range
          event.stop()

    return range

module.exports = {
  BracketFinder

    stack = []
    range = null
    @scanPair which, direction, from, (event) =>
      eventState = @getEventState(event)
      if eventState.state isnt which
        stack.push(eventState)
      else
        if fn(stack, eventState)
          range = event.range
          event.stop()

    return range

  spliceStack: (stack, eventState) ->
    stack.pop()

  findCloseForward: (from, {allowForwarding}={}) ->
    @findPair 'close', 'forward', from, (stack, closeState) =>
      openState = @spliceStack(stack, closeState)
      unless openState?
        return true

      if stack.length is 0
        {start} = openState.range
        start.isEqual(from) or (allowForwarding and start.row is from.row)

  findOpenBackward: (from) ->
    @findPair 'open', 'backward', from, (stack, openState) =>
      @spliceStack(stack, openState)
      stack.length is 0

  find: (from, options) ->
