class Rufo::Formatter
def visit_literal_elements(elements, inside_hash: false, inside_array: false, token_column:)
def visit_literal_elements(elements, inside_hash: false, inside_array: false, token_column:) base_column = @column base_line = @line needs_final_space = (inside_hash || inside_array) && space? first_space = skip_space if inside_hash case @spaces_inside_hash_brace when :never needs_final_space = false when :always needs_final_space = true end end if inside_array case @spaces_inside_array_bracket when :never needs_final_space = false when :always needs_final_space = true end end if newline? || comment? needs_final_space = false elsif needs_final_space if inside_hash && first_space && @spaces_inside_hash_brace == :dynamic write first_space[2] elsif inside_array && first_space && @spaces_inside_array_bracket == :dynamic write first_space[2] else consume_space end base_column = @column 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 last_has_comma = false first_space = nil elements.each_with_index do |elem, i| is_last = last?(i, elements) wrote_comma = false last_has_comma = false if needs_trailing_comma indent(needed_indent) { visit elem } else visit 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 wrote_comma = check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma) if is_last && !comma? && !wrote_comma && !needs_trailing_comma && !comment? if (inside_hash && @spaces_inside_hash_brace == :dynamic) || (inside_array && @spaces_inside_array_bracket == :dynamic) if first_space write first_space[2] else needs_final_space = false end end end next unless comma? last_has_comma = true 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 wrote_comma = check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma) if newline? || comment? if is_last # Nothing else indent(needed_indent) do consume_end_of_line(first_space: first_space) write_indent end end elsif !is_last && first_space && @spaces_after_comma == :dynamic write_space first_space[2] elsif @spaces_after_comma == :one write_space unless is_last end end if needs_trailing_comma case @trailing_commas when :always write "," unless wrote_comma when :never # Nothing when :dynamic write "," if last_has_comma && !wrote_comma 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 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