2024-07-02 17:16:27 +10:00
|
|
|
import { toolTips } from 'lang/langHelpers'
|
2024-06-04 16:29:20 +10:00
|
|
|
import { Selection, Selections } from 'lib/selections'
|
2024-08-12 15:38:42 -05:00
|
|
|
import { PathToNode, Program, Expr } from '../../lang/wasm'
|
2023-03-22 03:02:37 +11:00
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
|
|
|
} from '../../lang/queryAst'
|
|
|
|
import {
|
2023-10-16 08:54:38 +11:00
|
|
|
PathToNodeMap,
|
2023-03-22 03:02:37 +11:00
|
|
|
getRemoveConstraintsTransforms,
|
|
|
|
transformAstSketchLines,
|
|
|
|
} from '../../lang/std/sketchcombos'
|
2024-09-13 21:14:14 +10:00
|
|
|
import { TransformInfo } from 'lang/std/stdTypes'
|
2024-03-22 16:55:30 +11:00
|
|
|
import { kclManager } from 'lib/singletons'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { err } from 'lib/trap'
|
2024-10-30 16:52:17 -04:00
|
|
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
2023-03-22 03:02:37 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
export function removeConstrainingValuesInfo({
|
|
|
|
selectionRanges,
|
2024-06-04 16:29:20 +10:00
|
|
|
pathToNodes,
|
2023-10-16 08:54:38 +11:00
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
2024-06-04 16:29:20 +10:00
|
|
|
pathToNodes?: Array<PathToNode>
|
2024-06-24 11:45:40 -04:00
|
|
|
}):
|
|
|
|
| {
|
|
|
|
transforms: TransformInfo[]
|
|
|
|
enabled: boolean
|
|
|
|
updatedSelectionRanges: Selections
|
|
|
|
}
|
|
|
|
| Error {
|
2024-06-04 16:29:20 +10:00
|
|
|
const paths =
|
|
|
|
pathToNodes ||
|
|
|
|
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 _err1 = _nodes.find(err)
|
|
|
|
if (err(_err1)) return _err1
|
2024-08-12 15:38:42 -05:00
|
|
|
const nodes = _nodes as Expr[]
|
2024-06-24 11:45:40 -04:00
|
|
|
|
2024-06-04 16:29:20 +10:00
|
|
|
const updatedSelectionRanges = pathToNodes
|
|
|
|
? {
|
|
|
|
otherSelections: [],
|
|
|
|
codeBasedSelections: nodes.map(
|
|
|
|
(node): Selection => ({
|
|
|
|
range: [node.start, node.end],
|
|
|
|
type: 'default',
|
|
|
|
})
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: selectionRanges
|
2023-10-16 08:54:38 +11:00
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
toolTips.includes(node.callee.name as any)
|
|
|
|
)
|
2023-03-22 03:02:37 +11:00
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
const transforms = getRemoveConstraintsTransforms(
|
|
|
|
updatedSelectionRanges,
|
|
|
|
kclManager.ast,
|
|
|
|
'removeConstrainingValues'
|
|
|
|
)
|
|
|
|
if (err(transforms)) return transforms
|
2023-03-22 03:02:37 +11:00
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
const enabled = isAllTooltips && transforms.every(Boolean)
|
|
|
|
return { enabled, transforms, updatedSelectionRanges }
|
2023-10-16 08:54:38 +11:00
|
|
|
}
|
2023-03-22 03:02:37 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
export function applyRemoveConstrainingValues({
|
|
|
|
selectionRanges,
|
2024-06-04 16:29:20 +10:00
|
|
|
pathToNodes,
|
2023-10-16 08:54:38 +11:00
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
2024-06-04 16:29:20 +10:00
|
|
|
pathToNodes?: Array<PathToNode>
|
2024-06-24 11:45:40 -04:00
|
|
|
}):
|
|
|
|
| {
|
2024-10-30 16:52:17 -04:00
|
|
|
modifiedAst: Node<Program>
|
2024-06-24 11:45:40 -04:00
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
}
|
|
|
|
| Error {
|
|
|
|
const constraint = removeConstrainingValuesInfo({
|
2024-06-04 16:29:20 +10:00
|
|
|
selectionRanges,
|
|
|
|
pathToNodes,
|
|
|
|
})
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(constraint)) return constraint
|
|
|
|
const { transforms, updatedSelectionRanges } = constraint
|
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
return transformAstSketchLines({
|
|
|
|
ast: kclManager.ast,
|
2024-06-04 16:29:20 +10:00
|
|
|
selectionRanges: updatedSelectionRanges,
|
2023-10-16 08:54:38 +11:00
|
|
|
transformInfos: transforms,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
})
|
2023-03-22 03:02:37 +11:00
|
|
|
}
|