| 59 | } |
| 60 | |
| 61 | function findActiveNode({ |
| 62 | line, |
| 63 | autoExpand = true, |
| 64 | }: { |
| 65 | line: number; |
| 66 | autoExpand?: boolean; |
| 67 | }) { |
| 68 | function dfs(node: INode, ancestors: INode[] = []) { |
| 69 | const [start, end] = |
| 70 | (node.payload?.lines as string)?.split(',').map((s) => +s) || []; |
| 71 | if (start >= 0 && start <= line && line < end) { |
| 72 | best = node; |
| 73 | bestAncestors = ancestors; |
| 74 | } |
| 75 | ancestors = [...ancestors, node]; |
| 76 | node.children?.forEach((child) => { |
| 77 | dfs(child, ancestors); |
| 78 | }); |
| 79 | } |
| 80 | let best: INode | undefined; |
| 81 | let bestAncestors: INode[] = []; |
| 82 | dfs((state.content.value as ITransformResult).root as INode); |
| 83 | if (autoExpand) { |
| 84 | bestAncestors.forEach((node) => { |
| 85 | if (node.payload?.fold) { |
| 86 | node.payload.fold = 0; |
| 87 | } |
| 88 | }); |
| 89 | } |
| 90 | return best; |
| 91 | } |
| 92 | |
| 93 | async function setCursor(options: { line: number; autoExpand?: boolean }) { |
| 94 | if (!state.content.value) return; |