Bugfix: formatter changed [0..<4] to [0..4] (#7186)

In #7179 I added exclusive ranges for KCL, but I forgot to update the
formatter/unparser to handle them. So it was silently changing exclusive-end
ranges to inclusive-end ranges.
This commit is contained in:
Adam Chalmers
2025-05-23 12:03:46 -05:00
committed by GitHub
parent cd537cd9c2
commit 034366e65e

View File

@ -583,14 +583,16 @@ impl ArrayRangeExpression {
let s1 = self.start_element.recast(options, 0, ExprContext::Other);
let s2 = self.end_element.recast(options, 0, ExprContext::Other);
let range_op = if self.end_inclusive { ".." } else { "..<" };
// Format these items into a one-line array. Put spaces around the `..` if either expression
// is non-trivial. This is a bit arbitrary but people seem to like simple ranges to be formatted
// tightly, but this is a misleading visual representation of the precedence if the range
// components are compound expressions.
if expr_is_trivial(&self.start_element) && expr_is_trivial(&self.end_element) {
format!("[{s1}..{s2}]")
format!("[{s1}{range_op}{s2}]")
} else {
format!("[{s1} .. {s2}]")
format!("[{s1} {range_op} {s2}]")
}
// Assume a range expression fits on one line.
@ -2797,4 +2799,12 @@ yo = 'bing'
let recasted = ast.recast(&FormatOptions::new(), 0);
assert_eq!(recasted, code);
}
#[test]
fn array_range_end_exclusive() {
let code = "myArray = [0..<4]\n";
let ast = crate::parsing::top_level_parse(code).unwrap();
let recasted = ast.recast(&FormatOptions::new(), 0);
assert_eq!(recasted, code);
}
}