<commit_before>Trix.galleryFilter = (snapshot) ->
  filter = new Filter snapshot
  filter.perform()
  filter.getSnapshot()

class Filter
  constructor: (snapshot) ->
    {@document, @selectedRange} = snapshot

  perform: ->
    @removeGalleryAttribute()
    @applyGalleryAttribute()

  getSnapshot: ->
    {@document, @selectedRange}

  # Private

  removeGalleryAttribute: ->
    for range in @findRangesOfGalleryBlocks()
      @document = @document.removeAttributeAtRange("gallery", range)

  applyGalleryAttribute: ->
    offset = 0
    for range in @findRangesOfGalleryPieces() when range[1] - range[0] > 1
      range[0] += offset
      range[1] += offset

      unless @document.getCharacterAtPosition(range[1]) is "\n"
        @document = @document.insertBlockBreakAtRange(range[1])
        offset++

      unless range[0] is 0
        unless @document.getCharacterAtPosition(range[0] - 1) is "\n"
          @document = @document.insertBlockBreakAtRange(range[0])
          offset++

      @document = @document.applyBlockAttributeAtRange("gallery", true, range)

  findRangesOfGalleryBlocks: ->
    @document.findRangesForBlockAttribute("gallery")

  findRangesOfGalleryPieces: ->
    @document.findRangesForTextAttribute("presentation", withValue: "gallery")<commit_msg>Adjust selection when inserting gallery block breaks<commit_after>Trix.galleryFilter = (snapshot) ->
  filter = new Filter snapshot
  filter.perform()
  filter.getSnapshot()

class Filter
  constructor: (snapshot) ->
    {@document, @selectedRange} = snapshot

  perform: ->
    @removeGalleryAttribute()
    @applyGalleryAttribute()

  getSnapshot: ->
    {@document, @selectedRange}

  # Private

  removeGalleryAttribute: ->
    for range in @findRangesOfGalleryBlocks()
      @document = @document.removeAttributeAtRange("gallery", range)

  applyGalleryAttribute: ->
    offset = 0
    for range in @findRangesOfGalleryPieces() when range[1] - range[0] > 1
      range[0] += offset
      range[1] += offset

      unless @document.getCharacterAtPosition(range[1]) is "\n"
        @document = @document.insertBlockBreakAtRange(range[1])
        @moveSelectedRangeForward() if range[1] < @selectedRange[1]
        offset++

      unless range[0] is 0
        unless @document.getCharacterAtPosition(range[0] - 1) is "\n"
          @document = @document.insertBlockBreakAtRange(range[0])
          @moveSelectedRangeForward() if range[0] < @selectedRange[0]
          offset++

      @document = @document.applyBlockAttributeAtRange("gallery", true, range)

  findRangesOfGalleryBlocks: ->
    @document.findRangesForBlockAttribute("gallery")

  findRangesOfGalleryPieces: ->
    @document.findRangesForTextAttribute("presentation", withValue: "gallery")

  moveSelectedRangeForward: ->
    @selectedRange[0] += 1
    @selectedRange[1] += 1
{'fraction_non_alphanumeric': 0.12017015242821695, 'fraction_numerical': 0.012052463665366891, 'mean_word_length': 3.7911714770797964, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '4654620', 'n_tokens_mistral': 857, 'n_tokens_neox': 820, 'n_words': 166}