       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
+
   findPairRange: (which, direction, from, fn) ->
-    switch direction
-      when 'forward'
-        scanRange = new Range(from, @editor.buffer.getEndPosition())
-        scanFunctionName = 'scanInBufferRange'
-      when 'backward'
-        scanRange = new Range([0, 0], from)
-        scanFunctionName = 'backwardsScanInBufferRange'
-
     stack = []
     range = null
-
     findingState = which
     oppositeState = switch findingState
       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
-
-      if @filters.some((filter) -> filter(event))
-        return
-
+    @scanPairRange which, direction, from, (event) =>
       tagState = @getPairState(event)
-      if tagState.state is oppositeState
+      if tagState.state isnt which
         stack.push(tagState)
       else
-        if oppositeTagState = findTagState(stack, oppositeState, tagState.name)
+        if oppositeTagState = @findTagState(stack, oppositeState, tagState.name)
           stack = stack[0...stack.indexOf(oppositeTagState)]
 
         if fn(stack, oppositeTagState)

  setPatternForPair: (pair) ->
    @pattern = ///(#{_.escapeRegExp(pair[0])})///g

  getPairState: (editor, {matchText, range}) ->
    matchText = _.escapeRegExp(matchText)
    backslash = _.escapeRegExp('\\')
    patterns = [

  getPattern: ->
    @pattern

  scanPairRange: (which, direction, from, fn) ->
    switch direction
      when 'forward'
        scanRange = new Range(from, @editor.buffer.getEndPosition())
