Skip to content

Commit 3160e3d

Browse files
feat: add getFirstNode and getLastNode to cursor with tests (#8878)
* feat: add getFirstNode and getlastNode to line_cursor.ts * chore: add simple tests for getFirstNode and getLastNode * chore: broken tests for debugging * chore: additional cursor tests * chore: lint, format, reenable tasks
1 parent c5736bb commit 3160e3d

File tree

2 files changed

+403
-113
lines changed

2 files changed

+403
-113
lines changed

core/keyboard_nav/line_cursor.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,43 @@ export class LineCursor extends Marker {
756756
}
757757
}
758758
}
759+
760+
/**
761+
* Get the first navigable node on the workspace, or null if none exist.
762+
*
763+
* @returns The first navigable node on the workspace, or null.
764+
*/
765+
getFirstNode(): ASTNode | null {
766+
const topBlocks = this.workspace.getTopBlocks(true);
767+
if (!topBlocks.length) return null;
768+
return ASTNode.createTopNode(topBlocks[0]);
769+
}
770+
771+
/**
772+
* Get the last navigable node on the workspace, or null if none exist.
773+
*
774+
* @returns The last navigable node on the workspace, or null.
775+
*/
776+
getLastNode(): ASTNode | null {
777+
// Loop back to last block if it exists.
778+
const topBlocks = this.workspace.getTopBlocks(true);
779+
if (!topBlocks.length) return null;
780+
781+
// Find the last stack.
782+
const lastTopBlockNode = ASTNode.createStackNode(
783+
topBlocks[topBlocks.length - 1],
784+
);
785+
let prevNode = lastTopBlockNode;
786+
let nextNode: ASTNode | null = lastTopBlockNode;
787+
// Iterate until you fall off the end of the stack.
788+
while (nextNode) {
789+
prevNode = nextNode;
790+
nextNode = this.getNextNode(prevNode, (node) => {
791+
return !!node;
792+
});
793+
}
794+
return prevNode;
795+
}
759796
}
760797

761798
registry.register(registry.Type.CURSOR, registry.DEFAULT, LineCursor);

0 commit comments

Comments
 (0)