2023-10-16 21:20:05 +11:00
|
|
|
import { Selections } from 'lib/selections'
|
2024-11-18 10:04:09 -05:00
|
|
|
import {
|
|
|
|
PathToNode,
|
|
|
|
CallExpression,
|
|
|
|
Literal,
|
|
|
|
ArrayExpression,
|
|
|
|
BinaryExpression,
|
|
|
|
} from './wasm'
|
2024-08-03 18:08:51 +10:00
|
|
|
import { ArtifactGraph, filterArtifacts } from 'lang/std/artifactGraph'
|
2023-10-11 15:12:29 +11:00
|
|
|
import { isOverlap } from 'lib/utils'
|
2023-08-28 18:48:31 +10:00
|
|
|
|
2024-12-14 09:57:33 +11:00
|
|
|
/**
|
|
|
|
* Updates pathToNode body indices to account for the insertion of an expression
|
|
|
|
* PathToNode expression is after the insertion index, that the body index is incremented
|
|
|
|
* Negative insertion index means no insertion
|
|
|
|
*/
|
|
|
|
export function updatePathToNodePostExprInjection(
|
|
|
|
pathToNode: PathToNode,
|
|
|
|
exprInsertIndex: number
|
2024-05-31 11:36:08 +10:00
|
|
|
): PathToNode {
|
2024-12-14 09:57:33 +11:00
|
|
|
if (exprInsertIndex < 0) return pathToNode
|
|
|
|
const bodyIndex = Number(pathToNode[1][0])
|
|
|
|
if (bodyIndex < exprInsertIndex) return pathToNode
|
|
|
|
const clone = structuredClone(pathToNode)
|
|
|
|
clone[1][0] = bodyIndex + 1
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateSketchDetailsNodePaths({
|
|
|
|
sketchEntryNodePath,
|
|
|
|
sketchNodePaths,
|
|
|
|
planeNodePath,
|
|
|
|
exprInsertIndex,
|
|
|
|
}: {
|
|
|
|
sketchEntryNodePath: PathToNode
|
|
|
|
sketchNodePaths: Array<PathToNode>
|
|
|
|
planeNodePath: PathToNode
|
|
|
|
exprInsertIndex: number
|
|
|
|
}) {
|
|
|
|
return {
|
|
|
|
updatedSketchEntryNodePath: updatePathToNodePostExprInjection(
|
|
|
|
sketchEntryNodePath,
|
|
|
|
exprInsertIndex
|
|
|
|
),
|
|
|
|
updatedSketchNodePaths: sketchNodePaths.map((path) =>
|
|
|
|
updatePathToNodePostExprInjection(path, exprInsertIndex)
|
|
|
|
),
|
|
|
|
updatedPlaneNodePath: updatePathToNodePostExprInjection(
|
|
|
|
planeNodePath,
|
|
|
|
exprInsertIndex
|
|
|
|
),
|
|
|
|
}
|
2024-05-31 11:36:08 +10:00
|
|
|
}
|
|
|
|
|
2023-10-11 15:12:29 +11:00
|
|
|
export function isCursorInSketchCommandRange(
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph: ArtifactGraph,
|
2023-10-11 15:12:29 +11:00
|
|
|
selectionRanges: Selections
|
|
|
|
): string | false {
|
2024-08-03 18:08:51 +10:00
|
|
|
const overlappingEntries = filterArtifacts(
|
|
|
|
{
|
2024-12-14 09:57:33 +11:00
|
|
|
types: ['segment', 'path', 'plane'],
|
2024-08-03 18:08:51 +10:00
|
|
|
predicate: (artifact) => {
|
2024-11-21 15:04:30 +11:00
|
|
|
return selectionRanges.graphSelections.some(
|
2024-08-03 18:08:51 +10:00
|
|
|
(selection) =>
|
2024-11-21 15:04:30 +11:00
|
|
|
Array.isArray(selection?.codeRef?.range) &&
|
2024-08-03 18:08:51 +10:00
|
|
|
Array.isArray(artifact?.codeRef?.range) &&
|
2024-11-21 15:04:30 +11:00
|
|
|
isOverlap(selection?.codeRef?.range, artifact.codeRef.range)
|
2024-08-03 18:08:51 +10:00
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
const firstEntry = [...overlappingEntries.values()]?.[0]
|
|
|
|
const parentId = firstEntry?.type === 'segment' ? firstEntry.pathId : false
|
|
|
|
|
|
|
|
return parentId
|
2024-07-25 19:03:56 +10:00
|
|
|
? parentId
|
2024-08-03 18:08:51 +10:00
|
|
|
: [...overlappingEntries].find(
|
|
|
|
([, artifact]) => artifact.type === 'path'
|
2024-07-25 19:03:56 +10:00
|
|
|
)?.[0] || false
|
2023-10-11 15:12:29 +11:00
|
|
|
}
|
2024-11-18 10:04:09 -05:00
|
|
|
|
|
|
|
export function isCallExpression(e: any): e is CallExpression {
|
|
|
|
return e && e.type === 'CallExpression'
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isArrayExpression(e: any): e is ArrayExpression {
|
|
|
|
return e && e.type === 'ArrayExpression'
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isLiteral(e: any): e is Literal {
|
|
|
|
return e && e.type === 'Literal'
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isBinaryExpression(e: any): e is BinaryExpression {
|
|
|
|
return e && e.type === 'BinaryExpression'
|
|
|
|
}
|