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 { showSketchOnImportToast } from '@src/components/SketchOnImportToast'
import { useModelingContext } from '@src/hooks/useModelingContext' import { useModelingContext } from '@src/hooks/useModelingContext'
import { getNodeFromPath } from '@src/lang/queryAst' import { getNodeFromPath } from '@src/lang/queryAst'
import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils'
import type { SegmentArtifact } from '@src/lang/std/artifactGraph' import type { SegmentArtifact } from '@src/lang/std/artifactGraph'
import { import {
getArtifactOfTypes, getArtifactOfTypes,
@ -13,7 +12,7 @@ import {
getWallCodeRef, getWallCodeRef,
} from '@src/lang/std/artifactGraph' } from '@src/lang/std/artifactGraph'
import { isTopLevelModule } from '@src/lang/util' 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 { defaultSourceRange } from '@src/lang/wasm'
import type { DefaultPlaneStr } from '@src/lib/planes' import type { DefaultPlaneStr } from '@src/lib/planes'
import { getEventForSelectWithPoint } from '@src/lib/selections' import { getEventForSelectWithPoint } from '@src/lib/selections'
@ -247,10 +246,7 @@ export function useEngineConnectionSubscriptions() {
if (!faceInfo?.origin || !faceInfo?.z_axis || !faceInfo?.y_axis) if (!faceInfo?.origin || !faceInfo?.z_axis || !faceInfo?.y_axis)
return return
const { z_axis, y_axis, origin } = faceInfo const { z_axis, y_axis, origin } = faceInfo
const sketchPathToNode = getNodePathFromSourceRange( const sketchPathToNode = err(codeRef) ? [] : codeRef.pathToNode
kclManager.ast,
err(codeRef) ? defaultSourceRange() : codeRef.range
)
const getEdgeCutMeta = (): null | EdgeCutInfo => { const getEdgeCutMeta = (): null | EdgeCutInfo => {
let chamferInfo: { let chamferInfo: {
@ -332,16 +328,13 @@ export function useEngineConnectionSubscriptions() {
{ type: 'sweep', ...extrusion }, { type: 'sweep', ...extrusion },
kclManager.artifactGraph kclManager.artifactGraph
)[0] || null )[0] || null
const lastChildCodeRef = const lastChildCodeRef: PathToNode | null =
lastChild?.type === 'compositeSolid' lastChild?.type === 'compositeSolid'
? lastChild.codeRef.range ? lastChild.codeRef.pathToNode
: null : null
const extrudePathToNode = !err(extrusion) const extrudePathToNode = !err(extrusion)
? getNodePathFromSourceRange( ? lastChildCodeRef || extrusion.codeRef.pathToNode
kclManager.ast,
lastChildCodeRef || extrusion.codeRef.range
)
: [] : []
sceneInfra.modelingSend({ sceneInfra.modelingSend({

View File

@ -272,11 +272,7 @@ export function getPathToExtrudeForSegmentSelection(
const sweepArtifact = getSweepArtifactFromSelection(selection, artifactGraph) const sweepArtifact = getSweepArtifactFromSelection(selection, artifactGraph)
if (err(sweepArtifact)) return sweepArtifact if (err(sweepArtifact)) return sweepArtifact
const pathToExtrudeNode = getNodePathFromSourceRange( const pathToExtrudeNode = sweepArtifact.codeRef.pathToNode
ast,
sweepArtifact.codeRef.range
)
if (err(pathToExtrudeNode)) return pathToExtrudeNode
return { pathToSegmentNode, pathToExtrudeNode } return { pathToSegmentNode, pathToExtrudeNode }
} }

View File

@ -112,15 +112,14 @@ export async function deleteFromSelection(
selection.artifact?.type === 'startSketchOnPlane' || selection.artifact?.type === 'startSketchOnPlane' ||
selection.artifact?.type === 'startSketchOnFace' selection.artifact?.type === 'startSketchOnFace'
) { ) {
const sketchVarDec = getNodePathFromSourceRange( const sketchVarDec = selection.artifact.codeRef.pathToNode
astClone, if (sketchVarDec.length >= 2) {
selection.artifact.codeRef.range
)
const sketchBodyIndex = Number(sketchVarDec[1][0]) const sketchBodyIndex = Number(sketchVarDec[1][0])
astClone.body.splice(sketchBodyIndex, 1) astClone.body.splice(sketchBodyIndex, 1)
return astClone return astClone
} }
} }
}
// Module import and expression case, need to find and delete both // Module import and expression case, need to find and delete both
const statement = getNodeFromPath<ImportStatement>( const statement = getNodeFromPath<ImportStatement>(

View File

@ -30,7 +30,6 @@ import {
createTagDeclarator, createTagDeclarator,
} from '@src/lang/create' } from '@src/lang/create'
import { getNodeFromPath } from '@src/lang/queryAst' import { getNodeFromPath } from '@src/lang/queryAst'
import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils'
import { import {
addTagForSketchOnFace, addTagForSketchOnFace,
sketchLineHelperMapKw, sketchLineHelperMapKw,
@ -301,18 +300,9 @@ function modifyAstWithTagsForEdgeSelection(
artifactGraph artifactGraph
) )
if (err(sweepArtifact)) return sweepArtifact if (err(sweepArtifact)) return sweepArtifact
const pathToSweepNode = getNodePathFromSourceRange(
astClone,
sweepArtifact.codeRef.range
)
if (err(pathToSweepNode)) return pathToSweepNode
// Get path to segment // Get path to segment
const pathToSegmentNode = getNodePathFromSourceRange( const pathToSegmentNode = selection.codeRef.pathToNode
astClone,
selection.codeRef.range
)
if (err(pathToSegmentNode)) return pathToSegmentNode
const segmentNode = getNodeFromPath<CallExpressionKw>( const segmentNode = getNodeFromPath<CallExpressionKw>(
astClone, astClone,
@ -322,6 +312,11 @@ function modifyAstWithTagsForEdgeSelection(
if (err(segmentNode)) return segmentNode if (err(segmentNode)) return segmentNode
// Check whether selection is a valid segment // 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)) { if (!(segmentNode.node.callee.name.name in sketchLineHelperMapKw)) {
return new Error('Selection is not a sketch segment') return new Error('Selection is not a sketch segment')
} }
@ -431,10 +426,7 @@ function modifyAstWithTagForWallFace(
) )
if (err(segment)) return segment if (err(segment)) return segment
const pathToSegmentNode = getNodePathFromSourceRange( const pathToSegmentNode = segment.codeRef.pathToNode
astClone,
segment.codeRef.range
)
const result = modifyAstWithTagForSketchSegment(astClone, pathToSegmentNode) const result = modifyAstWithTagForSketchSegment(astClone, pathToSegmentNode)
if (err(result)) return result if (err(result)) return result
@ -482,10 +474,7 @@ function modifyAstWithTagForCapFace(
return new Error('Only extrusion and revolve caps are currently supported') return new Error('Only extrusion and revolve caps are currently supported')
} }
const pathToSweepNode = getNodePathFromSourceRange( const pathToSweepNode = sweepArtifact.codeRef.pathToNode
astClone,
sweepArtifact.codeRef.range
)
const callExp = getNodeFromPath<CallExpressionKw>(astClone, pathToSweepNode, [ const callExp = getNodeFromPath<CallExpressionKw>(astClone, pathToSweepNode, [
'CallExpressionKw', 'CallExpressionKw',