| 1180 | } |
| 1181 | |
| 1182 | static int jl_make_insertion(const char *text, size_t length, size_t object_start, |
| 1183 | const jl_object_t *object, const jl_buffer_t *member, |
| 1184 | jl_buffer_t *insertion) { |
| 1185 | jl_slice_t base; |
| 1186 | jl_slice_t unit; |
| 1187 | bool close_indent_only = false; |
| 1188 | jl_detect_indent(text, object, &base, &unit, &close_indent_only); |
| 1189 | const char *newline = jl_newline_style(text, length); |
| 1190 | |
| 1191 | size_t gap_start = object_start + 1U; |
| 1192 | if (object->member_count > 0U) { |
| 1193 | gap_start = object->trailing_comma ? object->last_comma_pos + 1U : object->last_value_end; |
| 1194 | } |
| 1195 | bool multiline = jl_range_has_newline(text, gap_start, object->close_pos); |
| 1196 | bool empty_tight = object->member_count == 0U && gap_start == object->close_pos; |
| 1197 | |
| 1198 | if (empty_tight) { |
| 1199 | if (jl_buffer_string(insertion, newline) != 0 || |
| 1200 | jl_append_styled_indent(insertion, base, unit, 1U) != 0 || |
| 1201 | jl_buffer_append(insertion, member->data, member->length) != 0 || |
| 1202 | jl_buffer_string(insertion, newline) != 0 || |
| 1203 | jl_buffer_append(insertion, base.data, base.length) != 0) { |
| 1204 | return -1; |
| 1205 | } |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | if (multiline) { |
| 1210 | if (!close_indent_only && jl_buffer_string(insertion, newline) != 0) { |
| 1211 | return -1; |
| 1212 | } |
| 1213 | if (jl_buffer_append(insertion, unit.data, unit.length) != 0 || |
| 1214 | jl_buffer_append(insertion, member->data, member->length) != 0) { |
| 1215 | return -1; |
| 1216 | } |
| 1217 | if (object->trailing_comma && jl_buffer_char(insertion, ',') != 0) { |
| 1218 | return -1; |
| 1219 | } |
| 1220 | if (jl_buffer_string(insertion, newline) != 0 || |
| 1221 | jl_buffer_append(insertion, base.data, base.length) != 0) { |
| 1222 | return -1; |
| 1223 | } |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | /* Zero-width insertion right at close_pos: whatever byte already sits at |
| 1228 | * close_pos - 1 is untouched and stays in the output. When the original |
| 1229 | * had a real gap there (close_pos != gap_start, e.g. "[ \"a\" ]"), that |
| 1230 | * preserved byte already supplies a separator on the leading side, so |
| 1231 | * mirroring it on the trailing side (space before the bracket) matches |
| 1232 | * the array's own loose style. The fully tight case ("[\"a\"]", nothing |
| 1233 | * at all between the last value and the bracket) has no such byte to |
| 1234 | * lean on: manufacture ONE space so `,new` reads `, new` — the |
| 1235 | * comma-spacing convention every other insertion path uses — and add |
| 1236 | * nothing after, so `new]` stays `new]` rather than gaining a trailing |
| 1237 | * space the original never had (#1826 byte-for-byte round-trip). */ |
| 1238 | bool tight = object->close_pos == gap_start; |
| 1239 | if (tight || !jl_is_space((unsigned char)text[object->close_pos - 1U])) { |
no test coverage detected