* Creates a node finder which will apply a transformation to the index of a * value node and return the node at the given index of the nodes parent * @param parentFinder A finder which will be applied to the parent to determine * whether it is a match * @param indexTransform A function that will
( parentFinder: NodeFinder, indexTransform: (index: number) => number )
| 46 | * @returns A node finder based on the given description |
| 47 | */ |
| 48 | function indexNodeFinder( |
| 49 | parentFinder: NodeFinder, |
| 50 | indexTransform: (index: number) => number |
| 51 | ) { |
| 52 | return (node: SyntaxNode) => { |
| 53 | const parent = node.parent; |
| 54 | |
| 55 | if (parent == null || parentFinder(parent) == null) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | const valueNodes = getValueNodes(parent); |
| 60 | |
| 61 | const nodeIndex = valueNodes.findIndex(({ id }) => id === node.id); |
| 62 | |
| 63 | if (nodeIndex === -1) { |
| 64 | // TODO: In the future we might conceivably try to handle saying "take |
| 65 | // item" when the selection is inside a comment between the key and value |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | const desiredIndex = indexTransform(nodeIndex); |
| 70 | |
| 71 | if (desiredIndex === -1) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | return valueNodes[desiredIndex]; |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | function itemFinder() { |
| 80 | return indexNodeFinder( |
no test coverage detected