import { PathToNode, VariableDeclarator } from 'lang/wasm' import { engineCommandManager } from 'lang/std/engineConnection' import { Axis, Selection, SelectionRangeTypeMap, Selections, } from 'lib/selections' import { assign, createMachine } from 'xstate' import { isCursorInSketchCommandRange } from 'lang/util' import { getNodePathFromSourceRange } from 'lang/queryAst' import { kclManager } from 'lang/KclSingleton' import { horzVertInfo, applyConstraintHorzVert, } from 'components/Toolbar/HorzVert' import { applyConstraintHorzVertAlign, horzVertDistanceInfo, } from 'components/Toolbar/SetHorzVertDistance' import { angleBetweenInfo } from 'components/Toolbar/SetAngleBetween' import { angleLengthInfo } from 'components/Toolbar/setAngleLength' import { applyConstraintEqualLength, setEqualLengthInfo, } from 'components/Toolbar/EqualLength' import { addStartProfileAt, extrudeSketch } from 'lang/modifyAst' import { getNodeFromPath } from '../lang/queryAst' import { CallExpression, PipeExpression } from '../lang/wasm' import { applyConstraintEqualAngle, equalAngleInfo, } from 'components/Toolbar/EqualAngle' import { applyRemoveConstrainingValues, removeConstrainingValuesInfo, } from 'components/Toolbar/RemoveConstrainingValues' import { intersectInfo } from 'components/Toolbar/Intersect' import { absDistanceInfo, applyConstraintAxisAlign, } from 'components/Toolbar/SetAbsDistance' import { Models } from '@kittycad/lib/dist/types/src' import { ModelingCommandSchema } from 'lib/commandBarConfigs/modelingCommandConfig' import { DefaultPlaneStr, sceneEntitiesManager, quaternionFromSketchGroup, sketchGroupFromPathToNode, } from 'clientSideScene/sceneEntities' import { sceneInfra } from 'clientSideScene/sceneInfra' export const MODELING_PERSIST_KEY = 'MODELING_PERSIST_KEY' export type SetSelections = | { selectionType: 'singleCodeCursor' selection?: Selection } | { selectionType: 'otherSelection' selection: Axis } | { selectionType: 'completeSelection' selection: Selections } | { selectionType: 'mirrorCodeMirrorSelections' selection: Selections } export type ModelingMachineEvent = | { type: 'Enter sketch' data?: { forceNewSketch?: boolean } } | { type: 'Select default plane' data: { plane: DefaultPlaneStr; normal: [number, number, number] } } | { type: 'Set selection'; data: SetSelections } | { type: 'Sketch no face' } | { type: 'Toggle gui mode' } | { type: 'Cancel' } | { type: 'CancelSketch' } | { type: 'Add start point' } | { type: 'Make segment horizontal' } | { type: 'Make segment vertical' } | { type: 'Constrain horizontal distance' } | { type: 'Constrain ABS X' } | { type: 'Constrain ABS Y' } | { type: 'Constrain vertical distance' } | { type: 'Constrain angle' } | { type: 'Constrain perpendicular distance' } | { type: 'Constrain horizontally align' } | { type: 'Constrain vertically align' } | { type: 'Constrain snap to X' } | { type: 'Constrain snap to Y' } | { type: 'Constrain length' } | { type: 'Constrain equal length' } | { type: 'Constrain parallel' } | { type: 'Constrain remove constraints' } | { type: 'Re-execute' } | { type: 'Extrude'; data?: ModelingCommandSchema['Extrude'] } | { type: 'Equip Line tool' } | { type: 'Equip tangential arc to' } | { type: 'done.invoke.animate-to-face' data: { sketchPathToNode: PathToNode sketchNormalBackUp: [number, number, number] | null } } export type MoveDesc = { line: number; snippet: string } export const modelingMachine = createMachine( { /** @xstate-layout N4IgpgJg5mDOIC5QFkD2EwBsCWA7KAxAMICGuAxlgNoAMAuoqAA6qzYAu2qujIAHogC0AdgCsAZgB04gEyjhADnEA2GgoUAWJQBoQAT0QBGGuICckmoZkbTM42YWKAvk91oMOfAQDKYdgAJYLDByTm5aBiQQFjYwniiBBFtpGhlxDRphGg1ZURlhXQMEcRLDSVF5UwV84XEK8Rc3dCw8KElsCEwwHz9A4NCuXAjeGI5B3kTBLWFJDWV5hVSZZTrlBUKjS1FZiUrDeWVDasaQdxb8ds7ugFFcdjAAJ0CAaz9yAAthqNG4iaF07bCNLiRYlGiZUyZDbFYQzfIaLSQ5Sw0TGZQnM6eNodLoEW73J6wV7sD5UQyRZisMbcP4IQRrGiSdRmfIqUxrCrQlbKcoQ4RHDTWUx5DHNLGXXHXPjsB4AVwwX0psXGCSEohWO3Uh2UOSsmmhhn2jPyNGUolMhjWwvZoo8rUk3mJH2IZEomEdb0+9BGVN+qoQhoUPOMwgRi1BplkBqOZUDCkN4ksClEClt5zaHpJ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VZGeg8jSFhGhDoQHWPGALsDmFoRcECn5wwHgCiCOxfzX0EEWDKFyGySQk0B0DckEEEiVSgX2DyFGktDehxDAEGKIPpEoUehehBFwhPGUXRwkDUA+3cMsGrXr3fHWPdXZCZmLxUFLyRimi1g2UqEDmcnZzAM5w-EwSuJAWehNh1HjDymqF2GjGSBvDylSGqAtBhPy32SGUqR8yxU+V+L3CDCZgyFLn5BqFSDBWSABDvHmFsH5DhJlQxUFT8xRL01byMBUU1V7ykQ3yUQDXowsG8m5mG1RDhJjRSibVRKOjsSkDUCKlH0SNEjKiwh2CqlUAWhTEO0xi+O5RcxXTGX5O9jsUp1oWI0rjHWqOrkHSqGhLhL-T5OpMjyOGFEQjvByAcDyEjBujUCZDjxg0yFGjqDhJU1VLNLXwtMZAyFWBM1HzbADW5FmhhMcGRCRktjhLq2K28yiyFSpNix9OTH-3ZHkjPDTxKH63R15mTAtEbBtEc0VMf0sTVMmXsBHhKGTEnXRNiNUBNmVxWBl1wmRF90Jxh3YHLMpX2GYTqhrMNLWC5HBPmByRWC7w2XbK1350eQ3G7PchsAnXHmKjUFjx-wQBT1mEUMjCKiDFkCnKf0D38ANyNz6OTKIOsAkBNiYS0HsDSENjcibCrnjATCOCbFvOz07PnMIk331msBMG3z7xgRjBo3UATCTHP2LPekv1u04Bvx7nnLkE0Fmj8hUAciemuhgQYwBKDA5GvisA+LULIiwOgMtzwNzAQO-KtHKF7FTL9VkEwqKEEh5DmAZTPyqnVBYLYKsLWO9I2L7BouWVUDqCekmhcIy2vHsGnS0FkFTCgt8M4H8MyMCN4vPPdUtCkBelZxsHkFpzkJ5AAojLnmmlUIVOKM0L8KgF0OyJolyNzG-McBNlhGQjynZHZSmibF5HsGoKFmKgKi6KcCAA */ id: 'Modeling', tsTypes: {} as import('./modelingMachine.typegen').Typegen0, predictableActionArguments: true, preserveActionOrder: true, context: { guiMode: 'default', tool: null as Models['SceneToolType_type'] | null, selection: [] as string[], selectionRanges: { otherSelections: [], codeBasedSelections: [], } as Selections, selectionRangeTypeMap: {} as SelectionRangeTypeMap, sketchPathToNode: null as PathToNode | null, // maybe too specific, and we should have a generic pathToNode, but being specific seems less risky when I'm not sure sketchEnginePathId: '' as string, sketchPlaneId: '' as string, sketchNormalBackUp: null as null | [number, number, number], moveDescs: [] as MoveDesc[], }, schema: { events: {} as ModelingMachineEvent, }, states: { idle: { on: { 'Set selection': { target: 'idle', internal: true, actions: 'Set selection', }, 'Enter sketch': [ { target: 'animating to existing sketch', cond: 'Selection is on face', actions: ['set sketch metadata'], }, 'Sketch no face', ], Extrude: { target: 'idle', cond: 'has valid extrude selection', actions: ['AST extrude'], internal: true, }, }, entry: 'reset client scene mouse handlers', }, Sketch: { states: { SketchIdle: { on: { 'Set selection': { target: 'SketchIdle', internal: true, actions: 'Set selection', }, 'Make segment vertical': { cond: 'Can make selection vertical', target: 'SketchIdle', internal: true, actions: ['Make selection vertical'], }, 'Make segment horizontal': { target: 'SketchIdle', internal: true, cond: 'Can make selection horizontal', actions: ['Make selection horizontal'], }, 'Constrain horizontal distance': { target: 'Await horizontal distance info', cond: 'Can constrain horizontal distance', }, 'Constrain vertical distance': { target: 'Await vertical distance info', cond: 'Can constrain vertical distance', }, 'Constrain ABS X': { target: 'Await ABS X info', cond: 'Can constrain ABS X', }, 'Constrain ABS Y': { target: 'Await ABS Y info', cond: 'Can constrain ABS Y', }, 'Constrain angle': { target: 'Await angle info', cond: 'Can constrain angle', }, 'Constrain length': { target: 'Await length info', cond: 'Can constrain length', }, 'Constrain perpendicular distance': { target: 'Await perpendicular distance info', cond: 'Can constrain perpendicular distance', }, 'Constrain horizontally align': { cond: 'Can constrain horizontally align', target: 'SketchIdle', internal: true, actions: ['Constrain horizontally align'], }, 'Constrain vertically align': { cond: 'Can constrain vertically align', target: 'SketchIdle', internal: true, actions: ['Constrain vertically align'], }, 'Constrain snap to X': { cond: 'Can constrain snap to X', target: 'SketchIdle', internal: true, actions: ['Constrain snap to X'], }, 'Constrain snap to Y': { cond: 'Can constrain snap to Y', target: 'SketchIdle', internal: true, actions: ['Constrain snap to Y'], }, 'Constrain equal length': { cond: 'Can constrain equal length', target: 'SketchIdle', internal: true, actions: ['Constrain equal length'], }, 'Constrain parallel': { target: 'SketchIdle', internal: true, cond: 'Can canstrain parallel', actions: ['Constrain parallel'], }, 'Constrain remove constraints': { target: 'SketchIdle', internal: true, cond: 'Can constrain remove constraints', actions: ['Constrain remove constraints'], }, 'Re-execute': { target: 'SketchIdle', internal: true, actions: ['set sketchMetadata from pathToNode'], }, 'Equip Line tool': 'Line tool', 'Equip tangential arc to': { target: 'Tangential arc to', cond: 'is editing existing sketch', }, }, entry: 'setup client side sketch segments', }, 'Await horizontal distance info': { invoke: { src: 'Get horizontal info', id: 'get-horizontal-info', onDone: { target: 'SketchIdle', actions: 'Set selection', }, onError: 'SketchIdle', }, }, 'Await vertical distance info': { invoke: { src: 'Get vertical info', id: 'get-vertical-info', onDone: { target: 'SketchIdle', actions: 'Set selection', }, onError: 'SketchIdle', }, }, 'Await ABS X info': { invoke: { src: 'Get ABS X info', id: 'get-abs-x-info', onDone: { target: 'SketchIdle', actions: 'Set selection', }, onError: 'SketchIdle', }, }, 'Await ABS Y info': { invoke: { src: 'Get ABS Y info', id: 'get-abs-y-info', onDone: { target: 'SketchIdle', actions: 'Set selection', }, onError: 'SketchIdle', }, }, 'Await angle info': { invoke: { src: 'Get angle info', id: 'get-angle-info', onDone: { target: 'SketchIdle', actions: 'Set selection', }, onError: 'SketchIdle', }, }, 'Await length info': { invoke: { src: 'Get length info', id: 'get-length-info', onDone: { target: 'SketchIdle', actions: 'Set selection', }, onError: 'SketchIdle', }, }, 'Await perpendicular distance info': { invoke: { src: 'Get perpendicular distance info', id: 'get-perpendicular-distance-info', onDone: { target: 'SketchIdle', actions: 'Set selection', }, onError: 'SketchIdle', }, }, 'Line tool': { exit: [], on: { 'Set selection': { target: 'Line tool', description: `This is just here to stop one of the higher level "Set selections" firing when we are just trying to set the IDE code without triggering a full engine-execute`, internal: true, }, 'Equip tangential arc to': { target: 'Tangential arc to', cond: 'is editing existing sketch', }, }, states: { Init: { always: [ { target: 'normal', cond: 'is editing existing sketch', actions: 'set up draft line', }, 'No Points', ], }, normal: { on: { 'Set selection': { target: 'normal', internal: true, }, }, }, 'No Points': { entry: 'setup noPoints onClick listener', on: { 'Add start point': { target: 'normal', actions: 'set up draft line without teardown', }, Cancel: '#Modeling.Sketch.undo startSketchOn', }, }, }, initial: 'Init', }, Init: { always: [ { target: 'SketchIdle', cond: 'is editing existing sketch', }, 'Line tool', ], }, 'Tangential arc to': { entry: 'set up draft arc', on: { 'Set selection': { target: 'Tangential arc to', internal: true, }, 'Equip Line tool': 'Line tool', }, }, 'undo startSketchOn': { invoke: { src: 'AST-undo-startSketchOn', id: 'AST-undo-startSketchOn', onDone: '#Modeling.idle', }, }, }, initial: 'Init', on: { CancelSketch: '.SketchIdle', }, exit: [ 'sketch exit execute', 'animate after sketch', 'tear down client sketch', 'remove sketch grid', ], entry: ['add axis n grid', 'conditionally equip line tool'], }, 'Sketch no face': { entry: 'show default planes', exit: 'hide default planes', on: { 'Select default plane': { target: 'animating to plane', actions: ['reset sketch metadata'], }, }, }, 'animating to plane': { invoke: { src: 'animate-to-face', id: 'animate-to-face', onDone: { target: 'Sketch', actions: 'set new sketch metadata', }, }, on: { 'Set selection': { target: 'animating to plane', internal: true, }, }, }, 'animating to existing sketch': { invoke: [ { src: 'animate-to-sketch', id: 'animate-to-sketch', onDone: 'Sketch', }, ], }, }, initial: 'idle', on: { Cancel: { target: 'idle', // TODO what if we're existing extrude equipped, should these actions still be fired? // maybe cancel needs to have a guard for if else logic? actions: ['reset sketch metadata'], }, 'Set selection': { target: '#Modeling', internal: true, actions: 'Set selection', }, }, }, { guards: { 'is editing existing sketch': ({ sketchPathToNode }) => { // should check that the variable declaration is a pipeExpression // and that the pipeExpression contains a "startProfileAt" callExpression if (!sketchPathToNode) return false const variableDeclaration = getNodeFromPath( kclManager.ast, sketchPathToNode, 'VariableDeclarator' ).node if (variableDeclaration.type !== 'VariableDeclarator') return false const pipeExpression = variableDeclaration.init if (pipeExpression.type !== 'PipeExpression') return false const hasStartProfileAt = pipeExpression.body.some( (item) => item.type === 'CallExpression' && item.callee.name === 'startProfileAt' ) return hasStartProfileAt && pipeExpression.body.length > 2 }, 'Can make selection horizontal': ({ selectionRanges }) => horzVertInfo(selectionRanges, 'horizontal').enabled, 'Can make selection vertical': ({ selectionRanges }) => horzVertInfo(selectionRanges, 'vertical').enabled, 'Can constrain horizontal distance': ({ selectionRanges }) => horzVertDistanceInfo({ selectionRanges, constraint: 'setHorzDistance' }) .enabled, 'Can constrain vertical distance': ({ selectionRanges }) => horzVertDistanceInfo({ selectionRanges, constraint: 'setVertDistance' }) .enabled, 'Can constrain ABS X': ({ selectionRanges }) => absDistanceInfo({ selectionRanges, constraint: 'xAbs' }).enabled, 'Can constrain ABS Y': ({ selectionRanges }) => absDistanceInfo({ selectionRanges, constraint: 'yAbs' }).enabled, 'Can constrain angle': ({ selectionRanges }) => angleBetweenInfo({ selectionRanges }).enabled || angleLengthInfo({ selectionRanges, angleOrLength: 'setAngle' }).enabled, 'Can constrain length': ({ selectionRanges }) => angleLengthInfo({ selectionRanges }).enabled, 'Can constrain perpendicular distance': ({ selectionRanges }) => intersectInfo({ selectionRanges }).enabled, 'Can constrain horizontally align': ({ selectionRanges }) => horzVertDistanceInfo({ selectionRanges, constraint: 'setHorzDistance' }) .enabled, 'Can constrain vertically align': ({ selectionRanges }) => horzVertDistanceInfo({ selectionRanges, constraint: 'setHorzDistance' }) .enabled, 'Can constrain snap to X': ({ selectionRanges }) => absDistanceInfo({ selectionRanges, constraint: 'snapToXAxis' }).enabled, 'Can constrain snap to Y': ({ selectionRanges }) => absDistanceInfo({ selectionRanges, constraint: 'snapToYAxis' }).enabled, 'Can constrain equal length': ({ selectionRanges }) => setEqualLengthInfo({ selectionRanges }).enabled, 'Can canstrain parallel': ({ selectionRanges }) => equalAngleInfo({ selectionRanges }).enabled, 'Can constrain remove constraints': ({ selectionRanges }) => removeConstrainingValuesInfo({ selectionRanges }).enabled, }, // end guards actions: { 'set sketchMetadata from pathToNode': assign(({ sketchPathToNode }) => { if (!sketchPathToNode) return {} return getSketchMetadataFromPathToNode(sketchPathToNode) }), 'hide default planes': () => { sceneInfra.removeDefaultPlanes() kclManager.hidePlanes() }, 'reset sketch metadata': assign({ sketchPathToNode: null, sketchEnginePathId: '', sketchPlaneId: '', }), 'set sketch metadata': assign(({ selectionRanges }) => { const sourceRange = selectionRanges.codeBasedSelections[0].range const sketchPathToNode = getNodePathFromSourceRange( kclManager.ast, sourceRange ) return getSketchMetadataFromPathToNode( sketchPathToNode, selectionRanges ) }), 'set new sketch metadata': assign((_, { data }) => data), // TODO implement source ranges for all of these constraints // need to make the async like the modal constraints 'Make selection horizontal': ({ selectionRanges, sketchPathToNode }) => { const { modifiedAst } = applyConstraintHorzVert( selectionRanges, 'horizontal', kclManager.ast, kclManager.programMemory ) sceneEntitiesManager.updateAstAndRejigSketch( sketchPathToNode || [], modifiedAst ) }, 'Make selection vertical': ({ selectionRanges, sketchPathToNode }) => { const { modifiedAst } = applyConstraintHorzVert( selectionRanges, 'vertical', kclManager.ast, kclManager.programMemory ) sceneEntitiesManager.updateAstAndRejigSketch( sketchPathToNode || [], modifiedAst ) }, 'Constrain horizontally align': ({ selectionRanges, sketchPathToNode, }) => { const { modifiedAst } = applyConstraintHorzVertAlign({ selectionRanges, constraint: 'setVertDistance', }) sceneEntitiesManager.updateAstAndRejigSketch( sketchPathToNode || [], modifiedAst ) }, 'Constrain vertically align': ({ selectionRanges, sketchPathToNode }) => { const { modifiedAst } = applyConstraintHorzVertAlign({ selectionRanges, constraint: 'setHorzDistance', }) sceneEntitiesManager.updateAstAndRejigSketch( sketchPathToNode || [], modifiedAst ) }, 'Constrain snap to X': ({ selectionRanges, sketchPathToNode }) => { const { modifiedAst } = applyConstraintAxisAlign({ selectionRanges, constraint: 'snapToXAxis', }) sceneEntitiesManager.updateAstAndRejigSketch( sketchPathToNode || [], modifiedAst ) }, 'Constrain snap to Y': ({ selectionRanges, sketchPathToNode }) => { const { modifiedAst } = applyConstraintAxisAlign({ selectionRanges, constraint: 'snapToYAxis', }) sceneEntitiesManager.updateAstAndRejigSketch( sketchPathToNode || [], modifiedAst ) }, 'Constrain equal length': ({ selectionRanges, sketchPathToNode }) => { const { modifiedAst } = applyConstraintEqualLength({ selectionRanges, }) sceneEntitiesManager.updateAstAndRejigSketch( sketchPathToNode || [], modifiedAst ) }, 'Constrain parallel': ({ selectionRanges, sketchPathToNode }) => { const { modifiedAst } = applyConstraintEqualAngle({ selectionRanges, }) sceneEntitiesManager.updateAstAndRejigSketch( sketchPathToNode || [], modifiedAst ) }, 'Constrain remove constraints': ({ selectionRanges, sketchPathToNode, }) => { const { modifiedAst } = applyRemoveConstrainingValues({ selectionRanges, }) sceneEntitiesManager.updateAstAndRejigSketch( sketchPathToNode || [], modifiedAst ) }, 'AST extrude': (_, event) => { if (!event.data) return const { selection, distance } = event.data let ast = kclManager.ast if ( 'variableName' in distance && distance.variableName && distance.insertIndex !== undefined ) { console.log('adding variable!', distance) const newBody = [...ast.body] newBody.splice( distance.insertIndex, 0, distance.variableDeclarationAst ) ast.body = newBody } const pathToNode = getNodePathFromSourceRange( ast, selection.codeBasedSelections[0].range ) const { modifiedAst, pathToExtrudeArg } = extrudeSketch( ast, pathToNode, true, 'variableName' in distance ? distance.variableIdentifierAst : distance.valueAst ) // TODO not handling focusPath correctly I think kclManager.updateAst(modifiedAst, true, { focusPath: pathToExtrudeArg, }) }, 'conditionally equip line tool': (_, { type }) => { if (type === 'done.invoke.animate-to-face') { sceneInfra.modelingSend('Equip Line tool') } }, 'setup client side sketch segments': ({ sketchPathToNode }, { type }) => { if (Object.keys(sceneEntitiesManager.activeSegments).length > 0) { sceneEntitiesManager .tearDownSketch({ removeAxis: false }) .then(() => { sceneEntitiesManager.setupSketch({ sketchPathToNode: sketchPathToNode || [], }) }) } else { sceneEntitiesManager.setupSketch({ sketchPathToNode: sketchPathToNode || [], }) } }, 'animate after sketch': () => { sceneEntitiesManager.animateAfterSketch() }, 'tear down client sketch': () => { if (sceneEntitiesManager.activeSegments) { sceneEntitiesManager.tearDownSketch({ removeAxis: false }) } }, 'remove sketch grid': () => sceneEntitiesManager.removeSketchGrid(), 'set up draft line': ({ sketchPathToNode }) => { sceneEntitiesManager.setUpDraftLine(sketchPathToNode || []) }, 'set up draft arc': ({ sketchPathToNode }) => { sceneEntitiesManager.setUpDraftArc(sketchPathToNode || []) }, 'set up draft line without teardown': ({ sketchPathToNode }) => sceneEntitiesManager.setupSketch({ sketchPathToNode: sketchPathToNode || [], draftSegment: 'line', }), 'show default planes': () => { sceneInfra.showDefaultPlanes() sceneEntitiesManager.setupDefaultPlaneHover() kclManager.showPlanes() }, 'setup noPoints onClick listener': ({ sketchPathToNode }) => { sceneEntitiesManager.createIntersectionPlane() const sketchGroup = sketchGroupFromPathToNode({ pathToNode: sketchPathToNode || [], ast: kclManager.ast, programMemory: kclManager.programMemory, }) const quaternion = quaternionFromSketchGroup(sketchGroup) sceneEntitiesManager.intersectionPlane && sceneEntitiesManager.intersectionPlane.setRotationFromQuaternion( quaternion ) sceneInfra.setCallbacks({ onClick: async (args) => { if (!args) return if (args.event.which !== 1) return const { intersection2d } = args if (!intersection2d || !sketchPathToNode) return const { modifiedAst } = addStartProfileAt( kclManager.ast, sketchPathToNode, [intersection2d.x, intersection2d.y] ) await kclManager.updateAst(modifiedAst, false) sceneEntitiesManager.removeIntersectionPlane() sceneInfra.modelingSend('Add start point') }, }) }, 'add axis n grid': ({ sketchPathToNode }) => sceneEntitiesManager.createSketchAxis(sketchPathToNode || []), 'reset client scene mouse handlers': () => { // when not in sketch mode we don't need any mouse listeners // (note the orbit controls are always active though) sceneInfra.resetMouseListeners() }, }, // end actions } ) function getSketchMetadataFromPathToNode( pathToNode: PathToNode, selectionRanges?: Selections ) { const pipeExpression = getNodeFromPath( kclManager.ast, pathToNode, 'PipeExpression' ).node if (pipeExpression.type !== 'PipeExpression') return {} const sketchCallExpression = pipeExpression.body.find( (e) => e.type === 'CallExpression' && e.callee.name === 'startSketchOn' ) as CallExpression if (!sketchCallExpression) return {} let sketchEnginePathId: string if (selectionRanges) { sketchEnginePathId = isCursorInSketchCommandRange( engineCommandManager.artifactMap, selectionRanges ) || '' } else { const _selectionRanges: Selections = { otherSelections: [], codeBasedSelections: [ { range: [pipeExpression.start, pipeExpression.end], type: 'default' }, ], } sketchEnginePathId = isCursorInSketchCommandRange( engineCommandManager.artifactMap, _selectionRanges ) || '' } return { sketchPathToNode: pathToNode, sketchEnginePathId, } }