Refactor writeToFile and updateCodeEditor to happen at appropriate times
This commit is contained in:
@ -656,10 +656,17 @@ const ConstraintSymbol = ({
|
|||||||
kclManager.ast,
|
kclManager.ast,
|
||||||
kclManager.programMemory
|
kclManager.programMemory
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!transform) return
|
if (!transform) return
|
||||||
const { modifiedAst } = transform
|
const { modifiedAst } = transform
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
// 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) {
|
} catch (e) {
|
||||||
console.log('error', e)
|
console.log('error', e)
|
||||||
}
|
}
|
||||||
|
@ -846,6 +846,10 @@ export class SceneEntities {
|
|||||||
segmentName
|
segmentName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const newCode = recast(modifiedAst)
|
||||||
|
if (err(newCode)) return
|
||||||
|
codeManager.updateCodeEditor(newCode)
|
||||||
},
|
},
|
||||||
onMove: (args) => {
|
onMove: (args) => {
|
||||||
this.onDragSegment({
|
this.onDragSegment({
|
||||||
@ -970,10 +974,14 @@ export class SceneEntities {
|
|||||||
if (trap(_node)) return
|
if (trap(_node)) return
|
||||||
const sketchInit = _node.node?.declarations?.[0]?.init
|
const sketchInit = _node.node?.declarations?.[0]?.init
|
||||||
|
|
||||||
if (sketchInit.type === 'PipeExpression') {
|
if (sketchInit.type !== 'PipeExpression') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
updateRectangleSketch(sketchInit, x, y, tags[0])
|
updateRectangleSketch(sketchInit, x, y, tags[0])
|
||||||
|
|
||||||
let _recastAst = parse(recast(_ast))
|
const newCode = recast(_ast)
|
||||||
|
let _recastAst = parse(newCode)
|
||||||
if (trap(_recastAst)) return
|
if (trap(_recastAst)) return
|
||||||
_ast = _recastAst
|
_ast = _recastAst
|
||||||
|
|
||||||
@ -1006,7 +1014,9 @@ export class SceneEntities {
|
|||||||
sgPaths.forEach((seg, index) =>
|
sgPaths.forEach((seg, index) =>
|
||||||
this.updateSegment(seg, index, 0, _ast, orthoFactor, sketch)
|
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
|
if (err(moddedResult)) return
|
||||||
modded = moddedResult.modifiedAst
|
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)
|
if (trap(_recastAst)) return Promise.reject(_recastAst)
|
||||||
_ast = _recastAst
|
_ast = _recastAst
|
||||||
|
|
||||||
// Update the primary AST and unequip the rectangle tool
|
// Update the primary AST and unequip the rectangle tool
|
||||||
await kclManager.executeAstMock(_ast)
|
await kclManager.executeAstMock(_ast)
|
||||||
sceneInfra.modelingSend({ type: 'Finish circle' })
|
sceneInfra.modelingSend({ type: 'Finish circle' })
|
||||||
|
|
||||||
|
codeManager.updateCodeEditor(newCode)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -1208,6 +1222,7 @@ export class SceneEntities {
|
|||||||
forward,
|
forward,
|
||||||
position,
|
position,
|
||||||
})
|
})
|
||||||
|
await codeManager.writeToFile()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onDrag: async ({
|
onDrag: async ({
|
||||||
|
@ -2,13 +2,13 @@ import {
|
|||||||
SetVarNameModal,
|
SetVarNameModal,
|
||||||
createSetVarNameModal,
|
createSetVarNameModal,
|
||||||
} from 'components/SetVarNameModal'
|
} from 'components/SetVarNameModal'
|
||||||
import { editorManager, kclManager } from 'lib/singletons'
|
import { editorManager, kclManager, codeManager } from 'lib/singletons'
|
||||||
import { reportRejection, trap } from 'lib/trap'
|
import { reportRejection, trap, err } from 'lib/trap'
|
||||||
import { moveValueIntoNewVariable } from 'lang/modifyAst'
|
import { moveValueIntoNewVariable } from 'lang/modifyAst'
|
||||||
import { isNodeSafeToReplace } from 'lang/queryAst'
|
import { isNodeSafeToReplace } from 'lang/queryAst'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { useModelingContext } from './useModelingContext'
|
import { useModelingContext } from './useModelingContext'
|
||||||
import { PathToNode, SourceRange } from 'lang/wasm'
|
import { PathToNode, SourceRange, recast } from 'lang/wasm'
|
||||||
import { useKclContext } from 'lang/KclProvider'
|
import { useKclContext } from 'lang/KclProvider'
|
||||||
import { toSync } from 'lib/utils'
|
import { toSync } from 'lib/utils'
|
||||||
|
|
||||||
@ -57,6 +57,11 @@ export function useConvertToVariable(range?: SourceRange) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
await kclManager.updateAst(_modifiedAst, true)
|
await kclManager.updateAst(_modifiedAst, true)
|
||||||
|
|
||||||
|
const newCode = recast(_modifiedAst)
|
||||||
|
if (err(newCode)) return
|
||||||
|
codeManager.updateCodeEditor(newCode)
|
||||||
|
|
||||||
return pathToReplacedNode
|
return pathToReplacedNode
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('error', e)
|
console.log('error', e)
|
||||||
|
@ -351,9 +351,6 @@ export class KclManager {
|
|||||||
this.clearAst()
|
this.clearAst()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
codeManager.updateCodeEditor(newCode)
|
|
||||||
// Write the file to disk.
|
|
||||||
await codeManager.writeToFile()
|
|
||||||
this._ast = { ...newAst }
|
this._ast = { ...newAst }
|
||||||
|
|
||||||
const { logs, errors, execState } = await executeAst({
|
const { logs, errors, execState } = await executeAst({
|
||||||
@ -491,11 +488,6 @@ export class KclManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (execute) {
|
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({
|
await this.executeAst({
|
||||||
ast: astWithUpdatedSource,
|
ast: astWithUpdatedSource,
|
||||||
zoomToFit: optionalParams?.zoomToFit,
|
zoomToFit: optionalParams?.zoomToFit,
|
||||||
|
@ -8,6 +8,7 @@ import {
|
|||||||
VariableDeclaration,
|
VariableDeclaration,
|
||||||
VariableDeclarator,
|
VariableDeclarator,
|
||||||
sketchFromKclValue,
|
sketchFromKclValue,
|
||||||
|
recast,
|
||||||
} from '../wasm'
|
} from '../wasm'
|
||||||
import {
|
import {
|
||||||
createCallExpressionStdLib,
|
createCallExpressionStdLib,
|
||||||
@ -35,7 +36,12 @@ import {
|
|||||||
ArtifactGraph,
|
ArtifactGraph,
|
||||||
getSweepFromSuspectedPath,
|
getSweepFromSuspectedPath,
|
||||||
} from 'lang/std/artifactGraph'
|
} 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'
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
||||||
|
|
||||||
// Apply Fillet To Selection
|
// Apply Fillet To Selection
|
||||||
@ -253,6 +259,11 @@ async function updateAstAndFocus(
|
|||||||
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
|
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
|
||||||
focusPath: pathToFilletNode,
|
focusPath: pathToFilletNode,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const newCode = recast(updatedAst.newAst)
|
||||||
|
if (err(newCode)) return
|
||||||
|
await codeManager.updateCodeEditor(newCode)
|
||||||
|
|
||||||
if (updatedAst?.selections) {
|
if (updatedAst?.selections) {
|
||||||
editorManager.selectRange(updatedAst?.selections)
|
editorManager.selectRange(updatedAst?.selections)
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import {
|
|||||||
sceneEntitiesManager,
|
sceneEntitiesManager,
|
||||||
engineCommandManager,
|
engineCommandManager,
|
||||||
editorManager,
|
editorManager,
|
||||||
|
codeManager,
|
||||||
} from 'lib/singletons'
|
} from 'lib/singletons'
|
||||||
import {
|
import {
|
||||||
horzVertInfo,
|
horzVertInfo,
|
||||||
@ -512,6 +513,13 @@ export const modelingMachine = setup({
|
|||||||
},
|
},
|
||||||
// end guards
|
// end guards
|
||||||
actions: {
|
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({
|
'assign tool in context': assign({
|
||||||
currentTool: ({ event }) =>
|
currentTool: ({ event }) =>
|
||||||
'data' in event && event.data && 'tool' in event.data
|
'data' in event && event.data && 'tool' in event.data
|
||||||
@ -599,7 +607,6 @@ export const modelingMachine = setup({
|
|||||||
if (trap(extrudeSketchRes)) return
|
if (trap(extrudeSketchRes)) return
|
||||||
const { modifiedAst, pathToExtrudeArg } = extrudeSketchRes
|
const { modifiedAst, pathToExtrudeArg } = extrudeSketchRes
|
||||||
|
|
||||||
store.videoElement?.pause()
|
|
||||||
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
|
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
|
||||||
focusPath: [pathToExtrudeArg],
|
focusPath: [pathToExtrudeArg],
|
||||||
zoomToFit: true,
|
zoomToFit: true,
|
||||||
@ -608,11 +615,11 @@ export const modelingMachine = setup({
|
|||||||
type: 'path',
|
type: 'path',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if (!engineCommandManager.engineConnection?.idleMode) {
|
|
||||||
store.videoElement?.play().catch((e) => {
|
const newCode = recast(updatedAst.newAst)
|
||||||
console.warn('Video playing was prevented', e)
|
if (err(newCode)) return
|
||||||
})
|
await codeManager.updateCodeEditor(newCode)
|
||||||
}
|
|
||||||
if (updatedAst?.selections) {
|
if (updatedAst?.selections) {
|
||||||
editorManager.selectRange(updatedAst?.selections)
|
editorManager.selectRange(updatedAst?.selections)
|
||||||
}
|
}
|
||||||
@ -646,7 +653,6 @@ export const modelingMachine = setup({
|
|||||||
if (trap(revolveSketchRes)) return
|
if (trap(revolveSketchRes)) return
|
||||||
const { modifiedAst, pathToRevolveArg } = revolveSketchRes
|
const { modifiedAst, pathToRevolveArg } = revolveSketchRes
|
||||||
|
|
||||||
store.videoElement?.pause()
|
|
||||||
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
|
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
|
||||||
focusPath: [pathToRevolveArg],
|
focusPath: [pathToRevolveArg],
|
||||||
zoomToFit: true,
|
zoomToFit: true,
|
||||||
@ -655,11 +661,11 @@ export const modelingMachine = setup({
|
|||||||
type: 'path',
|
type: 'path',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if (!engineCommandManager.engineConnection?.idleMode) {
|
|
||||||
store.videoElement?.play().catch((e) => {
|
const newCode = recast(updatedAst.newAst)
|
||||||
console.warn('Video playing was prevented', e)
|
if (err(newCode)) return
|
||||||
})
|
await codeManager.updateCodeEditor(newCode)
|
||||||
}
|
|
||||||
if (updatedAst?.selections) {
|
if (updatedAst?.selections) {
|
||||||
editorManager.selectRange(updatedAst?.selections)
|
editorManager.selectRange(updatedAst?.selections)
|
||||||
}
|
}
|
||||||
@ -689,6 +695,10 @@ export const modelingMachine = setup({
|
|||||||
}
|
}
|
||||||
|
|
||||||
await kclManager.updateAst(modifiedAst, true)
|
await kclManager.updateAst(modifiedAst, true)
|
||||||
|
|
||||||
|
const newCode = recast(modifiedAst)
|
||||||
|
if (err(newCode)) return
|
||||||
|
await codeManager.updateCodeEditor(newCode)
|
||||||
})().catch(reportRejection)
|
})().catch(reportRejection)
|
||||||
},
|
},
|
||||||
'AST fillet': ({ event }) => {
|
'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': {
|
'Await horizontal distance info': {
|
||||||
|
Reference in New Issue
Block a user