[BUG] mutate ast to keep comments for pipe split ast-mod (#6128)

* mutate ast to keep comments

* remove commented out code

* fix case where no comments in split
This commit is contained in:
Kurt Hutten
2025-04-04 14:01:23 +11:00
committed by GitHub
parent 1a59fc4f99
commit d270447777
4 changed files with 56 additions and 30 deletions

View File

@ -46,12 +46,5 @@ export function angleLengthInfo({
selectionRanges.graphSelections.length <= 1 &&
isAllTooltips &&
transforms.every(Boolean)
console.log(
'enabled',
enabled,
selectionRanges.graphSelections.length,
isAllTooltips,
transforms.every(Boolean)
)
return { enabled, transforms }
}

View File

@ -1030,19 +1030,25 @@ sketch003 = startSketchOn(XZ)
describe('Testing splitPipedProfile', () => {
it('should split the pipe expression correctly', () => {
const codeBefore = `part001 = startSketchOn(XZ)
const codeBefore = `// comment 1
part001 = startSketchOn(XZ)
|> startProfileAt([1, 2], %)
// comment 2
|> line([3, 4], %)
|> line([5, 6], %)
|> close(%)
// comment 3
extrude001 = extrude(5, part001)
`
const expectedCodeAfter = `sketch001 = startSketchOn(XZ)
const expectedCodeAfter = `// comment 1
sketch001 = startSketchOn(XZ)
part001 = startProfileAt([1, 2], sketch001)
// comment 2
|> line([3, 4], %)
|> line([5, 6], %)
|> close(%)
// comment 3
extrude001 = extrude(5, part001)
`

View File

@ -3,10 +3,10 @@ import type { Models } from '@kittycad/lib'
import type { BodyItem } from '@rust/kcl-lib/bindings/BodyItem'
import type { Name } from '@rust/kcl-lib/bindings/Name'
import type { Node } from '@rust/kcl-lib/bindings/Node'
import type { NonCodeMeta } from '@rust/kcl-lib/bindings/NonCodeMeta'
import {
createArrayExpression,
createCallExpression,
createCallExpressionStdLib,
createCallExpressionStdLibKw,
createIdentifier,
@ -1642,7 +1642,7 @@ export function splitPipedProfile(
}
| Error {
const _ast = structuredClone(ast)
const varDec = getNodeFromPath<VariableDeclaration>(
const varDec = getNodeFromPath<Node<VariableDeclaration>>(
_ast,
pathToPipe,
'VariableDeclaration'
@ -1666,26 +1666,53 @@ export function splitPipedProfile(
const newVarName = findUniqueName(_ast, 'sketch')
const secondCallArgs = structuredClone(secondCall.arguments)
secondCallArgs[1] = createLocalName(newVarName)
const firstCallOfNewPipe = createCallExpression(
'startProfileAt',
secondCallArgs
)
const newSketch = createVariableDeclaration(
newVarName,
varDec.node.declaration.init.body[0]
)
const newProfile = createVariableDeclaration(
varName,
varDec.node.declaration.init.body.length <= 2
? firstCallOfNewPipe
: createPipeExpression([
firstCallOfNewPipe,
...varDec.node.declaration.init.body.slice(2),
])
)
const startSketchOnBrokenIntoNewVarDec = structuredClone(varDec.node)
const profileBrokenIntoItsOwnVar = structuredClone(varDec.node)
if (
startSketchOnBrokenIntoNewVarDec.declaration.init.type !== 'PipeExpression'
) {
return new Error('clonedVarDec1 is not a PipeExpression')
}
varDec.node.declaration.init =
startSketchOnBrokenIntoNewVarDec.declaration.init.body[0]
varDec.node.declaration.id.name = newVarName
if (profileBrokenIntoItsOwnVar.declaration.init.type !== 'PipeExpression') {
return new Error('clonedVarDec2 is not a PipeExpression')
}
profileBrokenIntoItsOwnVar.declaration.init.body =
profileBrokenIntoItsOwnVar.declaration.init.body.slice(1)
if (
!(
profileBrokenIntoItsOwnVar.declaration.init.body[0].type ===
'CallExpression' &&
profileBrokenIntoItsOwnVar.declaration.init.body[0].callee.name.name ===
'startProfileAt'
)
) {
return new Error('problem breaking pipe, expect startProfileAt to be first')
}
profileBrokenIntoItsOwnVar.declaration.init.body[0].arguments[1] =
createLocalName(newVarName)
profileBrokenIntoItsOwnVar.declaration.id.name = varName
profileBrokenIntoItsOwnVar.preComments = [] // we'll duplicate the comments since the new variable will have it to
// new pipe has one less from the start, so need to decrement comments for them to remain in the same place
if (profileBrokenIntoItsOwnVar.declaration.init?.nonCodeMeta?.nonCodeNodes) {
let decrementedNonCodeMeta: NonCodeMeta['nonCodeNodes'] = {}
decrementedNonCodeMeta =
Object.entries(
profileBrokenIntoItsOwnVar.declaration.init?.nonCodeMeta?.nonCodeNodes
).reduce((acc, [key, value]) => {
acc[Number(key) - 1] = value
return acc
}, decrementedNonCodeMeta) || {}
profileBrokenIntoItsOwnVar.declaration.init.nonCodeMeta.nonCodeNodes =
decrementedNonCodeMeta
}
const index = getBodyIndex(pathToPipe)
if (err(index)) return index
_ast.body.splice(index, 1, newSketch, newProfile)
_ast.body.splice(index + 1, 0, profileBrokenIntoItsOwnVar)
const pathToPlane = structuredClone(pathToPipe)
const pathToProfile = structuredClone(pathToPipe)
pathToProfile[1][0] = index + 1

File diff suppressed because one or more lines are too long