Support paths to names rather than just raw idents (#5778)
* Support paths to names rather than just raw idents Signed-off-by: Nick Cameron <nrc@ncameron.org> * A snapshot a day keeps the bugs away! 📷🐛 * A snapshot a day keeps the bugs away! 📷🐛 * A snapshot a day keeps the bugs away! 📷🐛 * A snapshot a day keeps the bugs away! 📷🐛 --------- Signed-off-by: Nick Cameron <nrc@ncameron.org> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
@ -74,6 +74,7 @@ import {
|
||||
import { BodyItem } from '@rust/kcl-lib/bindings/BodyItem'
|
||||
import { findKwArg } from './util'
|
||||
import { deleteEdgeTreatment } from './modifyAst/addEdgeTreatment'
|
||||
import { Name } from '@rust/kcl-lib/bindings/Name'
|
||||
|
||||
export function startSketchOnDefault(
|
||||
node: Node<Program>,
|
||||
@ -135,7 +136,7 @@ export function insertNewStartProfileAt(
|
||||
createLiteral(roundOff(at[0])),
|
||||
createLiteral(roundOff(at[1])),
|
||||
]),
|
||||
createIdentifier(varDec.node.id.name),
|
||||
createLocalName(varDec.node.id.name),
|
||||
])
|
||||
)
|
||||
const insertIndex = getInsertIndex(sketchNodePaths, planeNodePath, insertType)
|
||||
@ -392,7 +393,7 @@ export function extrudeSketch({
|
||||
|
||||
const extrudeCall = createCallExpressionStdLibKw(
|
||||
'extrude',
|
||||
createIdentifier(variableDeclarator.id.name),
|
||||
createLocalName(variableDeclarator.id.name),
|
||||
[createLabeledArg('length', distance)]
|
||||
)
|
||||
// index of the 'length' arg above. If you reorder the labeled args above,
|
||||
@ -436,7 +437,7 @@ export function loftSketches(
|
||||
} {
|
||||
const modifiedAst = structuredClone(node)
|
||||
const name = findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.LOFT)
|
||||
const elements = declarators.map((d) => createIdentifier(d.id.name))
|
||||
const elements = declarators.map((d) => createLocalName(d.id.name))
|
||||
const loft = createCallExpressionStdLib('loft', [
|
||||
createArrayExpression(elements),
|
||||
])
|
||||
@ -477,7 +478,7 @@ export function addShell({
|
||||
variableName ?? findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.SHELL)
|
||||
const shell = createCallExpressionStdLibKw(
|
||||
'shell',
|
||||
createIdentifier(sweepName),
|
||||
createLocalName(sweepName),
|
||||
[
|
||||
createLabeledArg('faces', createArrayExpression(faces)),
|
||||
createLabeledArg('thickness', thickness),
|
||||
@ -538,9 +539,9 @@ export function addSweep({
|
||||
variableName ?? findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.SWEEP)
|
||||
const call = createCallExpressionStdLibKw(
|
||||
'sweep',
|
||||
createIdentifier(targetDeclarator.id.name),
|
||||
createLocalName(targetDeclarator.id.name),
|
||||
[
|
||||
createLabeledArg('path', createIdentifier(trajectoryDeclarator.id.name)),
|
||||
createLabeledArg('path', createLocalName(trajectoryDeclarator.id.name)),
|
||||
createLabeledArg('sectional', createLiteral(sectional)),
|
||||
]
|
||||
)
|
||||
@ -614,7 +615,7 @@ export function revolveSketch(
|
||||
// TODO: hard coded 'X' axis for revolve MVP, should be changed.
|
||||
axis: createLiteral('X'),
|
||||
}),
|
||||
createIdentifier(variableDeclarator.id.name),
|
||||
createLocalName(variableDeclarator.id.name),
|
||||
])
|
||||
|
||||
if (shouldPipe) {
|
||||
@ -712,12 +713,12 @@ export function sketchOnExtrudedFace(
|
||||
pathToNode: sketchPathToNode,
|
||||
node: _node,
|
||||
},
|
||||
expression.callee.name,
|
||||
expression.callee.name.name,
|
||||
info.type === 'edgeCut' ? info : null
|
||||
)
|
||||
if (err(__tag)) return __tag
|
||||
const { modifiedAst, tag } = __tag
|
||||
_tag = createIdentifier(tag)
|
||||
_tag = createLocalName(tag)
|
||||
_node = modifiedAst
|
||||
} else {
|
||||
_tag = createLiteral(info.subType.toUpperCase())
|
||||
@ -725,7 +726,7 @@ export function sketchOnExtrudedFace(
|
||||
const newSketch = createVariableDeclaration(
|
||||
newSketchName,
|
||||
createCallExpressionStdLib('startSketchOn', [
|
||||
createIdentifier(extrudeName ? extrudeName : oldSketchName),
|
||||
createLocalName(extrudeName ? extrudeName : oldSketchName),
|
||||
_tag,
|
||||
]),
|
||||
undefined,
|
||||
@ -921,7 +922,7 @@ export function sketchOnOffsetPlane(
|
||||
const newSketch = createVariableDeclaration(
|
||||
newSketchName,
|
||||
createCallExpressionStdLib('startSketchOn', [
|
||||
createIdentifier(offsetPlaneName),
|
||||
createLocalName(offsetPlaneName),
|
||||
]),
|
||||
undefined,
|
||||
'const'
|
||||
@ -1054,6 +1055,22 @@ export function createIdentifier(name: string): Node<Identifier> {
|
||||
}
|
||||
}
|
||||
|
||||
export function createLocalName(name: string): Node<Name> {
|
||||
return {
|
||||
type: 'Name',
|
||||
start: 0,
|
||||
end: 0,
|
||||
moduleId: 0,
|
||||
outerAttrs: [],
|
||||
preComments: [],
|
||||
commentStart: 0,
|
||||
|
||||
abs_path: false,
|
||||
path: [],
|
||||
name: createIdentifier(name),
|
||||
}
|
||||
}
|
||||
|
||||
export function createPipeSubstitution(): Node<PipeSubstitution> {
|
||||
return {
|
||||
type: 'PipeSubstitution',
|
||||
@ -1078,17 +1095,7 @@ export function createCallExpressionStdLib(
|
||||
outerAttrs: [],
|
||||
preComments: [],
|
||||
commentStart: 0,
|
||||
callee: {
|
||||
type: 'Identifier',
|
||||
start: 0,
|
||||
end: 0,
|
||||
moduleId: 0,
|
||||
outerAttrs: [],
|
||||
preComments: [],
|
||||
commentStart: 0,
|
||||
|
||||
name,
|
||||
},
|
||||
callee: createLocalName(name),
|
||||
arguments: args,
|
||||
}
|
||||
}
|
||||
@ -1107,17 +1114,7 @@ export function createCallExpressionStdLibKw(
|
||||
preComments: [],
|
||||
commentStart: 0,
|
||||
nonCodeMeta: nonCodeMetaEmpty(),
|
||||
callee: {
|
||||
type: 'Identifier',
|
||||
start: 0,
|
||||
end: 0,
|
||||
moduleId: 0,
|
||||
outerAttrs: [],
|
||||
preComments: [],
|
||||
commentStart: 0,
|
||||
|
||||
name,
|
||||
},
|
||||
callee: createLocalName(name),
|
||||
unlabeled,
|
||||
arguments: args,
|
||||
}
|
||||
@ -1135,17 +1132,7 @@ export function createCallExpression(
|
||||
outerAttrs: [],
|
||||
preComments: [],
|
||||
commentStart: 0,
|
||||
callee: {
|
||||
type: 'Identifier',
|
||||
start: 0,
|
||||
end: 0,
|
||||
moduleId: 0,
|
||||
outerAttrs: [],
|
||||
preComments: [],
|
||||
commentStart: 0,
|
||||
|
||||
name,
|
||||
},
|
||||
callee: createLocalName(name),
|
||||
arguments: args,
|
||||
}
|
||||
}
|
||||
@ -1375,7 +1362,7 @@ export function replaceValueAtNodePath({
|
||||
}
|
||||
const { isSafe, value, replacer } = replaceCheckResult
|
||||
|
||||
if (!isSafe || value.type === 'Identifier') {
|
||||
if (!isSafe || value.type === 'Name') {
|
||||
return new Error('Not safe to replace')
|
||||
}
|
||||
|
||||
@ -1395,7 +1382,7 @@ export function moveValueIntoNewVariablePath(
|
||||
if (trap(meta)) return { modifiedAst: ast }
|
||||
const { isSafe, value, replacer } = meta
|
||||
|
||||
if (!isSafe || value.type === 'Identifier') return { modifiedAst: ast }
|
||||
if (!isSafe || value.type === 'Name') return { modifiedAst: ast }
|
||||
|
||||
const { insertIndex } = findAllPreviousVariablesPath(ast, memVars, pathToNode)
|
||||
let _node = structuredClone(ast)
|
||||
@ -1423,7 +1410,7 @@ export function moveValueIntoNewVariable(
|
||||
const meta = isNodeSafeToReplace(ast, sourceRange)
|
||||
if (trap(meta)) return { modifiedAst: ast }
|
||||
const { isSafe, value, replacer } = meta
|
||||
if (!isSafe || value.type === 'Identifier') return { modifiedAst: ast }
|
||||
if (!isSafe || value.type === 'Name') return { modifiedAst: ast }
|
||||
|
||||
const { insertIndex } = findAllPreviousVariables(ast, memVars, sourceRange)
|
||||
let _node = structuredClone(ast)
|
||||
@ -1648,25 +1635,25 @@ export async function deleteFromSelection(
|
||||
const dec = node.declaration
|
||||
if (
|
||||
(dec.init.type === 'CallExpression' &&
|
||||
(dec.init.callee.name === 'extrude' ||
|
||||
dec.init.callee.name === 'revolve') &&
|
||||
dec.init.arguments?.[1].type === 'Identifier' &&
|
||||
dec.init.arguments?.[1].name === varDecName) ||
|
||||
(dec.init.callee.name.name === 'extrude' ||
|
||||
dec.init.callee.name.name === 'revolve') &&
|
||||
dec.init.arguments?.[1].type === 'Name' &&
|
||||
dec.init.arguments?.[1].name.name === varDecName) ||
|
||||
(dec.init.type === 'CallExpressionKw' &&
|
||||
(dec.init.callee.name === 'extrude' ||
|
||||
dec.init.callee.name === 'revolve') &&
|
||||
dec.init.unlabeled?.type === 'Identifier' &&
|
||||
dec.init.unlabeled?.name === varDecName)
|
||||
(dec.init.callee.name.name === 'extrude' ||
|
||||
dec.init.callee.name.name === 'revolve') &&
|
||||
dec.init.unlabeled?.type === 'Name' &&
|
||||
dec.init.unlabeled?.name.name === varDecName)
|
||||
) {
|
||||
pathToNode = path
|
||||
extrudeNameToDelete = dec.id.name
|
||||
}
|
||||
if (
|
||||
dec.init.type === 'CallExpression' &&
|
||||
dec.init.callee.name === 'loft' &&
|
||||
dec.init.callee.name.name === 'loft' &&
|
||||
dec.init.arguments?.[0].type === 'ArrayExpression' &&
|
||||
dec.init.arguments?.[0].elements.some(
|
||||
(a) => a.type === 'Identifier' && a.name === varDecName
|
||||
(a) => a.type === 'Name' && a.name.name === varDecName
|
||||
)
|
||||
) {
|
||||
pathToNode = path
|
||||
@ -1685,7 +1672,7 @@ export async function deleteFromSelection(
|
||||
'CallExpression'
|
||||
)
|
||||
if (err(callExp)) return callExp
|
||||
extrudeNameToDelete = callExp.node.callee.name
|
||||
extrudeNameToDelete = callExp.node.callee.name.name
|
||||
} else {
|
||||
extrudeNameToDelete = varDec.node.id.name
|
||||
}
|
||||
@ -1881,8 +1868,8 @@ export async function deleteFromSelection(
|
||||
if (
|
||||
pipeBody[0].type === 'CallExpression' &&
|
||||
doNotDeleteProfileIfItHasBeenExtruded &&
|
||||
(pipeBody[0].callee.name === 'startSketchOn' ||
|
||||
pipeBody[0].callee.name === 'startProfileAt')
|
||||
(pipeBody[0].callee.name.name === 'startSketchOn' ||
|
||||
pipeBody[0].callee.name.name === 'startProfileAt')
|
||||
) {
|
||||
// remove varDec
|
||||
const varDecIndex = varDec.shallowPath[1][0] as number
|
||||
@ -1893,7 +1880,7 @@ export async function deleteFromSelection(
|
||||
// single expression profiles
|
||||
(varDec.node.init.type === 'CallExpressionKw' ||
|
||||
varDec.node.init.type === 'CallExpression') &&
|
||||
['circleThreePoint', 'circle'].includes(varDec.node.init.callee.name)
|
||||
['circleThreePoint', 'circle'].includes(varDec.node.init.callee.name.name)
|
||||
) {
|
||||
const varDecIndex = varDec.shallowPath[1][0] as number
|
||||
astClone.body.splice(varDecIndex, 1)
|
||||
@ -2021,7 +2008,7 @@ export function splitPipedProfile(
|
||||
const varName = varDec.node.declaration.id.name
|
||||
const newVarName = findUniqueName(_ast, 'sketch')
|
||||
const secondCallArgs = structuredClone(secondCall.arguments)
|
||||
secondCallArgs[1] = createIdentifier(newVarName)
|
||||
secondCallArgs[1] = createLocalName(newVarName)
|
||||
const firstCallOfNewPipe = createCallExpression(
|
||||
'startProfileAt',
|
||||
secondCallArgs
|
||||
|
Reference in New Issue
Block a user