48611 {
48612 auto &
depth = format_state.depth;
48613 auto &line_length = format_state.line_length;
48614 auto &max_width = format_state.max_width;
48615 auto &c = format_state.component_idx;
48616 switch (component.type) {
48617 case JSONComponentType::BRACKET_OPEN: {
48618 depth += component.text ==
"{" ? format_state.indentation_size : 1;
48619 AddLiteral(format_state, component.text);
48620 if (inline_mode == InlineMode::STANDARD) {
48621
48622
48623 idx_t peek_depth = 0;
48624 idx_t render_size = line_length;
48625 idx_t peek_idx;
48626 InlineMode inline_child_mode = InlineMode::STANDARD;
48627 for (peek_idx = c + 1; peek_idx < components.size() && render_size <= max_width; peek_idx++) {
48628 auto &peek_component = components[peek_idx];
48629 if (peek_component.type == JSONComponentType::BRACKET_OPEN) {
48630 peek_depth++;
48631 } else if (peek_component.type == JSONComponentType::BRACKET_CLOSE) {
48632 if (peek_depth == 0) {
48633
48634 if (render_size + 1 < max_width) {
48635
48636 inline_child_mode = InlineMode::INLINED_SINGLE_LINE;
48637 }
48638 break;
48639 }
48640 peek_depth--;
48641 }
48643 if (peek_component.type == JSONComponentType::COMMA ||
48644 peek_component.type == JSONComponentType::COLON) {
48645 render_size++;
48646 }
48647 }
48648 if (component.text == "[") {
48649
48650
48651 for (peek_idx = c + 1; peek_idx < components.size(); peek_idx++) {
48652 auto &peek_component = components[peek_idx];
48653 peek_depth = 0;
48654 if (peek_component.type == JSONComponentType::BRACKET_OPEN) {
48655 if (peek_component.text == "{") {
48656
48657 break;
48658 }
48659 peek_depth++;
48660 }
48661 if (peek_component.type == JSONComponentType::BRACKET_CLOSE) {
48662 if (peek_depth == 0) {
48663 inline_child_mode = InlineMode::INLINED_MULTI_LINE;
48664 break;
48665 }
48666 peek_depth--;
48667 }
48668 }
48669 }
48670 if (inline_child_mode != InlineMode::STANDARD) {
48671
48672 for (idx_t inline_idx = c + 1; inline_idx <= peek_idx; inline_idx++) {
48673 auto &inline_component = components[inline_idx];
48674 if (inline_child_mode == InlineMode::INLINED_MULTI_LINE && inline_idx + 1 <= peek_idx) {
48675 auto &next_component = components[inline_idx + 1];
48676 if (next_component.type == JSONComponentType::COMMA ||
48677 next_component.type == JSONComponentType::BRACKET_CLOSE) {
48678 if (!LiteralFits(format_state, inline_component.text + next_component.text)) {
48679 AddNewline(format_state);
48680 }
48681 }
48682 }
48683 FormatComponent(format_state, inline_component, inline_child_mode);
48684 }
48685 c = peek_idx;
48686 return;
48687 }
48688 if (format_state.mode == JSONFormattingMode::COMPACT_VERTICAL) {
48689
48690 if (c + 1 < components.size() && components[c + 1].type == JSONComponentType::BRACKET_OPEN) {
48691
48692 return;
48693 }
48694 }
48695 AddNewline(format_state);
48696 }
48697 break;
48698 }
48699 case JSONComponentType::BRACKET_CLOSE: {
48700 idx_t depth_diff = component.text == "}" ? format_state.indentation_size : 1;
48701 if (depth < depth_diff) {
48702
48704 } else {
48705 depth -= depth_diff;
48706 }
48707 if (inline_mode == InlineMode::STANDARD) {
48708 AddNewline(format_state);
48709 }
48710 AddLiteral(format_state, component.text);
48711 break;
48712 }
48713 case JSONComponentType::COMMA:
48714 case JSONComponentType::COLON:
48715 AddLiteral(format_state, component.text);
48716 bool always_inline;
48717 if (format_state.mode == JSONFormattingMode::COMPACT_HORIZONTAL) {
48718
48719 always_inline = false;
48720 } else {
48721
48722 always_inline = component.type == JSONComponentType::COLON;
48723 }
48724 if (inline_mode != InlineMode::STANDARD || always_inline) {
48725 AddSpace(format_state);
48726 } else {
48727 if (format_state.mode != JSONFormattingMode::STANDARD) {
48728
48729 idx_t peek_depth = 0;
48730 idx_t render_size = line_length + 1;
48731 idx_t peek_idx;
48732 bool inline_comma = false;
48733 for (peek_idx = c + 1; peek_idx < components.size() && render_size <= max_width; peek_idx++) {
48734 auto &peek_component = components[peek_idx];
48735 if (peek_component.type == JSONComponentType::BRACKET_OPEN) {
48736 peek_depth++;
48737 } else if (peek_component.type == JSONComponentType::BRACKET_CLOSE) {
48738 if (peek_depth == 0) {
48739 inline_comma = render_size + 1 < max_width;
48740 break;
48741 }
48742 peek_depth--;
48743 }
48744 if (peek_depth == 0 && peek_component.type == JSONComponentType::COMMA) {
48745
48746 inline_comma = render_size + 2 <= max_width;
48747 break;
48748 }
48750 if (peek_component.type == JSONComponentType::COMMA ||
48751 peek_component.type == JSONComponentType::COLON) {
48752 render_size++;
48753 }
48754 }
48755 if (inline_comma) {
48756
48757 AddSpace(format_state);
48758 for (idx_t inline_idx = c + 1; inline_idx < peek_idx; inline_idx++) {
48759 auto &inline_component = components[inline_idx];
48760 FormatComponent(format_state, inline_component, InlineMode::INLINED_SINGLE_LINE);
48761 }
48762 c = peek_idx - 1;
48763 return;
48764 }
48765 }
48766 AddNewline(format_state);
48767 }
48768 break;
48769 case JSONComponentType::NULL_VALUE:
48770 case JSONComponentType::LITERAL:
48771 AddLiteral(format_state, component.text);
48772 break;
48773 default:
48774 throw InternalException("Unsupported JSON component type");
48775 }
48776 }