Skip to content

Commit c8a37a7

Browse files
committed
KCL: Faster recasting of arrays
1 parent d5393a8 commit c8a37a7

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

rust/kcl-lib/src/unparser.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl ArrayExpression {
554554
fn recast(&self, buf: &mut String, options: &FormatOptions, indentation_level: usize, ctxt: ExprContext) {
555555
let num_items = self.elements.len() + self.non_code_meta.non_code_nodes_len();
556556
let mut elems = self.elements.iter();
557-
let mut items_buf = String::with_capacity(512);
557+
let mut items_buf = String::with_capacity(256);
558558
let mut item_spans = Vec::with_capacity(num_items);
559559
let mut found_line_comment = false;
560560

@@ -568,17 +568,19 @@ impl ArrayExpression {
568568
} else {
569569
let el = elems.next().unwrap();
570570
el.recast(&mut items_buf, options, 0, ExprContext::Other);
571-
items_buf.push_str(", ");
571+
if i < num_items - 1 {
572+
items_buf.push_str(", ");
573+
}
572574
}
573575
let end = items_buf.len();
574576
item_spans.push((start, end));
575577
}
576578

579+
let flat_length = items_buf.len() + 2; // +2 is for the [ and ] which will surround the items.
577580
let max_array_length = 40;
578-
let flat_length = items_buf.trim_end_matches(", ").len();
579-
let use_flat = flat_length <= max_array_length && !found_line_comment;
581+
let multi_line = flat_length > max_array_length || found_line_comment;
580582

581-
if use_flat {
583+
if !multi_line {
582584
buf.push('[');
583585
buf.push_str(&items_buf[..flat_length]);
584586
buf.push(']');
@@ -2991,7 +2993,7 @@ fn function001() {
29912993
fn check_arr() {
29922994
let code = "[
29932995
-0.8111463382182231,
2994-
-0.41814807547140576,
2996+
-0.41814807547140576
29952997
]
29962998
";
29972999

0 commit comments

Comments
 (0)