<commit_before>_ = require 'underscore-plus'
{Range, View} = require 'atom'
TagFinder = require './tag-finder'

startPairMatches =
  '(': ')'
  '[': ']'
  '{': '}'

endPairMatches =
  ')': '('
  ']': '['
  '}': '{'

pairRegexes = {}
for startPair, endPair of startPairMatches
  pairRegexes[startPair] = new RegExp("[#{_.escapeRegExp(startPair + endPair)}]", 'g')

module.exports =
class BracketMatcherView extends View
  @content: ->
    @div =>
      @div class: 'bracket-matcher', style: 'display: none', outlet: 'startView'
      @div class: 'bracket-matcher', style: 'display: none', outlet: 'endView'

  initialize: (@editorView) ->
    {@editor} = @editorView
    @tagFinder = new TagFinder(@editor)
    @pairHighlighted = false
    @updateHighlights = false

    @subscribe atom.config.observe 'editor.fontSize', =>
      @updateMatch()

    @subscribe @editor.getBuffer(), 'changed', =>
      @updateHighlights = true

    @subscribe @editorView, 'editor:display-updated', =>
      if @updateHighlights
        @updateHighlights = false
        @updateMatch()

    @subscribe @editorView, 'editor:min-width-changed', =>
      @updateHighlights = true if @editor.getSoftWrap()

    @subscribe @editor, 'soft-wrap-changed', =>
      @updateHighlights = true

    @subscribe @editor, 'grammar-changed', =>
      @updateHighlights = true

    @subscribeToCursor()

    @subscribeToCommand @editorView, 'bracket-matcher:go-to-matching-bracket', =>
      @goToMatchingPair()

    @subscribeToCommand @editorView, 'bracket-matcher:go-to-enclosing-bracket', =>
      @goToEnclosingPair()

    @subscribeToCommand @editorView, 'bracket-matcher:select-inside-brackets', =>
      @selectInsidePair()

    @editorView.underlayer.append(this)
    @updateMatch()

  subscribeToCursor: ->
    cursor = @editor.getCursor()
    return unless cursor?

    @subscribe cursor, 'moved', =>
      @updateMatch()

    @subscribe cursor, 'destroyed', =>
      @unsubscribe(cursor)
      @subscribeToCursor()
      @updateMatch()

  updateMatch: ->
    if @pairHighlighted
      @startView.hide()
      @endView.hide()
    @pairHighlighted = false

    return unless @editor.getSelection().isEmpty()
    return if @editor.isFoldedAtCursorRow()

    {position, currentPair, matchingPair} = @findCurrentPair(startPairMatches)
    if position
      matchPosition = @findMatchingEndPair(position, currentPair, matchingPair)
    else
      {position, currentPair, matchingPair} = @findCurrentPair(endPairMatches)
      if position
        matchPosition = @findMatchingStartPair(position, matchingPair, currentPair)

    if position? and matchPosition?
      @moveStartView([position, position.translate([0, 1])])
      @moveEndView([matchPosition, matchPosition.translate([0, 1])])
      @pairHighlighted = true
    else
      if pair = @tagFinder.findMatchingTags()
        @moveStartView(pair.startRange)
        @moveEndView(pair.endRange)<commit_msg>Only update match if editor is still alive<commit_after>_ = require 'underscore-plus'
{Range, View} = require 'atom'
TagFinder = require './tag-finder'

startPairMatches =
  '(': ')'
  '[': ']'
  '{': '}'

endPairMatches =
  ')': '('
  ']': '['
  '}': '{'

pairRegexes = {}
for startPair, endPair of startPairMatches
  pairRegexes[startPair] = new RegExp("[#{_.escapeRegExp(startPair + endPair)}]", 'g')

module.exports =
class BracketMatcherView extends View
  @content: ->
    @div =>
      @div class: 'bracket-matcher', style: 'display: none', outlet: 'startView'
      @div class: 'bracket-matcher', style: 'display: none', outlet: 'endView'

  initialize: (@editorView) ->
    {@editor} = @editorView
    @tagFinder = new TagFinder(@editor)
    @pairHighlighted = false
    @updateHighlights = false

    @subscribe atom.config.observe 'editor.fontSize', =>
      @updateMatch()

    @subscribe @editor.getBuffer(), 'changed', =>
      @updateHighlights = true

    @subscribe @editorView, 'editor:display-updated', =>
      if @updateHighlights
        @updateHighlights = false
        @updateMatch()

    @subscribe @editorView, 'editor:min-width-changed', =>
      @updateHighlights = true if @editor.getSoftWrap()

    @subscribe @editor, 'soft-wrap-changed', =>
      @updateHighlights = true

    @subscribe @editor, 'grammar-changed', =>
      @updateHighlights = true

    @subscribeToCursor()

    @subscribeToCommand @editorView, 'bracket-matcher:go-to-matching-bracket', =>
      @goToMatchingPair()

    @subscribeToCommand @editorView, 'bracket-matcher:go-to-enclosing-bracket', =>
      @goToEnclosingPair()

    @subscribeToCommand @editorView, 'bracket-matcher:select-inside-brackets', =>
      @selectInsidePair()

    @editorView.underlayer.append(this)
    @updateMatch()

  subscribeToCursor: ->
    cursor = @editor.getCursor()
    return unless cursor?

    @subscribe cursor, 'moved', =>
      @updateMatch()

    @subscribe cursor, 'destroyed', =>
      @unsubscribe(cursor)
      @subscribeToCursor()
      @updateMatch() if @editor.isAlive()

  updateMatch: ->
    if @pairHighlighted
      @startView.hide()
      @endView.hide()
    @pairHighlighted = false

    return unless @editor.getSelection().isEmpty()
    return if @editor.isFoldedAtCursorRow()

    {position, currentPair, matchingPair} = @findCurrentPair(startPairMatches)
    if position
      matchPosition = @findMatchingEndPair(position, currentPair, matchingPair)
    else
      {position, currentPair, matchingPair} = @findCurrentPair(endPairMatches)
      if position
        matchPosition = @findMatchingStartPair(position, matchingPair, currentPair)

    if position? and matchPosition?
      @moveStartView([position, position.translate([0, 1])])
      @moveEndView([matchPosition, matchPosition.translate([0, 1])])
      @pairHighlighted = true
    else
      if pair = @tagFinder.findMatchingTags()
        @moveStartView(pair.startRange)
        @moveEndView(pair.endRange)
{'fraction_non_alphanumeric': 0.13966858302333446, 'fraction_numerical': 0.0013527223537368955, 'mean_word_length': 3.698173153296267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '7534283', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1776, 'n_words': 356}