Refactor writeToFile and updateCodeEditor to happen at appropriate times

This commit is contained in:
49lf
2024-11-13 01:09:17 -05:00
parent a4b7dd5182
commit 6c2d06c2c6
6 changed files with 106 additions and 63 deletions

View File

@ -656,10 +656,17 @@ const ConstraintSymbol = ({
kclManager.ast,
kclManager.programMemory
)
if (!transform) return
const { modifiedAst } = transform
// eslint-disable-next-line @typescript-eslint/no-floating-promises
kclManager.updateAst(modifiedAst, true)
await kclManager.updateAst(modifiedAst, true)
// Code editor will be updated in the modelingMachine.
const newCode = recast(modifiedAst)
if (err(newCode)) return
await codeManager.updateCodeEditor(newCode)
} catch (e) {
console.log('error', e)
}

View File

@ -846,6 +846,10 @@ export class SceneEntities {
segmentName
)
}
const newCode = recast(modifiedAst)
if (err(newCode)) return
codeManager.updateCodeEditor(newCode)
},
onMove: (args) => {
this.onDragSegment({
@ -970,10 +974,14 @@ export class SceneEntities {
if (trap(_node)) return
const sketchInit = _node.node?.declarations?.[0]?.init
if (sketchInit.type === 'PipeExpression') {
if (sketchInit.type !== 'PipeExpression') {
return
}
updateRectangleSketch(sketchInit, x, y, tags[0])
let _recastAst = parse(recast(_ast))
const newCode = recast(_ast)
let _recastAst = parse(newCode)
if (trap(_recastAst)) return
_ast = _recastAst
@ -1006,7 +1014,9 @@ export class SceneEntities {
sgPaths.forEach((seg, index) =>
this.updateSegment(seg, index, 0, _ast, orthoFactor, sketch)
)
}
if (err(newCode)) return
codeManager.updateCodeEditor(newCode)
},
})
}
@ -1166,13 +1176,17 @@ export class SceneEntities {
if (err(moddedResult)) return
modded = moddedResult.modifiedAst
let _recastAst = parse(recast(modded))
const newCode = recast(modded)
if (err(newCode)) return
let _recastAst = parse(newCode)
if (trap(_recastAst)) return Promise.reject(_recastAst)
_ast = _recastAst
// Update the primary AST and unequip the rectangle tool
await kclManager.executeAstMock(_ast)
sceneInfra.modelingSend({ type: 'Finish circle' })
codeManager.updateCodeEditor(newCode)
}
},
})
@ -1208,6 +1222,7 @@ export class SceneEntities {
forward,
position,
})
await codeManager.writeToFile()
}
},
onDrag: async ({

View File

@ -2,13 +2,13 @@ import {
SetVarNameModal,
createSetVarNameModal,
} from 'components/SetVarNameModal'
import { editorManager, kclManager } from 'lib/singletons'
import { reportRejection, trap } from 'lib/trap'
import { editorManager, kclManager, codeManager } from 'lib/singletons'
import { reportRejection, trap, err } from 'lib/trap'
import { moveValueIntoNewVariable } from 'lang/modifyAst'
import { isNodeSafeToReplace } from 'lang/queryAst'
import { useEffect, useState } from 'react'
import { useModelingContext } from './useModelingContext'
import { PathToNode, SourceRange } from 'lang/wasm'
import { PathToNode, SourceRange, recast } from 'lang/wasm'
import { useKclContext } from 'lang/KclProvider'
import { toSync } from 'lib/utils'
@ -57,6 +57,11 @@ export function useConvertToVariable(range?: SourceRange) {
)
await kclManager.updateAst(_modifiedAst, true)
const newCode = recast(_modifiedAst)
if (err(newCode)) return
codeManager.updateCodeEditor(newCode)
return pathToReplacedNode
} catch (e) {
console.log('error', e)

View File

@ -351,9 +351,6 @@ export class KclManager {
this.clearAst()
return
}
codeManager.updateCodeEditor(newCode)
// Write the file to disk.
await codeManager.writeToFile()
this._ast = { ...newAst }
const { logs, errors, execState } = await executeAst({
@ -491,11 +488,6 @@ export class KclManager {
}
if (execute) {
// Call execute on the set ast.
// Update the code state and editor.
codeManager.updateCodeEditor(newCode)
// Write the file to disk.
await codeManager.writeToFile()
await this.executeAst({
ast: astWithUpdatedSource,
zoomToFit: optionalParams?.zoomToFit,

View File

@ -8,6 +8,7 @@ import {
VariableDeclaration,
VariableDeclarator,
sketchFromKclValue,
recast,
} from '../wasm'
import {
createCallExpressionStdLib,
@ -35,7 +36,12 @@ import {
ArtifactGraph,
getSweepFromSuspectedPath,
} from 'lang/std/artifactGraph'
import { kclManager, engineCommandManager, editorManager } from 'lib/singletons'
import {
kclManager,
engineCommandManager,
editorManager,
codeManager,
} from 'lib/singletons'
import { Node } from 'wasm-lib/kcl/bindings/Node'
// Apply Fillet To Selection
@ -253,6 +259,11 @@ async function updateAstAndFocus(
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
focusPath: pathToFilletNode,
})
const newCode = recast(updatedAst.newAst)
if (err(newCode)) return
await codeManager.updateCodeEditor(newCode)
if (updatedAst?.selections) {
editorManager.selectRange(updatedAst?.selections)
}

View File

@ -18,6 +18,7 @@ import {
sceneEntitiesManager,
engineCommandManager,
editorManager,
codeManager,
} from 'lib/singletons'
import {
horzVertInfo,
@ -512,6 +513,13 @@ export const modelingMachine = setup({
},
// end guards
actions: {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
'update code editor and write to file': async () => {
const newCode = recast(kclManager.ast)
if (err(newCode)) return
await codeManager.updateCodeEditor(newCode)
await codeManager.writeToFile()
},
'assign tool in context': assign({
currentTool: ({ event }) =>
'data' in event && event.data && 'tool' in event.data
@ -599,7 +607,6 @@ export const modelingMachine = setup({
if (trap(extrudeSketchRes)) return
const { modifiedAst, pathToExtrudeArg } = extrudeSketchRes
store.videoElement?.pause()
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
focusPath: [pathToExtrudeArg],
zoomToFit: true,
@ -608,11 +615,11 @@ export const modelingMachine = setup({
type: 'path',
},
})
if (!engineCommandManager.engineConnection?.idleMode) {
store.videoElement?.play().catch((e) => {
console.warn('Video playing was prevented', e)
})
}
const newCode = recast(updatedAst.newAst)
if (err(newCode)) return
await codeManager.updateCodeEditor(newCode)
if (updatedAst?.selections) {
editorManager.selectRange(updatedAst?.selections)
}
@ -646,7 +653,6 @@ export const modelingMachine = setup({
if (trap(revolveSketchRes)) return
const { modifiedAst, pathToRevolveArg } = revolveSketchRes
store.videoElement?.pause()
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
focusPath: [pathToRevolveArg],
zoomToFit: true,
@ -655,11 +661,11 @@ export const modelingMachine = setup({
type: 'path',
},
})
if (!engineCommandManager.engineConnection?.idleMode) {
store.videoElement?.play().catch((e) => {
console.warn('Video playing was prevented', e)
})
}
const newCode = recast(updatedAst.newAst)
if (err(newCode)) return
await codeManager.updateCodeEditor(newCode)
if (updatedAst?.selections) {
editorManager.selectRange(updatedAst?.selections)
}
@ -689,6 +695,10 @@ export const modelingMachine = setup({
}
await kclManager.updateAst(modifiedAst, true)
const newCode = recast(modifiedAst)
if (err(newCode)) return
await codeManager.updateCodeEditor(newCode)
})().catch(reportRejection)
},
'AST fillet': ({ event }) => {
@ -1560,7 +1570,10 @@ export const modelingMachine = setup({
},
},
entry: 'setup client side sketch segments',
entry: [
'setup client side sketch segments',
'update code editor and write to file',
],
},
'Await horizontal distance info': {