diff --git a/src/hooks/useEngineConnectionSubscriptions.ts b/src/hooks/useEngineConnectionSubscriptions.ts index a4e512aa6..922d6c45d 100644 --- a/src/hooks/useEngineConnectionSubscriptions.ts +++ b/src/hooks/useEngineConnectionSubscriptions.ts @@ -3,7 +3,6 @@ import { useEffect, useRef } from 'react' import { showSketchOnImportToast } from '@src/components/SketchOnImportToast' import { useModelingContext } from '@src/hooks/useModelingContext' import { getNodeFromPath } from '@src/lang/queryAst' -import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils' import type { SegmentArtifact } from '@src/lang/std/artifactGraph' import { getArtifactOfTypes, @@ -13,7 +12,7 @@ import { getWallCodeRef, } from '@src/lang/std/artifactGraph' import { isTopLevelModule } from '@src/lang/util' -import type { CallExpressionKw } from '@src/lang/wasm' +import type { CallExpressionKw, PathToNode } from '@src/lang/wasm' import { defaultSourceRange } from '@src/lang/wasm' import type { DefaultPlaneStr } from '@src/lib/planes' import { getEventForSelectWithPoint } from '@src/lib/selections' @@ -247,10 +246,7 @@ export function useEngineConnectionSubscriptions() { if (!faceInfo?.origin || !faceInfo?.z_axis || !faceInfo?.y_axis) return const { z_axis, y_axis, origin } = faceInfo - const sketchPathToNode = getNodePathFromSourceRange( - kclManager.ast, - err(codeRef) ? defaultSourceRange() : codeRef.range - ) + const sketchPathToNode = err(codeRef) ? [] : codeRef.pathToNode const getEdgeCutMeta = (): null | EdgeCutInfo => { let chamferInfo: { @@ -332,16 +328,13 @@ export function useEngineConnectionSubscriptions() { { type: 'sweep', ...extrusion }, kclManager.artifactGraph )[0] || null - const lastChildCodeRef = + const lastChildCodeRef: PathToNode | null = lastChild?.type === 'compositeSolid' - ? lastChild.codeRef.range + ? lastChild.codeRef.pathToNode : null const extrudePathToNode = !err(extrusion) - ? getNodePathFromSourceRange( - kclManager.ast, - lastChildCodeRef || extrusion.codeRef.range - ) + ? lastChildCodeRef || extrusion.codeRef.pathToNode : [] sceneInfra.modelingSend({ diff --git a/src/lang/modifyAst/addEdgeTreatment.ts b/src/lang/modifyAst/addEdgeTreatment.ts index 9b02edeb1..3e6fb52ff 100644 --- a/src/lang/modifyAst/addEdgeTreatment.ts +++ b/src/lang/modifyAst/addEdgeTreatment.ts @@ -272,11 +272,7 @@ export function getPathToExtrudeForSegmentSelection( const sweepArtifact = getSweepArtifactFromSelection(selection, artifactGraph) if (err(sweepArtifact)) return sweepArtifact - const pathToExtrudeNode = getNodePathFromSourceRange( - ast, - sweepArtifact.codeRef.range - ) - if (err(pathToExtrudeNode)) return pathToExtrudeNode + const pathToExtrudeNode = sweepArtifact.codeRef.pathToNode return { pathToSegmentNode, pathToExtrudeNode } } diff --git a/src/lang/modifyAst/deleteFromSelection.ts b/src/lang/modifyAst/deleteFromSelection.ts index 0781bf5e9..f7eaaabf6 100644 --- a/src/lang/modifyAst/deleteFromSelection.ts +++ b/src/lang/modifyAst/deleteFromSelection.ts @@ -112,13 +112,12 @@ export async function deleteFromSelection( selection.artifact?.type === 'startSketchOnPlane' || selection.artifact?.type === 'startSketchOnFace' ) { - const sketchVarDec = getNodePathFromSourceRange( - astClone, - selection.artifact.codeRef.range - ) - const sketchBodyIndex = Number(sketchVarDec[1][0]) - astClone.body.splice(sketchBodyIndex, 1) - return astClone + const sketchVarDec = selection.artifact.codeRef.pathToNode + if (sketchVarDec.length >= 2) { + const sketchBodyIndex = Number(sketchVarDec[1][0]) + astClone.body.splice(sketchBodyIndex, 1) + return astClone + } } } diff --git a/src/lang/modifyAst/tagManagement.ts b/src/lang/modifyAst/tagManagement.ts index b2c6cabaf..e394664bc 100644 --- a/src/lang/modifyAst/tagManagement.ts +++ b/src/lang/modifyAst/tagManagement.ts @@ -30,7 +30,6 @@ import { createTagDeclarator, } from '@src/lang/create' import { getNodeFromPath } from '@src/lang/queryAst' -import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils' import { addTagForSketchOnFace, sketchLineHelperMapKw, @@ -301,18 +300,9 @@ function modifyAstWithTagsForEdgeSelection( artifactGraph ) if (err(sweepArtifact)) return sweepArtifact - const pathToSweepNode = getNodePathFromSourceRange( - astClone, - sweepArtifact.codeRef.range - ) - if (err(pathToSweepNode)) return pathToSweepNode // Get path to segment - const pathToSegmentNode = getNodePathFromSourceRange( - astClone, - selection.codeRef.range - ) - if (err(pathToSegmentNode)) return pathToSegmentNode + const pathToSegmentNode = selection.codeRef.pathToNode const segmentNode = getNodeFromPath( astClone, @@ -322,6 +312,11 @@ function modifyAstWithTagsForEdgeSelection( if (err(segmentNode)) return segmentNode // Check whether selection is a valid segment + if (segmentNode.node.type !== 'CallExpressionKw') { + return new Error('Selection segment node not found or wrong type', { + cause: segmentNode, + }) + } if (!(segmentNode.node.callee.name.name in sketchLineHelperMapKw)) { return new Error('Selection is not a sketch segment') } @@ -431,10 +426,7 @@ function modifyAstWithTagForWallFace( ) if (err(segment)) return segment - const pathToSegmentNode = getNodePathFromSourceRange( - astClone, - segment.codeRef.range - ) + const pathToSegmentNode = segment.codeRef.pathToNode const result = modifyAstWithTagForSketchSegment(astClone, pathToSegmentNode) if (err(result)) return result @@ -482,10 +474,7 @@ function modifyAstWithTagForCapFace( return new Error('Only extrusion and revolve caps are currently supported') } - const pathToSweepNode = getNodePathFromSourceRange( - astClone, - sweepArtifact.codeRef.range - ) + const pathToSweepNode = sweepArtifact.codeRef.pathToNode const callExp = getNodeFromPath(astClone, pathToSweepNode, [ 'CallExpressionKw',