<commit_before>{Range} = require 'atom'
{ScopeSelector} = require 'first-mate'

# Helper to find the matching start/end tag for the start/end tag under the
# cursor in XML, HTML, etc. editors.
module.exports =
class TagFinder
  constructor: (@editorView) ->
    {@editor} = @editorView

    @tagSelector = new ScopeSelector('entity.name.tag')
    @commentSelector = new ScopeSelector('comment.*')

  getTagName: ->
    wordRange = @editor.getCursor().getCurrentWordBufferRange()
    tagName = @editor.getTextInRange(wordRange)
    tagName.replace(/[<>/]/g, '').trim()

  getTagPattern: ->
    tagName = @getTagName()
    new RegExp("(<#{tagName}([\\s>]|$))|(</#{tagName}>)", 'gi')

  isRangeCommented: (range) ->
    scopes = @editor.scopesForBufferPosition(range.start)
    @commentSelector.matches(scopes)

  getTagStartRange: ->
    tagStartPosition = null
    tagEndPosition = null

    position = @editor.getCursorBufferPosition()
    @editor.backwardsScanInBufferRange /<\/?/, [[0, 0], position], ({match, range, stop}) ->
      tagStartPosition = range.translate([0, match[0].length]).start
      stop()
    @editor.scanInBufferRange /[\s>]|$/, [position, @editor.buffer.getEndPosition()], ({match, range, stop}) ->
      tagEndPosition = range.translate([0, -1]).end
      if range.start.row isnt range.end.row
        tagEndPosition = [range.start.row, Infinity]
      stop()

    [tagStartPosition, tagEndPosition]

  findStartTag: ->
    scanRange = new Range([0, 0], @editor.getCursorBufferPosition())
    startRange = null
    unpairedCount = 0
    console.log @getTagPattern()
    @editor.backwardsScanInBufferRange @getTagPattern(), scanRange, ({match, range, stop}) =>
      return if @isRangeCommented(range)
      if match[1]
        unpairedCount--
        if unpairedCount < 0
          startRange = range.translate([0, 1], [0, -match[2].length])
          stop()
      else
        unpairedCount++

    if startRange?
      {startRange, endRange: @getTagStartRange()}

  findEndTag: ->
    scanRange = new Range(@editor.getCursorBufferPosition(), @editor.buffer.getEndPosition())
    endRange = null
    unpairedCount = 0
    @editor.scanInBufferRange @getTagPattern(), scanRange, ({match, range, stop}) =>
      return if @isRangeCommented(range)
      if match[1]
        unpairedCount++
      else<commit_msg>Remove logging<commit_after>{Range} = require 'atom'
{ScopeSelector} = require 'first-mate'

# Helper to find the matching start/end tag for the start/end tag under the
# cursor in XML, HTML, etc. editors.
module.exports =
class TagFinder
  constructor: (@editorView) ->
    {@editor} = @editorView

    @tagSelector = new ScopeSelector('entity.name.tag')
    @commentSelector = new ScopeSelector('comment.*')

  getTagName: ->
    wordRange = @editor.getCursor().getCurrentWordBufferRange()
    tagName = @editor.getTextInRange(wordRange)
    tagName.replace(/[<>/]/g, '').trim()

  getTagPattern: ->
    tagName = @getTagName()
    new RegExp("(<#{tagName}([\\s>]|$))|(</#{tagName}>)", 'gi')

  isRangeCommented: (range) ->
    scopes = @editor.scopesForBufferPosition(range.start)
    @commentSelector.matches(scopes)

  getTagStartRange: ->
    tagStartPosition = null
    tagEndPosition = null

    position = @editor.getCursorBufferPosition()
    @editor.backwardsScanInBufferRange /<\/?/, [[0, 0], position], ({match, range, stop}) ->
      tagStartPosition = range.translate([0, match[0].length]).start
      stop()
    @editor.scanInBufferRange /[\s>]|$/, [position, @editor.buffer.getEndPosition()], ({match, range, stop}) ->
      tagEndPosition = range.translate([0, -1]).end
      if range.start.row isnt range.end.row
        tagEndPosition = [range.start.row, Infinity]
      stop()

    [tagStartPosition, tagEndPosition]

  findStartTag: ->
    scanRange = new Range([0, 0], @editor.getCursorBufferPosition())
    startRange = null
    unpairedCount = 0
    @editor.backwardsScanInBufferRange @getTagPattern(), scanRange, ({match, range, stop}) =>
      return if @isRangeCommented(range)
      if match[1]
        unpairedCount--
        if unpairedCount < 0
          startRange = range.translate([0, 1], [0, -match[2].length])
          stop()
      else
        unpairedCount++

    if startRange?
      {startRange, endRange: @getTagStartRange()}

  findEndTag: ->
    scanRange = new Range(@editor.getCursorBufferPosition(), @editor.buffer.getEndPosition())
    endRange = null
    unpairedCount = 0
    @editor.scanInBufferRange @getTagPattern(), scanRange, ({match, range, stop}) =>
      return if @isRangeCommented(range)
      if match[1]
        unpairedCount++
      else
{'fraction_non_alphanumeric': 0.14803625377643503, 'fraction_numerical': 0.0073370738023306, 'mean_word_length': 3.46102021174206, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '6573533', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1415, 'n_words': 310}