2024-07-02 17:16:27 +10:00
|
|
|
import { toolTips } from 'lang/langHelpers'
|
2024-09-13 21:14:14 +10:00
|
|
|
import { Program, Expr, VariableDeclarator } from '../../lang/wasm'
|
2023-03-05 07:34:56 +11:00
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
|
|
|
} from '../../lang/queryAst'
|
|
|
|
import { isSketchVariablesLinked } from '../../lang/std/sketchConstraints'
|
|
|
|
import {
|
2023-03-07 15:45:59 +11:00
|
|
|
transformSecondarySketchLinesTagFirst,
|
2023-03-05 07:34:56 +11:00
|
|
|
getTransformInfos,
|
2023-10-11 13:36:54 +11:00
|
|
|
PathToNodeMap,
|
2024-09-13 21:14:14 +10:00
|
|
|
isExprBinaryPart,
|
2023-03-05 07:34:56 +11:00
|
|
|
} from '../../lang/std/sketchcombos'
|
2024-09-13 21:14:14 +10:00
|
|
|
import { TransformInfo } from 'lang/std/stdTypes'
|
2023-10-16 08:54:38 +11:00
|
|
|
import { GetInfoModal, createInfoModal } from '../SetHorVertDistanceModal'
|
2023-04-06 12:45:56 +10:00
|
|
|
import { createLiteral, createVariableDeclaration } from '../../lang/modifyAst'
|
2023-04-02 17:20:11 +10:00
|
|
|
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
2024-03-22 16:55:30 +11:00
|
|
|
import { kclManager } from 'lib/singletons'
|
2023-10-16 21:20:05 +11:00
|
|
|
import { Selections } from 'lib/selections'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { cleanErrs, err } from 'lib/trap'
|
2023-03-07 15:45:59 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const getModalInfo = createInfoModal(GetInfoModal)
|
2023-10-11 13:36:54 +11:00
|
|
|
|
|
|
|
export function horzVertDistanceInfo({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
constraint: 'setHorzDistance' | 'setVertDistance'
|
2024-06-24 11:45:40 -04:00
|
|
|
}):
|
|
|
|
| {
|
|
|
|
transforms: TransformInfo[]
|
|
|
|
enabled: boolean
|
|
|
|
}
|
|
|
|
| Error {
|
2023-10-11 13:36:54 +11:00
|
|
|
const paths = selectionRanges.codeBasedSelections.map(({ range }) =>
|
|
|
|
getNodePathFromSourceRange(kclManager.ast, range)
|
|
|
|
)
|
2024-06-24 11:45:40 -04:00
|
|
|
const _nodes = paths.map((pathToNode) => {
|
2024-08-12 15:38:42 -05:00
|
|
|
const tmp = getNodeFromPath<Expr>(kclManager.ast, pathToNode)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(tmp)) return tmp
|
|
|
|
return tmp.node
|
|
|
|
})
|
|
|
|
const [hasErr, , nodesWErrs] = cleanErrs(_nodes)
|
|
|
|
|
|
|
|
if (hasErr) return nodesWErrs[0]
|
2024-08-12 15:38:42 -05:00
|
|
|
const nodes = _nodes as Expr[]
|
2024-06-24 11:45:40 -04:00
|
|
|
|
|
|
|
const _varDecs = paths.map((pathToNode) => {
|
|
|
|
const tmp = getNodeFromPath<VariableDeclarator>(
|
|
|
|
kclManager.ast,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclarator'
|
|
|
|
)
|
|
|
|
if (err(tmp)) return tmp
|
|
|
|
return tmp.node
|
|
|
|
})
|
|
|
|
const _err2 = _varDecs.find(err)
|
|
|
|
if (err(_err2)) return _err2
|
|
|
|
const varDecs = _varDecs as VariableDeclarator[]
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
const primaryLine = varDecs[0]
|
|
|
|
const secondaryVarDecs = varDecs.slice(1)
|
|
|
|
const isOthersLinkedToPrimary = secondaryVarDecs.every((secondary) =>
|
|
|
|
isSketchVariablesLinked(secondary, primaryLine, kclManager.ast)
|
|
|
|
)
|
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
[
|
|
|
|
...toolTips,
|
|
|
|
'startSketchAt', // TODO probably a better place for this to live
|
|
|
|
].includes(node.callee.name as any)
|
|
|
|
)
|
|
|
|
|
|
|
|
const theTransforms = getTransformInfos(
|
|
|
|
{
|
|
|
|
...selectionRanges,
|
|
|
|
codeBasedSelections: selectionRanges.codeBasedSelections.slice(1),
|
|
|
|
},
|
|
|
|
kclManager.ast,
|
|
|
|
constraint
|
|
|
|
)
|
|
|
|
const _enableEqual =
|
|
|
|
secondaryVarDecs.length === 1 &&
|
|
|
|
isAllTooltips &&
|
|
|
|
isOthersLinkedToPrimary &&
|
|
|
|
theTransforms.every(Boolean)
|
|
|
|
return { enabled: _enableEqual, transforms: theTransforms }
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function applyConstraintHorzVertDistance({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
// TODO align will always be false (covered by synconous applyConstraintHorzVertAlign), remove it
|
|
|
|
isAlign = false,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
constraint: 'setHorzDistance' | 'setVertDistance'
|
2023-10-16 08:54:38 +11:00
|
|
|
isAlign?: false
|
2023-10-11 13:36:54 +11:00
|
|
|
}): Promise<{
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
}> {
|
2024-06-24 11:45:40 -04:00
|
|
|
const info = horzVertDistanceInfo({
|
2023-10-11 13:36:54 +11:00
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
2024-06-24 11:45:40 -04:00
|
|
|
})
|
|
|
|
if (err(info)) return Promise.reject(info)
|
|
|
|
const transformInfos = info.transforms
|
|
|
|
const transformed = transformSecondarySketchLinesTagFirst({
|
2024-07-25 20:11:46 -04:00
|
|
|
ast: structuredClone(kclManager.ast),
|
2024-06-24 11:45:40 -04:00
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
})
|
|
|
|
if (err(transformed)) return Promise.reject(transformed)
|
2023-10-11 13:36:54 +11:00
|
|
|
const { modifiedAst, tagInfo, valueUsedInTransform, pathToNodeMap } =
|
2024-06-24 11:45:40 -04:00
|
|
|
transformed
|
2023-10-11 13:36:54 +11:00
|
|
|
const {
|
|
|
|
segName,
|
|
|
|
value,
|
|
|
|
valueNode,
|
|
|
|
variableName,
|
|
|
|
newVariableInsertIndex,
|
|
|
|
sign,
|
2023-10-16 08:54:38 +11:00
|
|
|
} = await getModalInfo({
|
|
|
|
segName: tagInfo?.tag,
|
|
|
|
isSegNameEditable: !tagInfo?.isTagExisting,
|
|
|
|
value: valueUsedInTransform,
|
|
|
|
initialVariableName: constraint === 'setHorzDistance' ? 'xDis' : 'yDis',
|
|
|
|
} as any)
|
2024-06-03 15:37:23 +10:00
|
|
|
if (
|
|
|
|
!variableName &&
|
|
|
|
segName === tagInfo?.tag &&
|
|
|
|
Number(value) === valueUsedInTransform
|
|
|
|
) {
|
2023-10-11 13:36:54 +11:00
|
|
|
return {
|
|
|
|
modifiedAst,
|
|
|
|
pathToNodeMap,
|
|
|
|
}
|
|
|
|
} else {
|
2024-09-13 21:14:14 +10:00
|
|
|
if (!isExprBinaryPart(valueNode))
|
|
|
|
return Promise.reject('Invalid valueNode, is not a BinaryPart')
|
2023-10-11 13:36:54 +11:00
|
|
|
let finalValue = isAlign
|
|
|
|
? createLiteral(0)
|
2024-09-13 21:14:14 +10:00
|
|
|
: removeDoubleNegatives(valueNode, sign, variableName)
|
2023-10-11 13:36:54 +11:00
|
|
|
// transform again but forcing certain values
|
2024-06-24 11:45:40 -04:00
|
|
|
const transformed = transformSecondarySketchLinesTagFirst({
|
|
|
|
ast: kclManager.ast,
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
forceSegName: segName,
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (err(transformed)) return Promise.reject(transformed)
|
|
|
|
const { modifiedAst: _modifiedAst, pathToNodeMap } = transformed
|
2023-10-11 13:36:54 +11:00
|
|
|
if (variableName) {
|
|
|
|
const newBody = [..._modifiedAst.body]
|
|
|
|
newBody.splice(
|
|
|
|
newVariableInsertIndex,
|
|
|
|
0,
|
|
|
|
createVariableDeclaration(variableName, valueNode)
|
|
|
|
)
|
|
|
|
_modifiedAst.body = newBody
|
2024-06-03 15:37:23 +10:00
|
|
|
Object.values(pathToNodeMap).forEach((pathToNode) => {
|
|
|
|
const index = pathToNode.findIndex((a) => a[0] === 'body') + 1
|
|
|
|
pathToNode[index][0] = Number(pathToNode[index][0]) + 1
|
|
|
|
})
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _modifiedAst,
|
|
|
|
pathToNodeMap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function applyConstraintHorzVertAlign({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
constraint: 'setHorzDistance' | 'setVertDistance'
|
2024-06-24 11:45:40 -04:00
|
|
|
}):
|
|
|
|
| {
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
}
|
|
|
|
| Error {
|
|
|
|
const info = horzVertDistanceInfo({
|
2023-10-11 13:36:54 +11:00
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
2024-06-24 11:45:40 -04:00
|
|
|
})
|
|
|
|
if (err(info)) return info
|
|
|
|
const transformInfos = info.transforms
|
2023-10-11 13:36:54 +11:00
|
|
|
let finalValue = createLiteral(0)
|
2024-06-24 11:45:40 -04:00
|
|
|
const retval = transformSecondarySketchLinesTagFirst({
|
2023-10-11 13:36:54 +11:00
|
|
|
ast: kclManager.ast,
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(retval)) return retval
|
|
|
|
const { modifiedAst, pathToNodeMap } = retval
|
2023-10-11 13:36:54 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: modifiedAst,
|
|
|
|
pathToNodeMap,
|
|
|
|
}
|
|
|
|
}
|