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-16 10:34:11 -05:00
|
|
|
export function updatePathToNodeFromMap(
|
|
|
|
oldPath: PathToNode,
|
|
|
|
pathToNodeMap: { [key: number]: PathToNode }
|
2024-05-31 11:36:08 +10:00
|
|
|
): PathToNode {
|
2024-12-16 10:34:11 -05:00
|
|
|
const updatedPathToNode = structuredClone(oldPath)
|
|
|
|
let max = 0
|
|
|
|
Object.values(pathToNodeMap).forEach((path) => {
|
|
|
|
const index = Number(path[1][0])
|
|
|
|
if (index > max) {
|
|
|
|
max = index
|
|
|
|
}
|
|
|
|
})
|
|
|
|
updatedPathToNode[1][0] = max
|
|
|
|
return updatedPathToNode
|
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-16 10:34:11 -05:00
|
|
|
types: ['segment', 'path'],
|
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'
|
|
|
|
}
|