diff --git a/src/clientSideScene/sceneEntities.ts b/src/clientSideScene/sceneEntities.ts index 7b95e10a3..058a47ee1 100644 --- a/src/clientSideScene/sceneEntities.ts +++ b/src/clientSideScene/sceneEntities.ts @@ -849,9 +849,7 @@ export class SceneEntities { ) } - const newCode = recast(modifiedAst) - if (err(newCode)) return - codeManager.updateCodeEditor(newCode) + await codeManager.updateEditorWithAstAndWriteToFile(modifiedAst) }, onMove: (args) => { this.onDragSegment({ @@ -991,6 +989,11 @@ export class SceneEntities { await kclManager.executeAstMock(_ast) sceneInfra.modelingSend({ type: 'Finish rectangle' }) + // lee: I had this at the bottom of the function, but it's + // possible sketchFromKclValue "fails" when sketching on a face, + // and this couldn't wouldn't run. + await codeManager.updateEditorWithAstAndWriteToFile(_ast) + const { execState } = await executeAst({ ast: _ast, useFakeExecutor: true, @@ -1016,9 +1019,6 @@ export class SceneEntities { sgPaths.forEach((seg, index) => this.updateSegment(seg, index, 0, _ast, orthoFactor, sketch) ) - - if (err(newCode)) return - codeManager.updateCodeEditor(newCode) }, }) } @@ -1188,7 +1188,7 @@ export class SceneEntities { await kclManager.executeAstMock(_ast) sceneInfra.modelingSend({ type: 'Finish circle' }) - codeManager.updateCodeEditor(newCode) + await codeManager.updateEditorWithAstAndWriteToFile(_ast) } }, }) diff --git a/src/components/ModelingMachineProvider.tsx b/src/components/ModelingMachineProvider.tsx index 493750c59..d532c5c71 100644 --- a/src/components/ModelingMachineProvider.tsx +++ b/src/components/ModelingMachineProvider.tsx @@ -733,6 +733,11 @@ export const ModelingMachineProvider = ({ sketchDetails.origin ) if (err(updatedAst)) return Promise.reject(updatedAst) + + await codeManager.updateEditorWithAstAndWriteToFile( + updatedAst.newAst + ) + const selection = updateSelections( pathToNodeMap, selectionRanges, @@ -769,6 +774,11 @@ export const ModelingMachineProvider = ({ sketchDetails.origin ) if (err(updatedAst)) return Promise.reject(updatedAst) + + await codeManager.updateEditorWithAstAndWriteToFile( + updatedAst.newAst + ) + const selection = updateSelections( pathToNodeMap, selectionRanges, @@ -814,6 +824,11 @@ export const ModelingMachineProvider = ({ sketchDetails.origin ) if (err(updatedAst)) return Promise.reject(updatedAst) + + await codeManager.updateEditorWithAstAndWriteToFile( + updatedAst.newAst + ) + const selection = updateSelections( pathToNodeMap, selectionRanges, @@ -847,6 +862,11 @@ export const ModelingMachineProvider = ({ sketchDetails.origin ) if (err(updatedAst)) return Promise.reject(updatedAst) + + await codeManager.updateEditorWithAstAndWriteToFile( + updatedAst.newAst + ) + const selection = updateSelections( pathToNodeMap, selectionRanges, @@ -882,6 +902,11 @@ export const ModelingMachineProvider = ({ sketchDetails.origin ) if (err(updatedAst)) return Promise.reject(updatedAst) + + await codeManager.updateEditorWithAstAndWriteToFile( + updatedAst.newAst + ) + const selection = updateSelections( pathToNodeMap, selectionRanges, @@ -918,6 +943,11 @@ export const ModelingMachineProvider = ({ sketchDetails.origin ) if (err(updatedAst)) return Promise.reject(updatedAst) + + await codeManager.updateEditorWithAstAndWriteToFile( + updatedAst.newAst + ) + const selection = updateSelections( pathToNodeMap, selectionRanges, @@ -954,6 +984,11 @@ export const ModelingMachineProvider = ({ sketchDetails.origin ) if (err(updatedAst)) return Promise.reject(updatedAst) + + await codeManager.updateEditorWithAstAndWriteToFile( + updatedAst.newAst + ) + const selection = updateSelections( pathToNodeMap, selectionRanges, @@ -1000,6 +1035,11 @@ export const ModelingMachineProvider = ({ sketchDetails.origin ) if (err(updatedAst)) return Promise.reject(updatedAst) + + await codeManager.updateEditorWithAstAndWriteToFile( + updatedAst.newAst + ) + const selection = updateSelections( { 0: pathToReplacedNode }, selectionRanges, diff --git a/src/lang/codeManager.ts b/src/lang/codeManager.ts index 476c5ba36..39a21e360 100644 --- a/src/lang/codeManager.ts +++ b/src/lang/codeManager.ts @@ -7,6 +7,8 @@ import toast from 'react-hot-toast' import { editorManager } from 'lib/singletons' import { Annotation, Transaction } from '@codemirror/state' import { KeyBinding } from '@codemirror/view' +import { recast, Program } from 'lang/wasm' +import { err } from 'lib/trap' const PERSIST_CODE_KEY = 'persistCode' @@ -147,6 +149,13 @@ export default class CodeManager { safeLSSetItem(PERSIST_CODE_KEY, this.code) } } + + async updateEditorWithAstAndWriteToFile(ast: Program) { + const newCode = recast(ast) + if (err(newCode)) return + this.updateCodeStateEditor(newCode) + await this.writeToFile() + } } function safeLSGetItem(key: string) { diff --git a/src/machines/modelingMachine.ts b/src/machines/modelingMachine.ts index 4cb9d2d68..dfde18b3c 100644 --- a/src/machines/modelingMachine.ts +++ b/src/machines/modelingMachine.ts @@ -513,15 +513,6 @@ export const modelingMachine = setup({ }, // end guards actions: { - 'code editor process changes': ({ event, context }) => { - ;(async () => { - const newCode = recast(kclManager.ast) - if (err(newCode)) return - await codeManager.updateCodeStateEditor(newCode) - - await codeManager.writeToFile() - })().catch(reportRejection) - }, 'assign tool in context': assign({ currentTool: ({ event }) => 'data' in event && event.data && 'tool' in event.data @@ -541,29 +532,25 @@ export const modelingMachine = setup({ } } ), - // eslint-disable-next-line @typescript-eslint/no-misused-promises - 'hide default planes': () => kclManager.hidePlanes(), + 'hide default planes': () => { + void kclManager.hidePlanes() + }, 'reset sketch metadata': assign({ sketchDetails: null, sketchEnginePathId: '', sketchPlaneId: '', }), 'reset camera position': () => { - ;(async () => { - await engineCommandManager.sendSceneCommand({ - type: 'modeling_cmd_req', - cmd_id: uuidv4(), - cmd: { - type: 'default_camera_look_at', - center: { x: 0, y: 0, z: 0 }, - vantage: { x: 0, y: -1250, z: 580 }, - up: { x: 0, y: 0, z: 1 }, - }, - }) - await sceneInfra.camControls.centerModelRelativeToPanes({ - resetLastPaneWidth: true, - }) - })().catch(reportRejection) + void engineCommandManager.sendSceneCommand({ + type: 'modeling_cmd_req', + cmd_id: uuidv4(), + cmd: { + type: 'default_camera_look_at', + center: { x: 0, y: 0, z: 0 }, + vantage: { x: 0, y: -1250, z: 580 }, + up: { x: 0, y: 0, z: 1 }, + }, + }) }, 'set new sketch metadata': assign(({ event }) => { if ( @@ -618,9 +605,7 @@ export const modelingMachine = setup({ }, }) - const newCode = recast(updatedAst.newAst) - if (err(newCode)) return - await codeManager.updateCodeEditor(newCode) + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) if (updatedAst?.selections) { editorManager.selectRange(updatedAst?.selections) @@ -664,9 +649,7 @@ export const modelingMachine = setup({ }, }) - const newCode = recast(updatedAst.newAst) - if (err(newCode)) return - await codeManager.updateCodeEditor(newCode) + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) if (updatedAst?.selections) { editorManager.selectRange(updatedAst?.selections) @@ -697,10 +680,7 @@ export const modelingMachine = setup({ } await kclManager.updateAst(modifiedAst, true) - - const newCode = recast(modifiedAst) - if (err(newCode)) return - await codeManager.updateCodeEditor(newCode) + await codeManager.updateEditorWithAstAndWriteToFile(modifiedAst) })().catch(reportRejection) }, 'AST fillet': ({ event }) => { @@ -718,6 +698,9 @@ export const modelingMachine = setup({ radius ) if (err(applyFilletToSelectionResult)) return applyFilletToSelectionResult + + console.log(applyFilletToSelectionResult) + void codeManager.updateEditorWithAstAndWriteToFile(kclManager.ast) }, 'set selection filter to curves only': () => { ;(async () => { @@ -774,25 +757,32 @@ export const modelingMachine = setup({ 'remove sketch grid': () => sceneEntitiesManager.removeSketchGrid(), 'set up draft line': ({ context: { sketchDetails } }) => { if (!sketchDetails) return - // eslint-disable-next-line @typescript-eslint/no-floating-promises - sceneEntitiesManager.setupDraftSegment( - sketchDetails.sketchPathToNode, - sketchDetails.zAxis, - sketchDetails.yAxis, - sketchDetails.origin, - 'line' - ) + + void sceneEntitiesManager + .setupDraftSegment( + sketchDetails.sketchPathToNode, + sketchDetails.zAxis, + sketchDetails.yAxis, + sketchDetails.origin, + 'line' + ) + .then(() => { + return codeManager.updateEditorWithAstAndWriteToFile(kclManager.ast) + }) }, 'set up draft arc': ({ context: { sketchDetails } }) => { if (!sketchDetails) return - // eslint-disable-next-line @typescript-eslint/no-floating-promises - sceneEntitiesManager.setupDraftSegment( - sketchDetails.sketchPathToNode, - sketchDetails.zAxis, - sketchDetails.yAxis, - sketchDetails.origin, - 'tangentialArcTo' - ) + void sceneEntitiesManager + .setupDraftSegment( + sketchDetails.sketchPathToNode, + sketchDetails.zAxis, + sketchDetails.yAxis, + sketchDetails.origin, + 'tangentialArcTo' + ) + .then(() => { + return codeManager.updateEditorWithAstAndWriteToFile(kclManager.ast) + }) }, 'listen for rectangle origin': ({ context: { sketchDetails } }) => { if (!sketchDetails) return @@ -850,38 +840,49 @@ export const modelingMachine = setup({ 'set up draft rectangle': ({ context: { sketchDetails }, event }) => { if (event.type !== 'Add rectangle origin') return if (!sketchDetails || !event.data) return - // eslint-disable-next-line @typescript-eslint/no-floating-promises - sceneEntitiesManager.setupDraftRectangle( - sketchDetails.sketchPathToNode, - sketchDetails.zAxis, - sketchDetails.yAxis, - sketchDetails.origin, - event.data - ) + + void sceneEntitiesManager + .setupDraftRectangle( + sketchDetails.sketchPathToNode, + sketchDetails.zAxis, + sketchDetails.yAxis, + sketchDetails.origin, + event.data + ) + .then(() => { + return codeManager.updateEditorWithAstAndWriteToFile(kclManager.ast) + }) }, 'set up draft circle': ({ context: { sketchDetails }, event }) => { if (event.type !== 'Add circle origin') return if (!sketchDetails || !event.data) return - // eslint-disable-next-line @typescript-eslint/no-floating-promises - sceneEntitiesManager.setupDraftCircle( - sketchDetails.sketchPathToNode, - sketchDetails.zAxis, - sketchDetails.yAxis, - sketchDetails.origin, - event.data - ) + + void sceneEntitiesManager + .setupDraftCircle( + sketchDetails.sketchPathToNode, + sketchDetails.zAxis, + sketchDetails.yAxis, + sketchDetails.origin, + event.data + ) + .then(() => { + return codeManager.updateEditorWithAstAndWriteToFile(kclManager.ast) + }) }, 'set up draft line without teardown': ({ context: { sketchDetails } }) => { if (!sketchDetails) return - // eslint-disable-next-line @typescript-eslint/no-floating-promises - sceneEntitiesManager.setupDraftSegment( - sketchDetails.sketchPathToNode, - sketchDetails.zAxis, - sketchDetails.yAxis, - sketchDetails.origin, - 'line', - false - ) + void sceneEntitiesManager + .setupDraftSegment( + sketchDetails.sketchPathToNode, + sketchDetails.zAxis, + sketchDetails.yAxis, + sketchDetails.origin, + 'line', + false + ) + .then(() => { + return codeManager.updateEditorWithAstAndWriteToFile(kclManager.ast) + }) }, 'show default planes': () => { // eslint-disable-next-line @typescript-eslint/no-floating-promises @@ -898,12 +899,14 @@ export const modelingMachine = setup({ 'add axis n grid': ({ context: { sketchDetails } }) => { if (!sketchDetails) return if (localStorage.getItem('disableAxis')) return - sceneEntitiesManager.createSketchAxis( + void sceneEntitiesManager.createSketchAxis( sketchDetails.sketchPathToNode || [], sketchDetails.zAxis, sketchDetails.yAxis, sketchDetails.origin ) + + void codeManager.updateEditorWithAstAndWriteToFile(kclManager.ast) }, 'reset client scene mouse handlers': () => { // when not in sketch mode we don't need any mouse listeners @@ -932,10 +935,12 @@ export const modelingMachine = setup({ 'Delete segment': ({ context: { sketchDetails }, event }) => { if (event.type !== 'Delete segment') return if (!sketchDetails || !event.data) return - // eslint-disable-next-line @typescript-eslint/no-floating-promises - deleteSegment({ + + void deleteSegment({ pathToNode: event.data, sketchDetails, + }).then(() => { + return codeManager.updateEditorWithAstAndWriteToFile(kclManager.ast) }) }, 'Reset Segment Overlays': () => sceneEntitiesManager.resetOverlays(), @@ -1000,6 +1005,9 @@ export const modelingMachine = setup({ ) if (trap(updatedAst, { suppress: true })) return if (!updatedAst) return + + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) + return { selectionType: 'completeSelection', selection: updateSelections( @@ -1034,6 +1042,7 @@ export const modelingMachine = setup({ ) if (trap(updatedAst, { suppress: true })) return if (!updatedAst) return + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) return { selectionType: 'completeSelection', selection: updateSelections( @@ -1068,6 +1077,7 @@ export const modelingMachine = setup({ ) if (trap(updatedAst, { suppress: true })) return if (!updatedAst) return + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) return { selectionType: 'completeSelection', selection: updateSelections( @@ -1100,6 +1110,7 @@ export const modelingMachine = setup({ ) if (trap(updatedAst, { suppress: true })) return if (!updatedAst) return + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) const updatedSelectionRanges = updateSelections( pathToNodeMap, selectionRanges, @@ -1133,6 +1144,7 @@ export const modelingMachine = setup({ ) if (trap(updatedAst, { suppress: true })) return if (!updatedAst) return + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) const updatedSelectionRanges = updateSelections( pathToNodeMap, selectionRanges, @@ -1166,6 +1178,7 @@ export const modelingMachine = setup({ ) if (trap(updatedAst, { suppress: true })) return if (!updatedAst) return + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) const updatedSelectionRanges = updateSelections( pathToNodeMap, selectionRanges, @@ -1199,6 +1212,7 @@ export const modelingMachine = setup({ ) if (trap(updatedAst, { suppress: true })) return if (!updatedAst) return + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) const updatedSelectionRanges = updateSelections( pathToNodeMap, selectionRanges, @@ -1236,6 +1250,8 @@ export const modelingMachine = setup({ ) if (trap(updatedAst, { suppress: true })) return if (!updatedAst) return + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) + const updatedSelectionRanges = updateSelections( pathToNodeMap, selectionRanges, @@ -1268,6 +1284,7 @@ export const modelingMachine = setup({ ) if (trap(updatedAst, { suppress: true })) return if (!updatedAst) return + await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst) const updatedSelectionRanges = updateSelections( pathToNodeMap, selectionRanges, @@ -1572,11 +1589,7 @@ export const modelingMachine = setup({ }, }, - entry: [ - 'setup client side sketch segments', - - 'code editor process changes', - ], + entry: ['setup client side sketch segments'], }, 'Await horizontal distance info': { @@ -1589,7 +1602,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, onError: 'SketchIdle', }, @@ -1605,7 +1618,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, onError: 'SketchIdle', }, @@ -1621,7 +1634,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, onError: 'SketchIdle', }, @@ -1637,7 +1650,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, onError: 'SketchIdle', }, @@ -1653,7 +1666,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, onError: 'SketchIdle', }, @@ -1669,7 +1682,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, onError: 'SketchIdle', }, @@ -1685,7 +1698,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, onError: 'SketchIdle', }, @@ -1821,7 +1834,7 @@ export const modelingMachine = setup({ onError: 'SketchIdle', onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, }, @@ -1842,7 +1855,7 @@ export const modelingMachine = setup({ }, onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, }, @@ -1857,7 +1870,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, }, @@ -1872,7 +1885,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, }, @@ -1887,7 +1900,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, }, @@ -1902,7 +1915,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, }, @@ -1917,7 +1930,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, }, @@ -1932,7 +1945,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, }, @@ -1947,7 +1960,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, }, @@ -1962,7 +1975,7 @@ export const modelingMachine = setup({ }), onDone: { target: 'SketchIdle', - actions: ['code editor process changes', 'Set selection'], + actions: 'Set selection', }, }, },