Allow deletion of only item in AST (#6335)
* Update test to check for deletion of only operation * Allow updateEditorWithAstAndWriteToFile to clear AST * Update src/lang/codeManager.ts Co-authored-by: Jonathan Tran <jonnytran@gmail.com> * Weave `isDeleting` through to `deleteSelectionPromise` * fmt --------- Co-authored-by: Jonathan Tran <jonnytran@gmail.com> Co-authored-by: Pierre Jacquier <pierrejacquier39@gmail.com>
This commit is contained in:
@ -430,5 +430,17 @@ profile003 = startProfileAt([0, -4.93], sketch001)
|
||||
await editor.expectEditor.not.toContain('sketch001 =')
|
||||
await editor.expectEditor.not.toContain('profile002 = ')
|
||||
})
|
||||
|
||||
await test.step(`Delete the remaining plane via feature tree`, async () => {
|
||||
const operationButton = await toolbar.getFeatureTreeOperation(
|
||||
'Offset Plane',
|
||||
0
|
||||
)
|
||||
await operationButton.click({ button: 'left' })
|
||||
await page.keyboard.press('Delete')
|
||||
|
||||
// Verify the plane code is gone, and https://github.com/KittyCAD/modeling-app/issues/5988 is fixed.
|
||||
await editor.expectEditor.not.toContain('plane001 =')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -165,13 +165,16 @@ export default class CodeManager {
|
||||
}
|
||||
}
|
||||
|
||||
async updateEditorWithAstAndWriteToFile(ast: Program) {
|
||||
async updateEditorWithAstAndWriteToFile(
|
||||
ast: Program,
|
||||
options?: Partial<{ isDeleting: boolean }>
|
||||
) {
|
||||
// We clear the AST when it cannot be parsed. If we are trying to write an
|
||||
// empty AST, it's probably because of an earlier error. That's a bad state
|
||||
// to be in, and it's not going to be pretty, but at the least, let's not
|
||||
// permanently delete the user's code. If you want to clear the scene, call
|
||||
// updateCodeStateEditor directly.
|
||||
if (ast.body.length === 0) return
|
||||
// permanently delete the user's code accidentally.
|
||||
// if you want to clear the scene, pass in the `isDeleting` option.
|
||||
if (ast.body.length === 0 && !options?.isDeleting) return
|
||||
const newCode = recast(ast)
|
||||
if (err(newCode)) return
|
||||
// Test to see if we can parse the recast code, and never update the editor with bad code.
|
||||
|
@ -54,6 +54,7 @@ export async function updateModelingState(
|
||||
},
|
||||
options?: {
|
||||
focusPath?: Array<PathToNode>
|
||||
isDeleting?: boolean
|
||||
}
|
||||
): Promise<void> {
|
||||
let updatedAst: {
|
||||
@ -69,7 +70,10 @@ export async function updateModelingState(
|
||||
|
||||
// Step 2: Update the code editor and save file
|
||||
await dependencies.codeManager.updateEditorWithAstAndWriteToFile(
|
||||
updatedAst.newAst
|
||||
updatedAst.newAst,
|
||||
{
|
||||
isDeleting: options?.isDeleting,
|
||||
}
|
||||
)
|
||||
|
||||
// Step 3: Set focus on the newly added code if needed
|
||||
|
@ -38,9 +38,16 @@ export async function deleteSelectionPromise(
|
||||
if (testExecute.errors.length) {
|
||||
return new Error(deletionErrorMessage)
|
||||
}
|
||||
await updateModelingState(modifiedAst, EXECUTION_TYPE_REAL, {
|
||||
await updateModelingState(
|
||||
modifiedAst,
|
||||
EXECUTION_TYPE_REAL,
|
||||
{
|
||||
kclManager,
|
||||
editorManager,
|
||||
codeManager,
|
||||
})
|
||||
},
|
||||
{
|
||||
isDeleting: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user