Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Extrude AST mod from XState action to actor #5146

Merged
merged 8 commits into from
Jan 31, 2025
135 changes: 82 additions & 53 deletions src/machines/modelingMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ export const modelingMachine = setup({
},
// end guards
actions: {
toastError: ({ event }) => {
if ('output' in event && event.output instanceof Error) {
toast.error(event.output.message)
} else if ('data' in event && event.data instanceof Error) {
toast.error(event.data.message)
}
},
'assign tool in context': assign({
currentTool: ({ event }) =>
'data' in event && event.data && 'tool' in event.data
Expand Down Expand Up @@ -639,56 +646,6 @@ export const modelingMachine = setup({
sketchDetails: event.output,
}
}),
'AST extrude': ({ context: { store }, event }) => {
if (event.type !== 'Extrude') return
;(async () => {
if (!event.data) return
const { selection, distance } = event.data
let ast = kclManager.ast
if (
'variableName' in distance &&
distance.variableName &&
distance.insertIndex !== undefined
) {
const newBody = [...ast.body]
newBody.splice(
distance.insertIndex,
0,
distance.variableDeclarationAst
)
ast.body = newBody
}
const pathToNode = getNodePathFromSourceRange(
ast,
selection.graphSelections[0]?.codeRef.range
)
const extrudeSketchRes = extrudeSketch(
ast,
pathToNode,
false,
'variableName' in distance
? distance.variableIdentifierAst
: distance.valueAst
)
if (trap(extrudeSketchRes)) return
const { modifiedAst, pathToExtrudeArg } = extrudeSketchRes

const updatedAst = await kclManager.updateAst(modifiedAst, true, {
focusPath: [pathToExtrudeArg],
zoomToFit: true,
zoomOnRangeAndType: {
range: selection.graphSelections[0]?.codeRef.range,
type: 'path',
},
})

await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst)

if (updatedAst?.selections) {
editorManager.selectRange(updatedAst?.selections)
}
})().catch(reportRejection)
},
'AST revolve': ({ context: { store }, event }) => {
if (event.type !== 'Revolve') return
;(async () => {
Expand Down Expand Up @@ -1493,6 +1450,63 @@ export const modelingMachine = setup({
return {} as SetSelections
}
),
extrudeAstMod: fromPromise<
unknown,
franknoirot marked this conversation as resolved.
Show resolved Hide resolved
ModelingCommandSchema['Extrude'] | undefined
>(async ({ input }) => {
if (!input) return new Error('No input provided')
franknoirot marked this conversation as resolved.
Show resolved Hide resolved
const { selection, distance } = input
let ast = structuredClone(kclManager.ast)
let extrudeName: string | undefined = undefined

const pathToNode = getNodePathFromSourceRange(
ast,
selection.graphSelections[0]?.codeRef.range
)
// Add an extrude statement to the AST
const extrudeSketchRes = extrudeSketch(
ast,
pathToNode,
false,
'variableName' in distance
? distance.variableIdentifierAst
: distance.valueAst
)
if (err(extrudeSketchRes)) return extrudeSketchRes
franknoirot marked this conversation as resolved.
Show resolved Hide resolved
const { modifiedAst, pathToExtrudeArg } = extrudeSketchRes

// Insert the distance variable if the user has provided a variable name
if (
'variableName' in distance &&
distance.variableName &&
typeof pathToExtrudeArg[1][0] === 'number'
) {
const insertIndex = Math.min(
pathToExtrudeArg[1][0],
distance.insertIndex
)
const newBody = [...modifiedAst.body]
newBody.splice(insertIndex, 0, distance.variableDeclarationAst)
modifiedAst.body = newBody
// Since we inserted a new variable, we need to update the path to the extrude argument
pathToExtrudeArg[1][0]++
}

const updatedAst = await kclManager.updateAst(modifiedAst, true, {
focusPath: [pathToExtrudeArg],
zoomToFit: true,
zoomOnRangeAndType: {
range: selection.graphSelections[0]?.codeRef.range,
type: 'path',
},
})

await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst)

if (updatedAst?.selections) {
editorManager.selectRange(updatedAst?.selections)
}
}),
offsetPlaneAstMod: fromPromise(
async ({
input,
Expand Down Expand Up @@ -1823,9 +1837,8 @@ export const modelingMachine = setup({
],

Extrude: {
target: 'idle',
actions: ['AST extrude'],
reenter: false,
target: 'Applying extrude',
reenter: true,
},

Revolve: {
Expand Down Expand Up @@ -2622,6 +2635,22 @@ export const modelingMachine = setup({
},
},

'Applying extrude': {
invoke: {
src: 'extrudeAstMod',
id: 'extrudeAstMod',
input: ({ event }) => {
if (event.type !== 'Extrude') return undefined
return event.data
},
onDone: ['idle'],
onError: {
target: 'idle',
actions: 'toastError',
},
},
},

'Applying offset plane': {
invoke: {
src: 'offsetPlaneAstMod',
Expand Down
Loading