2023-10-16 21:20:05 +11:00
|
|
|
import { ToolTip } from '../useStore'
|
2024-02-19 17:23:03 +11:00
|
|
|
import { Selection, Selections } from 'lib/selections'
|
2023-04-01 16:47:00 +11:00
|
|
|
import {
|
2024-05-21 16:44:08 +10:00
|
|
|
ArrayExpression,
|
2023-04-01 16:47:00 +11:00
|
|
|
BinaryExpression,
|
|
|
|
CallExpression,
|
|
|
|
ExpressionStatement,
|
2023-09-29 11:11:01 -07:00
|
|
|
PathToNode,
|
2024-05-21 16:44:08 +10:00
|
|
|
PipeExpression,
|
|
|
|
Program,
|
2023-09-29 11:11:01 -07:00
|
|
|
ProgramMemory,
|
2024-05-21 16:44:08 +10:00
|
|
|
ReturnStatement,
|
2023-09-29 11:11:01 -07:00
|
|
|
SketchGroup,
|
|
|
|
SourceRange,
|
2024-05-21 16:44:08 +10:00
|
|
|
SyntaxType,
|
|
|
|
Value,
|
|
|
|
VariableDeclaration,
|
|
|
|
VariableDeclarator,
|
2023-09-29 11:11:01 -07:00
|
|
|
} from './wasm'
|
2023-04-01 16:47:00 +11:00
|
|
|
import { createIdentifier, splitPathAtLastIndex } from './modifyAst'
|
2023-04-08 14:16:49 +10:00
|
|
|
import { getSketchSegmentFromSourceRange } from './std/sketchConstraints'
|
|
|
|
import { getAngle } from '../lib/utils'
|
|
|
|
import { getFirstArg } from './std/sketch'
|
|
|
|
import {
|
|
|
|
getConstraintLevelFromSourceRange,
|
|
|
|
getConstraintType,
|
|
|
|
} from './std/sketchcombos'
|
2023-03-03 20:35:48 +11:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
/**
|
|
|
|
* Retrieves a node from a given path within a Program node structure, optionally stopping at a specified node type.
|
|
|
|
* This function navigates through the AST (Abstract Syntax Tree) based on the provided path, attempting to locate
|
|
|
|
* and return the node at the end of this path.
|
|
|
|
* By default it will return the node of the deepest "stopAt" type encountered, or the node at the end of the path if no "stopAt" type is provided.
|
|
|
|
* If the "returnEarly" flag is set to true, the function will return as soon as a node of the specified type is found.
|
|
|
|
*/
|
2023-03-03 20:35:48 +11:00
|
|
|
export function getNodeFromPath<T>(
|
|
|
|
node: Program,
|
2023-04-01 16:47:00 +11:00
|
|
|
path: PathToNode,
|
2024-05-24 20:54:42 +10:00
|
|
|
stopAt?: SyntaxType | SyntaxType[],
|
2023-03-03 20:35:48 +11:00
|
|
|
returnEarly = false
|
|
|
|
): {
|
|
|
|
node: T
|
2023-04-01 16:47:00 +11:00
|
|
|
shallowPath: PathToNode
|
|
|
|
deepPath: PathToNode
|
2023-03-03 20:35:48 +11:00
|
|
|
} {
|
|
|
|
let currentNode = node as any
|
|
|
|
let stopAtNode = null
|
|
|
|
let successfulPaths: PathToNode = []
|
|
|
|
let pathsExplored: PathToNode = []
|
|
|
|
for (const pathItem of path) {
|
|
|
|
try {
|
2023-04-01 16:47:00 +11:00
|
|
|
if (typeof currentNode[pathItem[0]] !== 'object')
|
2023-03-03 20:35:48 +11:00
|
|
|
throw new Error('not an object')
|
2023-04-01 16:47:00 +11:00
|
|
|
currentNode = currentNode?.[pathItem[0]]
|
2023-03-03 20:35:48 +11:00
|
|
|
successfulPaths.push(pathItem)
|
|
|
|
if (!stopAtNode) {
|
|
|
|
pathsExplored.push(pathItem)
|
|
|
|
}
|
2023-04-01 16:47:00 +11:00
|
|
|
if (
|
2024-05-24 20:54:42 +10:00
|
|
|
typeof stopAt !== 'undefined' &&
|
|
|
|
(Array.isArray(stopAt)
|
2023-04-01 16:47:00 +11:00
|
|
|
? stopAt.includes(currentNode.type)
|
2024-05-24 20:54:42 +10:00
|
|
|
: currentNode.type === stopAt)
|
2023-04-01 16:47:00 +11:00
|
|
|
) {
|
2023-03-03 20:35:48 +11:00
|
|
|
// it will match the deepest node of the type
|
|
|
|
// instead of returning at the first match
|
|
|
|
stopAtNode = currentNode
|
|
|
|
if (returnEarly) {
|
|
|
|
return {
|
|
|
|
node: stopAtNode,
|
2023-04-01 16:47:00 +11:00
|
|
|
shallowPath: pathsExplored,
|
|
|
|
deepPath: successfulPaths,
|
2023-03-03 20:35:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-02-11 12:59:00 +11:00
|
|
|
// console.error(
|
|
|
|
// `Could not find path ${pathItem} in node ${JSON.stringify(
|
|
|
|
// currentNode,
|
|
|
|
// null,
|
|
|
|
// 2
|
|
|
|
// )}, successful path was ${successfulPaths}`
|
|
|
|
// )
|
2023-03-03 20:35:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
node: stopAtNode || currentNode,
|
2023-04-01 16:47:00 +11:00
|
|
|
shallowPath: pathsExplored,
|
|
|
|
deepPath: successfulPaths,
|
2023-03-03 20:35:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
/**
|
|
|
|
* Functions the same as getNodeFromPath, but returns a curried function that can be called with the stopAt and returnEarly arguments.
|
|
|
|
*/
|
2023-03-03 20:35:48 +11:00
|
|
|
export function getNodeFromPathCurry(
|
|
|
|
node: Program,
|
2023-04-01 16:47:00 +11:00
|
|
|
path: PathToNode
|
2023-03-03 20:35:48 +11:00
|
|
|
): <T>(
|
2024-05-24 20:54:42 +10:00
|
|
|
stopAt?: SyntaxType | SyntaxType[],
|
2023-03-03 20:35:48 +11:00
|
|
|
returnEarly?: boolean
|
|
|
|
) => {
|
|
|
|
node: T
|
|
|
|
path: PathToNode
|
|
|
|
} {
|
2024-05-24 20:54:42 +10:00
|
|
|
return <T>(stopAt?: SyntaxType | SyntaxType[], returnEarly = false) => {
|
2023-04-01 16:47:00 +11:00
|
|
|
const { node: _node, shallowPath } = getNodeFromPath<T>(
|
|
|
|
node,
|
|
|
|
path,
|
|
|
|
stopAt,
|
|
|
|
returnEarly
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
node: _node,
|
|
|
|
path: shallowPath,
|
|
|
|
}
|
2023-03-03 20:35:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 16:47:00 +11:00
|
|
|
function moreNodePathFromSourceRange(
|
|
|
|
node: Value | ExpressionStatement | VariableDeclaration | ReturnStatement,
|
2023-04-03 16:05:25 +10:00
|
|
|
sourceRange: Selection['range'],
|
2023-04-01 16:47:00 +11:00
|
|
|
previousPath: PathToNode = [['body', '']]
|
|
|
|
): PathToNode {
|
|
|
|
const [start, end] = sourceRange
|
|
|
|
let path: PathToNode = [...previousPath]
|
|
|
|
const _node = { ...node }
|
|
|
|
|
|
|
|
if (start < _node.start || end > _node.end) return path
|
|
|
|
|
|
|
|
const isInRange = _node.start <= start && _node.end >= end
|
|
|
|
|
|
|
|
if ((_node.type === 'Identifier' || _node.type === 'Literal') && isInRange)
|
|
|
|
return path
|
|
|
|
|
|
|
|
if (_node.type === 'CallExpression' && isInRange) {
|
|
|
|
const { callee, arguments: args } = _node
|
|
|
|
if (
|
|
|
|
callee.type === 'Identifier' &&
|
|
|
|
callee.start <= start &&
|
|
|
|
callee.end >= end
|
|
|
|
) {
|
|
|
|
path.push(['callee', 'CallExpression'])
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if (args.length > 0) {
|
|
|
|
for (let argIndex = 0; argIndex < args.length; argIndex++) {
|
|
|
|
const arg = args[argIndex]
|
|
|
|
if (arg.start <= start && arg.end >= end) {
|
|
|
|
path.push(['arguments', 'CallExpression'])
|
|
|
|
path.push([argIndex, 'index'])
|
|
|
|
return moreNodePathFromSourceRange(arg, sourceRange, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if (_node.type === 'BinaryExpression' && isInRange) {
|
|
|
|
const { left, right } = _node
|
|
|
|
if (left.start <= start && left.end >= end) {
|
|
|
|
path.push(['left', 'BinaryExpression'])
|
|
|
|
return moreNodePathFromSourceRange(left, sourceRange, path)
|
|
|
|
}
|
|
|
|
if (right.start <= start && right.end >= end) {
|
|
|
|
path.push(['right', 'BinaryExpression'])
|
|
|
|
return moreNodePathFromSourceRange(right, sourceRange, path)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if (_node.type === 'PipeExpression' && isInRange) {
|
|
|
|
const { body } = _node
|
|
|
|
for (let i = 0; i < body.length; i++) {
|
|
|
|
const pipe = body[i]
|
|
|
|
if (pipe.start <= start && pipe.end >= end) {
|
|
|
|
path.push(['body', 'PipeExpression'])
|
|
|
|
path.push([i, 'index'])
|
|
|
|
return moreNodePathFromSourceRange(pipe, sourceRange, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if (_node.type === 'ArrayExpression' && isInRange) {
|
|
|
|
const { elements } = _node
|
|
|
|
for (let elIndex = 0; elIndex < elements.length; elIndex++) {
|
|
|
|
const element = elements[elIndex]
|
|
|
|
if (element.start <= start && element.end >= end) {
|
|
|
|
path.push(['elements', 'ArrayExpression'])
|
|
|
|
path.push([elIndex, 'index'])
|
|
|
|
return moreNodePathFromSourceRange(element, sourceRange, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if (_node.type === 'ObjectExpression' && isInRange) {
|
|
|
|
const { properties } = _node
|
|
|
|
for (let propIndex = 0; propIndex < properties.length; propIndex++) {
|
|
|
|
const property = properties[propIndex]
|
|
|
|
if (property.start <= start && property.end >= end) {
|
|
|
|
path.push(['properties', 'ObjectExpression'])
|
|
|
|
path.push([propIndex, 'index'])
|
|
|
|
if (property.key.start <= start && property.key.end >= end) {
|
|
|
|
path.push(['key', 'Property'])
|
|
|
|
return moreNodePathFromSourceRange(property.key, sourceRange, path)
|
|
|
|
}
|
|
|
|
if (property.value.start <= start && property.value.end >= end) {
|
|
|
|
path.push(['value', 'Property'])
|
|
|
|
return moreNodePathFromSourceRange(property.value, sourceRange, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if (_node.type === 'ExpressionStatement' && isInRange) {
|
|
|
|
const { expression } = _node
|
|
|
|
path.push(['expression', 'ExpressionStatement'])
|
|
|
|
return moreNodePathFromSourceRange(expression, sourceRange, path)
|
|
|
|
}
|
|
|
|
if (_node.type === 'VariableDeclaration' && isInRange) {
|
|
|
|
const declarations = _node.declarations
|
|
|
|
|
|
|
|
for (let decIndex = 0; decIndex < declarations.length; decIndex++) {
|
|
|
|
const declaration = declarations[decIndex]
|
|
|
|
if (declaration.start <= start && declaration.end >= end) {
|
|
|
|
path.push(['declarations', 'VariableDeclaration'])
|
|
|
|
path.push([decIndex, 'index'])
|
|
|
|
const init = declaration.init
|
|
|
|
if (init.start <= start && init.end >= end) {
|
|
|
|
path.push(['init', ''])
|
|
|
|
return moreNodePathFromSourceRange(init, sourceRange, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_node.type === 'VariableDeclaration' && isInRange) {
|
|
|
|
const declarations = _node.declarations
|
|
|
|
|
|
|
|
for (let decIndex = 0; decIndex < declarations.length; decIndex++) {
|
|
|
|
const declaration = declarations[decIndex]
|
|
|
|
if (declaration.start <= start && declaration.end >= end) {
|
|
|
|
const init = declaration.init
|
|
|
|
if (init.start <= start && init.end >= end) {
|
|
|
|
path.push(['declarations', 'VariableDeclaration'])
|
|
|
|
path.push([decIndex, 'index'])
|
|
|
|
path.push(['init', ''])
|
|
|
|
return moreNodePathFromSourceRange(init, sourceRange, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if (_node.type === 'UnaryExpression' && isInRange) {
|
|
|
|
const { argument } = _node
|
|
|
|
if (argument.start <= start && argument.end >= end) {
|
|
|
|
path.push(['argument', 'UnaryExpression'])
|
|
|
|
return moreNodePathFromSourceRange(argument, sourceRange, path)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2023-09-21 15:40:41 +10:00
|
|
|
if (_node.type === 'FunctionExpression' && isInRange) {
|
|
|
|
for (let i = 0; i < _node.params.length; i++) {
|
|
|
|
const param = _node.params[i]
|
2023-11-20 11:19:08 -06:00
|
|
|
if (param.identifier.start <= start && param.identifier.end >= end) {
|
2023-09-21 15:40:41 +10:00
|
|
|
path.push(['params', 'FunctionExpression'])
|
|
|
|
path.push([i, 'index'])
|
2023-11-20 11:19:08 -06:00
|
|
|
return moreNodePathFromSourceRange(param.identifier, sourceRange, path)
|
2023-09-21 15:40:41 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_node.body.start <= start && _node.body.end >= end) {
|
|
|
|
path.push(['body', 'FunctionExpression'])
|
|
|
|
const fnBody = _node.body.body
|
|
|
|
for (let i = 0; i < fnBody.length; i++) {
|
|
|
|
const statement = fnBody[i]
|
|
|
|
if (statement.start <= start && statement.end >= end) {
|
|
|
|
path.push(['body', 'FunctionExpression'])
|
|
|
|
path.push([i, 'index'])
|
|
|
|
return moreNodePathFromSourceRange(statement, sourceRange, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-11 12:59:00 +11:00
|
|
|
if (_node.type === 'PipeSubstitution' && isInRange) return path
|
2023-09-21 15:40:41 +10:00
|
|
|
console.error('not implemented: ' + node.type)
|
2023-04-01 16:47:00 +11:00
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2023-03-03 20:35:48 +11:00
|
|
|
export function getNodePathFromSourceRange(
|
|
|
|
node: Program,
|
2023-04-03 16:05:25 +10:00
|
|
|
sourceRange: Selection['range'],
|
2023-04-01 16:47:00 +11:00
|
|
|
previousPath: PathToNode = [['body', '']]
|
2023-03-03 20:35:48 +11:00
|
|
|
): PathToNode {
|
2023-04-08 14:16:49 +10:00
|
|
|
const [start, end] = sourceRange || []
|
2023-04-01 16:47:00 +11:00
|
|
|
let path: PathToNode = [...previousPath]
|
2023-03-03 20:35:48 +11:00
|
|
|
const _node = { ...node }
|
2023-04-01 16:47:00 +11:00
|
|
|
|
2023-03-03 20:35:48 +11:00
|
|
|
// loop over each statement in body getting the index with a for loop
|
|
|
|
for (
|
|
|
|
let statementIndex = 0;
|
|
|
|
statementIndex < _node.body.length;
|
|
|
|
statementIndex++
|
|
|
|
) {
|
|
|
|
const statement = _node.body[statementIndex]
|
|
|
|
if (statement.start <= start && statement.end >= end) {
|
2023-04-01 16:47:00 +11:00
|
|
|
path.push([statementIndex, 'index'])
|
|
|
|
return moreNodePathFromSourceRange(statement, sourceRange, path)
|
2023-03-03 20:35:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2023-03-10 08:35:30 +11:00
|
|
|
|
2024-05-21 16:44:08 +10:00
|
|
|
type KCLNode =
|
|
|
|
| Value
|
|
|
|
| ExpressionStatement
|
|
|
|
| VariableDeclaration
|
|
|
|
| VariableDeclarator
|
|
|
|
| ReturnStatement
|
|
|
|
|
|
|
|
export function traverse(
|
|
|
|
node: KCLNode,
|
|
|
|
option: {
|
|
|
|
enter?: (node: KCLNode) => void
|
|
|
|
leave?: (node: KCLNode) => void
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
option?.enter?.(node)
|
|
|
|
const _traverse = (node: KCLNode) => traverse(node, option)
|
|
|
|
|
|
|
|
if (node.type === 'VariableDeclaration') {
|
|
|
|
node.declarations.forEach(_traverse)
|
|
|
|
} else if (node.type === 'VariableDeclarator') {
|
|
|
|
_traverse(node.init)
|
|
|
|
} else if (node.type === 'PipeExpression') {
|
|
|
|
node.body.forEach(_traverse)
|
|
|
|
} else if (node.type === 'CallExpression') {
|
|
|
|
_traverse(node.callee)
|
|
|
|
node.arguments.forEach(_traverse)
|
|
|
|
} else if (node.type === 'BinaryExpression') {
|
|
|
|
_traverse(node.left)
|
|
|
|
_traverse(node.right)
|
|
|
|
} else if (node.type === 'Identifier') {
|
|
|
|
// do nothing
|
|
|
|
} else if (node.type === 'Literal') {
|
|
|
|
// do nothing
|
|
|
|
} else if (node.type === 'ArrayExpression') {
|
|
|
|
node.elements.forEach(_traverse)
|
|
|
|
} else if (node.type === 'ObjectExpression') {
|
|
|
|
node.properties.forEach(({ key, value }) => {
|
|
|
|
_traverse(key)
|
|
|
|
_traverse(value)
|
|
|
|
})
|
|
|
|
} else if (node.type === 'UnaryExpression') {
|
|
|
|
_traverse(node.argument)
|
|
|
|
} else if (node.type === 'MemberExpression') {
|
|
|
|
// hmm this smell
|
|
|
|
_traverse(node.object)
|
|
|
|
_traverse(node.property)
|
|
|
|
} else if ('body' in node && Array.isArray(node.body)) {
|
|
|
|
node.body.forEach(_traverse)
|
|
|
|
}
|
|
|
|
option?.leave?.(node)
|
|
|
|
}
|
|
|
|
|
2023-03-10 08:48:50 +11:00
|
|
|
export interface PrevVariable<T> {
|
2023-03-10 08:35:30 +11:00
|
|
|
key: string
|
|
|
|
value: T
|
|
|
|
}
|
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
export function findAllPreviousVariablesPath(
|
2023-03-10 08:35:30 +11:00
|
|
|
ast: Program,
|
|
|
|
programMemory: ProgramMemory,
|
2024-05-24 20:54:42 +10:00
|
|
|
path: PathToNode,
|
2023-03-10 08:35:30 +11:00
|
|
|
type: 'number' | 'string' = 'number'
|
|
|
|
): {
|
|
|
|
variables: PrevVariable<typeof type extends 'number' ? number : string>[]
|
|
|
|
bodyPath: PathToNode
|
|
|
|
insertIndex: number
|
|
|
|
} {
|
2024-05-24 20:54:42 +10:00
|
|
|
const { shallowPath: pathToDec, node } = getNodeFromPath(
|
2023-04-01 16:47:00 +11:00
|
|
|
ast,
|
|
|
|
path,
|
|
|
|
'VariableDeclaration'
|
|
|
|
)
|
2024-05-24 20:54:42 +10:00
|
|
|
|
|
|
|
const startRange = (node as any).start
|
|
|
|
|
2023-03-10 08:35:30 +11:00
|
|
|
const { index: insertIndex, path: bodyPath } = splitPathAtLastIndex(pathToDec)
|
|
|
|
|
|
|
|
const { node: bodyItems } = getNodeFromPath<Program['body']>(ast, bodyPath)
|
|
|
|
|
|
|
|
const variables: PrevVariable<any>[] = []
|
2023-03-21 19:02:18 +11:00
|
|
|
bodyItems?.forEach?.((item) => {
|
2024-05-24 20:54:42 +10:00
|
|
|
if (item.type !== 'VariableDeclaration' || item.end > startRange) return
|
2023-03-10 08:35:30 +11:00
|
|
|
const varName = item.declarations[0].id.name
|
|
|
|
const varValue = programMemory?.root[varName]
|
|
|
|
if (typeof varValue?.value !== type) return
|
|
|
|
variables.push({
|
|
|
|
key: varName,
|
|
|
|
value: varValue.value,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
insertIndex,
|
|
|
|
bodyPath: bodyPath,
|
|
|
|
variables,
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 16:47:00 +11:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
export function findAllPreviousVariables(
|
|
|
|
ast: Program,
|
|
|
|
programMemory: ProgramMemory,
|
|
|
|
sourceRange: Selection['range'],
|
|
|
|
type: 'number' | 'string' = 'number'
|
|
|
|
): {
|
|
|
|
variables: PrevVariable<typeof type extends 'number' ? number : string>[]
|
|
|
|
bodyPath: PathToNode
|
|
|
|
insertIndex: number
|
|
|
|
} {
|
|
|
|
const path = getNodePathFromSourceRange(ast, sourceRange)
|
|
|
|
return findAllPreviousVariablesPath(ast, programMemory, path, type)
|
|
|
|
}
|
2023-04-01 16:47:00 +11:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
type ReplacerFn = (
|
|
|
|
_ast: Program,
|
|
|
|
varName: string
|
|
|
|
) => { modifiedAst: Program; pathToReplaced: PathToNode }
|
|
|
|
|
|
|
|
export function isNodeSafeToReplacePath(
|
2023-04-01 16:47:00 +11:00
|
|
|
ast: Program,
|
2024-05-24 20:54:42 +10:00
|
|
|
path: PathToNode
|
2023-04-01 16:47:00 +11:00
|
|
|
): {
|
|
|
|
isSafe: boolean
|
|
|
|
value: Value
|
|
|
|
replacer: ReplacerFn
|
|
|
|
} {
|
|
|
|
if (path[path.length - 1][0] === 'callee') {
|
|
|
|
path = path.slice(0, -1)
|
|
|
|
}
|
2024-05-24 20:54:42 +10:00
|
|
|
const acceptedNodeTypes: SyntaxType[] = [
|
2023-04-01 16:47:00 +11:00
|
|
|
'BinaryExpression',
|
|
|
|
'Identifier',
|
|
|
|
'CallExpression',
|
|
|
|
'Literal',
|
2024-05-24 20:54:42 +10:00
|
|
|
'UnaryExpression',
|
2023-04-01 16:47:00 +11:00
|
|
|
]
|
|
|
|
const { node: value, deepPath: outPath } = getNodeFromPath(
|
|
|
|
ast,
|
|
|
|
path,
|
|
|
|
acceptedNodeTypes
|
|
|
|
)
|
|
|
|
const { node: binValue, shallowPath: outBinPath } = getNodeFromPath(
|
|
|
|
ast,
|
|
|
|
path,
|
|
|
|
'BinaryExpression'
|
|
|
|
)
|
|
|
|
// binaryExpression should take precedence
|
|
|
|
const [finVal, finPath] =
|
|
|
|
(binValue as Value)?.type === 'BinaryExpression'
|
|
|
|
? [binValue, outBinPath]
|
|
|
|
: [value, outPath]
|
|
|
|
|
|
|
|
const replaceNodeWithIdentifier: ReplacerFn = (_ast, varName) => {
|
|
|
|
const identifier = createIdentifier(varName)
|
|
|
|
const last = finPath[finPath.length - 1]
|
2024-05-24 20:54:42 +10:00
|
|
|
const pathToReplaced = JSON.parse(JSON.stringify(finPath))
|
|
|
|
pathToReplaced[1][0] = pathToReplaced[1][0] + 1
|
2023-04-01 16:47:00 +11:00
|
|
|
const startPath = finPath.slice(0, -1)
|
|
|
|
const nodeToReplace = getNodeFromPath(_ast, startPath).node as any
|
|
|
|
nodeToReplace[last[0]] = identifier
|
2024-05-24 20:54:42 +10:00
|
|
|
return { modifiedAst: _ast, pathToReplaced }
|
2023-04-01 16:47:00 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
const hasPipeSub = isTypeInValue(finVal as Value, 'PipeSubstitution')
|
|
|
|
const isIdentifierCallee = path[path.length - 1][0] !== 'callee'
|
|
|
|
return {
|
|
|
|
isSafe:
|
|
|
|
!hasPipeSub &&
|
|
|
|
isIdentifierCallee &&
|
|
|
|
acceptedNodeTypes.includes((finVal as any)?.type) &&
|
|
|
|
finPath.map(([_, type]) => type).includes('VariableDeclaration'),
|
|
|
|
value: finVal as Value,
|
|
|
|
replacer: replaceNodeWithIdentifier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
export function isNodeSafeToReplace(
|
|
|
|
ast: Program,
|
|
|
|
sourceRange: [number, number]
|
|
|
|
): {
|
|
|
|
isSafe: boolean
|
|
|
|
value: Value
|
|
|
|
replacer: ReplacerFn
|
|
|
|
} {
|
|
|
|
let path = getNodePathFromSourceRange(ast, sourceRange)
|
|
|
|
return isNodeSafeToReplacePath(ast, path)
|
|
|
|
}
|
|
|
|
|
2023-04-01 16:47:00 +11:00
|
|
|
export function isTypeInValue(node: Value, syntaxType: SyntaxType): boolean {
|
|
|
|
if (node.type === syntaxType) return true
|
|
|
|
if (node.type === 'BinaryExpression') return isTypeInBinExp(node, syntaxType)
|
|
|
|
if (node.type === 'CallExpression') return isTypeInCallExp(node, syntaxType)
|
|
|
|
if (node.type === 'ArrayExpression') return isTypeInArrayExp(node, syntaxType)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
function isTypeInBinExp(
|
|
|
|
node: BinaryExpression,
|
|
|
|
syntaxType: SyntaxType
|
|
|
|
): boolean {
|
|
|
|
if (node.type === syntaxType) return true
|
|
|
|
if (node.left.type === syntaxType) return true
|
|
|
|
if (node.right.type === syntaxType) return true
|
|
|
|
|
|
|
|
return (
|
|
|
|
isTypeInValue(node.left, syntaxType) ||
|
|
|
|
isTypeInValue(node.right, syntaxType)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isTypeInCallExp(
|
|
|
|
node: CallExpression,
|
|
|
|
syntaxType: SyntaxType
|
|
|
|
): boolean {
|
|
|
|
if (node.callee.type === syntaxType) return true
|
|
|
|
return node.arguments.some((arg) => isTypeInValue(arg, syntaxType))
|
|
|
|
}
|
|
|
|
|
|
|
|
function isTypeInArrayExp(
|
|
|
|
node: ArrayExpression,
|
|
|
|
syntaxType: SyntaxType
|
|
|
|
): boolean {
|
|
|
|
return node.elements.some((el) => isTypeInValue(el, syntaxType))
|
|
|
|
}
|
2023-04-06 12:45:56 +10:00
|
|
|
|
|
|
|
export function isValueZero(val?: Value): boolean {
|
|
|
|
return (
|
|
|
|
(val?.type === 'Literal' && Number(val.value) === 0) ||
|
|
|
|
(val?.type === 'UnaryExpression' &&
|
|
|
|
val.operator === '-' &&
|
|
|
|
val.argument.type === 'Literal' &&
|
|
|
|
Number(val.argument.value) === 0)
|
|
|
|
)
|
|
|
|
}
|
2023-04-08 14:16:49 +10:00
|
|
|
|
|
|
|
export function isLinesParallelAndConstrained(
|
|
|
|
ast: Program,
|
|
|
|
programMemory: ProgramMemory,
|
|
|
|
primaryLine: Selection,
|
|
|
|
secondaryLine: Selection
|
|
|
|
): {
|
|
|
|
isParallelAndConstrained: boolean
|
|
|
|
sourceRange: SourceRange
|
|
|
|
} {
|
|
|
|
try {
|
|
|
|
const EPSILON = 0.005
|
|
|
|
const primaryPath = getNodePathFromSourceRange(ast, primaryLine.range)
|
|
|
|
const secondaryPath = getNodePathFromSourceRange(ast, secondaryLine.range)
|
|
|
|
const secondaryNode = getNodeFromPath<CallExpression>(
|
|
|
|
ast,
|
|
|
|
secondaryPath,
|
|
|
|
'CallExpression'
|
|
|
|
).node
|
|
|
|
const varDec = getNodeFromPath(ast, primaryPath, 'VariableDeclaration').node
|
|
|
|
const varName = (varDec as VariableDeclaration)?.declarations[0]?.id?.name
|
|
|
|
const path = programMemory?.root[varName] as SketchGroup
|
|
|
|
const primarySegment = getSketchSegmentFromSourceRange(
|
|
|
|
path,
|
|
|
|
primaryLine.range
|
|
|
|
).segment
|
|
|
|
const { segment: secondarySegment, index: secondaryIndex } =
|
|
|
|
getSketchSegmentFromSourceRange(path, secondaryLine.range)
|
|
|
|
const primaryAngle = getAngle(primarySegment.from, primarySegment.to)
|
|
|
|
const secondaryAngle = getAngle(secondarySegment.from, secondarySegment.to)
|
|
|
|
const secondaryAngleAlt = getAngle(
|
|
|
|
secondarySegment.to,
|
|
|
|
secondarySegment.from
|
|
|
|
)
|
|
|
|
const isParallel =
|
|
|
|
Math.abs(primaryAngle - secondaryAngle) < EPSILON ||
|
|
|
|
Math.abs(primaryAngle - secondaryAngleAlt) < EPSILON
|
|
|
|
|
|
|
|
// is secordary line fully constrain, or has constrain type of 'angle'
|
|
|
|
const secondaryFirstArg = getFirstArg(secondaryNode)
|
|
|
|
const constraintType = getConstraintType(
|
|
|
|
secondaryFirstArg.val,
|
2023-09-15 11:48:23 -04:00
|
|
|
secondaryNode.callee.name as ToolTip
|
2023-04-08 14:16:49 +10:00
|
|
|
)
|
|
|
|
const constraintLevel = getConstraintLevelFromSourceRange(
|
|
|
|
secondaryLine.range,
|
|
|
|
ast
|
2024-02-11 12:59:00 +11:00
|
|
|
).level
|
2023-04-08 14:16:49 +10:00
|
|
|
const isConstrained =
|
|
|
|
constraintType === 'angle' || constraintLevel === 'full'
|
|
|
|
|
|
|
|
// get the previous segment
|
|
|
|
const prevSegment = (programMemory.root[varName] as SketchGroup).value[
|
|
|
|
secondaryIndex - 1
|
|
|
|
]
|
|
|
|
const prevSourceRange = prevSegment.__geoMeta.sourceRange
|
|
|
|
|
|
|
|
const isParallelAndConstrained =
|
|
|
|
isParallel && isConstrained && !!prevSourceRange
|
|
|
|
|
|
|
|
return {
|
|
|
|
isParallelAndConstrained,
|
|
|
|
sourceRange: prevSourceRange,
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
isParallelAndConstrained: false,
|
|
|
|
sourceRange: [0, 0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
|
|
|
|
export function doesPipeHaveCallExp({
|
|
|
|
ast,
|
|
|
|
selection,
|
|
|
|
calleeName,
|
|
|
|
}: {
|
|
|
|
calleeName: string
|
|
|
|
ast: Program
|
|
|
|
selection: Selection
|
|
|
|
}): boolean {
|
|
|
|
const pathToNode = getNodePathFromSourceRange(ast, selection.range)
|
|
|
|
const pipeExpression = getNodeFromPath<PipeExpression>(
|
|
|
|
ast,
|
|
|
|
pathToNode,
|
|
|
|
'PipeExpression'
|
|
|
|
).node
|
|
|
|
if (pipeExpression.type !== 'PipeExpression') return false
|
|
|
|
return pipeExpression.body.some(
|
|
|
|
(expression) =>
|
|
|
|
expression.type === 'CallExpression' &&
|
|
|
|
expression.callee.name === calleeName
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasExtrudeSketchGroup({
|
|
|
|
ast,
|
|
|
|
selection,
|
|
|
|
programMemory,
|
|
|
|
}: {
|
|
|
|
ast: Program
|
|
|
|
selection: Selection
|
|
|
|
programMemory: ProgramMemory
|
|
|
|
}): boolean {
|
|
|
|
const pathToNode = getNodePathFromSourceRange(ast, selection.range)
|
|
|
|
const varDec = getNodeFromPath<VariableDeclaration>(
|
|
|
|
ast,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclaration'
|
|
|
|
).node
|
|
|
|
if (varDec.type !== 'VariableDeclaration') return false
|
|
|
|
const varName = varDec.declarations[0].id.name
|
|
|
|
const varValue = programMemory?.root[varName]
|
|
|
|
return varValue?.type === 'ExtrudeGroup' || varValue?.type === 'SketchGroup'
|
|
|
|
}
|
2024-02-19 17:23:03 +11:00
|
|
|
|
|
|
|
export function isSingleCursorInPipe(
|
|
|
|
selectionRanges: Selections,
|
|
|
|
ast: Program
|
|
|
|
) {
|
|
|
|
if (selectionRanges.codeBasedSelections.length !== 1) return false
|
|
|
|
const selection = selectionRanges.codeBasedSelections[0]
|
|
|
|
const pathToNode = getNodePathFromSourceRange(ast, selection.range)
|
|
|
|
const nodeTypes = pathToNode.map(([, type]) => type)
|
|
|
|
if (nodeTypes.includes('FunctionExpression')) return false
|
|
|
|
if (nodeTypes.includes('PipeExpression')) return true
|
|
|
|
return false
|
|
|
|
}
|
2024-05-24 20:54:42 +10:00
|
|
|
|
|
|
|
export function findUsesOfTagInPipe(
|
|
|
|
ast: Program,
|
|
|
|
pathToNode: PathToNode
|
|
|
|
): SourceRange[] {
|
|
|
|
const stdlibFunctionsThatTakeTagInputs = [
|
|
|
|
'segAng',
|
|
|
|
'segEndX',
|
|
|
|
'segEndY',
|
|
|
|
'segLen',
|
|
|
|
]
|
|
|
|
const node = getNodeFromPath<CallExpression>(
|
|
|
|
ast,
|
|
|
|
pathToNode,
|
|
|
|
'CallExpression'
|
|
|
|
).node
|
|
|
|
if (node.type !== 'CallExpression') return []
|
|
|
|
const tagIndex = node.callee.name === 'close' ? 1 : 2
|
|
|
|
const thirdParam = node.arguments[tagIndex]
|
|
|
|
if (thirdParam?.type !== 'Literal') return []
|
|
|
|
const tag = String(thirdParam.value)
|
|
|
|
|
|
|
|
const varDec = getNodeFromPath<VariableDeclaration>(
|
|
|
|
ast,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclaration'
|
|
|
|
).node
|
|
|
|
const dependentRanges: SourceRange[] = []
|
|
|
|
|
|
|
|
traverse(varDec, {
|
|
|
|
enter: (node) => {
|
|
|
|
if (
|
|
|
|
node.type !== 'CallExpression' ||
|
|
|
|
!stdlibFunctionsThatTakeTagInputs.includes(node.callee.name)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
const tagArg = node.arguments[0]
|
|
|
|
if (tagArg.type !== 'Literal') return
|
|
|
|
if (String(tagArg.value) === tag)
|
|
|
|
dependentRanges.push([node.start, node.end])
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return dependentRanges
|
|
|
|
}
|
2024-06-21 13:20:42 +10:00
|
|
|
|
|
|
|
export function hasSketchPipeBeenExtruded(selection: Selection, ast: Program) {
|
|
|
|
const path = getNodePathFromSourceRange(ast, selection.range)
|
|
|
|
const { node: pipeExpression } = getNodeFromPath<PipeExpression>(
|
|
|
|
ast,
|
|
|
|
path,
|
|
|
|
'PipeExpression'
|
|
|
|
)
|
|
|
|
if (pipeExpression.type !== 'PipeExpression') return false
|
|
|
|
const varDec = getNodeFromPath<VariableDeclarator>(
|
|
|
|
ast,
|
|
|
|
path,
|
|
|
|
'VariableDeclarator'
|
|
|
|
).node
|
|
|
|
let extruded = false
|
|
|
|
traverse(ast as any, {
|
|
|
|
enter(node) {
|
|
|
|
if (
|
|
|
|
node.type === 'CallExpression' &&
|
|
|
|
node.callee.type === 'Identifier' &&
|
|
|
|
node.callee.name === 'extrude' &&
|
|
|
|
node.arguments?.[1]?.type === 'Identifier' &&
|
|
|
|
node.arguments[1].name === varDec.id.name
|
|
|
|
) {
|
|
|
|
extruded = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return extruded
|
|
|
|
}
|
|
|
|
|
|
|
|
/** File must contain at least one sketch that has not been extruded already */
|
|
|
|
export function hasExtrudableGeometry(ast: Program) {
|
|
|
|
const theMap: any = {}
|
|
|
|
traverse(ast as any, {
|
|
|
|
enter(node) {
|
|
|
|
if (
|
|
|
|
node.type === 'VariableDeclarator' &&
|
|
|
|
node.init?.type === 'PipeExpression'
|
|
|
|
) {
|
|
|
|
let hasStartProfileAt = false
|
|
|
|
let hasStartSketchOn = false
|
|
|
|
let hasClose = false
|
|
|
|
for (const pipe of node.init.body) {
|
|
|
|
if (
|
|
|
|
pipe.type === 'CallExpression' &&
|
|
|
|
pipe.callee.name === 'startProfileAt'
|
|
|
|
) {
|
|
|
|
hasStartProfileAt = true
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
pipe.type === 'CallExpression' &&
|
|
|
|
pipe.callee.name === 'startSketchOn'
|
|
|
|
) {
|
|
|
|
hasStartSketchOn = true
|
|
|
|
}
|
|
|
|
if (pipe.type === 'CallExpression' && pipe.callee.name === 'close') {
|
|
|
|
hasClose = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasStartProfileAt && hasStartSketchOn && hasClose) {
|
|
|
|
theMap[node.id.name] = true
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
node.type === 'CallExpression' &&
|
|
|
|
node.callee.name === 'extrude' &&
|
|
|
|
node.arguments[1]?.type === 'Identifier' &&
|
|
|
|
theMap?.[node?.arguments?.[1]?.name]
|
|
|
|
) {
|
|
|
|
delete theMap[node.arguments[1].name]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return Object.keys(theMap).length > 0
|
|
|
|
}
|