class Rufo::Formatter
def do_align(elements, scope)
def do_align(elements, scope) lines = @output.lines # Chunk comments 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 |comments| next if comments.size == 1 if scope == :hash_key # Don't indent successive hash keys if any of them is in turn a hash # or array literal that is formatted in separate lines. has_brace_newline = comments.any? do |(l, c)| line_end = lines[l][c..-1] line_end.start_with?("=> {\n", "=> [\n", "=> [ #", "=> { #", "[\n", "{\n", "[ #", "{ #") end next if has_brace_newline end max_column = comments.map { |l, c| c }.max comments.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