Move error logic out of loftSketches, fix pw tests

This commit is contained in:
Pierre Jacquier
2024-12-04 11:39:35 -05:00
parent 2abd980de9
commit 8b25527f21
3 changed files with 24 additions and 37 deletions

View File

@ -692,24 +692,24 @@ loftPointAndClickCases.forEach(({ shouldPreselect }) => {
cmdBar,
}) => {
const initialCode = `sketch001 = startSketchOn('XZ')
|> circle({ center = [0, 0], radius = 30 }, %)
plane001 = offsetPlane('XZ', 50)
|> circle({ center = [0, 0], radius = 40 }, %)
plane001 = offsetPlane('XZ', -40)
sketch002 = startSketchOn(plane001)
|> circle({ center = [0, 0], radius = 20 }, %)
`
await app.initialise(initialCode)
// One dumb hardcoded screen pixel value
const testPoint = { x: 575, y: 200 }
const testPoint = { x: 560, y: 275 }
const [clickOnSketch1] = scene.makeMouseHelpers(testPoint.x, testPoint.y)
const [clickOnSketch2] = scene.makeMouseHelpers(
testPoint.x,
testPoint.y + 80
testPoint.x + 20,
testPoint.y
)
const loftDeclaration = 'loft001 = loft([sketch001, sketch002])'
await test.step(`Look for the white of the sketch001 shape`, async () => {
await scene.expectPixelColor([254, 254, 254], testPoint, 15)
await scene.expectPixelColor([208, 208, 208], testPoint, 15)
})
async function selectSketches() {
@ -764,7 +764,7 @@ sketch002 = startSketchOn(plane001)
activeLines: [loftDeclaration],
highlightedCode: '',
})
await scene.expectPixelColor([109, 109, 109], testPoint, 15)
await scene.expectPixelColor([16, 16, 16], testPoint, 15)
})
})
})

View File

@ -348,30 +348,14 @@ export function extrudeSketch(
export function loftSketches(
node: Node<Program>,
nodePaths: PathToNode[]
):
| {
modifiedAst: Node<Program>
pathToNode: PathToNode
}
| Error {
declarators: VariableDeclarator[]
): {
modifiedAst: Node<Program>
pathToNode: PathToNode
} {
const modifiedAst = structuredClone(node)
const elements = []
for (const path of nodePaths) {
const nodeFromPath = getNodeFromPath<VariableDeclarator>(
modifiedAst,
path,
'VariableDeclarator'
)
if (err(nodeFromPath)) {
return nodeFromPath
}
elements.push(createIdentifier(nodeFromPath.node.id.name))
}
const name = findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.LOFT)
const elements = declarators.map((d) => createIdentifier(d.id.name))
const loft = createCallExpressionStdLib('loft', [
createArrayExpression(elements),
])

View File

@ -1547,20 +1547,23 @@ export const modelingMachine = setup({
// Extract inputs
const ast = kclManager.ast
const { selection } = input
const declarators = selection.graphSelections.flatMap((s) => {
const path = getNodePathFromSourceRange(ast, s?.codeRef.range)
const nodeFromPath = getNodeFromPath<VariableDeclarator>(
ast,
path,
'VariableDeclarator'
)
return err(nodeFromPath) ? [] : nodeFromPath.node
})
// Perform the loft
const nodePaths = selection.graphSelections.map((s) =>
getNodePathFromSourceRange(ast, s?.codeRef.range)
)
// TODO: add better validation on selection
if (!(nodePaths && nodePaths.length > 1)) {
if (!(declarators && declarators.length > 1)) {
trap('Not enough sketches selected')
}
// Preform the loft
const loftSketchesRes = loftSketches(ast, nodePaths)
// TODO: improve this
if (trap(loftSketchesRes)) return
const loftSketchesRes = loftSketches(ast, declarators)
const updateAstResult = await kclManager.updateAst(
loftSketchesRes.modifiedAst,
true,