class Rufo::Formatter
def do_align(elements, scope)
def do_align(elements, scope) lines = @output.lines # Chunk elements that are in consecutive lines chunks = chunk_while(elements) do |(l1, c1, i1, id1), (l2, c2, i2, id2)| l1 + 1 == l2 && i1 == i2 && id1 == id2 end chunks.each do |elements| next if elements.size == 1 max_column = elements.map { |l, c| c }.max elements.each do |(line, column, _, _, offset)| next if column == max_column split_index = column split_index -= offset if offset target_line = lines[line] before = target_line[0...split_index] after = target_line[split_index..-1] filler_size = max_column - column filler = " " * filler_size # Move all lines affected by the assignment shift if scope == :assign && (range = @assignments_ranges[line]) (line + 1..range).each do |line_number| lines[line_number] = "#{filler}#{lines[line_number]}" # And move other elements too if applicable adjust_other_alignments scope, line_number, column, filler_size end end # Move comments to the right if a change happened if scope != :comment adjust_other_alignments scope, line, column, filler_size end lines[line] = "#{before}#{filler}#{after}" end end @output = lines.join end