JS: Rectangle and extrude buttons now generate keyword args

This commit is contained in:
Adam Chalmers
2025-01-07 11:34:34 -06:00
committed by Nick Cameron
parent 2848d6c77c
commit 725ffd952c
4 changed files with 51 additions and 14 deletions

1
.gitignore vendored
View File

@ -25,6 +25,7 @@ yarn-error.log*
.idea .idea
.vscode .vscode
.helix
src/wasm-lib/.idea src/wasm-lib/.idea
src/wasm-lib/.vscode src/wasm-lib/.vscode

View File

@ -3,6 +3,8 @@ import { Selection } from 'lib/selections'
import { import {
Program, Program,
CallExpression, CallExpression,
LabeledArg,
CallExpressionKw,
PipeExpression, PipeExpression,
VariableDeclaration, VariableDeclaration,
VariableDeclarator, VariableDeclarator,
@ -132,10 +134,11 @@ export function addSketchTo(
createLiteral('default'), createLiteral('default'),
createPipeSubstitution(), createPipeSubstitution(),
]) ])
const initialLineTo = createCallExpressionStdLib('line', [ const initialLineTo = createCallExpressionStdLibKw(
createLiteral('default'), 'line',
createPipeSubstitution(), createPipeSubstitution(),
]) [createLabeledArg('end', createLiteral('default'))]
)
const pipeBody = [startSketchOn, startProfileAt, initialLineTo] const pipeBody = [startSketchOn, startProfileAt, initialLineTo]
@ -286,11 +289,11 @@ export function extrudeSketch(
if (err(_node3)) return _node3 if (err(_node3)) return _node3
const { node: variableDeclarator, shallowPath: pathToDecleration } = _node3 const { node: variableDeclarator, shallowPath: pathToDecleration } = _node3
const extrudeCall = createCallExpressionStdLib('extrude', [ const sketchToExtrude = shouldPipe
distance,
shouldPipe
? createPipeSubstitution() ? createPipeSubstitution()
: createIdentifier(variableDeclarator.id.name), : createIdentifier(variableDeclarator.id.name)
const extrudeCall = createCallExpressionStdLibKw('extrude', sketchToExtrude, [
createLabeledArg('length', distance),
]) ])
if (shouldPipe) { if (shouldPipe) {
@ -810,6 +813,29 @@ export function createCallExpressionStdLib(
} }
} }
export function createCallExpressionStdLibKw(
name: string,
unlabeled: CallExpressionKw['unlabeled'],
args: CallExpressionKw['arguments']
): Node<CallExpressionKw> {
return {
type: 'CallExpressionKw',
start: 0,
end: 0,
moduleId: 0,
callee: {
type: 'Identifier',
start: 0,
end: 0,
moduleId: 0,
name,
},
unlabeled,
arguments: args,
}
}
export function createCallExpression( export function createCallExpression(
name: string, name: string,
args: CallExpression['arguments'] args: CallExpression['arguments']
@ -1374,3 +1400,7 @@ export async function deleteFromSelection(
const nonCodeMetaEmpty = () => { const nonCodeMetaEmpty = () => {
return { nonCodeNodes: {}, startNodes: [], start: 0, end: 0 } return { nonCodeNodes: {}, startNodes: [], start: 0, end: 0 }
} }
export const createLabeledArg = (name: string, arg: Expr): LabeledArg => {
return { label: createIdentifier(name), arg, type: 'LabeledArg' }
}

View File

@ -84,6 +84,8 @@ export type { BinaryExpression } from '../wasm-lib/kcl/bindings/BinaryExpression
export type { ReturnStatement } from '../wasm-lib/kcl/bindings/ReturnStatement' export type { ReturnStatement } from '../wasm-lib/kcl/bindings/ReturnStatement'
export type { ExpressionStatement } from '../wasm-lib/kcl/bindings/ExpressionStatement' export type { ExpressionStatement } from '../wasm-lib/kcl/bindings/ExpressionStatement'
export type { CallExpression } from '../wasm-lib/kcl/bindings/CallExpression' export type { CallExpression } from '../wasm-lib/kcl/bindings/CallExpression'
export type { CallExpressionKw } from '../wasm-lib/kcl/bindings/CallExpressionKw'
export type { LabeledArg } from '../wasm-lib/kcl/bindings/LabeledArg'
export type { VariableDeclarator } from '../wasm-lib/kcl/bindings/VariableDeclarator' export type { VariableDeclarator } from '../wasm-lib/kcl/bindings/VariableDeclarator'
export type { BinaryPart } from '../wasm-lib/kcl/bindings/BinaryPart' export type { BinaryPart } from '../wasm-lib/kcl/bindings/BinaryPart'
export type { Literal } from '../wasm-lib/kcl/bindings/Literal' export type { Literal } from '../wasm-lib/kcl/bindings/Literal'

View File

@ -2,7 +2,9 @@ import {
createArrayExpression, createArrayExpression,
createBinaryExpression, createBinaryExpression,
createCallExpressionStdLib, createCallExpressionStdLib,
createCallExpressionStdLibKw,
createIdentifier, createIdentifier,
createLabeledArg,
createLiteral, createLiteral,
createPipeSubstitution, createPipeSubstitution,
createTagDeclarator, createTagDeclarator,
@ -62,12 +64,14 @@ export const getRectangleCallExpressions = (
createPipeSubstitution(), createPipeSubstitution(),
createTagDeclarator(tags[2]), createTagDeclarator(tags[2]),
]), ]),
createCallExpressionStdLib('lineTo', [ createCallExpressionStdLibKw('line', createPipeSubstitution(), [
createLabeledArg(
'endAbsolute',
createArrayExpression([ createArrayExpression([
createCallExpressionStdLib('profileStartX', [createPipeSubstitution()]), createCallExpressionStdLib('profileStartX', [createPipeSubstitution()]),
createCallExpressionStdLib('profileStartY', [createPipeSubstitution()]), createCallExpressionStdLib('profileStartY', [createPipeSubstitution()]),
]), ])
createPipeSubstitution(), ),
]), // close the rectangle ]), // close the rectangle
createCallExpressionStdLib('close', [createPipeSubstitution()]), createCallExpressionStdLib('close', [createPipeSubstitution()]),
] ]