[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:
@ -46,12 +46,5 @@ export function angleLengthInfo({
|
|||||||
selectionRanges.graphSelections.length <= 1 &&
|
selectionRanges.graphSelections.length <= 1 &&
|
||||||
isAllTooltips &&
|
isAllTooltips &&
|
||||||
transforms.every(Boolean)
|
transforms.every(Boolean)
|
||||||
console.log(
|
|
||||||
'enabled',
|
|
||||||
enabled,
|
|
||||||
selectionRanges.graphSelections.length,
|
|
||||||
isAllTooltips,
|
|
||||||
transforms.every(Boolean)
|
|
||||||
)
|
|
||||||
return { enabled, transforms }
|
return { enabled, transforms }
|
||||||
}
|
}
|
||||||
|
@ -1030,19 +1030,25 @@ sketch003 = startSketchOn(XZ)
|
|||||||
|
|
||||||
describe('Testing splitPipedProfile', () => {
|
describe('Testing splitPipedProfile', () => {
|
||||||
it('should split the pipe expression correctly', () => {
|
it('should split the pipe expression correctly', () => {
|
||||||
const codeBefore = `part001 = startSketchOn(XZ)
|
const codeBefore = `// comment 1
|
||||||
|
part001 = startSketchOn(XZ)
|
||||||
|> startProfileAt([1, 2], %)
|
|> startProfileAt([1, 2], %)
|
||||||
|
// comment 2
|
||||||
|> line([3, 4], %)
|
|> line([3, 4], %)
|
||||||
|> line([5, 6], %)
|
|> line([5, 6], %)
|
||||||
|> close(%)
|
|> close(%)
|
||||||
|
// comment 3
|
||||||
extrude001 = extrude(5, part001)
|
extrude001 = extrude(5, part001)
|
||||||
`
|
`
|
||||||
|
|
||||||
const expectedCodeAfter = `sketch001 = startSketchOn(XZ)
|
const expectedCodeAfter = `// comment 1
|
||||||
|
sketch001 = startSketchOn(XZ)
|
||||||
part001 = startProfileAt([1, 2], sketch001)
|
part001 = startProfileAt([1, 2], sketch001)
|
||||||
|
// comment 2
|
||||||
|> line([3, 4], %)
|
|> line([3, 4], %)
|
||||||
|> line([5, 6], %)
|
|> line([5, 6], %)
|
||||||
|> close(%)
|
|> close(%)
|
||||||
|
// comment 3
|
||||||
extrude001 = extrude(5, part001)
|
extrude001 = extrude(5, part001)
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -3,10 +3,10 @@ import type { Models } from '@kittycad/lib'
|
|||||||
import type { BodyItem } from '@rust/kcl-lib/bindings/BodyItem'
|
import type { BodyItem } from '@rust/kcl-lib/bindings/BodyItem'
|
||||||
import type { Name } from '@rust/kcl-lib/bindings/Name'
|
import type { Name } from '@rust/kcl-lib/bindings/Name'
|
||||||
import type { Node } from '@rust/kcl-lib/bindings/Node'
|
import type { Node } from '@rust/kcl-lib/bindings/Node'
|
||||||
|
import type { NonCodeMeta } from '@rust/kcl-lib/bindings/NonCodeMeta'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
createArrayExpression,
|
createArrayExpression,
|
||||||
createCallExpression,
|
|
||||||
createCallExpressionStdLib,
|
createCallExpressionStdLib,
|
||||||
createCallExpressionStdLibKw,
|
createCallExpressionStdLibKw,
|
||||||
createIdentifier,
|
createIdentifier,
|
||||||
@ -1642,7 +1642,7 @@ export function splitPipedProfile(
|
|||||||
}
|
}
|
||||||
| Error {
|
| Error {
|
||||||
const _ast = structuredClone(ast)
|
const _ast = structuredClone(ast)
|
||||||
const varDec = getNodeFromPath<VariableDeclaration>(
|
const varDec = getNodeFromPath<Node<VariableDeclaration>>(
|
||||||
_ast,
|
_ast,
|
||||||
pathToPipe,
|
pathToPipe,
|
||||||
'VariableDeclaration'
|
'VariableDeclaration'
|
||||||
@ -1666,26 +1666,53 @@ export function splitPipedProfile(
|
|||||||
const newVarName = findUniqueName(_ast, 'sketch')
|
const newVarName = findUniqueName(_ast, 'sketch')
|
||||||
const secondCallArgs = structuredClone(secondCall.arguments)
|
const secondCallArgs = structuredClone(secondCall.arguments)
|
||||||
secondCallArgs[1] = createLocalName(newVarName)
|
secondCallArgs[1] = createLocalName(newVarName)
|
||||||
const firstCallOfNewPipe = createCallExpression(
|
const startSketchOnBrokenIntoNewVarDec = structuredClone(varDec.node)
|
||||||
'startProfileAt',
|
const profileBrokenIntoItsOwnVar = structuredClone(varDec.node)
|
||||||
secondCallArgs
|
if (
|
||||||
)
|
startSketchOnBrokenIntoNewVarDec.declaration.init.type !== 'PipeExpression'
|
||||||
const newSketch = createVariableDeclaration(
|
) {
|
||||||
newVarName,
|
return new Error('clonedVarDec1 is not a PipeExpression')
|
||||||
varDec.node.declaration.init.body[0]
|
}
|
||||||
)
|
varDec.node.declaration.init =
|
||||||
const newProfile = createVariableDeclaration(
|
startSketchOnBrokenIntoNewVarDec.declaration.init.body[0]
|
||||||
varName,
|
varDec.node.declaration.id.name = newVarName
|
||||||
varDec.node.declaration.init.body.length <= 2
|
if (profileBrokenIntoItsOwnVar.declaration.init.type !== 'PipeExpression') {
|
||||||
? firstCallOfNewPipe
|
return new Error('clonedVarDec2 is not a PipeExpression')
|
||||||
: createPipeExpression([
|
}
|
||||||
firstCallOfNewPipe,
|
profileBrokenIntoItsOwnVar.declaration.init.body =
|
||||||
...varDec.node.declaration.init.body.slice(2),
|
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)
|
const index = getBodyIndex(pathToPipe)
|
||||||
if (err(index)) return index
|
if (err(index)) return index
|
||||||
_ast.body.splice(index, 1, newSketch, newProfile)
|
_ast.body.splice(index + 1, 0, profileBrokenIntoItsOwnVar)
|
||||||
const pathToPlane = structuredClone(pathToPipe)
|
const pathToPlane = structuredClone(pathToPipe)
|
||||||
const pathToProfile = structuredClone(pathToPipe)
|
const pathToProfile = structuredClone(pathToPipe)
|
||||||
pathToProfile[1][0] = index + 1
|
pathToProfile[1][0] = index + 1
|
||||||
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user