<commit_before>helper         = require './helper'
operatorConfig = require './operator-config'

module.exports =
  ###
  @name formatRange
  @description
  Align character within a certain range of text in the editor
  @param {Editor} editor
  @param {Range} range
  @param {string} character
  @param {Array} offsets
  @param {Object} sectionizedLines
  ###
  formatRange: (editor, range, character, offsets, sectionizedLines) ->
    scope     = editor.getRootScopeDescriptor().getScopeChain()
    config    = operatorConfig.getConfig character, scope
    textBlock = ""

    for currentRow in range.getRows()
      tokenizedLine = helper.getTokenizedLineForBufferRow(editor, currentRow)
      lineCharacter = helper.getAlignCharacter editor, currentRow
      canAlignWith  = operatorConfig.canAlignWith character, lineCharacter, config

      if not lineCharacter or not canAlignWith or tokenizedLine.isComment()
        textBlock += editor.lineTextForBufferRow(currentRow)
        textBlock += "\n" unless currentRow is range.end.row
        continue

      sectionizedLine = sectionizedLines[currentRow]
      currentLine     = ""

      for section, i in sectionizedLine.sections
        offset = section.offset + (if sectionizedLine.hasPrefix() then 1 else 0)

        # New whitespaces to add before/after alignment character
        newSpace = @buildWhitespaces(offsets[i] - offset)

        if config.multiple
          type      = if isNaN(+section.before) then "string" else "number"
          alignment = config.multiple[type]?.alignment or "left"

        else
          alignment = config.alignment

        leftSpace  = if alignment is "left" then newSpace else ""
        leftSpace += " " if config.leftSpace

        rightSpace = if alignment is "right" then newSpace else ""
        # ignore right space config when aligning multiple on the same line
        if config.rightSpace and not (config.multiple and i is 0)
          rightSpace += " "

        if config.multiple
          # NOTE: rightSpace here instead of after lineCharacter to get the proper
          # offset for the token
          before       = section.before
          before       = before.trim() if i > 0 # not the first one
          currentLine += rightSpace + before
          currentLine += leftSpace + lineCharacter unless i is sectionizedLine.length - 1

        else
          currentLine += section.before
          currentLine += leftSpace + lineCharacter + rightSpace
          currentLine += section.after

      textBlock += currentLine
      textBlock += "\n" unless currentRow is range.end.row

    # Set the first line to the start of the line
    range.start.column = 0
    # Set the last line column to the end
    range.end.column = Infinity

    # Replace the whole block
    editor.setTextInBufferRange(range, textBlock)

    # Update the cursor to the end of the original line<commit_msg>Refactor formatter to using lines array<commit_after>helper         = require './helper'
operatorConfig = require './operator-config'

module.exports =
  ###
  @name formatRange
  @description
  Align character within a certain range of text in the editor
  @param {Editor} editor
  @param {Range} range
  @param {string} character
  @param {Array} offsets
  @param {Object} sectionizedLines
  ###
  formatRange: (editor, range, character, offsets, sectionizedLines) ->
    scope  = editor.getRootScopeDescriptor().getScopeChain()
    config = operatorConfig.getConfig character, scope
    lines  = []

    for currentRow in range.getRows()
      currentLine   = ""
      tokenizedLine = helper.getTokenizedLineForBufferRow(editor, currentRow)
      lineCharacter = helper.getAlignCharacter editor, currentRow
      canAlignWith  = operatorConfig.canAlignWith character, lineCharacter, config

      if not lineCharacter or not canAlignWith or tokenizedLine.isComment()
        lines.push(editor.lineTextForBufferRow(currentRow))
        continue

      sectionizedLine = sectionizedLines[currentRow]

      for section, i in sectionizedLine.sections
        offset = section.offset + (if sectionizedLine.hasPrefix() then 1 else 0)

        # New whitespaces to add before/after alignment character
        newSpace = @buildWhitespaces(offsets[i] - offset)

        if config.multiple
          type      = if isNaN(+section.before) then "string" else "number"
          alignment = config.multiple[type]?.alignment or "left"

        else
          alignment = config.alignment

        leftSpace  = if alignment is "left" then newSpace else ""
        leftSpace += " " if config.leftSpace

        rightSpace = if alignment is "right" then newSpace else ""
        # ignore right space config when aligning multiple on the same line
        if config.rightSpace and not (config.multiple and i is 0)
          rightSpace += " "

        if config.multiple
          # NOTE: rightSpace here instead of after lineCharacter to get the proper
          # offset for the token
          before       = section.before
          before       = before.trim() if i > 0 # not the first one
          currentLine += rightSpace + before
          currentLine += leftSpace + lineCharacter unless i is sectionizedLine.length - 1

        else
          currentLine += section.before
          currentLine += leftSpace + lineCharacter + rightSpace
          currentLine += section.after

      lines.push currentLine

    # Set the first line to the start of the line
    range.start.column = 0
    # Set the last line column to the end
    range.end.column = Infinity

    # Replace the whole block
    editor.setTextInBufferRange(range, lines.join('\n'))

    # Update the cursor to the end of the original line
{'fraction_non_alphanumeric': 0.07334620108791017, 'fraction_numerical': 0.002105632567117038, 'mean_word_length': 2.6917098445595853, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7446051', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1413, 'n_words': 507}