From 1c697d30ee81bfafd0f2d208e354cf341a546192 Mon Sep 17 00:00:00 2001 From: Jonathan Tran Date: Fri, 2 May 2025 09:16:07 -0400 Subject: [PATCH] Fix to not add extra spaces when formatting call (#6652) --- docs/kcl/reduce.md | 2 +- docs/kcl/std.json | 2 +- public/kcl-samples/bench/bench-parts.kcl | 2 +- rust/kcl-lib/src/unparser.rs | 54 ++++++++++++++++++- .../artifact_graph_flowchart.snap.md | 16 +++--- .../bench/artifact_graph_flowchart.snap.md | 8 +-- 6 files changed, 68 insertions(+), 16 deletions(-) diff --git a/docs/kcl/reduce.md b/docs/kcl/reduce.md index b7dd42e1e..135e3e8f4 100644 --- a/docs/kcl/reduce.md +++ b/docs/kcl/reduce.md @@ -55,7 +55,7 @@ fn sum(arr): // We use `assert` to check that our `sum` function gives the // expected result. It's good to check your work! assert( - sum([1, 2, 3]), + sum([1, 2, 3]), isEqualTo = 6, tolerance = 0.1, error = "1 + 2 + 3 summed is 6", diff --git a/docs/kcl/std.json b/docs/kcl/std.json index b33ee21e4..a26cc456b 100644 --- a/docs/kcl/std.json +++ b/docs/kcl/std.json @@ -232205,7 +232205,7 @@ "unpublished": false, "deprecated": false, "examples": [ - "// This function adds two numbers.\nfn add(a, b) {\n return a + b\n}\n\n// This function adds an array of numbers.\n// It uses the `reduce` function, to call the `add` function on every\n// element of the `arr` parameter. The starting value is 0.\nfn sum(@arr) {\n return reduce(arr, initial = 0, f = add)\n}\n\n/* The above is basically like this pseudo-code:\nfn sum(arr):\n sumSoFar = 0\n for i in arr:\n sumSoFar = add(sumSoFar, i)\n return sumSoFar */\n\n// We use `assert` to check that our `sum` function gives the\n// expected result. It's good to check your work!\nassert(\n sum([1, 2, 3]),\n isEqualTo = 6,\n tolerance = 0.1,\n error = \"1 + 2 + 3 summed is 6\",\n)", + "// This function adds two numbers.\nfn add(a, b) {\n return a + b\n}\n\n// This function adds an array of numbers.\n// It uses the `reduce` function, to call the `add` function on every\n// element of the `arr` parameter. The starting value is 0.\nfn sum(@arr) {\n return reduce(arr, initial = 0, f = add)\n}\n\n/* The above is basically like this pseudo-code:\nfn sum(arr):\n sumSoFar = 0\n for i in arr:\n sumSoFar = add(sumSoFar, i)\n return sumSoFar */\n\n// We use `assert` to check that our `sum` function gives the\n// expected result. It's good to check your work!\nassert(\n sum([1, 2, 3]),\n isEqualTo = 6,\n tolerance = 0.1,\n error = \"1 + 2 + 3 summed is 6\",\n)", "// This example works just like the previous example above, but it uses\n// an anonymous `add` function as its parameter, instead of declaring a\n// named function outside.\narr = [1, 2, 3]\nsum = reduce(\n arr,\n initial = 0,\n f = fn(i, result_so_far) {\n return i + result_so_far\n },\n)\n\n// We use `assert` to check that our `sum` function gives the\n// expected result. It's good to check your work!\nassert(\n sum,\n isEqualTo = 6,\n tolerance = 0.1,\n error = \"1 + 2 + 3 summed is 6\",\n)", "// Declare a function that sketches a decagon.\nfn decagon(@radius) {\n // Each side of the decagon is turned this many radians from the previous angle.\n stepAngle = (1 / 10 * TAU): number(rad)\n\n // Start the decagon sketch at this point.\n startOfDecagonSketch = startSketchOn(XY)\n |> startProfile(at = [cos(0) * radius, sin(0) * radius])\n\n // Use a `reduce` to draw the remaining decagon sides.\n // For each number in the array 1..10, run the given function,\n // which takes a partially-sketched decagon and adds one more edge to it.\n fullDecagon = reduce(\n [1..10],\n initial = startOfDecagonSketch,\n f = fn(i, partialDecagon) {\n // Draw one edge of the decagon.\n x = cos(stepAngle * i) * radius\n y = sin(stepAngle * i) * radius\n return line(partialDecagon, end = [x, y])\n },\n )\n\n return fullDecagon\n}\n\n/* The `decagon` above is basically like this pseudo-code:\nfn decagon(radius):\n stepAngle = ((1/10) * TAU): number(rad)\n plane = startSketchOn(XY)\n startOfDecagonSketch = startProfile(plane, at = [(cos(0)*radius), (sin(0) * radius)])\n\n // Here's the reduce part.\n partialDecagon = startOfDecagonSketch\n for i in [1..10]:\n x = cos(stepAngle * i) * radius\n y = sin(stepAngle * i) * radius\n partialDecagon = line(partialDecagon, end = [x, y])\n fullDecagon = partialDecagon // it's now full\n return fullDecagon */\n\n// Use the `decagon` function declared above, to sketch a decagon with radius 5.\ndecagon(5.0)\n |> close()" ] diff --git a/public/kcl-samples/bench/bench-parts.kcl b/public/kcl-samples/bench/bench-parts.kcl index 306d7c452..8ec7b91e1 100644 --- a/public/kcl-samples/bench/bench-parts.kcl +++ b/public/kcl-samples/bench/bench-parts.kcl @@ -126,7 +126,7 @@ fn armRestProfile(@plane, offset) { export fn armRest(@plane, offset) { path = armRestPath( offsetPlane(plane, offset = offset)) - profile = armRestProfile( offsetPlane(-XZ, offset = 20), offset = -offset) + profile = armRestProfile(offsetPlane(-XZ, offset = 20), offset = -offset) sweep(profile, path = path) return 0 } diff --git a/rust/kcl-lib/src/unparser.rs b/rust/kcl-lib/src/unparser.rs index de2a82230..815f383f3 100644 --- a/rust/kcl-lib/src/unparser.rs +++ b/rust/kcl-lib/src/unparser.rs @@ -377,7 +377,7 @@ impl CallExpression { impl CallExpressionKw { fn recast_args(&self, options: &FormatOptions, indentation_level: usize, ctxt: ExprContext) -> Vec { let mut arg_list = if let Some(first_arg) = &self.unlabeled { - vec![first_arg.recast(options, indentation_level, ctxt)] + vec![first_arg.recast(options, indentation_level, ctxt).trim().to_owned()] } else { Vec::with_capacity(self.arguments.len()) }; @@ -2584,6 +2584,58 @@ sketch002 = startSketchOn({ assert_eq!(actual, input); } + #[test] + fn unparse_call_inside_function_single_line() { + let input = r#"fn foo() { + toDegrees(atan(0.5), foo = 1) + return 0 +} +"#; + let ast = crate::parsing::top_level_parse(input).unwrap(); + let actual = ast.recast(&FormatOptions::new(), 0); + assert_eq!(actual, input); + } + + #[test] + fn unparse_call_inside_function_args_multiple_lines() { + let input = r#"fn foo() { + toDegrees( + atan(0.5), + foo = 1, + bar = 2, + baz = 3, + qux = 4, + ) + return 0 +} +"#; + let ast = crate::parsing::top_level_parse(input).unwrap(); + let actual = ast.recast(&FormatOptions::new(), 0); + assert_eq!(actual, input); + } + + #[test] + fn unparse_call_inside_function_single_arg_multiple_lines() { + let input = r#"fn foo() { + toDegrees( + [ + profile0, + profile1, + profile2, + profile3, + profile4, + profile5 + ], + key = 1, + ) + return 0 +} +"#; + let ast = crate::parsing::top_level_parse(input).unwrap(); + let actual = ast.recast(&FormatOptions::new(), 0); + assert_eq!(actual, input); + } + #[test] fn recast_objects_with_comments() { use winnow::Parser; diff --git a/rust/kcl-lib/tests/kcl_samples/axial-fan/artifact_graph_flowchart.snap.md b/rust/kcl-lib/tests/kcl_samples/axial-fan/artifact_graph_flowchart.snap.md index 3a0811db8..fd3949c82 100644 --- a/rust/kcl-lib/tests/kcl_samples/axial-fan/artifact_graph_flowchart.snap.md +++ b/rust/kcl-lib/tests/kcl_samples/axial-fan/artifact_graph_flowchart.snap.md @@ -605,19 +605,19 @@ flowchart LR 84 --- 147 84 --- 233 87 --- 169 - 87 x--> 184 + 87 x--> 183 87 --- 212 87 --- 257 89 --- 168 - 89 x--> 184 + 89 x--> 183 89 --- 214 89 --- 258 90 --- 167 - 90 x--> 184 + 90 x--> 183 90 --- 211 90 --- 256 92 --- 166 - 92 x--> 184 + 92 x--> 183 92 --- 213 92 --- 259 119 --- 162 @@ -865,10 +865,10 @@ flowchart LR 221 <--x 181 222 <--x 181 193 <--x 182 - 211 <--x 183 - 212 <--x 183 - 213 <--x 183 - 214 <--x 183 + 211 <--x 184 + 212 <--x 184 + 213 <--x 184 + 214 <--x 184 203 <--x 186 204 <--x 186 205 <--x 186 diff --git a/rust/kcl-lib/tests/kcl_samples/bench/artifact_graph_flowchart.snap.md b/rust/kcl-lib/tests/kcl_samples/bench/artifact_graph_flowchart.snap.md index 7db77e92a..ef17bab06 100644 --- a/rust/kcl-lib/tests/kcl_samples/bench/artifact_graph_flowchart.snap.md +++ b/rust/kcl-lib/tests/kcl_samples/bench/artifact_graph_flowchart.snap.md @@ -241,8 +241,8 @@ flowchart LR 7["Plane
[334, 354, 8]"] 8["Plane
[3807, 3842, 8]"] 9["Plane
[3807, 3842, 8]"] - 10["Plane
[3873, 3902, 8]"] - 11["Plane
[3873, 3902, 8]"] + 10["Plane
[3871, 3900, 8]"] + 11["Plane
[3871, 3900, 8]"] 12["StartSketchOnPlane
[2700, 2720, 8]"] 13["StartSketchOnPlane
[1737, 1757, 8]"] 14["StartSketchOnPlane
[3258, 3278, 8]"] @@ -268,8 +268,8 @@ flowchart LR 234["Sweep Extrusion
[2618, 2642, 8]"] 235["Sweep Extrusion
[3180, 3204, 8]"] 236["Sweep Extrusion
[3180, 3204, 8]"] - 237["Sweep Sweep
[3924, 3951, 8]"] - 238["Sweep Sweep
[3924, 3951, 8]"] + 237["Sweep Sweep
[3922, 3949, 8]"] + 238["Sweep Sweep
[3922, 3949, 8]"] 239[Wall] 240[Wall] 241[Wall]