Add edit flow for named constants / parameters (#5911)

* Add support for forcing kcl input create variable

* Command palette padding tweak

* Make traverse function work for ExpressionStatements

* Add utilities for getting earliest safe index in AST

* Fix the insertIndex logic to not be based on the selection anymore

* Add workflow to create a named constant

* Fix bug with nameEndInDigits matcher

* Tweak command config

* Add a three-dot menu to feature tree pane to create parameters

* Add E2E test for create parameter flow

* Remove edit flow oops

* Fix tsc error

* Fix E2E test

* Update named constant position in edit flow test

* Add tags into consideration for safe insert index

Per @Irev-dev's helpful feedback, with unit tests!

* Fix tsc by removing a generic type

* Remove unused imports

* Fix lints

* A snapshot a day keeps the bugs away! 📷🐛

* Add utilities for working with variable declarations

* Add "edit parameter" user flow

* Add edit flow config

* WIP working on de-bloating useCalculateKclExpreesion

* Add the ability to specify a `displayName` for an arg

* Add utility to type check on SourceRanges

* Review step design tweak fixes

* Refactor useCalculateKclExpression to take a sourceRange

* Make option arg validation work for objects and arrays

Using an admittedly dumb stringification approach

* Make edit flow never move the constant to be edited

* Add E2E test section

* Fix lints

* Remove lying comment, tiny CSS tweak

* A snapshot a day keeps the bugs away! 📷🐛

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Frank Noirot
2025-03-20 16:41:09 -04:00
committed by GitHub
parent 2c6404f671
commit 9da8574103
18 changed files with 301 additions and 29 deletions

View File

@ -97,6 +97,8 @@ import { createProfileStartHandle } from 'clientSideScene/segments'
import { DRAFT_POINT } from 'clientSideScene/sceneInfra'
import { setAppearance } from 'lang/modifyAst/setAppearance'
import { DRAFT_DASHED_LINE } from 'clientSideScene/sceneEntities'
import { Node } from '@rust/kcl-lib/bindings/Node'
import { updateModelingState } from 'lang/modelingWorkflows'
export const MODELING_PERSIST_KEY = 'MODELING_PERSIST_KEY'
@ -311,6 +313,10 @@ export type ModelingMachineEvent =
type: 'event.parameter.create'
data: ModelingCommandSchema['event.parameter.create']
}
| {
type: 'event.parameter.edit'
data: ModelingCommandSchema['event.parameter.edit']
}
| { type: 'Export'; data: ModelingCommandSchema['Export'] }
| { type: 'Make'; data: ModelingCommandSchema['Make'] }
| { type: 'Extrude'; data?: ModelingCommandSchema['Extrude'] }
@ -2323,6 +2329,39 @@ export const modelingMachine = setup({
}
}
),
'actor.parameter.edit': fromPromise(
async ({
input,
}: {
input: ModelingCommandSchema['event.parameter.edit'] | undefined
}) => {
if (!input) return new Error('No input provided')
// Get the variable AST node to edit
const { nodeToEdit, value } = input
const newAst = structuredClone(kclManager.ast)
const variableNode = getNodeFromPath<Node<VariableDeclarator>>(
newAst,
nodeToEdit
)
if (
err(variableNode) ||
variableNode.node.type !== 'VariableDeclarator' ||
!variableNode.node
) {
return new Error('No variable found, this is a bug')
}
// Mutate the variable's value
variableNode.node.init = value.valueAst
await updateModelingState(newAst, {
codeManager,
editorManager,
kclManager,
})
}
),
'set-up-draft-circle': fromPromise(
async (_: {
input: Pick<ModelingMachineContext, 'sketchDetails'> & {
@ -2574,6 +2613,9 @@ export const modelingMachine = setup({
'event.parameter.create': {
target: '#Modeling.parameter.creating',
},
'event.parameter.edit': {
target: '#Modeling.parameter.editing',
},
Export: {
target: 'Exporting',
@ -3893,6 +3935,18 @@ export const modelingMachine = setup({
onError: ['#Modeling.idle'],
},
},
editing: {
invoke: {
src: 'actor.parameter.edit',
id: 'actor.parameter.edit',
input: ({ event }) => {
if (event.type !== 'event.parameter.edit') return undefined
return event.data
},
onDone: ['#Modeling.idle'],
onError: ['#Modeling.idle'],
},
},
},
},