class Rufo::Formatter
def visit_literal_elements(elements, inside_hash: false, inside_array: false, token_column:, keep_final_newline: false, &block)
def visit_literal_elements(elements, inside_hash: false, inside_array: false, token_column:, keep_final_newline: false, &block) base_column = @column base_line = @line needs_final_space = (inside_hash || inside_array) && space? first_space = skip_space if inside_hash needs_final_space = false end if inside_array needs_final_space = false end if newline? || comment? needs_final_space = false end # If there's a newline right at the beginning, # write it, and we'll indent element and always # add a trailing comma to the last element needs_trailing_comma = newline? || comment? if needs_trailing_comma if (call_info = @line_to_call_info[@line]) call_info << true end needed_indent = next_indent indent { consume_end_of_line } write_indent(needed_indent) else needed_indent = base_column end wrote_comma = false first_space = nil visitor = block_given? ? block : ->(elem) { visit elem } elements.each_with_index do |elem, i| @literal_elements_level = @node_level is_last = last?(i, elements) wrote_comma = false if needs_trailing_comma indent(needed_indent) { visitor.call(elem) } else visitor.call(elem) end # We have to be careful not to aumatically write a heredoc on next_token, # because we miss the chance to write a comma to separate elements first_space = skip_space_no_heredoc_check indent(needed_indent) do wrote_comma = check_heredocs_in_literal_elements(is_last, wrote_comma) end next unless comma? unless is_last write "," wrote_comma = true end # We have to be careful not to aumatically write a heredoc on next_token, # because we miss the chance to write a comma to separate elements next_token_no_heredoc_check first_space = skip_space_no_heredoc_check indent(needed_indent) do wrote_comma = check_heredocs_in_literal_elements(is_last, wrote_comma) end if newline? || comment? if is_last # Nothing else indent(needed_indent) do consume_end_of_line(first_space: first_space) write_indent end end else write_space unless is_last end end @literal_elements_level = nil if needs_trailing_comma if !wrote_comma && trailing_commas if @last_was_heredoc @output.insert(@end_of_heredoc_position, ",") else write "," end end consume_end_of_line(first_space: first_space) write_indent elsif comment? consume_end_of_line(first_space: first_space) else if needs_final_space consume_space elsif keep_final_newline skip_space else skip_space_or_newline end end if current_token_column == token_column && needed_indent < token_column # If the closing token is aligned with the opening token, we want to # keep it like that, for example in: # # foo([ # 2, # ]) @literal_indents << [base_line, @line, token_column + INDENT_SIZE - needed_indent] elsif call_info && call_info[0] == current_token_column # If the closing literal position matches the column where # the call started, we want to preserve it like that # (otherwise we align it to the first parameter) call_info << @line end end