From 8f59649956346440b417fda861e00d8375fa8be2 Mon Sep 17 00:00:00 2001 From: Grace <145345672+microbit-grace@users.noreply.github.com> Date: Fri, 25 Apr 2025 16:26:58 +0100 Subject: [PATCH] fix: LineCursor can loop forward, but not back (#8926) * fix: loop cursor when moving to prev node * chore: add loop tests for LineCursor prev and out * chore: fix out loop test for line cursor --- core/keyboard_nav/line_cursor.ts | 7 +++++-- tests/mocha/cursor_test.js | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/keyboard_nav/line_cursor.ts b/core/keyboard_nav/line_cursor.ts index b2bda39c7..2025da7bf 100644 --- a/core/keyboard_nav/line_cursor.ts +++ b/core/keyboard_nav/line_cursor.ts @@ -146,10 +146,12 @@ export class LineCursor extends Marker { if (!curNode) { return null; } - const newNode = this.getPreviousNodeImpl( + const newNode = this.getPreviousNode( curNode, this.validLineNode.bind(this), + true, ); + if (newNode) { this.setCurNode(newNode); } @@ -168,9 +170,10 @@ export class LineCursor extends Marker { if (!curNode) { return null; } - const newNode = this.getPreviousNodeImpl( + const newNode = this.getPreviousNode( curNode, this.validInLineNode.bind(this), + true, ); if (newNode) { diff --git a/tests/mocha/cursor_test.js b/tests/mocha/cursor_test.js index 905f48c09..53f0714da 100644 --- a/tests/mocha/cursor_test.js +++ b/tests/mocha/cursor_test.js @@ -125,6 +125,15 @@ suite('Cursor', function () { assert.equal(curNode.getLocation(), this.blocks.A.nextConnection); }); + test('Prev - From first connection loop to last next connection', function () { + const prevConnection = this.blocks.A.previousConnection; + const prevConnectionNode = ASTNode.createConnectionNode(prevConnection); + this.cursor.setCurNode(prevConnectionNode); + this.cursor.prev(); + const curNode = this.cursor.getCurNode(); + assert.equal(curNode.getLocation(), this.blocks.D.nextConnection); + }); + test('Out - From field does not skip over block node', function () { const field = this.blocks.E.inputList[0].fieldRow[0]; const fieldNode = ASTNode.createFieldNode(field); @@ -133,6 +142,15 @@ suite('Cursor', function () { const curNode = this.cursor.getCurNode(); assert.equal(curNode.getLocation(), this.blocks.E); }); + + test('Out - From first connection loop to last next connection', function () { + const prevConnection = this.blocks.A.previousConnection; + const prevConnectionNode = ASTNode.createConnectionNode(prevConnection); + this.cursor.setCurNode(prevConnectionNode); + this.cursor.out(); + const curNode = this.cursor.getCurNode(); + assert.equal(curNode.getLocation(), this.blocks.D.nextConnection); + }); }); suite('Searching', function () { setup(function () {