Fix for code enabling/disabling output context on single node (#8776)

This commit is contained in:
Michael Mauderer 2024-01-17 10:18:40 +00:00 committed by GitHub
parent 184128949e
commit 080690b3ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 18 deletions

View File

@ -179,24 +179,23 @@ const isOutputContextOverridden = computed({
set(shouldOverride) { set(shouldOverride) {
const module = projectStore.module const module = projectStore.module
if (!module) return if (!module) return
const replacements = shouldOverride
? [Ast.TextLiteral.new(projectStore.executionMode)]
: undefined
const edit = props.node.rootSpan.module.edit() const edit = props.node.rootSpan.module.edit()
const newAst = prefixes.modify( const replacementText = shouldOverride
edit, ? [Ast.TextLiteral.new(projectStore.executionMode, edit)]
props.node.rootSpan, : undefined
projectStore.isOutputContextEnabled const replacements = projectStore.isOutputContextEnabled
? { ? {
enableOutputContext: undefined, enableOutputContext: undefined,
disableOutputContext: replacements, disableOutputContext: replacementText,
} }
: { : {
enableOutputContext: replacements, enableOutputContext: replacementText,
disableOutputContext: undefined, disableOutputContext: undefined,
}, }
) const expression = props.node.rootSpan
graph.setNodeContent(props.node.rootSpan.exprId, newAst.code()) const newAst = prefixes.modify(edit, expression, replacements)
const code = newAst.code()
graph.setNodeContent(props.node.rootSpan.exprId, code)
}, },
}) })

View File

@ -106,4 +106,11 @@ test.each([
const intron = Ast.parse(source, edit) const intron = Ast.parse(source, edit)
const instantiated = pattern.instantiate(edit, [intron.exprId]) const instantiated = pattern.instantiate(edit, [intron.exprId])
expect(instantiated.code(edit)).toBe(result) expect(instantiated.code(edit)).toBe(result)
// Check that `instantiate` has not affected the base module.
const intron2 = Ast.parse(source, edit)
const originalParent = intron2.parent
const edit2 = edit.edit()
pattern.instantiate(edit2, [intron2.exprId])
expect(edit.get(intron2.exprId)!.parent).toBe(originalParent)
}) })

View File

@ -1,3 +1,4 @@
import { assert } from '@/util/assert'
import { Ast } from '@/util/ast' import { Ast } from '@/util/ast'
import { MutableModule } from '@/util/ast/abstract' import { MutableModule } from '@/util/ast/abstract'
@ -44,7 +45,14 @@ export class Pattern {
for (const matched of placeholders(ast, this.placeholder)) { for (const matched of placeholders(ast, this.placeholder)) {
const replacement = subtrees.shift() const replacement = subtrees.shift()
if (replacement === undefined) break if (replacement === undefined) break
edit.get(replacement)!.parent = matched.parent const replacementAst = edit.splice(edit.get(replacement))
if (replacementAst === null) {
console.error(
'Subtree ID provided to `instantiate` is not accessible in the `edit` module.',
)
continue
}
replacementAst.parent = matched.parent
matched.ref.node = replacement matched.ref.node = replacement
} }
return ast return ast