diff --git a/src/components/Toolbar/RemoveConstrainingValues.tsx b/src/components/Toolbar/RemoveConstrainingValues.tsx index 7a28370ff..1ab9d7ae6 100644 --- a/src/components/Toolbar/RemoveConstrainingValues.tsx +++ b/src/components/Toolbar/RemoveConstrainingValues.tsx @@ -15,21 +15,15 @@ import type { Selection, Selections } from '@src/lib/selections' import { kclManager } from '@src/lib/singletons' import { err } from '@src/lib/trap' -export function removeConstrainingValuesInfo({ - selectionRanges, - pathToNodes, -}: { - selectionRanges: Selections - pathToNodes?: Array -}): +export function removeConstrainingValuesInfo(pathToNodes: Array): | { transforms: TransformInfo[] enabled: boolean updatedSelectionRanges: Selections } | Error { - const _nodes = selectionRanges.graphSelections.map(({ codeRef }) => { - const tmp = getNodeFromPath(kclManager.ast, codeRef.pathToNode) + const _nodes = pathToNodes.map((pathToNode) => { + const tmp = getNodeFromPath(kclManager.ast, pathToNode) if (tmp instanceof Error) return tmp return tmp.node }) @@ -37,19 +31,17 @@ export function removeConstrainingValuesInfo({ if (err(_err1)) return _err1 const nodes = _nodes as Expr[] - const updatedSelectionRanges = pathToNodes - ? { - otherSelections: [], - graphSelections: nodes.map( - (node): Selection => ({ - codeRef: codeRefFromRange( - topLevelRange(node.start, node.end), - kclManager.ast - ), - }) + const updatedSelectionRanges = { + otherSelections: [], + graphSelections: nodes.map( + (node): Selection => ({ + codeRef: codeRefFromRange( + topLevelRange(node.start, node.end), + kclManager.ast ), - } - : selectionRanges + }) + ), + } const isAllTooltips = nodes.every( (node) => node?.type === 'CallExpressionKw' && @@ -78,10 +70,12 @@ export function applyRemoveConstrainingValues({ pathToNodeMap: PathToNodeMap } | Error { - const constraint = removeConstrainingValuesInfo({ - selectionRanges, - pathToNodes, - }) + pathToNodes = + pathToNodes || + selectionRanges.graphSelections.map(({ codeRef }) => { + return codeRef.pathToNode + }) + const constraint = removeConstrainingValuesInfo(pathToNodes) if (err(constraint)) return constraint const { transforms, updatedSelectionRanges } = constraint diff --git a/src/editor/manager.ts b/src/editor/manager.ts index 2f591c8a5..276bff0e1 100644 --- a/src/editor/manager.ts +++ b/src/editor/manager.ts @@ -333,6 +333,15 @@ export default class EditorManager { createEditorSelection(selections: Selections) { let codeBasedSelections = [] + + // Handle empty graphSelections array to avoid runtime errors + if (selections.graphSelections.length === 0) { + const defaultCursor = EditorSelection.cursor( + this._editorView?.state.doc.length || 0 + ) + return EditorSelection.create([defaultCursor], 0) + } + for (const selection of selections.graphSelections) { const safeEnd = Math.min( selection.codeRef.range[1], diff --git a/src/machines/modelingMachine.test.ts b/src/machines/modelingMachine.test.ts index eabf4469e..2181d30d8 100644 --- a/src/machines/modelingMachine.test.ts +++ b/src/machines/modelingMachine.test.ts @@ -1252,18 +1252,11 @@ p3 = [342.51, 216.38], if (err(callExp)) { throw new Error('Failed to get CallExpressionKw node') } - const constraintInfo = getConstraintInfoKw( - callExp.node, - codeManager.code, - artifact.codeRef.pathToNode, - filter - ) - const constraint = constraintInfo[constraintIndex] // Now that we're in sketchIdle state, test the "Constrain with named value" event actor.send({ type: 'Constrain remove constraints', - data: constraint.pathToNode, + data: artifact.codeRef.pathToNode, }) // Wait for the state to change in response to the constraint diff --git a/src/machines/modelingMachine.ts b/src/machines/modelingMachine.ts index c8cb86628..534820c5d 100644 --- a/src/machines/modelingMachine.ts +++ b/src/machines/modelingMachine.ts @@ -752,10 +752,13 @@ export const modelingMachine = setup({ event, }) => { if (event.type !== 'Constrain remove constraints') return false - const info = removeConstrainingValuesInfo({ - selectionRanges, - pathToNodes: event.data && [event.data], - }) + + const pathToNodes = event.data + ? [event.data] + : selectionRanges.graphSelections.map(({ codeRef }) => { + return codeRef.pathToNode + }) + const info = removeConstrainingValuesInfo(pathToNodes) if (err(info)) return false return info.enabled },