Change to use artifact pathToNode (#7361)

* Change to use artifact pathToNode

* Fix to do bounds checking
This commit is contained in:
Jonathan Tran
2025-06-05 20:28:21 -04:00
committed by GitHub
parent 8e6483a47b
commit c43ec280f2
4 changed files with 20 additions and 43 deletions

View File

@ -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({

View File

@ -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 }
}

View File

@ -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
}
}
}

View File

@ -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<CallExpressionKw>(
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<CallExpressionKw>(astClone, pathToSweepNode, [
'CallExpressionKw',