       if @filters.some((filter) -> filter(event))
         return
 
-      tagState = getTagState(event)
+      tagState = @getPairState(event)
       if tagState.state is oppositeState
         stack.push(tagState)
       else

  isEscapedCharAtPoint
} = require './utils'

pairStateInBufferRange = (editor, {matchText, range}) ->
  matchText = _.escapeRegExp(matchText)
  backslash = _.escapeRegExp('\\')
  patterns = [
    "#{backslash}#{backslash}#{matchText}"
    "[^#{backslash}]?#{matchText}"
  ]
  pattern = new RegExp(patterns.join('|'))
  lineText = getLineTextToBufferPosition(editor, range.end)
  charCount = countChar(lineText, pattern)
  if charCount % 2 is 0
    'close'
  else
    'open'

getQuotePairRule = (char) ->
  pattern: ///(#{_.escapeRegExp(char)})///g
  getPairState: pairStateInBufferRange

getBracketPairRule = (open, close) ->
  pattern: ///(#{_.escapeRegExp(open)})|(#{_.escapeRegExp(close)})///g
  getPairState: (editor, {match}) ->
    switch
      when match[1] then 'open'
      when match[2] then 'close'

getRuleForPair = (editor, pair) ->
  [open, close] = pair
  if open is close
    getQuotePairRule(open)
  else
    getBracketPairRule(open, close)

class PairFinder
  filters: []

  constructor: (@options) ->
    {@editor, @pair, @filters, @allowNextLine} = @options
    if @constructor.name isnt 'TagFinder'
      {@pattern, @getPairState} = getRuleForPair(@editor, @pair)

  findPairRange: (which, direction, from, fn) ->
    switch direction

      when 'open' then 'close'
      when 'close' then 'open'

    @editor[scanFunctionName] @getPattern(), scanRange, (event) =>
      if not @allowNextLine and (from.row isnt event.range.start.row)
        event.stop()
        return
