Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/kcl/reduce.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion docs/kcl/std.json
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
Expand Down
2 changes: 1 addition & 1 deletion public/kcl-samples/bench/bench-parts.kcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
54 changes: 53 additions & 1 deletion rust/kcl-lib/src/unparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ impl CallExpression {
impl CallExpressionKw {
fn recast_args(&self, options: &FormatOptions, indentation_level: usize, ctxt: ExprContext) -> Vec<String> {
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())
};
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ flowchart LR
7["Plane<br>[334, 354, 8]"]
8["Plane<br>[3807, 3842, 8]"]
9["Plane<br>[3807, 3842, 8]"]
10["Plane<br>[3873, 3902, 8]"]
11["Plane<br>[3873, 3902, 8]"]
10["Plane<br>[3871, 3900, 8]"]
11["Plane<br>[3871, 3900, 8]"]
12["StartSketchOnPlane<br>[2700, 2720, 8]"]
13["StartSketchOnPlane<br>[1737, 1757, 8]"]
14["StartSketchOnPlane<br>[3258, 3278, 8]"]
Expand All @@ -268,8 +268,8 @@ flowchart LR
234["Sweep Extrusion<br>[2618, 2642, 8]"]
235["Sweep Extrusion<br>[3180, 3204, 8]"]
236["Sweep Extrusion<br>[3180, 3204, 8]"]
237["Sweep Sweep<br>[3924, 3951, 8]"]
238["Sweep Sweep<br>[3924, 3951, 8]"]
237["Sweep Sweep<br>[3922, 3949, 8]"]
238["Sweep Sweep<br>[3922, 3949, 8]"]
239[Wall]
240[Wall]
241[Wall]
Expand Down
Loading