Compare commits

..

128 Commits

Author SHA1 Message Date
df8977407c make delet of extrusions work for multi profile 2025-02-13 10:06:34 -05:00
42016b7335 get delet working for walls 2025-02-13 10:06:34 -05:00
09f3e6e170 Add support for deleting sketches on caps (not walls) 2025-02-13 10:06:34 -05:00
3747db01b8 Delete paths associated with sketch when the sketch plane is deleted 2025-02-13 10:05:41 -05:00
bf1a42f6c0 remove stale comment 2025-02-13 21:06:36 +11:00
b4d1a36bd9 fix closing profile bug (tangentArcTo being ignored) 2025-02-13 20:55:57 +11:00
b937934834 fix close profiles 2025-02-13 19:38:09 +11:00
0208eecefa fix delete of circle profile 2025-02-13 19:06:38 +11:00
9999e4e62f fix changing tools part way through circle/rect tools 2025-02-13 19:01:28 +11:00
b390e3e083 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-13 17:41:10 +11:00
9a89926749 clean up for face codeRef 2025-02-13 12:18:07 +11:00
d7f834f23d Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-13 12:02:32 +11:00
500d92d1fc Merge branch 'main' into kurt-multi-profile-again 2025-02-12 18:34:02 -05:00
bd10c65bdb Fix edit/select sketch-on-cap via feature tree 2025-02-12 17:51:15 -05:00
039092ec24 Update output after adding cap-to-path graph edge 2025-02-12 13:54:39 -05:00
8a920b6cb7 Prevent double write to KCL code on revolve 2025-02-12 11:58:20 -05:00
c1db093e0f nit 2025-02-12 19:24:36 +11:00
5f8ae22b04 Update src/clientSideScene/segments.ts
Co-authored-by: Frank Noirot <frank@zoo.dev>
2025-02-12 19:23:30 +11:00
dc7b901eb9 remove file tree from diff 2025-02-12 19:21:07 +11:00
60dcf9d79e rename error 2025-02-12 19:08:30 +11:00
520f899ad4 Update src/lib/rectangleTool.ts
Co-authored-by: Frank Noirot <frank@zoo.dev>
2025-02-12 18:58:43 +11:00
41340c8bf0 explain function name 2025-02-12 18:51:05 +11:00
a78ec6cd17 fix snapshot test 2025-02-12 18:46:23 +11:00
de526ae36e make other startSketchOn's work 2025-02-12 18:19:39 +11:00
4a8e582865 fix going into edit sketch 2025-02-12 17:28:06 +11:00
1854064274 add path ids to cap 2025-02-12 17:07:32 +11:00
4a8897be4b Merge branch 'main' into kurt-multi-profile-again 2025-02-11 19:30:27 -05:00
83f458fc36 Merge branch 'main' into kurt-multi-profile-again 2025-02-11 17:58:00 -05:00
a686fe914b Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-11 17:33:43 +11:00
cba2349064 fix profile start snap bug 2025-02-11 17:31:49 +11:00
5ae92bcf5c Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-11 15:46:56 +11:00
ad8e306bdb Fix clippy warning 2025-02-10 19:46:33 -05:00
508e1c919c Update output after changing back to skipping none and both 2025-02-10 19:18:44 -05:00
58ec6100c4 Change back to skipping face cap none and both 2025-02-10 19:18:17 -05:00
11eceefedf Fix to use correct source ranges
This also reduces cloning.
2025-02-10 18:39:57 -05:00
11a678df5b Update output from simulation tests 2025-02-10 18:36:38 -05:00
22c0003eb1 Update src/lang/modifyAst.ts
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-11 07:52:35 +11:00
8f0a40ba6e remove onboarding test changes 2025-02-11 07:30:16 +11:00
89b0ccb090 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-11 07:25:52 +11:00
5d22308ad2 remove test skip 2025-02-11 07:21:17 +11:00
5580631c8f remove bad doc comment 2025-02-11 07:21:04 +11:00
e1494c9f16 A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) 2025-02-10 11:53:51 +00:00
4a0d852a3c A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) 2025-02-10 11:37:53 +00:00
b213834316 clean up 2025-02-10 22:23:34 +11:00
1d8348c2cf 2 test for three point circle 2025-02-10 21:44:55 +11:00
2227287c9d 1 test for three point circle 2025-02-10 21:29:17 +11:00
680fc30682 add draft point for first click of three point circ 2025-02-10 18:45:14 +11:00
40fb6a44d3 fmt 2025-02-10 18:44:05 +11:00
5713bfd9fa fix up state diagram 2025-02-10 17:46:42 +11:00
77902d550f make three point have cross hair to make it consistent with othe rtools 2025-02-10 17:45:57 +11:00
db895d6801 small bug 2025-02-10 17:45:35 +11:00
3e1f8584ea test works now without me changing anything big-fixed-itself 2025-02-10 16:31:51 +11:00
2501a98cd9 clippy again 2025-02-10 16:11:14 +11:00
e60b0e64ba Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-10 16:01:27 +11:00
3379cc489a is clippy happy now? 2025-02-10 16:00:52 +11:00
a8b7328f65 commint snaps? 2025-02-10 15:59:43 +11:00
ab6995bde3 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-10 15:43:06 +11:00
6df5e70186 update docs 2025-02-10 15:24:38 +11:00
6b1cc36911 more test tweaks 2025-02-10 13:27:20 +11:00
6a16e47491 tsc 2025-02-10 12:37:37 +11:00
f4f0533179 fix electron specific test 2025-02-10 12:36:03 +11:00
6360b8acac fix more tests 2025-02-10 11:08:00 +11:00
064a41d675 fix more tests and add a fix me 2025-02-10 10:00:14 +11:00
e075622a7f fix sweep point-and-click test 2025-02-10 09:52:51 +11:00
7a7929211a Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-10 09:08:59 +11:00
1f5f42963d fix some tests 2025-02-07 17:00:05 +11:00
235e6a1056 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-07 13:50:26 +11:00
09cfbc1837 fix unit test 2025-02-07 13:50:16 +11:00
319029235c fix circ dep 2025-02-07 12:59:19 +11:00
a7f4b0f037 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-07 12:02:42 +11:00
d3afa38bd5 small thing 2025-02-06 23:18:02 +11:00
45416df215 maybe fix a unit test 2025-02-06 22:39:29 +11:00
ee54cdd27a lint and typing 2025-02-06 22:34:21 +11:00
84ae567f37 A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) 2025-02-06 11:21:42 +00:00
f94671f1bb Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-06 22:09:21 +11:00
bcf3790266 remove duplicate test 2025-02-06 22:09:13 +11:00
d70ebca165 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-06 17:40:24 +11:00
d8a9abba69 overlay fix 2025-02-06 17:16:51 +11:00
0fd18c14ef more rust fixes 2025-02-05 14:29:39 +11:00
36d4830c34 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-05 13:56:55 +11:00
4ce6054e64 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-05 13:56:44 +11:00
ced49f8ddc fix rust 2025-02-05 12:50:58 +11:00
e063622139 more 2025-02-05 12:42:23 +11:00
42178fa649 more 2025-02-05 12:34:19 +11:00
4bb23bc917 bad attempts at fixing rust 2025-02-05 12:27:58 +11:00
72272d5d98 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-05 11:49:09 +11:00
5ef0a1e75f Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-05 11:48:43 +11:00
d8dc49b08a some things needed for multi-profile tests 2025-02-04 15:55:57 +11:00
87eabef450 fix sketch on face after updates to rust side artifact graph 2025-02-04 15:23:35 +11:00
40e4f2236f Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-04 15:18:30 +11:00
663076f790 add face codef ref for walls and caps 2025-02-04 15:17:50 +11:00
f2c76b0509 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-04 06:46:35 +11:00
481bef859a cargo fmt 2025-02-03 22:12:05 +11:00
1a67d344ee Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-03 22:08:13 +11:00
774e3efcb7 fix types 2025-02-03 22:06:24 +11:00
4ec44690bf fmt 2025-02-03 18:37:16 +11:00
d2f0865f95 Merge remote-tracking branch 'origin' into kurt-multi-profile-again 2025-02-03 18:36:38 +11:00
84d17454e9 get overlays working for circle three point 2025-02-03 17:53:08 +11:00
5a5138a703 most of the fix for 3 point circle 2025-01-31 21:26:19 +11:00
33468c4c96 rust changes to make three point confrom to same as others since we're not ready with name params yet 2025-01-31 13:31:04 +11:00
b3467bbe5a wip 2025-01-27 17:36:29 -05:00
90086488b5 WIP 2025-01-24 15:58:54 -05:00
32e8975799 wip 2025-01-17 15:37:28 -05:00
648616c667 Merge branch 'main' into lf94/kurt-bring-back-multi-profile 2025-01-16 11:13:59 -05:00
482487cf57 Merge branch 'main' into lf94/kurt-bring-back-multi-profile 2025-01-15 14:07:47 -05:00
5fe3023be9 Fix partial execution 2025-01-10 15:39:25 -05:00
30397ba7ab Fix up all the tests 2025-01-09 15:59:11 -05:00
3344208c63 Trigger CI 2025-01-08 21:28:36 -05:00
fcf3272ad2 A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) 2025-01-09 02:17:14 +00:00
d3e4b123d0 Merge branch 'main' into kurt-bring-back-multi-profile 2025-01-08 21:08:47 -05:00
2bb548c000 Trigger CI 2024-12-20 11:11:39 -05:00
b09c240e36 A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) 2024-12-20 12:32:43 +00:00
6c9d14af93 partial fixes 2024-12-20 23:24:15 +11:00
0642e49189 A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) 2024-12-20 23:21:32 +11:00
6add1d73ad chore: disabled file watcher which prevents faster file write (#4835) 2024-12-20 23:21:21 +11:00
68c89746c7 trigger CI 2024-12-20 22:14:49 +11:00
9f323c207c A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-16-cores) 2024-12-20 11:06:49 +00:00
7197b6c85d A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) 2024-12-20 10:54:43 +00:00
913f2641c3 A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-macos-8-cores) 2024-12-20 10:53:02 +00:00
e9086c54ba lint 2024-12-20 21:46:48 +11:00
9f93346dc6 lint 2024-12-20 21:45:04 +11:00
1b9f5f20f5 Merge remote-tracking branch 'origin' into kurt-bring-back-multi-profile 2024-12-20 21:43:41 +11:00
3865637c61 Add Rust side artifacts for startSketchOn face or plane (#4834)
* Add Rust side artifacts for startSketchOn face or plane

* move ast digging

---------

Co-authored-by: Kurt Hutten Irev-Dev <k.hutten@protonmail.ch>
2024-12-19 11:03:21 +11:00
2c40e8a97c trigger CI 2024-12-18 10:12:18 +11:00
c696f0837a A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest-8-cores) 2024-12-17 22:58:41 +00:00
30edf2ad56 Merge remote-tracking branch 'origin' into kurt-bring-back-multi-profile 2024-12-18 09:53:34 +11:00
e60cabb193 fix poor 1000ms wait UX 2024-12-18 08:44:35 +11:00
1e9cf6f256 Revert "Revert multi-profile (#4812)"
This reverts commit efe8089b08.
2024-12-18 07:08:41 +11:00
382 changed files with 4417 additions and 33099 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -59,7 +59,23 @@ Any KCL value.
|----------|------|-------------|----------| |----------|------|-------------|----------|
| `type` |enum: `Number`| | No | | `type` |enum: `Number`| | No |
| `value` |`number`| | No | | `value` |`number`| | No |
| `ty` |[`NumericType`](/docs/kcl/types/NumericType)| Any KCL value. | No | | `__meta` |`[` [`Metadata`](/docs/kcl/types/Metadata) `]`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Int`| | No |
| `value` |`integer`| | No |
| `__meta` |`[` [`Metadata`](/docs/kcl/types/Metadata) `]`| | No | | `__meta` |`[` [`Metadata`](/docs/kcl/types/Metadata) `]`| | No |

View File

@ -1,250 +0,0 @@
---
title: "NumericType"
excerpt: ""
layout: manual
---
**This schema accepts exactly one of the following:**
**Type:** `object`
**This schema accepts exactly one of the following:**
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Count`| | No |
----
**Type:** `object`
**This schema accepts exactly one of the following:**
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Mm`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Cm`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `M`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Inches`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Feet`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Yards`| | No |
----
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Length`| | No |
----
**Type:** `object`
**This schema accepts exactly one of the following:**
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Degrees`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Radians`| | No |
----
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Angle`| | No |
----
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Known`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Default`| | No |
| `len` |[`UnitLen`](/docs/kcl/types/UnitLen)| | No |
| `angle` |[`UnitAngle`](/docs/kcl/types/UnitAngle)| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Unknown`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Any`| | No |
----

View File

@ -1,47 +0,0 @@
---
title: "UnitAngle"
excerpt: ""
layout: manual
---
**This schema accepts exactly one of the following:**
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Degrees`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Radians`| | No |
----

View File

@ -1,186 +0,0 @@
---
title: "UnitType"
excerpt: ""
layout: manual
---
**This schema accepts exactly one of the following:**
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Count`| | No |
----
**Type:** `object`
**This schema accepts exactly one of the following:**
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Mm`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Cm`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `M`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Inches`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Feet`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Yards`| | No |
----
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Length`| | No |
----
**Type:** `object`
**This schema accepts exactly one of the following:**
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Degrees`| | No |
----
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Radians`| | No |
----
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `type` |enum: `Angle`| | No |
----

File diff suppressed because one or more lines are too long

View File

@ -24,7 +24,7 @@ sketch001 = startSketchOn('XZ')
revolve001 = revolve({ axis = "X" }, sketch001) revolve001 = revolve({ axis = "X" }, sketch001)
triangle() triangle()
|> extrude(length = 30) |> extrude(length = 30)
plane001 = offsetPlane('XY', offset = 10) plane001 = offsetPlane('XY', 10)
sketch002 = startSketchOn(plane001) sketch002 = startSketchOn(plane001)
|> startProfileAt([-20, 0], %) |> startProfileAt([-20, 0], %)
|> line(end = [5, -15]) |> line(end = [5, -15])
@ -35,7 +35,7 @@ sketch002 = startSketchOn(plane001)
extrude001 = extrude(sketch002, length = 10) extrude001 = extrude(sketch002, length = 10)
` `
const FEATURE_TREE_SKETCH_CODE = `sketch001 = startSketchOn('XZ') const FEAUTRE_TREE_SKETCH_CODE = `sketch001 = startSketchOn('XZ')
|> startProfileAt([0, 0], %) |> startProfileAt([0, 0], %)
|> angledLine([0, 4], %, $rectangleSegmentA001) |> angledLine([0, 4], %, $rectangleSegmentA001)
|> angledLine([ |> angledLine([
@ -54,7 +54,7 @@ sketch002 = startSketchOn(extrude001, rectangleSegmentB001)
center = [-1, 2], center = [-1, 2],
radius = .5 radius = .5
}, %) }, %)
plane001 = offsetPlane('XZ', offset = -5) plane001 = offsetPlane('XZ', -5)
sketch003 = startSketchOn(plane001) sketch003 = startSketchOn(plane001)
|> circle({ center = [0, 0], radius = 5 }, %) |> circle({ center = [0, 0], radius = 5 }, %)
` `
@ -116,7 +116,7 @@ test.describe('Feature Tree pane', () => {
await testViewSource({ await testViewSource({
operationName: 'Offset Plane', operationName: 'Offset Plane',
operationIndex: 0, operationIndex: 0,
expectedActiveLine: "plane001 = offsetPlane('XY', offset = 10)", expectedActiveLine: "plane001 = offsetPlane('XY', 10)",
}) })
await testViewSource({ await testViewSource({
operationName: 'Extrude', operationName: 'Extrude',
@ -153,16 +153,33 @@ test.describe('Feature Tree pane', () => {
`User can edit sketch (but not on offset plane yet) from the feature tree`, `User can edit sketch (but not on offset plane yet) from the feature tree`,
{ tag: '@electron' }, { tag: '@electron' },
async ({ context, homePage, scene, editor, toolbar, page }) => { async ({ context, homePage, scene, editor, toolbar, page }) => {
await context.addInitScript((initialCode) => { const unavailableToastMessage = page.getByText(
localStorage.setItem('persistCode', initialCode) 'Editing sketches on faces or offset planes through the feature tree is not yet supported'
}, FEATURE_TREE_SKETCH_CODE) )
await page.setBodyDimensions({ width: 1000, height: 500 })
await homePage.goToModelingScene()
await test.step('force re-exe', async () => { await context.folderSetupFn(async (dir) => {
await page.waitForTimeout(1000) const bracketDir = join(dir, 'test-sample')
await editor.replaceCode('90', '91') await fsp.mkdir(bracketDir, { recursive: true })
await page.waitForTimeout(1500) await fsp.writeFile(
join(bracketDir, 'main.kcl'),
FEAUTRE_TREE_SKETCH_CODE,
'utf-8'
)
})
await test.step('setup test', async () => {
await homePage.expectState({
projectCards: [
{
title: 'test-sample',
fileCount: 1,
},
],
sortBy: 'last-modified-desc',
})
await homePage.openProject('test-sample')
await scene.waitForExecutionDone()
await toolbar.openFeatureTreePane()
}) })
await test.step('On a default plane should work', async () => { await test.step('On a default plane should work', async () => {
@ -182,23 +199,24 @@ test.describe('Feature Tree pane', () => {
await test.step('On an extrude face should *not* work', async () => { await test.step('On an extrude face should *not* work', async () => {
// Tooltip is getting in the way of clicking, so I'm first closing the pane // Tooltip is getting in the way of clicking, so I'm first closing the pane
await toolbar.closeFeatureTreePane() await toolbar.closeFeatureTreePane()
await page.waitForTimeout(1000)
await editor.replaceCode('91', '90')
await page.waitForTimeout(2000)
await (await toolbar.getFeatureTreeOperation('Sketch', 1)).dblclick() await (await toolbar.getFeatureTreeOperation('Sketch', 1)).dblclick()
await expect( await expect(
toolbar.exitSketchBtn, unavailableToastMessage,
'We should be in sketch mode now' 'We should see a toast message about this'
).toBeVisible() ).toBeVisible()
await editor.expectState({ await unavailableToastMessage.waitFor({ state: 'detached' })
highlightedCode: '', // TODO - turn on once we update the artifactGraph in Rust
diagnostics: [], // to include the proper source location for the extrude face
activeLines: [ // await expect(
'sketch002=startSketchOn(extrude001,rectangleSegmentB001)', // toolbar.exitSketchBtn,
], // 'We should be in sketch mode now'
}) // ).toBeVisible()
await toolbar.exitSketchBtn.click() // await editor.expectState({
// highlightedCode: '',
// diagnostics: [],
// activeLines: ['|>circle({center=[-1,2],radius=.5},%)'],
// })
// await toolbar.exitSketchBtn.click()
}) })
await test.step('On an offset plane should *not* work', async () => { await test.step('On an offset plane should *not* work', async () => {
@ -208,7 +226,7 @@ test.describe('Feature Tree pane', () => {
await editor.expectState({ await editor.expectState({
highlightedCode: '', highlightedCode: '',
diagnostics: [], diagnostics: [],
activeLines: ['sketch003=startSketchOn(plane001)'], activeLines: ['|>circle({center=[0,0],radius=5},%)'],
}) })
await expect( await expect(
toolbar.exitSketchBtn, toolbar.exitSketchBtn,
@ -324,8 +342,7 @@ test.describe('Feature Tree pane', () => {
toolbar, toolbar,
cmdBar, cmdBar,
}) => { }) => {
const testCode = (value: string) => const testCode = (value: string) => `p = offsetPlane('XY', ${value})`
`p = offsetPlane('XY', offset = ${value})`
const initialInput = '10' const initialInput = '10'
const initialCode = testCode(initialInput) const initialCode = testCode(initialInput)
const newInput = '5 + 10' const newInput = '5 + 10'

View File

@ -1028,7 +1028,7 @@ openSketch = startSketchOn('XY')
// One dumb hardcoded screen pixel value // One dumb hardcoded screen pixel value
const testPoint = { x: 700, y: 150 } const testPoint = { x: 700, y: 150 }
const [clickOnXzPlane] = scene.makeMouseHelpers(testPoint.x, testPoint.y) const [clickOnXzPlane] = scene.makeMouseHelpers(testPoint.x, testPoint.y)
const expectedOutput = `plane001 = offsetPlane('XZ', offset = 5)` const expectedOutput = `plane001 = offsetPlane('XZ', 5)`
await homePage.goToModelingScene() await homePage.goToModelingScene()
// FIXME: Since there is no KCL code loaded. We need to wait for the scene to load before we continue. // FIXME: Since there is no KCL code loaded. We need to wait for the scene to load before we continue.
@ -1164,7 +1164,7 @@ openSketch = startSketchOn('XY')
}) => { }) => {
const initialCode = `sketch001 = startSketchOn('XZ') const initialCode = `sketch001 = startSketchOn('XZ')
|> circle({ center = [0, 0], radius = 30 }, %) |> circle({ center = [0, 0], radius = 30 }, %)
plane001 = offsetPlane('XZ', offset = 50) plane001 = offsetPlane('XZ', 50)
sketch002 = startSketchOn(plane001) sketch002 = startSketchOn(plane001)
|> circle({ center = [0, 0], radius = 20 }, %) |> circle({ center = [0, 0], radius = 20 }, %)
` `
@ -1250,7 +1250,7 @@ openSketch = startSketchOn('XY')
}) => { }) => {
const initialCode = `sketch001 = startSketchOn('XZ') const initialCode = `sketch001 = startSketchOn('XZ')
|> circle({ center = [0, 0], radius = 30 }, %) |> circle({ center = [0, 0], radius = 30 }, %)
plane001 = offsetPlane('XZ', offset = 50) plane001 = offsetPlane('XZ', 50)
sketch002 = startSketchOn(plane001) sketch002 = startSketchOn(plane001)
|> circle({ center = [0, 0], radius = 20 }, %) |> circle({ center = [0, 0], radius = 20 }, %)
loft001 = loft([sketch001, sketch002]) loft001 = loft([sketch001, sketch002])
@ -1297,7 +1297,7 @@ loft001 = loft([sketch001, sketch002])
await page.waitForTimeout(1000) await page.waitForTimeout(1000)
await clickOnSketch2() await clickOnSketch2()
await expect(page.locator('.cm-activeLine')).toHaveText(` await expect(page.locator('.cm-activeLine')).toHaveText(`
plane001 = offsetPlane('XZ', offset = 50) plane001 = offsetPlane('XZ', 50)
`) `)
await page.keyboard.press('Backspace') await page.keyboard.press('Backspace')
// Check for sketch 1 // Check for sketch 1
@ -2448,18 +2448,19 @@ extrude002 = extrude(sketch002, length = 50)
await context.addInitScript((initialCode) => { await context.addInitScript((initialCode) => {
localStorage.setItem('persistCode', initialCode) localStorage.setItem('persistCode', initialCode)
}, initialCode) }, initialCode)
await page.setBodyDimensions({ width: 1200, height: 500 }) await page.setBodyDimensions({ width: 1000, height: 500 })
await homePage.goToModelingScene() await homePage.goToModelingScene()
await scene.waitForExecutionDone() await scene.waitForExecutionDone()
// One dumb hardcoded screen pixel value // One dumb hardcoded screen pixel value
const testPoint = { x: 580, y: 320 } const testPoint = { x: 550, y: 295 }
const [clickOnCap] = scene.makeMouseHelpers(testPoint.x, testPoint.y) const [clickOnCap] = scene.makeMouseHelpers(testPoint.x, testPoint.y)
const shellTarget = hasExtrudesInPipe ? 'sketch002' : 'extrude002' const shellTarget = hasExtrudesInPipe ? 'sketch002' : 'extrude002'
const shellDeclaration = `shell001 = shell(${shellTarget}, faces = ['end'], thickness = 5)` const shellDeclaration = `shell001 = shell(${shellTarget}, faces = ['end'], thickness = 5)`
await test.step(`Look for the grey of the shape`, async () => { await test.step(`Look for the grey of the shape`, async () => {
await scene.expectPixelColor([113, 113, 113], testPoint, 15) await toolbar.closePane('code')
await scene.expectPixelColor([128, 128, 128], testPoint, 15)
}) })
await test.step(`Go through the command bar flow, selecting a cap and keeping default thickness`, async () => { await test.step(`Go through the command bar flow, selecting a cap and keeping default thickness`, async () => {

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 127 KiB

After

Width:  |  Height:  |  Size: 127 KiB

View File

@ -390,47 +390,43 @@ profile003 = startProfileAt([40.16, -120.48], sketch006)
`rev = revolve({ axis: 'y' }, part009)` `rev = revolve({ axis: 'y' }, part009)`
) )
// FIXME (commented section below), this test would select a wall that had a sketch on it, and delete the underlying extrude // DELETE PARENT EXTRUDE
// and replace the sketch on face with a hard coded custom plane, but since there was a sketch on that plane maybe it await page.mouse.click(parentExtrude.x, parentExtrude.y)
// should have delete the sketch? it's broken atm, but not sure if worth fixing since desired behaviour is a little await page.waitForTimeout(100)
// vague await expect(page.locator('.cm-activeLine')).toHaveText(
// // DELETE PARENT EXTRUDE '|> line(end = [170.36, -121.61], tag = $seg01)'
// await page.mouse.click(parentExtrude.x, parentExtrude.y) )
// await page.waitForTimeout(100) await u.clearCommandLogs()
// await expect(page.locator('.cm-activeLine')).toHaveText( await page.keyboard.press('Backspace')
// '|> line(end = [170.36, -121.61], tag = $seg01)' await u.expectCmdLog('[data-message-type="execution-done"]', 10_000)
// ) await page.waitForTimeout(200)
// await u.clearCommandLogs() await expect(u.codeLocator).not.toContainText(
// await page.keyboard.press('Backspace') `extrude001 = extrude(sketch001, length = 50)`
// await u.expectCmdLog('[data-message-type="execution-done"]', 10_000) )
// await page.waitForTimeout(200) await expect(u.codeLocator).toContainText(`sketch005 = startSketchOn({
// await expect(u.codeLocator).not.toContainText( plane = {
// `extrude001 = extrude(sketch001, length = 50)` origin = { x = 0, y = -50, z = 0 },
// ) xAxis = { x = 1, y = 0, z = 0 },
// await expect(u.codeLocator).toContainText(`sketch005 = startSketchOn({ yAxis = { x = 0, y = 0, z = 1 },
// plane = { zAxis = { x = 0, y = -1, z = 0 }
// origin = { x = 0, y = -50, z = 0 }, }
// xAxis = { x = 1, y = 0, z = 0 }, })`)
// yAxis = { x = 0, y = 0, z = 1 }, await expect(u.codeLocator).toContainText(`sketch003 = startSketchOn({
// zAxis = { x = 0, y = -1, z = 0 } plane = {
// } origin = { x = 116.53, y = 0, z = 163.25 },
// })`) xAxis = { x = -0.81, y = 0, z = 0.58 },
// await expect(u.codeLocator).toContainText(`sketch003 = startSketchOn({ yAxis = { x = 0, y = -1, z = 0 },
// plane = { zAxis = { x = 0.58, y = 0, z = 0.81 }
// origin = { x = 116.53, y = 0, z = 163.25 }, }
// xAxis = { x = -0.81, y = 0, z = 0.58 }, })`)
// yAxis = { x = 0, y = -1, z = 0 }, await expect(u.codeLocator).toContainText(`sketch002 = startSketchOn({
// zAxis = { x = 0.58, y = 0, z = 0.81 } plane = {
// } origin = { x = -91.74, y = 0, z = 80.89 },
// })`) xAxis = { x = -0.66, y = 0, z = -0.75 },
// await expect(u.codeLocator).toContainText(`sketch002 = startSketchOn({ yAxis = { x = 0, y = -1, z = 0 },
// plane = { zAxis = { x = -0.75, y = 0, z = 0.66 }
// origin = { x = -91.74, y = 0, z = 80.89 }, }
// xAxis = { x = -0.66, y = 0, z = -0.75 }, })`)
// yAxis = { x = 0, y = -1, z = 0 },
// zAxis = { x = -0.75, y = 0, z = 0.66 }
// }
// })`)
// DELETE SOLID 2D // DELETE SOLID 2D
await page.mouse.click(solid2d.x, solid2d.y) await page.mouse.click(solid2d.x, solid2d.y)
@ -458,14 +454,15 @@ profile003 = startProfileAt([40.16, -120.48], sketch006)
await page.waitForTimeout(200) await page.waitForTimeout(200)
await expect(u.codeLocator).not.toContainText(codeToBeDeletedSnippet) await expect(u.codeLocator).not.toContainText(codeToBeDeletedSnippet)
}) })
test.fixme( test("Deleting solid that the AST mod can't handle results in a toast message", async ({
"Deleting solid that the AST mod can't handle results in a toast message", page,
async ({ page, homePage }) => { homePage,
const u = await getUtils(page) }) => {
await page.addInitScript(async () => { const u = await getUtils(page)
localStorage.setItem( await page.addInitScript(async () => {
'persistCode', localStorage.setItem(
`sketch001 = startSketchOn('XZ') 'persistCode',
`sketch001 = startSketchOn('XZ')
|> startProfileAt([-79.26, 95.04], %) |> startProfileAt([-79.26, 95.04], %)
|> line(end = [112.54, 127.64], tag = $seg02) |> line(end = [112.54, 127.64], tag = $seg02)
|> line(end = [170.36, -121.61], tag = $seg01) |> line(end = [170.36, -121.61], tag = $seg01)
@ -480,49 +477,48 @@ profile003 = startProfileAt([40.16, -120.48], sketch006)
|> line(endAbsolute = [profileStartX(%), profileStartY(%)]) |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
|> close() |> close()
` `
)
}, KCL_DEFAULT_LENGTH)
await page.setBodyDimensions({ width: 1000, height: 500 })
await homePage.goToModelingScene()
await u.openDebugPanel()
await u.expectCmdLog('[data-message-type="execution-done"]', 10_000)
await u.closeDebugPanel()
await u.openAndClearDebugPanel()
await u.sendCustomCmd({
type: 'modeling_cmd_req',
cmd_id: uuidv4(),
cmd: {
type: 'default_camera_look_at',
vantage: { x: 1139.49, y: -7053, z: 8597.31 },
center: { x: -2206.68, y: -1298.36, z: 60 },
up: { x: 0, y: 0, z: 1 },
},
})
await page.waitForTimeout(100)
await u.sendCustomCmd({
type: 'modeling_cmd_req',
cmd_id: uuidv4(),
cmd: {
type: 'default_camera_get_settings',
},
})
await page.waitForTimeout(100)
// attempt delete
await page.mouse.click(930, 139)
await page.waitForTimeout(100)
await expect(page.locator('.cm-activeLine')).toHaveText(
'|> line(end = [170.36, -121.61], tag = $seg01)'
) )
await u.clearCommandLogs() }, KCL_DEFAULT_LENGTH)
await page.keyboard.press('Backspace') await page.setBodyDimensions({ width: 1000, height: 500 })
await expect(page.getByText('Unable to delete selection')).toBeVisible() await homePage.goToModelingScene()
}
) await u.openDebugPanel()
await u.expectCmdLog('[data-message-type="execution-done"]', 10_000)
await u.closeDebugPanel()
await u.openAndClearDebugPanel()
await u.sendCustomCmd({
type: 'modeling_cmd_req',
cmd_id: uuidv4(),
cmd: {
type: 'default_camera_look_at',
vantage: { x: 1139.49, y: -7053, z: 8597.31 },
center: { x: -2206.68, y: -1298.36, z: 60 },
up: { x: 0, y: 0, z: 1 },
},
})
await page.waitForTimeout(100)
await u.sendCustomCmd({
type: 'modeling_cmd_req',
cmd_id: uuidv4(),
cmd: {
type: 'default_camera_get_settings',
},
})
await page.waitForTimeout(100)
// attempt delete
await page.mouse.click(930, 139)
await page.waitForTimeout(100)
await expect(page.locator('.cm-activeLine')).toHaveText(
'|> line(end = [170.36, -121.61], tag = $seg01)'
)
await u.clearCommandLogs()
await page.keyboard.press('Backspace')
await expect(page.getByText('Unable to delete selection')).toBeVisible()
})
test('Hovering over 3d features highlights code, clicking puts the cursor in the right place and sends selection id to engine', async ({ test('Hovering over 3d features highlights code, clicking puts the cursor in the right place and sends selection id to engine', async ({
page, page,
homePage, homePage,

View File

@ -16,7 +16,8 @@ mac:
arch: arch:
- x64 - x64
- arm64 - arm64
notarize: true notarize:
teamId: 92H8YB3B95
fileAssociations: fileAssociations:
- ext: kcl - ext: kcl
name: kcl name: kcl
@ -31,11 +32,10 @@ win:
arch: arch:
- x64 - x64
- arm64 - arm64
signtoolOptions: signingHashAlgorithms:
sign: "./scripts/sign-win.js" - sha256
signingHashAlgorithms: sign: "./scripts/sign-win.js"
- sha256 publisherName: "KittyCAD Inc" # needs to be exactly like on Digicert
publisherName: "KittyCAD Inc" # needs to be exactly like on Digicert
icon: "assets/icon.ico" icon: "assets/icon.ico"
fileAssociations: fileAssociations:
- ext: kcl - ext: kcl

View File

@ -40,7 +40,7 @@
"codemirror": "^6.0.1", "codemirror": "^6.0.1",
"decamelize": "^6.0.0", "decamelize": "^6.0.0",
"diff": "^7.0.0", "diff": "^7.0.0",
"electron-updater": "^6.5.0", "electron-updater": "6.3.0",
"fuse.js": "^7.0.0", "fuse.js": "^7.0.0",
"html2canvas-pro": "^1.5.8", "html2canvas-pro": "^1.5.8",
"isomorphic-fetch": "^3.0.0", "isomorphic-fetch": "^3.0.0",
@ -85,8 +85,8 @@
"fmt": "prettier --write ./src *.ts *.json *.js ./e2e ./packages", "fmt": "prettier --write ./src *.ts *.json *.js ./e2e ./packages",
"fmt-check": "prettier --check ./src *.ts *.json *.js ./e2e ./packages", "fmt-check": "prettier --check ./src *.ts *.json *.js ./e2e ./packages",
"fetch:wasm": "./get-latest-wasm-bundle.sh", "fetch:wasm": "./get-latest-wasm-bundle.sh",
"fetch:samples": "echo \"Fetching latest KCL samples...\" && curl -o public/kcl-samples-manifest-fallback.json https://raw.githubusercontent.com/KittyCAD/kcl-samples/next/manifest.json", "fetch:samples": "echo \"Fetching latest KCL samples...\" && curl -o public/kcl-samples-manifest-fallback.json https://raw.githubusercontent.com/KittyCAD/kcl-samples/achalmers/kw-pattern-transform2/manifest.json",
"isomorphic-copy-wasm": "(copy src\\wasm-lib\\pkg\\wasm_lib_bg.wasm public || cp src/wasm-lib/pkg/wasm_lib_bg.wasm public)", "isomorphic-copy-wasm": "(copy src/wasm-lib/pkg/wasm_lib_bg.wasm public || cp src/wasm-lib/pkg/wasm_lib_bg.wasm public)",
"build:wasm-dev": "yarn wasm-prep && (cd src/wasm-lib && wasm-pack build --dev --target web --out-dir pkg && cargo test -p kcl-lib export_bindings) && yarn isomorphic-copy-wasm && yarn fmt", "build:wasm-dev": "yarn wasm-prep && (cd src/wasm-lib && wasm-pack build --dev --target web --out-dir pkg && cargo test -p kcl-lib export_bindings) && yarn isomorphic-copy-wasm && yarn fmt",
"build:wasm": "yarn wasm-prep && cd src/wasm-lib && wasm-pack build --release --target web --out-dir pkg && cargo test -p kcl-lib export_bindings && cd ../.. && yarn isomorphic-copy-wasm && yarn fmt", "build:wasm": "yarn wasm-prep && cd src/wasm-lib && wasm-pack build --release --target web --out-dir pkg && cargo test -p kcl-lib export_bindings && cd ../.. && yarn isomorphic-copy-wasm && yarn fmt",
"remove-importmeta": "sed -i 's/import.meta.url/window.location.origin/g' \"./src/wasm-lib/pkg/wasm_lib.js\"; sed -i '' 's/import.meta.url/window.location.origin/g' \"./src/wasm-lib/pkg/wasm_lib.js\" || echo \"sed for both mac and linux\"", "remove-importmeta": "sed -i 's/import.meta.url/window.location.origin/g' \"./src/wasm-lib/pkg/wasm_lib.js\"; sed -i '' 's/import.meta.url/window.location.origin/g' \"./src/wasm-lib/pkg/wasm_lib.js\" || echo \"sed for both mac and linux\"",
@ -145,11 +145,10 @@
"devDependencies": { "devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11", "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/preset-env": "^7.25.4", "@babel/preset-env": "^7.25.4",
"@electron-forge/cli": "^7.6.1", "@electron-forge/cli": "7.4.0",
"@electron-forge/plugin-fuses": "^7.6.1", "@electron-forge/plugin-fuses": "7.4.0",
"@electron-forge/plugin-vite": "^7.6.1", "@electron-forge/plugin-vite": "7.4.0",
"@electron/fuses": "^1.8.0", "@electron/fuses": "1.8.0",
"@electron/notarize": "^2.5.0",
"@iarna/toml": "^2.2.5", "@iarna/toml": "^2.2.5",
"@lezer/generator": "^1.7.2", "@lezer/generator": "^1.7.2",
"@nabla/vite-plugin-eslint": "^2.0.5", "@nabla/vite-plugin-eslint": "^2.0.5",
@ -176,8 +175,9 @@
"@vitest/web-worker": "^1.5.0", "@vitest/web-worker": "^1.5.0",
"@xstate/cli": "^0.5.17", "@xstate/cli": "^0.5.17",
"autoprefixer": "^10.4.19", "autoprefixer": "^10.4.19",
"electron": "^34.1.1", "electron": "32.1.2",
"electron-builder": "^26.0.6", "electron-builder": "24.13.3",
"electron-notarize": "1.2.2",
"eslint": "^8.0.1", "eslint": "^8.0.1",
"eslint-plugin-css-modules": "^2.12.0", "eslint-plugin-css-modules": "^2.12.0",
"eslint-plugin-import": "^2.30.0", "eslint-plugin-import": "^2.30.0",

View File

@ -1,5 +1,4 @@
@precedence { @precedence {
annotation
member member
call call
exp @left exp @left
@ -21,12 +20,9 @@ statement[@isGroup=Statement] {
FunctionDeclaration { kw<"export">? kw<"fn"> VariableDefinition Equals? ParamList Arrow? Body } | FunctionDeclaration { kw<"export">? kw<"fn"> VariableDefinition Equals? ParamList Arrow? Body } |
VariableDeclaration { kw<"export">? (kw<"var"> | kw<"let"> | kw<"const">)? VariableDefinition Equals expression } | VariableDeclaration { kw<"export">? (kw<"var"> | kw<"let"> | kw<"const">)? VariableDefinition Equals expression } |
ReturnStatement { kw<"return"> expression } | ReturnStatement { kw<"return"> expression } |
ExpressionStatement { expression } | ExpressionStatement { expression }
Annotation { AnnotationName AnnotationList? }
} }
AnnotationList { !annotation "(" commaSep<AnnotationProperty> ")" }
ParamList { "(" commaSep<Parameter { VariableDefinition "?"? (":" type)? }> ")" } ParamList { "(" commaSep<Parameter { VariableDefinition "?"? (":" type)? }> ")" }
Body { "{" statement* "}" } Body { "{" statement* "}" }
@ -63,12 +59,6 @@ UnaryOp { AddOp | BangOp }
ObjectProperty { PropertyName (":" | Equals) expression } ObjectProperty { PropertyName (":" | Equals) expression }
AnnotationProperty {
PropertyName
( AddOp | MultOp | ExpOp | LogicOp | BangOp | CompOp | Equals | Arrow | PipeOperator | PipeSubstitution )
expression
}
LabeledArgument { ArgumentLabel Equals expression } LabeledArgument { ArgumentLabel Equals expression }
ArgumentList { "(" commaSep<LabeledArgument | expression> ")" } ArgumentList { "(" commaSep<LabeledArgument | expression> ")" }
@ -115,7 +105,6 @@ commaSep1NoTrailingComma<term> { term ("," term)* }
PipeSubstitution { "%" } PipeSubstitution { "%" }
identifier { (@asciiLetter | "_") (@asciiLetter | @digit | "_")* } identifier { (@asciiLetter | "_") (@asciiLetter | @digit | "_")* }
AnnotationName { "@" identifier? }
PropertyName { identifier } PropertyName { identifier }
TagDeclarator { "$" identifier } TagDeclarator { "$" identifier }

View File

@ -1,153 +0,0 @@
# alone
@a
==>
Program(Annotation(AnnotationName))
# alone and anonymous
@
==>
Program(Annotation(AnnotationName))
# empty
@ann()
==>
Program(Annotation(AnnotationName,
AnnotationList))
# empty and anonymous
@()
==>
Program(Annotation(AnnotationName,
AnnotationList))
# equals
@setting(a=1)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))
# operator
@ann(a*1)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
MultOp,
Number))))
# anonymous
@(a=1)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))
# complex expr
@ann(a=(1+2+f('yes')))
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
ParenthesizedExpression(BinaryExpression(BinaryExpression(Number,
AddOp,
Number),
AddOp,
CallExpression(VariableName,
ArgumentList(String))))))))
# many args
@ann(a=1, b=2)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number),
AnnotationProperty(PropertyName,
Equals,
Number))))
# space around op
@ann(a / 1)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
MultOp,
Number))))
# space around sep
@ann(a/1 , b/2)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
MultOp,
Number),
AnnotationProperty(PropertyName,
MultOp,
Number))))
# trailing sep
@ann(a=1,)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))
# lone sep
@ann(,)
==>
Program(Annotation(AnnotationName,
AnnotationList))
# inside fn
fn f() {
@anno(b=2)
}
==>
Program(FunctionDeclaration(fn,
VariableDefinition,
ParamList,
Body(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))))
# laxer with space than the language parser is
@anno (b=2)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))

View File

@ -69,7 +69,6 @@ import {
startSketchOnDefault, startSketchOnDefault,
} from 'lang/modifyAst' } from 'lang/modifyAst'
import { import {
KclValue,
PathToNode, PathToNode,
Program, Program,
VariableDeclaration, VariableDeclaration,
@ -805,33 +804,12 @@ export const ModelingMachineProvider = ({
engineCommandManager.artifactGraph engineCommandManager.artifactGraph
) )
if (err(plane)) return Promise.reject(plane) if (err(plane)) return Promise.reject(plane)
let sketch: KclValue | null = null
for (const variable of Object.values( const sketch = Object.values(kclManager.execState.variables).find(
kclManager.execState.variables (variable) =>
)) {
// find programMemory that matches path artifact
if (
variable?.type === 'Sketch' && variable?.type === 'Sketch' &&
variable.value.artifactId === plane.pathIds[0] variable.value.artifactId === plane.pathIds[0]
) { )
sketch = variable
break
}
if (
// if the variable is an sweep, check if the underlying sketch matches the artifact
variable?.type === 'Solid' &&
variable.value.sketch.on.type === 'plane' &&
variable.value.sketch.artifactId === plane.pathIds[0]
) {
sketch = {
type: 'Sketch',
value: variable.value.sketch,
}
break
}
}
if (!sketch || sketch.type !== 'Sketch')
return Promise.reject(new Error('No sketch'))
if (!sketch || sketch.type !== 'Sketch') if (!sketch || sketch.type !== 'Sketch')
return Promise.reject(new Error('No sketch')) return Promise.reject(new Error('No sketch'))
const info = await getSketchOrientationDetails(sketch.value) const info = await getSketchOrientationDetails(sketch.value)
@ -1573,11 +1551,8 @@ export const ModelingMachineProvider = ({
? pathToProfile ? pathToProfile
: updatedSketchNodePaths[0] : updatedSketchNodePaths[0]
} }
await kclManager.executeAstMock(moddedAst)
if (doesNeedSplitting || indexToDelete >= 0) { await codeManager.updateEditorWithAstAndWriteToFile(moddedAst)
await kclManager.executeAstMock(moddedAst)
await codeManager.updateEditorWithAstAndWriteToFile(moddedAst)
}
return { return {
updatedEntryNodePath: pathToProfile, updatedEntryNodePath: pathToProfile,
updatedSketchNodePaths: updatedSketchNodePaths, updatedSketchNodePaths: updatedSketchNodePaths,

View File

@ -8,7 +8,7 @@ import { editorManager } from 'lib/singletons'
import { Annotation, Transaction } from '@codemirror/state' import { Annotation, Transaction } from '@codemirror/state'
import { EditorView, KeyBinding } from '@codemirror/view' import { EditorView, KeyBinding } from '@codemirror/view'
import { recast, Program } from 'lang/wasm' import { recast, Program } from 'lang/wasm'
import { err, reportRejection } from 'lib/trap' import { err } from 'lib/trap'
import { Compartment } from '@codemirror/state' import { Compartment } from '@codemirror/state'
import { history } from '@codemirror/commands' import { history } from '@codemirror/commands'
@ -168,7 +168,7 @@ export default class CodeManager {
const newCode = recast(ast) const newCode = recast(ast)
if (err(newCode)) return if (err(newCode)) return
this.updateCodeStateEditor(newCode) this.updateCodeStateEditor(newCode)
this.writeToFile().catch(reportRejection) await this.writeToFile()
} }
} }

View File

@ -239,7 +239,6 @@ const newVar = myVar + 1`
expect(mem['three']).toEqual({ expect(mem['three']).toEqual({
type: 'Number', type: 'Number',
value: 3, value: 3,
ty: expect.any(Object),
__meta: [ __meta: [
{ {
sourceRange: [14, 15, 0], sourceRange: [14, 15, 0],
@ -249,23 +248,12 @@ const newVar = myVar + 1`
expect(mem['yo']).toEqual({ expect(mem['yo']).toEqual({
type: 'Array', type: 'Array',
value: [ value: [
{ { type: 'Number', value: 1, __meta: [{ sourceRange: [28, 29, 0] }] },
type: 'Number',
value: 1,
ty: expect.any(Object),
__meta: [{ sourceRange: [28, 29, 0] }],
},
{ type: 'String', value: '2', __meta: [{ sourceRange: [31, 34, 0] }] }, { type: 'String', value: '2', __meta: [{ sourceRange: [31, 34, 0] }] },
{ { type: 'Number', value: 3, __meta: [{ sourceRange: [14, 15, 0] }] },
type: 'Number',
value: 3,
ty: expect.any(Object),
__meta: [{ sourceRange: [14, 15, 0] }],
},
{ {
type: 'Number', type: 'Number',
value: 9, value: 9,
ty: expect.any(Object),
__meta: [{ sourceRange: [43, 44, 0] }, { sourceRange: [47, 48, 0] }], __meta: [{ sourceRange: [43, 44, 0] }, { sourceRange: [47, 48, 0] }],
}, },
], ],
@ -293,19 +281,16 @@ const newVar = myVar + 1`
anum: { anum: {
type: 'Number', type: 'Number',
value: 2, value: 2,
ty: expect.any(Object),
__meta: [{ sourceRange: [47, 48, 0] }], __meta: [{ sourceRange: [47, 48, 0] }],
}, },
identifier: { identifier: {
type: 'Number', type: 'Number',
value: 3, value: 3,
ty: expect.any(Object),
__meta: [{ sourceRange: [14, 15, 0] }], __meta: [{ sourceRange: [14, 15, 0] }],
}, },
binExp: { binExp: {
type: 'Number', type: 'Number',
value: 9, value: 9,
ty: expect.any(Object),
__meta: [{ sourceRange: [77, 78, 0] }, { sourceRange: [81, 82, 0] }], __meta: [{ sourceRange: [77, 78, 0] }, { sourceRange: [81, 82, 0] }],
}, },
}, },
@ -419,7 +404,6 @@ describe('testing math operators', () => {
], ],
type: 'Number', type: 'Number',
value: 1, value: 1,
ty: expect.any(Object),
}, },
{ {
__meta: [ __meta: [
@ -429,7 +413,6 @@ describe('testing math operators', () => {
], ],
type: 'Number', type: 'Number',
value: -3, value: -3,
ty: expect.any(Object),
}, },
]) ])
}) })

View File

@ -32,7 +32,7 @@ child_process.spawnSync('git', [
'clone', 'clone',
'--single-branch', '--single-branch',
'--branch', '--branch',
'next', 'achalmers/kw-pattern-transform2',
URL_GIT_KCL_SAMPLES, URL_GIT_KCL_SAMPLES,
DIR_KCL_SAMPLES, DIR_KCL_SAMPLES,
]) ])

View File

@ -821,146 +821,144 @@ sketch003 = startSketchOn('XZ')
type: 'segment', type: 'segment',
}, },
], ],
// TODO FIXME, similar to fix me in e2e/playwright/testing-selections.spec.ts [
// also related to deleting, deleting in general probably is due for a refactor 'delete extrude',
// [ {
// 'delete extrude', codeBefore: `sketch001 = startSketchOn('XZ')
// { |> startProfileAt([3.29, 7.86], %)
// codeBefore: `sketch001 = startSketchOn('XZ') |> line(end = [2.48, 2.44])
// |> startProfileAt([3.29, 7.86], %) |> line(end = [2.66, 1.17])
// |> line(end = [2.48, 2.44]) |> line(end = [3.75, 0.46])
// |> line(end = [2.66, 1.17]) |> line(end = [4.99, -0.46], tag = $seg01)
// |> line(end = [3.75, 0.46]) |> line(end = [-3.86, -2.73])
// |> line(end = [4.99, -0.46], tag = $seg01) |> line(end = [-17.67, 0.85])
// |> line(end = [-3.86, -2.73]) |> close()
// |> line(end = [-17.67, 0.85]) const extrude001 = extrude(sketch001, length = 10)`,
// |> close() codeAfter: `sketch001 = startSketchOn('XZ')
// const extrude001 = extrude(sketch001, length = 10)`, |> startProfileAt([3.29, 7.86], %)
// codeAfter: `sketch001 = startSketchOn('XZ') |> line(end = [2.48, 2.44])
// |> startProfileAt([3.29, 7.86], %) |> line(end = [2.66, 1.17])
// |> line(end = [2.48, 2.44]) |> line(end = [3.75, 0.46])
// |> line(end = [2.66, 1.17]) |> line(end = [4.99, -0.46], tag = $seg01)
// |> line(end = [3.75, 0.46]) |> line(end = [-3.86, -2.73])
// |> line(end = [4.99, -0.46], tag = $seg01) |> line(end = [-17.67, 0.85])
// |> line(end = [-3.86, -2.73]) |> close()\n`,
// |> line(end = [-17.67, 0.85]) lineOfInterest: 'line(end = [2.66, 1.17])',
// |> close()\n`, type: 'wall',
// lineOfInterest: 'line(end = [2.66, 1.17])', },
// type: 'wall', ],
// }, [
// ], 'delete extrude with sketch on it',
// [ {
// 'delete extrude with sketch on it', codeBefore: `myVar = 5
// { sketch001 = startSketchOn('XZ')
// codeBefore: `myVar = 5 |> startProfileAt([4.46, 5.12], %, $tag)
// sketch001 = startSketchOn('XZ') |> line(end = [0.08, myVar])
// |> startProfileAt([4.46, 5.12], %, $tag) |> line(end = [13.03, 2.02], tag = $seg01)
// |> line(end = [0.08, myVar]) |> line(end = [3.9, -7.6])
// |> line(end = [13.03, 2.02], tag = $seg01) |> line(end = [-11.18, -2.15])
// |> line(end = [3.9, -7.6]) |> line(end = [5.41, -9.61])
// |> line(end = [-11.18, -2.15]) |> line(end = [-8.54, -2.51])
// |> line(end = [5.41, -9.61]) |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
// |> line(end = [-8.54, -2.51]) |> close()
// |> line(endAbsolute = [profileStartX(%), profileStartY(%)]) const extrude001 = extrude(sketch001, length = 5)
// |> close() sketch002 = startSketchOn(extrude001, seg01)
// const extrude001 = extrude(sketch001, length = 5) |> startProfileAt([-12.55, 2.89], %)
// sketch002 = startSketchOn(extrude001, seg01) |> line(end = [3.02, 1.9])
// |> startProfileAt([-12.55, 2.89], %) |> line(end = [1.82, -1.49], tag = $seg02)
// |> line(end = [3.02, 1.9]) |> angledLine([-86, segLen(seg02)], %)
// |> line(end = [1.82, -1.49], tag = $seg02) |> line(end = [-3.97, -0.53])
// |> angledLine([-86, segLen(seg02)], %) |> line(end = [0.3, 0.84])
// |> line(end = [-3.97, -0.53]) |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
// |> line(end = [0.3, 0.84]) |> close()`,
// |> line(endAbsolute = [profileStartX(%), profileStartY(%)]) codeAfter: `myVar = 5
// |> close()`, sketch001 = startSketchOn('XZ')
// codeAfter: `myVar = 5 |> startProfileAt([4.46, 5.12], %, $tag)
// sketch001 = startSketchOn('XZ') |> line(end = [0.08, myVar])
// |> startProfileAt([4.46, 5.12], %, $tag) |> line(end = [13.03, 2.02], tag = $seg01)
// |> line(end = [0.08, myVar]) |> line(end = [3.9, -7.6])
// |> line(end = [13.03, 2.02], tag = $seg01) |> line(end = [-11.18, -2.15])
// |> line(end = [3.9, -7.6]) |> line(end = [5.41, -9.61])
// |> line(end = [-11.18, -2.15]) |> line(end = [-8.54, -2.51])
// |> line(end = [5.41, -9.61]) |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
// |> line(end = [-8.54, -2.51]) |> close()
// |> line(endAbsolute = [profileStartX(%), profileStartY(%)]) sketch002 = startSketchOn({
// |> close() plane = {
// sketch002 = startSketchOn({ origin = { x = 1, y = 2, z = 3 },
// plane = { xAxis = { x = 4, y = 5, z = 6 },
// origin = { x = 1, y = 2, z = 3 }, yAxis = { x = 7, y = 8, z = 9 },
// xAxis = { x = 4, y = 5, z = 6 }, zAxis = { x = 10, y = 11, z = 12 }
// yAxis = { x = 7, y = 8, z = 9 }, }
// zAxis = { x = 10, y = 11, z = 12 } })
// } |> startProfileAt([-12.55, 2.89], %)
// }) |> line(end = [3.02, 1.9])
// |> startProfileAt([-12.55, 2.89], %) |> line(end = [1.82, -1.49], tag = $seg02)
// |> line(end = [3.02, 1.9]) |> angledLine([-86, segLen(seg02)], %)
// |> line(end = [1.82, -1.49], tag = $seg02) |> line(end = [-3.97, -0.53])
// |> angledLine([-86, segLen(seg02)], %) |> line(end = [0.3, 0.84])
// |> line(end = [-3.97, -0.53]) |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
// |> line(end = [0.3, 0.84]) |> close()
// |> line(endAbsolute = [profileStartX(%), profileStartY(%)]) `,
// |> close() lineOfInterest: 'line(end = [-11.18, -2.15])',
// `, type: 'wall',
// lineOfInterest: 'line(end = [-11.18, -2.15])', },
// type: 'wall', ],
// }, [
// ], 'delete extrude with sketch on it 2',
// [ {
// 'delete extrude with sketch on it 2', codeBefore: `myVar = 5
// { sketch001 = startSketchOn('XZ')
// codeBefore: `myVar = 5 |> startProfileAt([4.46, 5.12], %, $tag)
// sketch001 = startSketchOn('XZ') |> line(end = [0.08, myVar])
// |> startProfileAt([4.46, 5.12], %, $tag) |> line(end = [13.03, 2.02], tag = $seg01)
// |> line(end = [0.08, myVar]) |> line(end = [3.9, -7.6])
// |> line(end = [13.03, 2.02], tag = $seg01) |> line(end = [-11.18, -2.15])
// |> line(end = [3.9, -7.6]) |> line(end = [5.41, -9.61])
// |> line(end = [-11.18, -2.15]) |> line(end = [-8.54, -2.51])
// |> line(end = [5.41, -9.61]) |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
// |> line(end = [-8.54, -2.51]) |> close()
// |> line(endAbsolute = [profileStartX(%), profileStartY(%)]) const extrude001 = extrude(sketch001, length = 5)
// |> close() sketch002 = startSketchOn(extrude001, seg01)
// const extrude001 = extrude(sketch001, length = 5) |> startProfileAt([-12.55, 2.89], %)
// sketch002 = startSketchOn(extrude001, seg01) |> line(end = [3.02, 1.9])
// |> startProfileAt([-12.55, 2.89], %) |> line(end = [1.82, -1.49], tag = $seg02)
// |> line(end = [3.02, 1.9]) |> angledLine([-86, segLen(seg02)], %)
// |> line(end = [1.82, -1.49], tag = $seg02) |> line(end = [-3.97, -0.53])
// |> angledLine([-86, segLen(seg02)], %) |> line(end = [0.3, 0.84])
// |> line(end = [-3.97, -0.53]) |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
// |> line(end = [0.3, 0.84]) |> close()`,
// |> line(endAbsolute = [profileStartX(%), profileStartY(%)]) codeAfter: `myVar = 5
// |> close()`, sketch001 = startSketchOn('XZ')
// codeAfter: `myVar = 5 |> startProfileAt([4.46, 5.12], %, $tag)
// sketch001 = startSketchOn('XZ') |> line(end = [0.08, myVar])
// |> startProfileAt([4.46, 5.12], %, $tag) |> line(end = [13.03, 2.02], tag = $seg01)
// |> line(end = [0.08, myVar]) |> line(end = [3.9, -7.6])
// |> line(end = [13.03, 2.02], tag = $seg01) |> line(end = [-11.18, -2.15])
// |> line(end = [3.9, -7.6]) |> line(end = [5.41, -9.61])
// |> line(end = [-11.18, -2.15]) |> line(end = [-8.54, -2.51])
// |> line(end = [5.41, -9.61]) |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
// |> line(end = [-8.54, -2.51]) |> close()
// |> line(endAbsolute = [profileStartX(%), profileStartY(%)]) sketch002 = startSketchOn({
// |> close() plane = {
// sketch002 = startSketchOn({ origin = { x = 1, y = 2, z = 3 },
// plane = { xAxis = { x = 4, y = 5, z = 6 },
// origin = { x = 1, y = 2, z = 3 }, yAxis = { x = 7, y = 8, z = 9 },
// xAxis = { x = 4, y = 5, z = 6 }, zAxis = { x = 10, y = 11, z = 12 }
// yAxis = { x = 7, y = 8, z = 9 }, }
// zAxis = { x = 10, y = 11, z = 12 } })
// } |> startProfileAt([-12.55, 2.89], %)
// }) |> line(end = [3.02, 1.9])
// |> startProfileAt([-12.55, 2.89], %) |> line(end = [1.82, -1.49], tag = $seg02)
// |> line(end = [3.02, 1.9]) |> angledLine([-86, segLen(seg02)], %)
// |> line(end = [1.82, -1.49], tag = $seg02) |> line(end = [-3.97, -0.53])
// |> angledLine([-86, segLen(seg02)], %) |> line(end = [0.3, 0.84])
// |> line(end = [-3.97, -0.53]) |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
// |> line(end = [0.3, 0.84]) |> close()
// |> line(endAbsolute = [profileStartX(%), profileStartY(%)]) `,
// |> close() lineOfInterest: 'startProfileAt([4.46, 5.12], %, $tag)',
// `, type: 'cap',
// lineOfInterest: 'startProfileAt([4.46, 5.12], %, $tag)', },
// type: 'cap', ],
// },
// ],
] as const ] as const
test.each(cases)( test.each(cases)(
'%s', '%s',
@ -982,7 +980,6 @@ sketch003 = startSketchOn('XZ')
artifact, artifact,
}, },
execState.variables, execState.variables,
execState.artifactGraph,
async () => { async () => {
await new Promise((resolve) => setTimeout(resolve, 100)) await new Promise((resolve) => setTimeout(resolve, 100))
return { return {

View File

@ -38,7 +38,6 @@ import {
isCallExprWithName, isCallExprWithName,
ARG_INDEX_FIELD, ARG_INDEX_FIELD,
LABELED_ARG_FIELD, LABELED_ARG_FIELD,
UNLABELED_ARG,
} from './queryAst' } from './queryAst'
import { import {
addTagForSketchOnFace, addTagForSketchOnFace,
@ -69,12 +68,12 @@ import {
expandWall, expandWall,
getArtifactOfTypes, getArtifactOfTypes,
getArtifactsOfTypes, getArtifactsOfTypes,
getFaceCodeRef,
getPathsFromArtifact, getPathsFromArtifact,
} from './std/artifactGraph' } from './std/artifactGraph'
import { BodyItem } from 'wasm-lib/kcl/bindings/BodyItem' import { BodyItem } from 'wasm-lib/kcl/bindings/BodyItem'
import { findKwArg } from './util' import { findKwArg } from './util'
import { deleteEdgeTreatment } from './modifyAst/addEdgeTreatment' import { deleteEdgeTreatment } from './modifyAst/addEdgeTreatment'
import { engineCommandManager } from 'lib/singletons'
export function startSketchOnDefault( export function startSketchOnDefault(
node: Node<Program>, node: Node<Program>,
@ -677,11 +676,10 @@ export function addOffsetPlane({
const newPlane = createVariableDeclaration( const newPlane = createVariableDeclaration(
newPlaneName, newPlaneName,
createCallExpressionStdLibKw( createCallExpressionStdLib('offsetPlane', [
'offsetPlane',
createLiteral(defaultPlane.toUpperCase()), createLiteral(defaultPlane.toUpperCase()),
[createLabeledArg('offset', offset)] offset,
) ])
) )
const insertAt = const insertAt =
@ -699,7 +697,8 @@ export function addOffsetPlane({
[insertAt, 'index'], [insertAt, 'index'],
['declaration', 'VariableDeclaration'], ['declaration', 'VariableDeclaration'],
['init', 'VariableDeclarator'], ['init', 'VariableDeclarator'],
['unlabeled', UNLABELED_ARG], ['arguments', 'CallExpression'],
[0, 'index'],
] ]
return { return {
modifiedAst, modifiedAst,
@ -984,7 +983,6 @@ export function createCallExpressionStdLibKw(
end: 0, end: 0,
moduleId: 0, moduleId: 0,
outerAttrs: [], outerAttrs: [],
nonCodeMeta: nonCodeMetaEmpty(),
callee: { callee: {
type: 'Identifier', type: 'Identifier',
start: 0, start: 0,
@ -1392,7 +1390,6 @@ export async function deleteFromSelection(
ast: Node<Program>, ast: Node<Program>,
selection: Selection, selection: Selection,
variables: VariableMap, variables: VariableMap,
artifactGraph: ArtifactGraph,
getFaceDetails: (id: string) => Promise<Models['FaceIsPlanar_type']> = () => getFaceDetails: (id: string) => Promise<Models['FaceIsPlanar_type']> = () =>
({} as any) ({} as any)
): Promise<Node<Program> | Error> { ): Promise<Node<Program> | Error> {
@ -1405,12 +1402,12 @@ export async function deleteFromSelection(
) { ) {
const plane = const plane =
selection.artifact.type === 'plane' selection.artifact.type === 'plane'
? expandPlane(selection.artifact, artifactGraph) ? expandPlane(selection.artifact, engineCommandManager.artifactGraph)
: selection.artifact.type === 'wall' : selection.artifact.type === 'wall'
? expandWall(selection.artifact, artifactGraph) ? expandWall(selection.artifact, engineCommandManager.artifactGraph)
: expandCap(selection.artifact, artifactGraph) : expandCap(selection.artifact, engineCommandManager.artifactGraph)
for (const path of plane.paths.sort( for (const path of plane.paths.sort(
(a, b) => b.codeRef.range?.[0] - a.codeRef.range?.[0] (a, b) => b.codeRef.range[0] - a.codeRef.range[0]
)) { )) {
const varDec = getNodeFromPath<VariableDeclarator>( const varDec = getNodeFromPath<VariableDeclarator>(
ast, ast,
@ -1427,15 +1424,6 @@ export async function deleteFromSelection(
selection.artifact.type === 'cap' || selection.artifact.type === 'cap' ||
selection.artifact.type === 'wall' selection.artifact.type === 'wall'
) { ) {
// Delete the sketch node, which would not work if
// we continued down the traditional code path below.
// faceCodeRef's pathToNode is empty for some reason
// so using source range instead
const codeRef = getFaceCodeRef(selection.artifact)
if (!codeRef) return new Error('Could not find face code ref')
const sketchVarDec = getNodePathFromSourceRange(astClone, codeRef.range)
const sketchBodyIndex = Number(sketchVarDec[1][0])
astClone.body.splice(sketchBodyIndex, 1)
return astClone return astClone
} }
} }
@ -1535,20 +1523,20 @@ export async function deleteFromSelection(
selection.artifact.surfaceId selection.artifact.surfaceId
? getArtifactOfTypes( ? getArtifactOfTypes(
{ key: selection.artifact.surfaceId, types: ['wall'] }, { key: selection.artifact.surfaceId, types: ['wall'] },
artifactGraph engineCommandManager.artifactGraph
) )
: null : null
if (err(wallArtifact)) return if (err(wallArtifact)) return
if (wallArtifact) { if (wallArtifact) {
const sweep = getArtifactOfTypes( const sweep = getArtifactOfTypes(
{ key: wallArtifact.sweepId, types: ['sweep'] }, { key: wallArtifact.sweepId, types: ['sweep'] },
artifactGraph engineCommandManager.artifactGraph
) )
if (err(sweep)) return if (err(sweep)) return
const wallsWithDependencies = Array.from( const wallsWithDependencies = Array.from(
getArtifactsOfTypes( getArtifactsOfTypes(
{ keys: sweep.surfaceIds, types: ['wall', 'cap'] }, { keys: sweep.surfaceIds, types: ['wall', 'cap'] },
artifactGraph engineCommandManager.artifactGraph
).values() ).values()
).filter((wall) => wall?.pathIds?.length) ).filter((wall) => wall?.pathIds?.length)
const wallIds = wallsWithDependencies.map((wall) => wall.id) const wallIds = wallsWithDependencies.map((wall) => wall.id)
@ -1726,7 +1714,7 @@ export async function deleteFromSelection(
return new Error('Selection not recognised, could not delete') return new Error('Selection not recognised, could not delete')
} }
export const nonCodeMetaEmpty = () => { const nonCodeMetaEmpty = () => {
return { nonCodeNodes: {}, startNodes: [], start: 0, end: 0 } return { nonCodeNodes: {}, startNodes: [], start: 0, end: 0 }
} }

View File

@ -582,7 +582,7 @@ sketch002 = startSketchOn(extrude001, $seg01)
it('finds sketch001 and sketch002 pipes to be lofted', async () => { it('finds sketch001 and sketch002 pipes to be lofted', async () => {
const exampleCode = `sketch001 = startSketchOn('XZ') const exampleCode = `sketch001 = startSketchOn('XZ')
|> circle({ center = [0, 0], radius = 1 }, %) |> circle({ center = [0, 0], radius = 1 }, %)
plane001 = offsetPlane('XZ', offset = 2) plane001 = offsetPlane('XZ', 2)
sketch002 = startSketchOn(plane001) sketch002 = startSketchOn(plane001)
|> circle({ center = [0, 0], radius = 3 }, %) |> circle({ center = [0, 0], radius = 3 }, %)
` `

View File

@ -45,7 +45,6 @@ import { ImportStatement } from 'wasm-lib/kcl/bindings/ImportStatement'
import { KclSettingsAnnotation } from 'lib/settings/settingsTypes' import { KclSettingsAnnotation } from 'lib/settings/settingsTypes'
export const LABELED_ARG_FIELD = 'LabeledArg -> Arg' export const LABELED_ARG_FIELD = 'LabeledArg -> Arg'
export const UNLABELED_ARG = 'unlabeled first arg'
export const ARG_INDEX_FIELD = 'arg index' export const ARG_INDEX_FIELD = 'arg index'
/** /**

View File

@ -532,32 +532,6 @@ function getPlaneFromSolid2D(
if (err(path)) return path if (err(path)) return path
return getPlaneFromPath(path, graph) return getPlaneFromPath(path, graph)
} }
function getPlaneFromCap(
cap: CapArtifact,
graph: ArtifactGraph
): PlaneArtifact | WallArtifact | CapArtifact | Error {
const sweep = getArtifactOfTypes(
{ key: cap.sweepId, types: ['sweep'] },
graph
)
if (err(sweep)) return sweep
const path = getArtifactOfTypes({ key: sweep.pathId, types: ['path'] }, graph)
if (err(path)) return path
return getPlaneFromPath(path, graph)
}
function getPlaneFromWall(
wall: WallArtifact,
graph: ArtifactGraph
): PlaneArtifact | WallArtifact | CapArtifact | Error {
const sweep = getArtifactOfTypes(
{ key: wall.sweepId, types: ['sweep'] },
graph
)
if (err(sweep)) return sweep
const path = getArtifactOfTypes({ key: sweep.pathId, types: ['path'] }, graph)
if (err(path)) return path
return getPlaneFromPath(path, graph)
}
function getPlaneFromSweepEdge(edge: SweepEdge, graph: ArtifactGraph) { function getPlaneFromSweepEdge(edge: SweepEdge, graph: ArtifactGraph) {
const sweep = getArtifactOfTypes( const sweep = getArtifactOfTypes(
{ key: edge.sweepId, types: ['sweep'] }, { key: edge.sweepId, types: ['sweep'] },
@ -578,15 +552,7 @@ export function getPlaneFromArtifact(
if (artifact.type === 'path') return getPlaneFromPath(artifact, graph) if (artifact.type === 'path') return getPlaneFromPath(artifact, graph)
if (artifact.type === 'segment') return getPlaneFromSegment(artifact, graph) if (artifact.type === 'segment') return getPlaneFromSegment(artifact, graph)
if (artifact.type === 'solid2d') return getPlaneFromSolid2D(artifact, graph) if (artifact.type === 'solid2d') return getPlaneFromSolid2D(artifact, graph)
if ( if (artifact.type === 'wall' || artifact.type === 'cap') return artifact
// if the user selects a face with sketch on it (pathIds.length), they probably wanted to edit that sketch,
// not the sketch for the underlying sweep sketch
(artifact.type === 'wall' || artifact.type === 'cap') &&
artifact?.pathIds?.length
)
return artifact
if (artifact.type === 'cap') return getPlaneFromCap(artifact, graph)
if (artifact.type === 'wall') return getPlaneFromWall(artifact, graph)
if (artifact.type === 'sweepEdge') if (artifact.type === 'sweepEdge')
return getPlaneFromSweepEdge(artifact, graph) return getPlaneFromSweepEdge(artifact, graph)
return new Error(`Artifact type ${artifact.type} does not have a plane`) return new Error(`Artifact type ${artifact.type} does not have a plane`)

View File

@ -30,7 +30,6 @@ import { toolTips, ToolTip } from 'lang/langHelpers'
import { import {
createPipeExpression, createPipeExpression,
mutateKwArg, mutateKwArg,
nonCodeMetaEmpty,
splitPathAtPipeExpression, splitPathAtPipeExpression,
} from '../modifyAst' } from '../modifyAst'
@ -2830,7 +2829,6 @@ function addTagKw(): addTagFn {
unlabeled: callExpr.node.arguments.length unlabeled: callExpr.node.arguments.length
? callExpr.node.arguments[0] ? callExpr.node.arguments[0]
: null, : null,
nonCodeMeta: nonCodeMetaEmpty(),
arguments: [], arguments: [],
} }
const tagArg = findKwArg(ARG_TAG, primaryCallExp) const tagArg = findKwArg(ARG_TAG, primaryCallExp)

View File

@ -14,7 +14,6 @@ describe('KCL expression calculations', () => {
variables['x'] = { variables['x'] = {
type: 'Number', type: 'Number',
value: 2, value: 2,
ty: { type: 'Any' },
__meta: [], __meta: [],
} }
const actual = await getCalculatedKclExpressionValue('1 + x', variables) const actual = await getCalculatedKclExpressionValue('1 + x', variables)
@ -33,7 +32,6 @@ describe('KCL expression calculations', () => {
variables['y'] = { variables['y'] = {
type: 'Number', type: 'Number',
value: 2, value: 2,
ty: { type: 'Any' },
__meta: [], __meta: [],
} }
const actual = await getCalculatedKclExpressionValue('1 + x', variables) const actual = await getCalculatedKclExpressionValue('1 + x', variables)
@ -46,7 +44,6 @@ describe('KCL expression calculations', () => {
variables['x'] = { variables['x'] = {
type: 'Number', type: 'Number',
value: 2, value: 2,
ty: { type: 'Any' },
__meta: [], __meta: [],
} }
const actual = await getCalculatedKclExpressionValue( const actual = await getCalculatedKclExpressionValue(

View File

@ -126,7 +126,8 @@ const prepareToEditOffsetPlane: PrepareToEditCallback = async ({
if ( if (
operation.type !== 'StdLibCall' || operation.type !== 'StdLibCall' ||
!operation.labeledArgs || !operation.labeledArgs ||
!operation.unlabeledArg || !('std_plane' in operation.labeledArgs) ||
!operation.labeledArgs.std_plane ||
!('offset' in operation.labeledArgs) || !('offset' in operation.labeledArgs) ||
!operation.labeledArgs.offset !operation.labeledArgs.offset
) { ) {
@ -134,9 +135,11 @@ const prepareToEditOffsetPlane: PrepareToEditCallback = async ({
} }
// TODO: Implement conversion to arbitrary plane selection // TODO: Implement conversion to arbitrary plane selection
// once the Offset Plane command supports it. // once the Offset Plane command supports it.
const stdPlane = operation.unlabeledArg
const planeName = codeManager.code const planeName = codeManager.code
.slice(stdPlane.sourceRange[0], stdPlane.sourceRange[1]) .slice(
operation.labeledArgs.std_plane.sourceRange[0],
operation.labeledArgs.std_plane.sourceRange[1]
)
.replaceAll(`'`, ``) .replaceAll(`'`, ``)
if (!isDefaultPlaneStr(planeName)) { if (!isDefaultPlaneStr(planeName)) {

View File

@ -375,7 +375,14 @@ export const toolbarConfig: Record<ToolbarModeName, ToolbarMode> = {
}, },
icon: 'line', icon: 'line',
status: 'available', status: 'available',
disabled: (state) => state.matches('Sketch no face'), disabled: (state) =>
state.matches('Sketch no face') ||
state.matches({
Sketch: { 'Rectangle tool': 'Awaiting second corner' },
}) ||
state.matches({
Sketch: { 'Circle tool': 'Awaiting Radius' },
}),
title: 'Line', title: 'Line',
hotkey: (state) => hotkey: (state) =>
state.matches({ Sketch: 'Line tool' }) ? ['Esc', 'L'] : 'L', state.matches({ Sketch: 'Line tool' }) ? ['Esc', 'L'] : 'L',

File diff suppressed because one or more lines are too long

View File

@ -53,7 +53,6 @@ process.env.VITE_KC_CONNECTION_TIMEOUT_MS ??=
console.log('process.env', process.env) console.log('process.env', process.env)
/// Register our application to handle all "zoo-studio:" protocols. /// Register our application to handle all "zoo-studio:" protocols.
const singleInstanceLock = app.requestSingleInstanceLock()
if (process.defaultApp) { if (process.defaultApp) {
if (process.argv.length >= 2) { if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(ZOO_STUDIO_PROTOCOL, process.execPath, [ app.setAsDefaultProtocolClient(ZOO_STUDIO_PROTOCOL, process.execPath, [
@ -66,13 +65,7 @@ if (process.defaultApp) {
// Global app listeners // Global app listeners
// Must be done before ready event. // Must be done before ready event.
// Checking against this lock is needed for Windows and Linux, see registerStartupListeners()
// https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app#windows-and-linux-code
if (!singleInstanceLock && !process.env.IS_PLAYWRIGHT) {
app.quit()
} else {
registerStartupListeners()
}
const createWindow = (pathToOpen?: string, reuse?: boolean): BrowserWindow => { const createWindow = (pathToOpen?: string, reuse?: boolean): BrowserWindow => {
let newWindow let newWindow
@ -82,7 +75,7 @@ const createWindow = (pathToOpen?: string, reuse?: boolean): BrowserWindow => {
} }
if (!newWindow) { if (!newWindow) {
newWindow = new BrowserWindow({ newWindow = new BrowserWindow({
autoHideMenuBar: false, autoHideMenuBar: true,
show: false, show: false,
width: 1800, width: 1800,
height: 1200, height: 1200,

View File

@ -730,7 +730,7 @@ dependencies = [
[[package]] [[package]]
name = "derive-docs" name = "derive-docs"
version = "0.1.38" version = "0.1.36"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"anyhow", "anyhow",
@ -1712,7 +1712,7 @@ dependencies = [
[[package]] [[package]]
name = "kcl-lib" name = "kcl-lib"
version = "0.2.38" version = "0.2.35"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"approx 0.5.1", "approx 0.5.1",
@ -1779,7 +1779,7 @@ dependencies = [
[[package]] [[package]]
name = "kcl-test-server" name = "kcl-test-server"
version = "0.1.38" version = "0.1.21"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"hyper 0.14.32", "hyper 0.14.32",

View File

@ -1,7 +1,7 @@
[package] [package]
name = "derive-docs" name = "derive-docs"
description = "A tool for generating documentation from Rust derive macros" description = "A tool for generating documentation from Rust derive macros"
version = "0.1.38" version = "0.1.36"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/KittyCAD/modeling-app" repository = "https://github.com/KittyCAD/modeling-app"

View File

@ -1,7 +1,7 @@
[package] [package]
name = "kcl-test-server" name = "kcl-test-server"
description = "A test server for KCL" description = "A test server for KCL"
version = "0.1.38" version = "0.1.21"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"

View File

@ -1,4 +1,7 @@
use std::{collections::HashMap, sync::Arc}; use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use anyhow::Result; use anyhow::Result;
use indexmap::IndexMap; use indexmap::IndexMap;
@ -21,20 +24,22 @@ const NEED_PLANES: bool = true;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EngineConnection { pub struct EngineConnection {
batch: Arc<RwLock<Vec<(WebSocketRequest, kcl_lib::SourceRange)>>>, batch: Arc<Mutex<Vec<(WebSocketRequest, kcl_lib::SourceRange)>>>,
batch_end: Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, kcl_lib::SourceRange)>>>, batch_end: Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, kcl_lib::SourceRange)>>>,
core_test: Arc<RwLock<String>>, core_test: Arc<Mutex<String>>,
default_planes: Arc<RwLock<Option<DefaultPlanes>>>, default_planes: Arc<RwLock<Option<DefaultPlanes>>>,
execution_kind: Arc<RwLock<ExecutionKind>>, execution_kind: Arc<Mutex<ExecutionKind>>,
} }
impl EngineConnection { impl EngineConnection {
pub async fn new(result: Arc<RwLock<String>>) -> Result<EngineConnection> { pub async fn new(result: Arc<Mutex<String>>) -> Result<EngineConnection> {
result.write().await.push_str(CPP_PREFIX); if let Ok(mut code) = result.lock() {
code.push_str(CPP_PREFIX);
}
Ok(EngineConnection { Ok(EngineConnection {
batch: Arc::new(RwLock::new(Vec::new())), batch: Arc::new(Mutex::new(Vec::new())),
batch_end: Arc::new(RwLock::new(IndexMap::new())), batch_end: Arc::new(Mutex::new(IndexMap::new())),
core_test: result, core_test: result,
default_planes: Default::default(), default_planes: Default::default(),
execution_kind: Default::default(), execution_kind: Default::default(),
@ -357,29 +362,29 @@ fn codegen_cpp_repl_uuid_setters(reps_id: &str, entity_ids: &[uuid::Uuid]) -> St
#[async_trait::async_trait] #[async_trait::async_trait]
impl kcl_lib::EngineManager for EngineConnection { impl kcl_lib::EngineManager for EngineConnection {
fn batch(&self) -> Arc<RwLock<Vec<(WebSocketRequest, kcl_lib::SourceRange)>>> { fn batch(&self) -> Arc<Mutex<Vec<(WebSocketRequest, kcl_lib::SourceRange)>>> {
self.batch.clone() self.batch.clone()
} }
fn batch_end(&self) -> Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, kcl_lib::SourceRange)>>> { fn batch_end(&self) -> Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, kcl_lib::SourceRange)>>> {
self.batch_end.clone() self.batch_end.clone()
} }
fn responses(&self) -> Arc<RwLock<IndexMap<Uuid, WebSocketResponse>>> { fn responses(&self) -> IndexMap<Uuid, WebSocketResponse> {
Arc::new(RwLock::new(IndexMap::new())) IndexMap::new()
} }
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> { fn take_artifact_commands(&self) -> Vec<ArtifactCommand> {
Arc::new(RwLock::new(Vec::new())) Vec::new()
} }
async fn execution_kind(&self) -> ExecutionKind { fn execution_kind(&self) -> ExecutionKind {
let guard = self.execution_kind.read().await; let guard = self.execution_kind.lock().unwrap();
*guard *guard
} }
async fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind { fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind {
let mut guard = self.execution_kind.write().await; let mut guard = self.execution_kind.lock().unwrap();
let original = *guard; let original = *guard;
*guard = execution_kind; *guard = execution_kind;
original original
@ -430,18 +435,24 @@ impl kcl_lib::EngineManager for EngineConnection {
}) => { }) => {
let mut responses = HashMap::new(); let mut responses = HashMap::new();
for request in requests { for request in requests {
let (new_code, this_response) = self.handle_command(&request.cmd_id, &request.cmd); let (new_code, this_response);
if !new_code.is_empty() { if let Ok(mut test_code) = self.core_test.lock() {
let new_code = new_code (new_code, this_response) = self.handle_command(&request.cmd_id, &request.cmd);
.trim()
.split(' ') if !new_code.is_empty() {
.filter(|s| !s.is_empty()) let new_code = new_code
.collect::<Vec<_>>() .trim()
.join(" ") .split(' ')
+ "\n"; .filter(|s| !s.is_empty())
//println!("{new_code}"); .collect::<Vec<_>>()
self.core_test.write().await.push_str(&new_code); .join(" ")
+ "\n";
//println!("{new_code}");
test_code.push_str(&new_code);
}
} else {
this_response = OkModelingCmdResponse::Empty {};
} }
responses.insert( responses.insert(
@ -459,18 +470,24 @@ impl kcl_lib::EngineManager for EngineConnection {
} }
WebSocketRequest::ModelingCmdReq(ModelingCmdReq { cmd, cmd_id }) => { WebSocketRequest::ModelingCmdReq(ModelingCmdReq { cmd, cmd_id }) => {
//also handle unbatched requests inline //also handle unbatched requests inline
let (new_code, this_response) = self.handle_command(&cmd_id, &cmd); let (new_code, this_response);
if !new_code.is_empty() { if let Ok(mut test_code) = self.core_test.lock() {
let new_code = new_code (new_code, this_response) = self.handle_command(&cmd_id, &cmd);
.trim()
.split(' ') if !new_code.is_empty() {
.filter(|s| !s.is_empty()) let new_code = new_code
.collect::<Vec<_>>() .trim()
.join(" ") .split(' ')
+ "\n"; .filter(|s| !s.is_empty())
//println!("{new_code}"); .collect::<Vec<_>>()
self.core_test.write().await.push_str(&new_code); .join(" ")
+ "\n";
//println!("{new_code}");
test_code.push_str(&new_code);
}
} else {
this_response = OkModelingCmdResponse::Empty {};
} }
Ok(WebSocketResponse::Success(kcmc::websocket::SuccessWebSocketResponse { Ok(WebSocketResponse::Success(kcmc::websocket::SuccessWebSocketResponse {

View File

@ -1,8 +1,7 @@
use std::sync::Arc; use std::sync::{Arc, Mutex};
use anyhow::Result; use anyhow::Result;
use kcl_lib::{ExecState, ExecutorContext}; use kcl_lib::{ExecState, ExecutorContext};
use tokio::sync::RwLock;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
mod conn_mock_core; mod conn_mock_core;
@ -11,7 +10,7 @@ mod conn_mock_core;
pub async fn kcl_to_engine_core(code: &str) -> Result<String> { pub async fn kcl_to_engine_core(code: &str) -> Result<String> {
let program = kcl_lib::Program::parse_no_errs(code)?; let program = kcl_lib::Program::parse_no_errs(code)?;
let result = Arc::new(RwLock::new("".into())); let result = Arc::new(Mutex::new("".into()));
let ref_result = Arc::clone(&result); let ref_result = Arc::clone(&result);
let ctx = ExecutorContext::new_forwarded_mock(Arc::new(Box::new( let ctx = ExecutorContext::new_forwarded_mock(Arc::new(Box::new(
@ -19,6 +18,6 @@ pub async fn kcl_to_engine_core(code: &str) -> Result<String> {
))); )));
ctx.run(&program, &mut ExecState::new(&ctx.settings)).await?; ctx.run(&program, &mut ExecState::new(&ctx.settings)).await?;
let result = result.read().await.clone(); let result = result.lock().expect("mutex lock").clone();
Ok(result) Ok(result)
} }

View File

@ -1,7 +1,7 @@
[package] [package]
name = "kcl-lib" name = "kcl-lib"
description = "KittyCAD Language implementation and tools" description = "KittyCAD Language implementation and tools"
version = "0.2.38" version = "0.2.35"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/KittyCAD/modeling-app" repository = "https://github.com/KittyCAD/modeling-app"

View File

@ -1,9 +1,13 @@
//! Functions for setting up our WebSocket and WebRTC connections for communications with the //! Functions for setting up our WebSocket and WebRTC connections for communications with the
//! engine. //! engine.
use std::{collections::HashMap, sync::Arc}; use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use dashmap::DashMap;
use futures::{SinkExt, StreamExt}; use futures::{SinkExt, StreamExt};
use indexmap::IndexMap; use indexmap::IndexMap;
use kcmc::{ use kcmc::{
@ -13,7 +17,9 @@ use kcmc::{
}, },
ModelingCmd, ModelingCmd,
}; };
use kittycad_modeling_cmds::{self as kcmc}; use kittycad_modeling_cmds::{
self as kcmc, id::ModelingCmdId, ok_response::OkModelingCmdResponse, websocket::ModelingBatch,
};
use tokio::sync::{mpsc, oneshot, RwLock}; use tokio::sync::{mpsc, oneshot, RwLock};
use tokio_tungstenite::tungstenite::Message as WsMsg; use tokio_tungstenite::tungstenite::Message as WsMsg;
use uuid::Uuid; use uuid::Uuid;
@ -37,21 +43,21 @@ type WebSocketTcpWrite = futures::stream::SplitSink<tokio_tungstenite::WebSocket
pub struct EngineConnection { pub struct EngineConnection {
engine_req_tx: mpsc::Sender<ToEngineReq>, engine_req_tx: mpsc::Sender<ToEngineReq>,
shutdown_tx: mpsc::Sender<()>, shutdown_tx: mpsc::Sender<()>,
responses: Arc<RwLock<IndexMap<uuid::Uuid, WebSocketResponse>>>, responses: Arc<DashMap<uuid::Uuid, WebSocketResponse>>,
pending_errors: Arc<RwLock<Vec<String>>>, pending_errors: Arc<Mutex<Vec<String>>>,
#[allow(dead_code)] #[allow(dead_code)]
tcp_read_handle: Arc<TcpReadHandle>, tcp_read_handle: Arc<TcpReadHandle>,
socket_health: Arc<RwLock<SocketHealth>>, socket_health: Arc<Mutex<SocketHealth>>,
batch: Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>>, batch: Arc<Mutex<Vec<(WebSocketRequest, SourceRange)>>>,
batch_end: Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>, batch_end: Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>,
artifact_commands: Arc<RwLock<Vec<ArtifactCommand>>>, artifact_commands: Arc<Mutex<Vec<ArtifactCommand>>>,
/// The default planes for the scene. /// The default planes for the scene.
default_planes: Arc<RwLock<Option<DefaultPlanes>>>, default_planes: Arc<RwLock<Option<DefaultPlanes>>>,
/// If the server sends session data, it'll be copied to here. /// If the server sends session data, it'll be copied to here.
session_data: Arc<RwLock<Option<ModelingSessionData>>>, session_data: Arc<Mutex<Option<ModelingSessionData>>>,
execution_kind: Arc<RwLock<ExecutionKind>>, execution_kind: Arc<Mutex<ExecutionKind>>,
} }
pub struct TcpRead { pub struct TcpRead {
@ -224,12 +230,12 @@ impl EngineConnection {
let mut tcp_read = TcpRead { stream: tcp_read }; let mut tcp_read = TcpRead { stream: tcp_read };
let session_data: Arc<RwLock<Option<ModelingSessionData>>> = Arc::new(RwLock::new(None)); let session_data: Arc<Mutex<Option<ModelingSessionData>>> = Arc::new(Mutex::new(None));
let session_data2 = session_data.clone(); let session_data2 = session_data.clone();
let responses: Arc<RwLock<IndexMap<uuid::Uuid, WebSocketResponse>>> = Arc::new(RwLock::new(IndexMap::new())); let responses: Arc<DashMap<uuid::Uuid, WebSocketResponse>> = Arc::new(DashMap::new());
let responses_clone = responses.clone(); let responses_clone = responses.clone();
let socket_health = Arc::new(RwLock::new(SocketHealth::Active)); let socket_health = Arc::new(Mutex::new(SocketHealth::Active));
let pending_errors = Arc::new(RwLock::new(Vec::new())); let pending_errors = Arc::new(Mutex::new(Vec::new()));
let pending_errors_clone = pending_errors.clone(); let pending_errors_clone = pending_errors.clone();
let socket_health_tcp_read = socket_health.clone(); let socket_health_tcp_read = socket_health.clone();
@ -254,7 +260,7 @@ impl EngineConnection {
let id: uuid::Uuid = (*resp_id).into(); let id: uuid::Uuid = (*resp_id).into();
match batch_response { match batch_response {
BatchResponse::Success { response } => { BatchResponse::Success { response } => {
responses_clone.write().await.insert( responses_clone.insert(
id, id,
WebSocketResponse::Success(SuccessWebSocketResponse { WebSocketResponse::Success(SuccessWebSocketResponse {
success: true, success: true,
@ -266,7 +272,7 @@ impl EngineConnection {
); );
} }
BatchResponse::Failure { errors } => { BatchResponse::Failure { errors } => {
responses_clone.write().await.insert( responses_clone.insert(
id, id,
WebSocketResponse::Failure(FailureWebSocketResponse { WebSocketResponse::Failure(FailureWebSocketResponse {
success: false, success: false,
@ -282,7 +288,7 @@ impl EngineConnection {
resp: OkWebSocketResponseData::ModelingSessionData { session }, resp: OkWebSocketResponseData::ModelingSessionData { session },
.. ..
}) => { }) => {
let mut sd = session_data2.write().await; let mut sd = session_data2.lock().unwrap();
sd.replace(session.clone()); sd.replace(session.clone());
} }
WebSocketResponse::Failure(FailureWebSocketResponse { WebSocketResponse::Failure(FailureWebSocketResponse {
@ -291,7 +297,7 @@ impl EngineConnection {
errors, errors,
}) => { }) => {
if let Some(id) = request_id { if let Some(id) = request_id {
responses_clone.write().await.insert( responses_clone.insert(
*id, *id,
WebSocketResponse::Failure(FailureWebSocketResponse { WebSocketResponse::Failure(FailureWebSocketResponse {
success: false, success: false,
@ -301,20 +307,19 @@ impl EngineConnection {
); );
} else { } else {
// Add it to our pending errors. // Add it to our pending errors.
let mut pe = pending_errors_clone.write().await; let mut pe = pending_errors_clone.lock().unwrap();
for error in errors { for error in errors {
if !pe.contains(&error.message) { if !pe.contains(&error.message) {
pe.push(error.message.clone()); pe.push(error.message.clone());
} }
} }
drop(pe);
} }
} }
_ => {} _ => {}
} }
if let Some(id) = id { if let Some(id) = id {
responses_clone.write().await.insert(id, ws_resp.clone()); responses_clone.insert(id, ws_resp.clone());
} }
} }
Err(e) => { Err(e) => {
@ -322,7 +327,7 @@ impl EngineConnection {
WebSocketReadError::Read(e) => crate::logln!("could not read from WS: {:?}", e), WebSocketReadError::Read(e) => crate::logln!("could not read from WS: {:?}", e),
WebSocketReadError::Deser(e) => crate::logln!("could not deserialize msg from WS: {:?}", e), WebSocketReadError::Deser(e) => crate::logln!("could not deserialize msg from WS: {:?}", e),
} }
*socket_health_tcp_read.write().await = SocketHealth::Inactive; *socket_health_tcp_read.lock().unwrap() = SocketHealth::Inactive;
return Err(e); return Err(e);
} }
} }
@ -338,41 +343,70 @@ impl EngineConnection {
responses, responses,
pending_errors, pending_errors,
socket_health, socket_health,
batch: Arc::new(RwLock::new(Vec::new())), batch: Arc::new(Mutex::new(Vec::new())),
batch_end: Arc::new(RwLock::new(IndexMap::new())), batch_end: Arc::new(Mutex::new(IndexMap::new())),
artifact_commands: Arc::new(RwLock::new(Vec::new())), artifact_commands: Arc::new(Mutex::new(Vec::new())),
default_planes: Default::default(), default_planes: Default::default(),
session_data, session_data,
execution_kind: Default::default(), execution_kind: Default::default(),
}) })
} }
fn handle_command(
&self,
cmd: &ModelingCmd,
cmd_id: ModelingCmdId,
id_to_source_range: &HashMap<Uuid, SourceRange>,
) -> Result<(), KclError> {
let cmd_id = *cmd_id.as_ref();
let range = id_to_source_range
.get(&cmd_id)
.copied()
.ok_or_else(|| KclError::internal(format!("Failed to get source range for command ID: {:?}", cmd_id)))?;
// Add artifact command.
let mut artifact_commands = self.artifact_commands.lock().unwrap();
artifact_commands.push(ArtifactCommand {
cmd_id,
range,
command: cmd.clone(),
});
Ok(())
}
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl EngineManager for EngineConnection { impl EngineManager for EngineConnection {
fn batch(&self) -> Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>> { fn batch(&self) -> Arc<Mutex<Vec<(WebSocketRequest, SourceRange)>>> {
self.batch.clone() self.batch.clone()
} }
fn batch_end(&self) -> Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>> { fn batch_end(&self) -> Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>> {
self.batch_end.clone() self.batch_end.clone()
} }
fn responses(&self) -> Arc<RwLock<IndexMap<Uuid, WebSocketResponse>>> { fn responses(&self) -> IndexMap<Uuid, WebSocketResponse> {
self.responses.clone() self.responses
.iter()
.map(|entry| {
let (k, v) = entry.pair();
(*k, v.clone())
})
.collect()
} }
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> { fn take_artifact_commands(&self) -> Vec<ArtifactCommand> {
self.artifact_commands.clone() let mut artifact_commands = self.artifact_commands.lock().unwrap();
std::mem::take(&mut *artifact_commands)
} }
async fn execution_kind(&self) -> ExecutionKind { fn execution_kind(&self) -> ExecutionKind {
let guard = self.execution_kind.read().await; let guard = self.execution_kind.lock().unwrap();
*guard *guard
} }
async fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind { fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind {
let mut guard = self.execution_kind.write().await; let mut guard = self.execution_kind.lock().unwrap();
let original = *guard; let original = *guard;
*guard = execution_kind; *guard = execution_kind;
original original
@ -413,8 +447,49 @@ impl EngineManager for EngineConnection {
id: uuid::Uuid, id: uuid::Uuid,
source_range: SourceRange, source_range: SourceRange,
cmd: WebSocketRequest, cmd: WebSocketRequest,
_id_to_source_range: HashMap<Uuid, SourceRange>, id_to_source_range: HashMap<Uuid, SourceRange>,
) -> Result<WebSocketResponse, KclError> { ) -> Result<WebSocketResponse, KclError> {
match &cmd {
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { requests, .. }) => {
for request in requests {
self.handle_command(&request.cmd, request.cmd_id, &id_to_source_range)?;
}
}
WebSocketRequest::ModelingCmdReq(request) => {
self.handle_command(&request.cmd, request.cmd_id, &id_to_source_range)?;
}
_ => {}
}
// In isolated mode, we don't send the command to the engine.
if self.execution_kind().is_isolated() {
return match &cmd {
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { requests, .. }) => {
let mut responses = HashMap::with_capacity(requests.len());
for request in requests {
responses.insert(
request.cmd_id,
BatchResponse::Success {
response: OkModelingCmdResponse::Empty {},
},
);
}
Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
request_id: Some(id),
resp: OkWebSocketResponseData::ModelingBatch { responses },
success: true,
}))
}
_ => Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
request_id: Some(id),
resp: OkWebSocketResponseData::Modeling {
modeling_response: OkModelingCmdResponse::Empty {},
},
success: true,
})),
};
}
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
// Send the request to the engine, via the actor. // Send the request to the engine, via the actor.
@ -449,24 +524,25 @@ impl EngineManager for EngineConnection {
// Wait for the response. // Wait for the response.
let current_time = std::time::Instant::now(); let current_time = std::time::Instant::now();
while current_time.elapsed().as_secs() < 60 { while current_time.elapsed().as_secs() < 60 {
let guard = self.socket_health.read().await; if let Ok(guard) = self.socket_health.lock() {
if *guard == SocketHealth::Inactive { if *guard == SocketHealth::Inactive {
// Check if we have any pending errors. // Check if we have any pending errors.
let pe = self.pending_errors.read().await; let pe = self.pending_errors.lock().unwrap();
if !pe.is_empty() { if !pe.is_empty() {
return Err(KclError::Engine(KclErrorDetails { return Err(KclError::Engine(KclErrorDetails {
message: pe.join(", ").to_string(), message: pe.join(", ").to_string(),
source_ranges: vec![source_range], source_ranges: vec![source_range],
})); }));
} else { } else {
return Err(KclError::Engine(KclErrorDetails { return Err(KclError::Engine(KclErrorDetails {
message: "Modeling command failed: websocket closed early".to_string(), message: "Modeling command failed: websocket closed early".to_string(),
source_ranges: vec![source_range], source_ranges: vec![source_range],
})); }));
}
} }
} }
// We pop off the responses to cleanup our mappings. // We pop off the responses to cleanup our mappings.
if let Some(resp) = self.responses.write().await.shift_remove(&id) { if let Some((_, resp)) = self.responses.remove(&id) {
return Ok(resp); return Ok(resp);
} }
} }
@ -477,16 +553,17 @@ impl EngineManager for EngineConnection {
})) }))
} }
async fn get_session_data(&self) -> Option<ModelingSessionData> { fn get_session_data(&self) -> Option<ModelingSessionData> {
self.session_data.read().await.clone() self.session_data.lock().unwrap().clone()
} }
async fn close(&self) { async fn close(&self) {
let _ = self.shutdown_tx.send(()).await; let _ = self.shutdown_tx.send(()).await;
loop { loop {
let guard = self.socket_health.read().await; if let Ok(guard) = self.socket_health.lock() {
if *guard == SocketHealth::Inactive { if *guard == SocketHealth::Inactive {
return; return;
}
} }
} }
} }

View File

@ -1,7 +1,10 @@
//! Functions for setting up our WebSocket and WebRTC connections for communications with the //! Functions for setting up our WebSocket and WebRTC connections for communications with the
//! engine. //! engine.
use std::{collections::HashMap, sync::Arc}; use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use anyhow::Result; use anyhow::Result;
use indexmap::IndexMap; use indexmap::IndexMap;
@ -12,8 +15,7 @@ use kcmc::{
WebSocketResponse, WebSocketResponse,
}, },
}; };
use kittycad_modeling_cmds::{self as kcmc}; use kittycad_modeling_cmds::{self as kcmc, id::ModelingCmdId, ModelingCmd};
use tokio::sync::RwLock;
use uuid::Uuid; use uuid::Uuid;
use super::ExecutionKind; use super::ExecutionKind;
@ -26,48 +28,71 @@ use crate::{
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EngineConnection { pub struct EngineConnection {
batch: Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>>, batch: Arc<Mutex<Vec<(WebSocketRequest, SourceRange)>>>,
batch_end: Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>, batch_end: Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>,
artifact_commands: Arc<RwLock<Vec<ArtifactCommand>>>, artifact_commands: Arc<Mutex<Vec<ArtifactCommand>>>,
execution_kind: Arc<RwLock<ExecutionKind>>, execution_kind: Arc<Mutex<ExecutionKind>>,
} }
impl EngineConnection { impl EngineConnection {
pub async fn new() -> Result<EngineConnection> { pub async fn new() -> Result<EngineConnection> {
Ok(EngineConnection { Ok(EngineConnection {
batch: Arc::new(RwLock::new(Vec::new())), batch: Arc::new(Mutex::new(Vec::new())),
batch_end: Arc::new(RwLock::new(IndexMap::new())), batch_end: Arc::new(Mutex::new(IndexMap::new())),
artifact_commands: Arc::new(RwLock::new(Vec::new())), artifact_commands: Arc::new(Mutex::new(Vec::new())),
execution_kind: Default::default(), execution_kind: Default::default(),
}) })
} }
fn handle_command(
&self,
cmd: &ModelingCmd,
cmd_id: ModelingCmdId,
id_to_source_range: &HashMap<Uuid, SourceRange>,
) -> Result<(), KclError> {
let cmd_id = *cmd_id.as_ref();
let range = id_to_source_range
.get(&cmd_id)
.copied()
.ok_or_else(|| KclError::internal(format!("Failed to get source range for command ID: {:?}", cmd_id)))?;
// Add artifact command.
let mut artifact_commands = self.artifact_commands.lock().unwrap();
artifact_commands.push(ArtifactCommand {
cmd_id,
range,
command: cmd.clone(),
});
Ok(())
}
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl crate::engine::EngineManager for EngineConnection { impl crate::engine::EngineManager for EngineConnection {
fn batch(&self) -> Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>> { fn batch(&self) -> Arc<Mutex<Vec<(WebSocketRequest, SourceRange)>>> {
self.batch.clone() self.batch.clone()
} }
fn batch_end(&self) -> Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>> { fn batch_end(&self) -> Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>> {
self.batch_end.clone() self.batch_end.clone()
} }
fn responses(&self) -> Arc<RwLock<IndexMap<Uuid, WebSocketResponse>>> { fn responses(&self) -> IndexMap<Uuid, WebSocketResponse> {
Arc::new(RwLock::new(IndexMap::new())) IndexMap::new()
} }
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> { fn take_artifact_commands(&self) -> Vec<ArtifactCommand> {
self.artifact_commands.clone() let mut artifact_commands = self.artifact_commands.lock().unwrap();
std::mem::take(&mut *artifact_commands)
} }
async fn execution_kind(&self) -> ExecutionKind { fn execution_kind(&self) -> ExecutionKind {
let guard = self.execution_kind.read().await; let guard = self.execution_kind.lock().unwrap();
*guard *guard
} }
async fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind { fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind {
let mut guard = self.execution_kind.write().await; let mut guard = self.execution_kind.lock().unwrap();
let original = *guard; let original = *guard;
*guard = execution_kind; *guard = execution_kind;
original original
@ -94,7 +119,7 @@ impl crate::engine::EngineManager for EngineConnection {
id: uuid::Uuid, id: uuid::Uuid,
_source_range: SourceRange, _source_range: SourceRange,
cmd: WebSocketRequest, cmd: WebSocketRequest,
_id_to_source_range: HashMap<Uuid, SourceRange>, id_to_source_range: HashMap<Uuid, SourceRange>,
) -> Result<WebSocketResponse, KclError> { ) -> Result<WebSocketResponse, KclError> {
match cmd { match cmd {
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { WebSocketRequest::ModelingCmdBatchReq(ModelingBatch {
@ -105,6 +130,7 @@ impl crate::engine::EngineManager for EngineConnection {
// Create the empty responses. // Create the empty responses.
let mut responses = HashMap::with_capacity(requests.len()); let mut responses = HashMap::with_capacity(requests.len());
for request in requests { for request in requests {
self.handle_command(&request.cmd, request.cmd_id, &id_to_source_range)?;
responses.insert( responses.insert(
request.cmd_id, request.cmd_id,
BatchResponse::Success { BatchResponse::Success {
@ -118,13 +144,17 @@ impl crate::engine::EngineManager for EngineConnection {
success: true, success: true,
})) }))
} }
WebSocketRequest::ModelingCmdReq(_) => Ok(WebSocketResponse::Success(SuccessWebSocketResponse { WebSocketRequest::ModelingCmdReq(request) => {
request_id: Some(id), self.handle_command(&request.cmd, request.cmd_id, &id_to_source_range)?;
resp: OkWebSocketResponseData::Modeling {
modeling_response: OkModelingCmdResponse::Empty {}, Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
}, request_id: Some(id),
success: true, resp: OkWebSocketResponseData::Modeling {
})), modeling_response: OkModelingCmdResponse::Empty {},
},
success: true,
}))
}
_ => Ok(WebSocketResponse::Success(SuccessWebSocketResponse { _ => Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
request_id: Some(id), request_id: Some(id),
resp: OkWebSocketResponseData::Modeling { resp: OkWebSocketResponseData::Modeling {

View File

@ -1,12 +1,22 @@
//! Functions for setting up our WebSocket and WebRTC connections for communications with the //! Functions for setting up our WebSocket and WebRTC connections for communications with the
//! engine. //! engine.
use std::{collections::HashMap, sync::Arc}; use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use anyhow::Result; use anyhow::Result;
use indexmap::IndexMap; use indexmap::IndexMap;
use kcmc::websocket::{WebSocketRequest, WebSocketResponse}; use kcmc::{
id::ModelingCmdId,
ok_response::OkModelingCmdResponse,
websocket::{
BatchResponse, ModelingBatch, OkWebSocketResponseData, SuccessWebSocketResponse, WebSocketRequest,
WebSocketResponse,
},
ModelingCmd,
};
use kittycad_modeling_cmds as kcmc; use kittycad_modeling_cmds as kcmc;
use tokio::sync::RwLock;
use uuid::Uuid; use uuid::Uuid;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -44,11 +54,11 @@ extern "C" {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EngineConnection { pub struct EngineConnection {
manager: Arc<EngineCommandManager>, manager: Arc<EngineCommandManager>,
batch: Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>>, batch: Arc<Mutex<Vec<(WebSocketRequest, SourceRange)>>>,
batch_end: Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>, batch_end: Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>,
responses: Arc<RwLock<IndexMap<Uuid, WebSocketResponse>>>, responses: Arc<Mutex<IndexMap<Uuid, WebSocketResponse>>>,
artifact_commands: Arc<RwLock<Vec<ArtifactCommand>>>, artifact_commands: Arc<Mutex<Vec<ArtifactCommand>>>,
execution_kind: Arc<RwLock<ExecutionKind>>, execution_kind: Arc<Mutex<ExecutionKind>>,
} }
// Safety: WebAssembly will only ever run in a single-threaded context. // Safety: WebAssembly will only ever run in a single-threaded context.
@ -60,101 +70,66 @@ impl EngineConnection {
#[allow(clippy::arc_with_non_send_sync)] #[allow(clippy::arc_with_non_send_sync)]
Ok(EngineConnection { Ok(EngineConnection {
manager: Arc::new(manager), manager: Arc::new(manager),
batch: Arc::new(RwLock::new(Vec::new())), batch: Arc::new(Mutex::new(Vec::new())),
batch_end: Arc::new(RwLock::new(IndexMap::new())), batch_end: Arc::new(Mutex::new(IndexMap::new())),
responses: Arc::new(RwLock::new(IndexMap::new())), responses: Arc::new(Mutex::new(IndexMap::new())),
artifact_commands: Arc::new(RwLock::new(Vec::new())), artifact_commands: Arc::new(Mutex::new(Vec::new())),
execution_kind: Default::default(), execution_kind: Default::default(),
}) })
} }
}
async fn do_send_modeling_cmd( impl EngineConnection {
fn handle_command(
&self, &self,
id: uuid::Uuid, cmd: &ModelingCmd,
source_range: SourceRange, cmd_id: ModelingCmdId,
cmd: WebSocketRequest, id_to_source_range: &HashMap<Uuid, SourceRange>,
id_to_source_range: HashMap<uuid::Uuid, SourceRange>, ) -> Result<(), KclError> {
) -> Result<WebSocketResponse, KclError> { let cmd_id = *cmd_id.as_ref();
let source_range_str = serde_json::to_string(&source_range).map_err(|e| { let range = id_to_source_range
KclError::Engine(KclErrorDetails { .get(&cmd_id)
message: format!("Failed to serialize source range: {:?}", e), .copied()
source_ranges: vec![source_range], .ok_or_else(|| KclError::internal(format!("Failed to get source range for command ID: {:?}", cmd_id)))?;
})
})?;
let cmd_str = serde_json::to_string(&cmd).map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to serialize modeling command: {:?}", e),
source_ranges: vec![source_range],
})
})?;
let id_to_source_range_str = serde_json::to_string(&id_to_source_range).map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to serialize id to source range: {:?}", e),
source_ranges: vec![source_range],
})
})?;
let promise = self // Add artifact command.
.manager let mut artifact_commands = self.artifact_commands.lock().unwrap();
.send_modeling_cmd_from_wasm(id.to_string(), source_range_str, cmd_str, id_to_source_range_str) artifact_commands.push(ArtifactCommand {
.map_err(|e| { cmd_id,
KclError::Engine(KclErrorDetails { range,
message: e.to_string().into(), command: cmd.clone(),
source_ranges: vec![source_range], });
}) Ok(())
})?;
let value = crate::wasm::JsFuture::from(promise).await.map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to wait for promise from engine: {:?}", e),
source_ranges: vec![source_range],
})
})?;
// Parse the value as a string.
let s = value.as_string().ok_or_else(|| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to get string from response from engine: `{:?}`", value),
source_ranges: vec![source_range],
})
})?;
let ws_result: WebSocketResponse = serde_json::from_str(&s).map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to deserialize response from engine: {:?}", e),
source_ranges: vec![source_range],
})
})?;
Ok(ws_result)
} }
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl crate::engine::EngineManager for EngineConnection { impl crate::engine::EngineManager for EngineConnection {
fn batch(&self) -> Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>> { fn batch(&self) -> Arc<Mutex<Vec<(WebSocketRequest, SourceRange)>>> {
self.batch.clone() self.batch.clone()
} }
fn batch_end(&self) -> Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>> { fn batch_end(&self) -> Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>> {
self.batch_end.clone() self.batch_end.clone()
} }
fn responses(&self) -> Arc<RwLock<IndexMap<Uuid, WebSocketResponse>>> { fn responses(&self) -> IndexMap<Uuid, WebSocketResponse> {
self.responses.clone() let responses = self.responses.lock().unwrap();
responses.clone()
} }
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> { fn take_artifact_commands(&self) -> Vec<ArtifactCommand> {
self.artifact_commands.clone() let mut artifact_commands = self.artifact_commands.lock().unwrap();
std::mem::take(&mut *artifact_commands)
} }
async fn execution_kind(&self) -> ExecutionKind { fn execution_kind(&self) -> ExecutionKind {
let guard = self.execution_kind.read().await; let guard = self.execution_kind.lock().unwrap();
*guard *guard
} }
async fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind { fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind {
let mut guard = self.execution_kind.write().await; let mut guard = self.execution_kind.lock().unwrap();
let original = *guard; let original = *guard;
*guard = execution_kind; *guard = execution_kind;
original original
@ -239,18 +214,100 @@ impl crate::engine::EngineManager for EngineConnection {
cmd: WebSocketRequest, cmd: WebSocketRequest,
id_to_source_range: HashMap<uuid::Uuid, SourceRange>, id_to_source_range: HashMap<uuid::Uuid, SourceRange>,
) -> Result<WebSocketResponse, KclError> { ) -> Result<WebSocketResponse, KclError> {
let ws_result = self match &cmd {
.do_send_modeling_cmd(id, source_range, cmd, id_to_source_range) WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { requests, .. }) => {
.await?; for request in requests {
self.handle_command(&request.cmd, request.cmd_id, &id_to_source_range)?;
// In isolated mode, we don't save the response. }
if self.execution_kind().await.is_isolated() { }
return Ok(ws_result); WebSocketRequest::ModelingCmdReq(request) => {
self.handle_command(&request.cmd, request.cmd_id, &id_to_source_range)?;
}
_ => {}
} }
let mut responses = self.responses.write().await; // In isolated mode, we don't send the command to the engine.
if self.execution_kind().is_isolated() {
return match &cmd {
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { requests, .. }) => {
let mut responses = HashMap::with_capacity(requests.len());
for request in requests {
responses.insert(
request.cmd_id,
BatchResponse::Success {
response: OkModelingCmdResponse::Empty {},
},
);
}
Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
request_id: Some(id),
resp: OkWebSocketResponseData::ModelingBatch { responses },
success: true,
}))
}
_ => Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
request_id: Some(id),
resp: OkWebSocketResponseData::Modeling {
modeling_response: OkModelingCmdResponse::Empty {},
},
success: true,
})),
};
}
let source_range_str = serde_json::to_string(&source_range).map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to serialize source range: {:?}", e),
source_ranges: vec![source_range],
})
})?;
let cmd_str = serde_json::to_string(&cmd).map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to serialize modeling command: {:?}", e),
source_ranges: vec![source_range],
})
})?;
let id_to_source_range_str = serde_json::to_string(&id_to_source_range).map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to serialize id to source range: {:?}", e),
source_ranges: vec![source_range],
})
})?;
let promise = self
.manager
.send_modeling_cmd_from_wasm(id.to_string(), source_range_str, cmd_str, id_to_source_range_str)
.map_err(|e| {
KclError::Engine(KclErrorDetails {
message: e.to_string().into(),
source_ranges: vec![source_range],
})
})?;
let value = crate::wasm::JsFuture::from(promise).await.map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to wait for promise from engine: {:?}", e),
source_ranges: vec![source_range],
})
})?;
// Parse the value as a string.
let s = value.as_string().ok_or_else(|| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to get string from response from engine: `{:?}`", value),
source_ranges: vec![source_range],
})
})?;
let ws_result: WebSocketResponse = serde_json::from_str(&s).map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to deserialize response from engine: {:?}", e),
source_ranges: vec![source_range],
})
})?;
let mut responses = self.responses.lock().unwrap();
responses.insert(id, ws_result.clone()); responses.insert(id, ws_result.clone());
drop(responses);
Ok(ws_result) Ok(ws_result)
} }

View File

@ -8,12 +8,14 @@ pub mod conn_mock;
#[cfg(feature = "engine")] #[cfg(feature = "engine")]
pub mod conn_wasm; pub mod conn_wasm;
use std::{collections::HashMap, sync::Arc}; use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use indexmap::IndexMap; use indexmap::IndexMap;
use kcmc::{ use kcmc::{
each_cmd as mcmd, each_cmd as mcmd,
id::ModelingCmdId,
length_unit::LengthUnit, length_unit::LengthUnit,
ok_response::OkModelingCmdResponse, ok_response::OkModelingCmdResponse,
shared::Color, shared::Color,
@ -26,7 +28,6 @@ use kcmc::{
use kittycad_modeling_cmds as kcmc; use kittycad_modeling_cmds as kcmc;
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
@ -61,38 +62,28 @@ impl ExecutionKind {
#[async_trait::async_trait] #[async_trait::async_trait]
pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static { pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
/// Get the batch of commands to be sent to the engine. /// Get the batch of commands to be sent to the engine.
fn batch(&self) -> Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>>; fn batch(&self) -> Arc<Mutex<Vec<(WebSocketRequest, SourceRange)>>>;
/// Get the batch of end commands to be sent to the engine. /// Get the batch of end commands to be sent to the engine.
fn batch_end(&self) -> Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>; fn batch_end(&self) -> Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>;
/// Get the command responses from the engine. /// Get the command responses from the engine.
fn responses(&self) -> Arc<RwLock<IndexMap<Uuid, WebSocketResponse>>>; fn responses(&self) -> IndexMap<Uuid, WebSocketResponse>;
/// Get the artifact commands that have accumulated so far. /// Take the artifact commands generated up to this point and clear them.
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>>; fn take_artifact_commands(&self) -> Vec<ArtifactCommand>;
/// Clear all artifact commands that have accumulated so far. /// Clear all artifact commands that have accumulated so far.
async fn clear_artifact_commands(&self) { fn clear_artifact_commands(&self) {
self.artifact_commands().write().await.clear(); self.take_artifact_commands();
}
/// Take the artifact commands that have accumulated so far and clear them.
async fn take_artifact_commands(&self) -> Vec<ArtifactCommand> {
std::mem::take(&mut *self.artifact_commands().write().await)
}
/// Take the responses that have accumulated so far and clear them.
async fn take_responses(&self) -> IndexMap<Uuid, WebSocketResponse> {
std::mem::take(&mut *self.responses().write().await)
} }
/// Get the current execution kind. /// Get the current execution kind.
async fn execution_kind(&self) -> ExecutionKind; fn execution_kind(&self) -> ExecutionKind;
/// Replace the current execution kind with a new value and return the /// Replace the current execution kind with a new value and return the
/// existing value. /// existing value.
async fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind; fn replace_execution_kind(&self, execution_kind: ExecutionKind) -> ExecutionKind;
/// Get the default planes. /// Get the default planes.
async fn default_planes( async fn default_planes(
@ -136,7 +127,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
// Ensure artifact commands are cleared so that we don't accumulate them // Ensure artifact commands are cleared so that we don't accumulate them
// across runs. // across runs.
self.clear_artifact_commands().await; self.clear_artifact_commands();
// Do the after clear scene hook. // Do the after clear scene hook.
self.clear_scene_post_hook(id_generator, source_range).await?; self.clear_scene_post_hook(id_generator, source_range).await?;
@ -160,27 +151,6 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
Ok(()) Ok(())
} }
async fn handle_artifact_command(
&self,
cmd: &ModelingCmd,
cmd_id: ModelingCmdId,
id_to_source_range: &HashMap<Uuid, SourceRange>,
) -> Result<(), KclError> {
let cmd_id = *cmd_id.as_ref();
let range = id_to_source_range
.get(&cmd_id)
.copied()
.ok_or_else(|| KclError::internal(format!("Failed to get source range for command ID: {:?}", cmd_id)))?;
// Add artifact command.
self.artifact_commands().write().await.push(ArtifactCommand {
cmd_id,
range,
command: cmd.clone(),
});
Ok(())
}
async fn set_units( async fn set_units(
&self, &self,
units: crate::UnitLength, units: crate::UnitLength,
@ -227,18 +197,13 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
source_range: SourceRange, source_range: SourceRange,
cmd: &ModelingCmd, cmd: &ModelingCmd,
) -> Result<(), crate::errors::KclError> { ) -> Result<(), crate::errors::KclError> {
// In isolated mode, we don't send the command to the engine.
if self.execution_kind().await.is_isolated() {
return Ok(());
}
let req = WebSocketRequest::ModelingCmdReq(ModelingCmdReq { let req = WebSocketRequest::ModelingCmdReq(ModelingCmdReq {
cmd: cmd.clone(), cmd: cmd.clone(),
cmd_id: id.into(), cmd_id: id.into(),
}); });
// Add cmd to the batch. // Add cmd to the batch.
self.batch().write().await.push((req, source_range)); self.batch().lock().unwrap().push((req, source_range));
Ok(()) Ok(())
} }
@ -252,18 +217,13 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
source_range: SourceRange, source_range: SourceRange,
cmd: &ModelingCmd, cmd: &ModelingCmd,
) -> Result<(), crate::errors::KclError> { ) -> Result<(), crate::errors::KclError> {
// In isolated mode, we don't send the command to the engine.
if self.execution_kind().await.is_isolated() {
return Ok(());
}
let req = WebSocketRequest::ModelingCmdReq(ModelingCmdReq { let req = WebSocketRequest::ModelingCmdReq(ModelingCmdReq {
cmd: cmd.clone(), cmd: cmd.clone(),
cmd_id: id.into(), cmd_id: id.into(),
}); });
// Add cmd to the batch end. // Add cmd to the batch end.
self.batch_end().write().await.insert(id, (req, source_range)); self.batch_end().lock().unwrap().insert(id, (req, source_range));
Ok(()) Ok(())
} }
@ -289,11 +249,11 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
source_range: SourceRange, source_range: SourceRange,
) -> Result<OkWebSocketResponseData, crate::errors::KclError> { ) -> Result<OkWebSocketResponseData, crate::errors::KclError> {
let all_requests = if batch_end { let all_requests = if batch_end {
let mut requests = self.batch().read().await.clone(); let mut requests = self.batch().lock().unwrap().clone();
requests.extend(self.batch_end().read().await.values().cloned()); requests.extend(self.batch_end().lock().unwrap().values().cloned());
requests requests
} else { } else {
self.batch().read().await.clone() self.batch().lock().unwrap().clone()
}; };
// Return early if we have no commands to send. // Return early if we have no commands to send.
@ -344,27 +304,10 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
} }
} }
// Do the artifact commands.
for (req, _) in all_requests.iter() {
match &req {
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { requests, .. }) => {
for request in requests {
self.handle_artifact_command(&request.cmd, request.cmd_id, &id_to_source_range)
.await?;
}
}
WebSocketRequest::ModelingCmdReq(request) => {
self.handle_artifact_command(&request.cmd, request.cmd_id, &id_to_source_range)
.await?;
}
_ => {}
}
}
// Throw away the old batch queue. // Throw away the old batch queue.
self.batch().write().await.clear(); self.batch().lock().unwrap().clear();
if batch_end { if batch_end {
self.batch_end().write().await.clear(); self.batch_end().lock().unwrap().clear();
} }
// We pop off the responses to cleanup our mappings. // We pop off the responses to cleanup our mappings.
@ -653,7 +596,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
/// Get session data, if it has been received. /// Get session data, if it has been received.
/// Returns None if the server never sent it. /// Returns None if the server never sent it.
async fn get_session_data(&self) -> Option<ModelingSessionData> { fn get_session_data(&self) -> Option<ModelingSessionData> {
None None
} }

View File

@ -9,8 +9,8 @@ use crate::{
execution::{ execution::{
annotations, annotations,
cad_op::{OpArg, Operation}, cad_op::{OpArg, Operation},
kcl_value::NumericType,
memory, memory,
memory::ProgramMemory,
state::ModuleState, state::ModuleState,
BodyType, EnvironmentRef, ExecState, ExecutorContext, KclValue, MemoryFunction, Metadata, TagEngineInfo, BodyType, EnvironmentRef, ExecState, ExecutorContext, KclValue, MemoryFunction, Metadata, TagEngineInfo,
TagIdentifier, TagIdentifier,
@ -48,7 +48,7 @@ impl ExecutorContext {
let old_units = exec_state.length_unit(); let old_units = exec_state.length_unit();
exec_state.mod_local.settings.update_from_annotation(annotation)?; exec_state.mod_local.settings.update_from_annotation(annotation)?;
let new_units = exec_state.length_unit(); let new_units = exec_state.length_unit();
if !self.engine.execution_kind().await.is_isolated() && old_units != new_units { if !self.engine.execution_kind().is_isolated() && old_units != new_units {
self.engine self.engine
.set_units(new_units.into(), annotation.as_source_range()) .set_units(new_units.into(), annotation.as_source_range())
.await?; .await?;
@ -393,7 +393,7 @@ impl ExecutorContext {
exec_state.global.mod_loader.enter_module(path); exec_state.global.mod_loader.enter_module(path);
std::mem::swap(&mut exec_state.mod_local, &mut local_state); std::mem::swap(&mut exec_state.mod_local, &mut local_state);
exec_state.mut_memory().push_new_root_env(); exec_state.mut_memory().push_new_root_env();
let original_execution = self.engine.replace_execution_kind(exec_kind).await; let original_execution = self.engine.replace_execution_kind(exec_kind);
let result = self let result = self
.exec_program(program, exec_state, crate::execution::BodyType::Root) .exec_program(program, exec_state, crate::execution::BodyType::Root)
@ -406,7 +406,7 @@ impl ExecutorContext {
if !exec_kind.is_isolated() && new_units != old_units { if !exec_kind.is_isolated() && new_units != old_units {
self.engine.set_units(old_units.into(), Default::default()).await?; self.engine.set_units(old_units.into(), Default::default()).await?;
} }
self.engine.replace_execution_kind(original_execution).await; self.engine.replace_execution_kind(original_execution);
result result
.map_err(|err| { .map_err(|err| {
@ -437,7 +437,7 @@ impl ExecutorContext {
) -> Result<KclValue, KclError> { ) -> Result<KclValue, KclError> {
let item = match init { let item = match init {
Expr::None(none) => KclValue::from(none), Expr::None(none) => KclValue::from(none),
Expr::Literal(literal) => KclValue::from_literal((**literal).clone(), &exec_state.mod_local.settings), Expr::Literal(literal) => KclValue::from(literal),
Expr::TagDeclarator(tag) => tag.execute(exec_state).await?, Expr::TagDeclarator(tag) => tag.execute(exec_state).await?,
Expr::Identifier(identifier) => { Expr::Identifier(identifier) => {
let value = exec_state.memory().get(&identifier.name, identifier.into())?.clone(); let value = exec_state.memory().get(&identifier.name, identifier.into())?.clone();
@ -518,10 +518,7 @@ impl BinaryPart {
#[async_recursion] #[async_recursion]
pub async fn get_result(&self, exec_state: &mut ExecState, ctx: &ExecutorContext) -> Result<KclValue, KclError> { pub async fn get_result(&self, exec_state: &mut ExecState, ctx: &ExecutorContext) -> Result<KclValue, KclError> {
match self { match self {
BinaryPart::Literal(literal) => Ok(KclValue::from_literal( BinaryPart::Literal(literal) => Ok(literal.into()),
(**literal).clone(),
&exec_state.mod_local.settings,
)),
BinaryPart::Identifier(identifier) => { BinaryPart::Identifier(identifier) => {
let value = exec_state.memory().get(&identifier.name, identifier.into())?; let value = exec_state.memory().get(&identifier.name, identifier.into())?;
Ok(value.clone()) Ok(value.clone())
@ -707,32 +704,26 @@ impl Node<BinaryExpression> {
BinaryOperator::Add => KclValue::Number { BinaryOperator::Add => KclValue::Number {
value: left + right, value: left + right,
meta, meta,
ty: NumericType::Unknown,
}, },
BinaryOperator::Sub => KclValue::Number { BinaryOperator::Sub => KclValue::Number {
value: left - right, value: left - right,
meta, meta,
ty: NumericType::Unknown,
}, },
BinaryOperator::Mul => KclValue::Number { BinaryOperator::Mul => KclValue::Number {
value: left * right, value: left * right,
meta, meta,
ty: NumericType::Unknown,
}, },
BinaryOperator::Div => KclValue::Number { BinaryOperator::Div => KclValue::Number {
value: left / right, value: left / right,
meta, meta,
ty: NumericType::Unknown,
}, },
BinaryOperator::Mod => KclValue::Number { BinaryOperator::Mod => KclValue::Number {
value: left % right, value: left % right,
meta, meta,
ty: NumericType::Unknown,
}, },
BinaryOperator::Pow => KclValue::Number { BinaryOperator::Pow => KclValue::Number {
value: left.powf(right), value: left.powf(right),
meta, meta,
ty: NumericType::Unknown,
}, },
BinaryOperator::Neq => KclValue::Bool { BinaryOperator::Neq => KclValue::Bool {
value: left != right, value: left != right,
@ -795,14 +786,19 @@ impl Node<UnaryExpression> {
let value = &self.argument.get_result(exec_state, ctx).await?; let value = &self.argument.get_result(exec_state, ctx).await?;
match value { match value {
KclValue::Number { value, ty, .. } => { KclValue::Number { value, meta: _ } => {
let meta = vec![Metadata {
source_range: self.into(),
}];
Ok(KclValue::Number { value: -value, meta })
}
KclValue::Int { value, meta: _ } => {
let meta = vec![Metadata { let meta = vec![Metadata {
source_range: self.into(), source_range: self.into(),
}]; }];
Ok(KclValue::Number { Ok(KclValue::Number {
value: -value, value: (-value) as f64,
meta, meta,
ty: ty.clone(),
}) })
} }
_ => Err(KclError::Semantic(KclErrorDetails { _ => Err(KclError::Semantic(KclErrorDetails {
@ -1303,9 +1299,8 @@ impl Node<ArrayRangeExpression> {
Ok(KclValue::Array { Ok(KclValue::Array {
value: range value: range
.into_iter() .into_iter()
.map(|num| KclValue::Number { .map(|num| KclValue::Int {
value: num as f64, value: num,
ty: NumericType::Unknown,
meta: meta.clone(), meta: meta.clone(),
}) })
.collect(), .collect(),
@ -1347,6 +1342,8 @@ fn article_for(s: &str) -> &'static str {
pub fn parse_number_as_f64(v: &KclValue, source_range: SourceRange) -> Result<f64, KclError> { pub fn parse_number_as_f64(v: &KclValue, source_range: SourceRange) -> Result<f64, KclError> {
if let KclValue::Number { value: n, .. } = &v { if let KclValue::Number { value: n, .. } = &v {
Ok(*n) Ok(*n)
} else if let KclValue::Int { value: n, .. } = &v {
Ok(*n as f64)
} else { } else {
let actual_type = v.human_friendly_type(); let actual_type = v.human_friendly_type();
let article = if actual_type.starts_with(['a', 'e', 'i', 'o', 'u']) { let article = if actual_type.starts_with(['a', 'e', 'i', 'o', 'u']) {
@ -1463,7 +1460,16 @@ fn jvalue_to_prop(value: &KclValue, property_sr: Vec<SourceRange>, name: &str) -
})) }))
}; };
match value { match value {
KclValue::Number{value: num, .. } => { KclValue::Int { value:num, meta: _ } => {
let maybe_int: Result<usize, _> = (*num).try_into();
if let Ok(uint) = maybe_int {
Ok(Property::UInt(uint))
}
else {
make_err(format!("'{num}' is negative, so you can't index an array with it"))
}
}
KclValue::Number{value: num, meta:_} => {
let num = *num; let num = *num;
if num < 0.0 { if num < 0.0 {
return make_err(format!("'{num}' is negative, so you can't index an array with it")) return make_err(format!("'{num}' is negative, so you can't index an array with it"))
@ -1504,7 +1510,7 @@ impl Node<PipeExpression> {
fn assign_args_to_params( fn assign_args_to_params(
function_expression: NodeRef<'_, FunctionExpression>, function_expression: NodeRef<'_, FunctionExpression>,
args: Vec<Arg>, args: Vec<Arg>,
exec_state: &mut ExecState, fn_memory: &mut ProgramMemory,
) -> Result<(), KclError> { ) -> Result<(), KclError> {
let num_args = function_expression.number_of_args(); let num_args = function_expression.number_of_args();
let (min_params, max_params) = num_args.into_inner(); let (min_params, max_params) = num_args.into_inner();
@ -1524,15 +1530,12 @@ fn assign_args_to_params(
return Err(err_wrong_number_args); return Err(err_wrong_number_args);
} }
let mem = &mut exec_state.global.memory;
let settings = &exec_state.mod_local.settings;
// Add the arguments to the memory. A new call frame should have already // Add the arguments to the memory. A new call frame should have already
// been created. // been created.
for (index, param) in function_expression.params.iter().enumerate() { for (index, param) in function_expression.params.iter().enumerate() {
if let Some(arg) = args.get(index) { if let Some(arg) = args.get(index) {
// Argument was provided. // Argument was provided.
mem.add( fn_memory.add(
param.identifier.name.clone(), param.identifier.name.clone(),
arg.value.clone(), arg.value.clone(),
(&param.identifier).into(), (&param.identifier).into(),
@ -1542,9 +1545,9 @@ fn assign_args_to_params(
if let Some(ref default_val) = param.default_value { if let Some(ref default_val) = param.default_value {
// If the corresponding parameter is optional, // If the corresponding parameter is optional,
// then it's fine, the user doesn't need to supply it. // then it's fine, the user doesn't need to supply it.
mem.add( fn_memory.add(
param.identifier.name.clone(), param.identifier.name.clone(),
KclValue::from_default_param(default_val.clone(), settings), default_val.clone().into(),
(&param.identifier).into(), (&param.identifier).into(),
)?; )?;
} else { } else {
@ -1560,21 +1563,18 @@ fn assign_args_to_params(
fn assign_args_to_params_kw( fn assign_args_to_params_kw(
function_expression: NodeRef<'_, FunctionExpression>, function_expression: NodeRef<'_, FunctionExpression>,
mut args: crate::std::args::KwArgs, mut args: crate::std::args::KwArgs,
exec_state: &mut ExecState, fn_memory: &mut ProgramMemory,
) -> Result<(), KclError> { ) -> Result<(), KclError> {
// Add the arguments to the memory. A new call frame should have already // Add the arguments to the memory. A new call frame should have already
// been created. // been created.
let source_ranges = vec![function_expression.into()]; let source_ranges = vec![function_expression.into()];
let mem = &mut exec_state.global.memory;
let settings = &exec_state.mod_local.settings;
for param in function_expression.params.iter() { for param in function_expression.params.iter() {
if param.labeled { if param.labeled {
let arg = args.labeled.get(&param.identifier.name); let arg = args.labeled.get(&param.identifier.name);
let arg_val = match arg { let arg_val = match arg {
Some(arg) => arg.value.clone(), Some(arg) => arg.value.clone(),
None => match param.default_value { None => match param.default_value {
Some(ref default_val) => KclValue::from_default_param(default_val.clone(), settings), Some(ref default_val) => KclValue::from(default_val.clone()),
None => { None => {
return Err(KclError::Semantic(KclErrorDetails { return Err(KclError::Semantic(KclErrorDetails {
source_ranges, source_ranges,
@ -1586,7 +1586,7 @@ fn assign_args_to_params_kw(
} }
}, },
}; };
mem.add(param.identifier.name.clone(), arg_val, (&param.identifier).into())?; fn_memory.add(param.identifier.name.clone(), arg_val, (&param.identifier).into())?;
} else { } else {
let Some(unlabeled) = args.unlabeled.take() else { let Some(unlabeled) = args.unlabeled.take() else {
let param_name = &param.identifier.name; let param_name = &param.identifier.name;
@ -1603,7 +1603,7 @@ fn assign_args_to_params_kw(
}) })
}); });
}; };
mem.add( fn_memory.add(
param.identifier.name.clone(), param.identifier.name.clone(),
unlabeled.value.clone(), unlabeled.value.clone(),
(&param.identifier).into(), (&param.identifier).into(),
@ -1624,7 +1624,7 @@ pub(crate) async fn call_user_defined_function(
// variables shadow variables in the parent scope. The new environment's // variables shadow variables in the parent scope. The new environment's
// parent should be the environment of the closure. // parent should be the environment of the closure.
exec_state.mut_memory().push_new_env_for_call(memory); exec_state.mut_memory().push_new_env_for_call(memory);
if let Err(e) = assign_args_to_params(function_expression, args, exec_state) { if let Err(e) = assign_args_to_params(function_expression, args, exec_state.mut_memory()) {
exec_state.mut_memory().pop_env(); exec_state.mut_memory().pop_env();
return Err(e); return Err(e);
} }
@ -1657,7 +1657,7 @@ pub(crate) async fn call_user_defined_function_kw(
// variables shadow variables in the parent scope. The new environment's // variables shadow variables in the parent scope. The new environment's
// parent should be the environment of the closure. // parent should be the environment of the closure.
exec_state.mut_memory().push_new_env_for_call(memory); exec_state.mut_memory().push_new_env_for_call(memory);
if let Err(e) = assign_args_to_params_kw(function_expression, args, exec_state) { if let Err(e) = assign_args_to_params_kw(function_expression, args, exec_state.mut_memory()) {
exec_state.mut_memory().pop_env(); exec_state.mut_memory().pop_env();
return Err(e); return Err(e);
} }
@ -1720,19 +1720,19 @@ impl JsonSchema for FunctionParam<'_> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*;
use crate::{ use crate::{
execution::{memory::ProgramMemory, parse_execute}, execution::parse_execute,
parsing::ast::types::{DefaultParamVal, Identifier, Parameter}, parsing::ast::types::{DefaultParamVal, Identifier, Parameter},
}; };
use super::*;
#[test] #[test]
fn test_assign_args_to_params() { fn test_assign_args_to_params() {
// Set up a little framework for this test. // Set up a little framework for this test.
fn mem(number: usize) -> KclValue { fn mem(number: usize) -> KclValue {
KclValue::Number { KclValue::Int {
value: number as f64, value: number as i64,
ty: NumericType::count(),
meta: Default::default(), meta: Default::default(),
} }
} }
@ -1838,8 +1838,8 @@ mod test {
digest: None, digest: None,
}); });
let args = args.into_iter().map(Arg::synthetic).collect(); let args = args.into_iter().map(Arg::synthetic).collect();
let mut exec_state = ExecState::new(&Default::default()); let mut actual = ProgramMemory::new();
let actual = assign_args_to_params(func_expr, args, &mut exec_state).map(|_| exec_state.global.memory); let actual = assign_args_to_params(func_expr, args, &mut actual).map(|_| actual);
assert_eq!( assert_eq!(
actual, expected, actual, expected,
"failed test '{test_name}':\ngot {actual:?}\nbut expected\n{expected:?}" "failed test '{test_name}':\ngot {actual:?}\nbut expected\n{expected:?}"

View File

@ -289,7 +289,7 @@ pub struct PreImportedGeometry {
} }
pub async fn send_to_engine(pre: PreImportedGeometry, ctxt: &ExecutorContext) -> Result<ImportedGeometry, KclError> { pub async fn send_to_engine(pre: PreImportedGeometry, ctxt: &ExecutorContext) -> Result<ImportedGeometry, KclError> {
if ctxt.no_engine_commands().await { if ctxt.no_engine_commands() {
return Ok(ImportedGeometry { return Ok(ImportedGeometry {
id: pre.id, id: pre.id,
value: pre.command.files.iter().map(|f| f.path.to_string()).collect(), value: pre.command.files.iter().map(|f| f.path.to_string()).collect(),

View File

@ -12,16 +12,14 @@ use crate::{
TagIdentifier, TagIdentifier,
}, },
parsing::{ parsing::{
ast::types::{ ast::types::{FunctionExpression, KclNone, LiteralValue, TagDeclarator, TagNode},
DefaultParamVal, FunctionExpression, KclNone, Literal, LiteralValue, Node, TagDeclarator, TagNode,
},
token::NumericSuffix, token::NumericSuffix,
}, },
std::{args::Arg, FnAsArg}, std::{args::Arg, FnAsArg},
ExecutorContext, KclError, ModuleId, SourceRange, ExecutorContext, KclError, ModuleId, SourceRange,
}; };
use super::{memory::EnvironmentRef, MetaSettings}; use super::memory::EnvironmentRef;
pub type KclObjectFields = HashMap<String, KclValue>; pub type KclObjectFields = HashMap<String, KclValue>;
@ -42,7 +40,11 @@ pub enum KclValue {
}, },
Number { Number {
value: f64, value: f64,
ty: NumericType, #[serde(rename = "__meta")]
meta: Vec<Metadata>,
},
Int {
value: i64,
#[serde(rename = "__meta")] #[serde(rename = "__meta")]
meta: Vec<Metadata>, meta: Vec<Metadata>,
}, },
@ -166,6 +168,7 @@ impl From<KclValue> for Vec<SourceRange> {
KclValue::Face { value } => to_vec_sr(&value.meta), KclValue::Face { value } => to_vec_sr(&value.meta),
KclValue::Bool { meta, .. } => to_vec_sr(&meta), KclValue::Bool { meta, .. } => to_vec_sr(&meta),
KclValue::Number { meta, .. } => to_vec_sr(&meta), KclValue::Number { meta, .. } => to_vec_sr(&meta),
KclValue::Int { meta, .. } => to_vec_sr(&meta),
KclValue::String { meta, .. } => to_vec_sr(&meta), KclValue::String { meta, .. } => to_vec_sr(&meta),
KclValue::Array { meta, .. } => to_vec_sr(&meta), KclValue::Array { meta, .. } => to_vec_sr(&meta),
KclValue::Object { meta, .. } => to_vec_sr(&meta), KclValue::Object { meta, .. } => to_vec_sr(&meta),
@ -197,6 +200,7 @@ impl From<&KclValue> for Vec<SourceRange> {
KclValue::Face { value } => to_vec_sr(&value.meta), KclValue::Face { value } => to_vec_sr(&value.meta),
KclValue::Bool { meta, .. } => to_vec_sr(meta), KclValue::Bool { meta, .. } => to_vec_sr(meta),
KclValue::Number { meta, .. } => to_vec_sr(meta), KclValue::Number { meta, .. } => to_vec_sr(meta),
KclValue::Int { meta, .. } => to_vec_sr(meta),
KclValue::String { meta, .. } => to_vec_sr(meta), KclValue::String { meta, .. } => to_vec_sr(meta),
KclValue::Uuid { meta, .. } => to_vec_sr(meta), KclValue::Uuid { meta, .. } => to_vec_sr(meta),
KclValue::Array { meta, .. } => to_vec_sr(meta), KclValue::Array { meta, .. } => to_vec_sr(meta),
@ -213,7 +217,8 @@ impl KclValue {
match self { match self {
KclValue::Uuid { value: _, meta } => meta.clone(), KclValue::Uuid { value: _, meta } => meta.clone(),
KclValue::Bool { value: _, meta } => meta.clone(), KclValue::Bool { value: _, meta } => meta.clone(),
KclValue::Number { meta, .. } => meta.clone(), KclValue::Number { value: _, meta } => meta.clone(),
KclValue::Int { value: _, meta } => meta.clone(),
KclValue::String { value: _, meta } => meta.clone(), KclValue::String { value: _, meta } => meta.clone(),
KclValue::Array { value: _, meta } => meta.clone(), KclValue::Array { value: _, meta } => meta.clone(),
KclValue::Object { value: _, meta } => meta.clone(), KclValue::Object { value: _, meta } => meta.clone(),
@ -292,6 +297,7 @@ impl KclValue {
KclValue::Face { .. } => "Face", KclValue::Face { .. } => "Face",
KclValue::Bool { .. } => "boolean (true/false value)", KclValue::Bool { .. } => "boolean (true/false value)",
KclValue::Number { .. } => "number", KclValue::Number { .. } => "number",
KclValue::Int { .. } => "integer",
KclValue::String { .. } => "string (text)", KclValue::String { .. } => "string (text)",
KclValue::Array { .. } => "array (list)", KclValue::Array { .. } => "array (list)",
KclValue::Object { .. } => "object", KclValue::Object { .. } => "object",
@ -301,29 +307,14 @@ impl KclValue {
} }
} }
pub(crate) fn from_literal(literal: Node<Literal>, settings: &MetaSettings) -> Self { pub(crate) fn from_literal(literal: LiteralValue, meta: Vec<Metadata>) -> Self {
let meta = vec![literal.metadata()]; match literal {
match literal.inner.value { LiteralValue::Number { value, .. } => KclValue::Number { value, meta },
LiteralValue::Number { value, suffix } => KclValue::Number {
value,
meta,
ty: NumericType::from_parsed(suffix, settings),
},
LiteralValue::String(value) => KclValue::String { value, meta }, LiteralValue::String(value) => KclValue::String { value, meta },
LiteralValue::Bool(value) => KclValue::Bool { value, meta }, LiteralValue::Bool(value) => KclValue::Bool { value, meta },
} }
} }
pub(crate) fn from_default_param(param: DefaultParamVal, settings: &MetaSettings) -> Self {
match param {
DefaultParamVal::Literal(lit) => Self::from_literal(lit, settings),
DefaultParamVal::KclNone(none) => KclValue::KclNone {
value: none,
meta: Default::default(),
},
}
}
pub(crate) fn map_env_ref(&self, env_map: &HashMap<EnvironmentRef, EnvironmentRef>) -> Self { pub(crate) fn map_env_ref(&self, env_map: &HashMap<EnvironmentRef, EnvironmentRef>) -> Self {
let mut result = self.clone(); let mut result = self.clone();
if let KclValue::Function { ref mut memory, .. } = result { if let KclValue::Function { ref mut memory, .. } = result {
@ -336,30 +327,20 @@ impl KclValue {
/// Put the number into a KCL value. /// Put the number into a KCL value.
pub const fn from_number(f: f64, meta: Vec<Metadata>) -> Self { pub const fn from_number(f: f64, meta: Vec<Metadata>) -> Self {
Self::Number { Self::Number { value: f, meta }
value: f,
meta,
ty: NumericType::Unknown,
}
}
pub const fn from_number_with_type(f: f64, ty: NumericType, meta: Vec<Metadata>) -> Self {
Self::Number { value: f, meta, ty }
} }
/// Put the point into a KCL value. /// Put the point into a KCL value.
pub fn from_point2d(p: [f64; 2], ty: NumericType, meta: Vec<Metadata>) -> Self { pub fn from_point2d(p: [f64; 2], meta: Vec<Metadata>) -> Self {
Self::Array { Self::Array {
value: vec![ value: vec![
Self::Number { Self::Number {
value: p[0], value: p[0],
meta: meta.clone(), meta: meta.clone(),
ty: ty.clone(),
}, },
Self::Number { Self::Number {
value: p[1], value: p[1],
meta: meta.clone(), meta: meta.clone(),
ty,
}, },
], ],
meta, meta,
@ -368,6 +349,7 @@ impl KclValue {
pub(crate) fn as_usize(&self) -> Option<usize> { pub(crate) fn as_usize(&self) -> Option<usize> {
match self { match self {
KclValue::Int { value, .. } if *value > 0 => Some(*value as usize),
KclValue::Number { value, .. } => crate::try_f64_to_usize(*value), KclValue::Number { value, .. } => crate::try_f64_to_usize(*value),
_ => None, _ => None,
} }
@ -375,6 +357,7 @@ impl KclValue {
pub fn as_int(&self) -> Option<i64> { pub fn as_int(&self) -> Option<i64> {
match self { match self {
KclValue::Int { value, .. } => Some(*value),
KclValue::Number { value, .. } => crate::try_f64_to_i64(*value), KclValue::Number { value, .. } => crate::try_f64_to_i64(*value),
_ => None, _ => None,
} }
@ -455,8 +438,10 @@ impl KclValue {
} }
pub fn as_f64(&self) -> Option<f64> { pub fn as_f64(&self) -> Option<f64> {
if let KclValue::Number { value, .. } = &self { if let KclValue::Number { value, meta: _ } = &self {
Some(*value) Some(*value)
} else if let KclValue::Int { value, meta: _ } = &self {
Some(*value as f64)
} else { } else {
None None
} }
@ -621,73 +606,6 @@ impl KclValue {
} }
} }
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(tag = "type")]
pub enum NumericType {
// Specified by the user (directly or indirectly)
Known(UnitType),
// Unspecified, using defaults
Default { len: UnitLen, angle: UnitAngle },
// Exceeded the ability of the type system to track.
Unknown,
// Type info has been explicitly cast away.
Any,
}
impl NumericType {
pub fn count() -> Self {
NumericType::Known(UnitType::Count)
}
pub fn combine(self, other: &NumericType) -> NumericType {
if &self == other {
self
} else {
NumericType::Unknown
}
}
pub fn from_parsed(suffix: NumericSuffix, settings: &super::MetaSettings) -> Self {
match suffix {
NumericSuffix::None => NumericType::Default {
len: settings.default_length_units,
angle: settings.default_angle_units,
},
NumericSuffix::Count => NumericType::Known(UnitType::Count),
NumericSuffix::Mm => NumericType::Known(UnitType::Length(UnitLen::Mm)),
NumericSuffix::Cm => NumericType::Known(UnitType::Length(UnitLen::Cm)),
NumericSuffix::M => NumericType::Known(UnitType::Length(UnitLen::M)),
NumericSuffix::Inch => NumericType::Known(UnitType::Length(UnitLen::Inches)),
NumericSuffix::Ft => NumericType::Known(UnitType::Length(UnitLen::Feet)),
NumericSuffix::Yd => NumericType::Known(UnitType::Length(UnitLen::Yards)),
NumericSuffix::Deg => NumericType::Known(UnitType::Angle(UnitAngle::Degrees)),
NumericSuffix::Rad => NumericType::Known(UnitType::Angle(UnitAngle::Radians)),
}
}
}
impl From<UnitLen> for NumericType {
fn from(value: UnitLen) -> Self {
NumericType::Known(UnitType::Length(value))
}
}
impl From<UnitAngle> for NumericType {
fn from(value: UnitAngle) -> Self {
NumericType::Known(UnitType::Angle(value))
}
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(tag = "type")]
pub enum UnitType {
Count,
Length(UnitLen),
Angle(UnitAngle),
}
// TODO called UnitLen so as not to clash with UnitLength in settings) // TODO called UnitLen so as not to clash with UnitLength in settings)
#[derive(Debug, Default, Clone, Copy, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Eq)] #[derive(Debug, Default, Clone, Copy, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Eq)]
#[ts(export)] #[ts(export)]

View File

@ -845,8 +845,6 @@ mod env {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::execution::kcl_value::NumericType;
use super::*; use super::*;
fn sr() -> SourceRange { fn sr() -> SourceRange {
@ -854,9 +852,8 @@ mod test {
} }
fn val(value: i64) -> KclValue { fn val(value: i64) -> KclValue {
KclValue::Number { KclValue::Int {
value: value as f64, value,
ty: NumericType::count(),
meta: Vec::new(), meta: Vec::new(),
} }
} }
@ -864,14 +861,14 @@ mod test {
#[track_caller] #[track_caller]
fn assert_get(mem: &ProgramMemory, key: &str, n: i64) { fn assert_get(mem: &ProgramMemory, key: &str, n: i64) {
match mem.get(key, sr()).unwrap() { match mem.get(key, sr()).unwrap() {
KclValue::Number { value, .. } => assert_eq!(*value as i64, n), KclValue::Int { value, .. } => assert_eq!(*value, n),
_ => unreachable!(), _ => unreachable!(),
} }
} }
fn expect_small_number(value: &KclValue) -> Option<i64> { fn expect_int(value: &KclValue) -> Option<i64> {
match value { match value {
KclValue::Number { value, .. } if value > &0.0 && value < &10.0 => Some(*value as i64), KclValue::Int { value, .. } => Some(*value),
_ => None, _ => None,
} }
} }
@ -879,7 +876,7 @@ mod test {
#[track_caller] #[track_caller]
fn assert_get_from(mem: &ProgramMemory, key: &str, n: i64, snapshot: EnvironmentRef) { fn assert_get_from(mem: &ProgramMemory, key: &str, n: i64, snapshot: EnvironmentRef) {
match mem.get_from(key, snapshot, sr()).unwrap() { match mem.get_from(key, snapshot, sr()).unwrap() {
KclValue::Number { value, .. } => assert_eq!(*value as i64, n), KclValue::Int { value, .. } => assert_eq!(*value, n),
_ => unreachable!(), _ => unreachable!(),
} }
} }
@ -1130,7 +1127,7 @@ mod test {
assert_get_from(mem, "b", 3, sn3); assert_get_from(mem, "b", 3, sn3);
assert_get_from(mem, "b", 4, sn4); assert_get_from(mem, "b", 4, sn4);
let vals: Vec<_> = mem.walk_call_stack().filter_map(expect_small_number).collect(); let vals: Vec<_> = mem.walk_call_stack().filter_map(expect_int).collect();
let expected = [6, 1, 3, 1, 7]; let expected = [6, 1, 3, 1, 7];
assert_eq!(vals, expected); assert_eq!(vals, expected);
@ -1139,7 +1136,7 @@ mod test {
mem.get_from("b", sn1, sr()).unwrap_err(); mem.get_from("b", sn1, sr()).unwrap_err();
assert_get_from(mem, "b", 3, sn2); assert_get_from(mem, "b", 3, sn2);
let vals: Vec<_> = mem.walk_call_stack().filter_map(expect_small_number).collect(); let vals: Vec<_> = mem.walk_call_stack().filter_map(expect_int).collect();
let expected = [1, 7]; let expected = [1, 7];
assert_eq!(vals, expected); assert_eq!(vals, expected);

View File

@ -484,8 +484,8 @@ impl ExecutorContext {
} }
/// Returns true if we should not send engine commands for any reason. /// Returns true if we should not send engine commands for any reason.
pub async fn no_engine_commands(&self) -> bool { pub fn no_engine_commands(&self) -> bool {
self.is_mock() || self.engine.execution_kind().await.is_isolated() self.is_mock() || self.engine.execution_kind().is_isolated()
} }
pub async fn send_clear_scene( pub async fn send_clear_scene(
@ -713,7 +713,7 @@ impl ExecutorContext {
"Post interpretation KCL memory stats: {:#?}", "Post interpretation KCL memory stats: {:#?}",
exec_state.memory().stats exec_state.memory().stats
)); ));
let session_data = self.engine.get_session_data().await; let session_data = self.engine.get_session_data();
Ok(session_data) Ok(session_data)
} }
@ -734,11 +734,8 @@ impl ExecutorContext {
exec_state exec_state
.global .global
.artifact_commands .artifact_commands
.extend(self.engine.take_artifact_commands().await); .extend(self.engine.take_artifact_commands());
exec_state exec_state.global.artifact_responses.extend(self.engine.responses());
.global
.artifact_responses
.extend(self.engine.take_responses().await);
// Build the artifact graph. // Build the artifact graph.
match build_artifact_graph( match build_artifact_graph(
&exec_state.global.artifact_commands, &exec_state.global.artifact_commands,

View File

@ -1581,7 +1581,7 @@ pub struct CallExpression {
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", tag = "type")] #[serde(tag = "type")]
pub struct CallExpressionKw { pub struct CallExpressionKw {
pub callee: Node<Identifier>, pub callee: Node<Identifier>,
pub unlabeled: Option<Expr>, pub unlabeled: Option<Expr>,
@ -1591,9 +1591,6 @@ pub struct CallExpressionKw {
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)] #[ts(optional)]
pub digest: Option<Digest>, pub digest: Option<Digest>,
#[serde(default, skip_serializing_if = "NonCodeMeta::is_empty")]
pub non_code_meta: NonCodeMeta,
} }
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
@ -1717,7 +1714,6 @@ impl CallExpressionKw {
unlabeled, unlabeled,
arguments, arguments,
digest: None, digest: None,
non_code_meta: Default::default(),
})) }))
} }
@ -2084,6 +2080,30 @@ impl Literal {
} }
} }
impl From<Node<Literal>> for KclValue {
fn from(literal: Node<Literal>) -> Self {
let meta = vec![literal.metadata()];
match literal.inner.value {
LiteralValue::Number { value, .. } => KclValue::Number { value, meta },
LiteralValue::String(value) => KclValue::String { value, meta },
LiteralValue::Bool(value) => KclValue::Bool { value, meta },
}
}
}
impl From<&Node<Literal>> for KclValue {
fn from(literal: &Node<Literal>) -> Self {
Self::from(literal.to_owned())
}
}
impl From<&BoxNode<Literal>> for KclValue {
fn from(literal: &BoxNode<Literal>) -> Self {
let b: &Node<Literal> = literal;
Self::from(b)
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Eq)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Eq)]
#[ts(export)] #[ts(export)]
#[serde(tag = "type")] #[serde(tag = "type")]
@ -3068,7 +3088,20 @@ pub enum FnArgType {
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
pub enum DefaultParamVal { pub enum DefaultParamVal {
KclNone(KclNone), KclNone(KclNone),
Literal(Node<Literal>), Literal(Literal),
}
// TODO: This should actually take metadata.
impl From<DefaultParamVal> for KclValue {
fn from(v: DefaultParamVal) -> Self {
match v {
DefaultParamVal::KclNone(kcl_none) => Self::KclNone {
value: kcl_none,
meta: Default::default(),
},
DefaultParamVal::Literal(literal) => Self::from_literal(literal.value, Vec::new()),
}
}
} }
impl DefaultParamVal { impl DefaultParamVal {

View File

@ -882,17 +882,6 @@ fn property_separator(i: &mut TokenSlice) -> PResult<()> {
.parse_next(i) .parse_next(i)
} }
/// Match something that separates the labeled arguments of a fn call.
fn labeled_arg_separator(i: &mut TokenSlice) -> PResult<()> {
alt((
// Normally you need a comma.
comma_sep,
// But, if the argument list is ending, no need for a comma.
peek(preceded(opt(whitespace), close_paren)).void(),
))
.parse_next(i)
}
/// Parse a KCL object value. /// Parse a KCL object value.
pub(crate) fn object(i: &mut TokenSlice) -> PResult<Node<ObjectExpression>> { pub(crate) fn object(i: &mut TokenSlice) -> PResult<Node<ObjectExpression>> {
let open = open_brace(i)?; let open = open_brace(i)?;
@ -2507,6 +2496,14 @@ fn labeled_argument(i: &mut TokenSlice) -> PResult<LabeledArg> {
.parse_next(i) .parse_next(i)
} }
/// Arguments are passed into a function,
/// preceded by the name of the parameter (the label).
fn labeled_arguments(i: &mut TokenSlice) -> PResult<Vec<LabeledArg>> {
separated(0.., labeled_argument, comma_sep)
.context(expected("function arguments"))
.parse_next(i)
}
/// A type of a function argument. /// A type of a function argument.
/// This can be: /// This can be:
/// - a primitive type, e.g. 'number' or 'string' or 'bool' /// - a primitive type, e.g. 'number' or 'string' or 'bool'
@ -2582,7 +2579,7 @@ fn parameter(i: &mut TokenSlice) -> PResult<ParamDescription> {
arg_name, arg_name,
type_, type_,
default_value: match (question_mark.is_some(), default_literal) { default_value: match (question_mark.is_some(), default_literal) {
(true, Some(lit)) => Some(DefaultParamVal::Literal(*lit)), (true, Some(lit)) => Some(DefaultParamVal::Literal(lit.inner)),
(true, None) => Some(DefaultParamVal::none()), (true, None) => Some(DefaultParamVal::none()),
(false, None) => None, (false, None) => None,
(false, Some(lit)) => { (false, Some(lit)) => {
@ -2786,28 +2783,7 @@ fn fn_call_kw(i: &mut TokenSlice) -> PResult<Node<CallExpressionKw>> {
ignore_whitespace(i); ignore_whitespace(i);
let initial_unlabeled_arg = opt((expression, comma, opt(whitespace)).map(|(arg, _, _)| arg)).parse_next(i)?; let initial_unlabeled_arg = opt((expression, comma, opt(whitespace)).map(|(arg, _, _)| arg)).parse_next(i)?;
let args: Vec<_> = repeat( let args = labeled_arguments(i)?;
0..,
alt((
terminated(non_code_node.map(NonCodeOr::NonCode), whitespace),
terminated(labeled_argument, labeled_arg_separator).map(NonCodeOr::Code),
)),
)
.parse_next(i)?;
let (args, non_code_nodes): (Vec<_>, BTreeMap<usize, _>) = args.into_iter().enumerate().fold(
(Vec::new(), BTreeMap::new()),
|(mut args, mut non_code_nodes), (i, e)| {
match e {
NonCodeOr::NonCode(x) => {
non_code_nodes.insert(i, vec![x]);
}
NonCodeOr::Code(x) => {
args.push(x);
}
}
(args, non_code_nodes)
},
);
if let Some(std_fn) = crate::std::get_stdlib_fn(&fn_name.name) { if let Some(std_fn) = crate::std::get_stdlib_fn(&fn_name.name) {
let just_args: Vec<_> = args.iter().collect(); let just_args: Vec<_> = args.iter().collect();
typecheck_all_kw(std_fn, &just_args)?; typecheck_all_kw(std_fn, &just_args)?;
@ -2816,10 +2792,6 @@ fn fn_call_kw(i: &mut TokenSlice) -> PResult<Node<CallExpressionKw>> {
opt(comma_sep).parse_next(i)?; opt(comma_sep).parse_next(i)?;
let end = close_paren.parse_next(i)?.end; let end = close_paren.parse_next(i)?.end;
let non_code_meta = NonCodeMeta {
non_code_nodes,
..Default::default()
};
Ok(Node { Ok(Node {
start: fn_name.start, start: fn_name.start,
end, end,
@ -2829,7 +2801,6 @@ fn fn_call_kw(i: &mut TokenSlice) -> PResult<Node<CallExpressionKw>> {
unlabeled: initial_unlabeled_arg, unlabeled: initial_unlabeled_arg,
arguments: args, arguments: args,
digest: None, digest: None,
non_code_meta,
}, },
outer_attrs: Vec::new(), outer_attrs: Vec::new(),
}) })
@ -4419,6 +4390,14 @@ let myBox = box([0,0], -3, -16, -10)
crate::parsing::top_level_parse(some_program_string).unwrap(); crate::parsing::top_level_parse(some_program_string).unwrap();
} }
#[test]
fn arg_labels() {
let input = r#"length: 3"#;
let module_id = ModuleId::default();
let tokens = crate::parsing::token::lex(input, module_id).unwrap();
super::labeled_arguments(&mut tokens.as_slice()).unwrap();
}
#[test] #[test]
fn kw_fn() { fn kw_fn() {
for input in ["val = foo(x, y = z)", "val = foo(y = z)"] { for input in ["val = foo(x, y = z)", "val = foo(y = z)"] {
@ -4900,22 +4879,6 @@ my14 = 4 ^ 2 - 3 ^ 2 * 2
r#"fn foo(x?: number = 2) { return 1 }"# r#"fn foo(x?: number = 2) { return 1 }"#
); );
snapshot_test!(kw_function_call_in_pipe, r#"val = 1 |> f(arg = x)"#); snapshot_test!(kw_function_call_in_pipe, r#"val = 1 |> f(arg = x)"#);
snapshot_test!(
kw_function_call_multiline,
r#"val = f(
arg = x,
foo = x,
bar = x,
)"#
);
snapshot_test!(
kw_function_call_multiline_with_comments,
r#"val = f(
arg = x,
// foo = x,
bar = x,
)"#
);
} }
#[allow(unused)] #[allow(unused)]

View File

@ -1,86 +0,0 @@
---
source: kcl/src/parsing/parser.rs
expression: actual
snapshot_kind: text
---
{
"body": [
{
"declaration": {
"end": 87,
"id": {
"end": 3,
"name": "val",
"start": 0,
"type": "Identifier"
},
"init": {
"arguments": [
{
"type": "LabeledArg",
"label": {
"type": "Identifier",
"name": "arg"
},
"arg": {
"end": 29,
"name": "x",
"start": 28,
"type": "Identifier",
"type": "Identifier"
}
},
{
"type": "LabeledArg",
"label": {
"type": "Identifier",
"name": "foo"
},
"arg": {
"end": 51,
"name": "x",
"start": 50,
"type": "Identifier",
"type": "Identifier"
}
},
{
"type": "LabeledArg",
"label": {
"type": "Identifier",
"name": "bar"
},
"arg": {
"end": 73,
"name": "x",
"start": 72,
"type": "Identifier",
"type": "Identifier"
}
}
],
"callee": {
"end": 7,
"name": "f",
"start": 6,
"type": "Identifier"
},
"end": 87,
"start": 6,
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": null
},
"start": 0,
"type": "VariableDeclarator"
},
"end": 87,
"kind": "const",
"start": 0,
"type": "VariableDeclaration",
"type": "VariableDeclaration"
}
],
"end": 87,
"start": 0
}

Some files were not shown because too many files have changed in this diff Show More