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:
Frank Noirot
2025-04-19 05:27:23 -04:00
committed by GitHub
parent 8c1a95833d
commit f30fc376ee
4 changed files with 36 additions and 10 deletions

View File

@ -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.