From 794b9c70c98d87505070a9a3fda277e4fe527207 Mon Sep 17 00:00:00 2001 From: Dmitry Vasilev Date: Fri, 13 Oct 2023 11:49:14 +0300 Subject: [PATCH] Fix selection selects unevaluatable node --- src/cmd.js | 11 +++-------- test/test.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/cmd.js b/src/cmd.js index e7ade81..91360e5 100644 --- a/src/cmd.js +++ b/src/cmd.js @@ -374,16 +374,11 @@ const get_next_selection_state = (selection_state, frame, is_expand, index) => { } } } else { - // TODO when collapsing, also check that node is evaluatable // collapse if(selection_state.node.children != null){ - next_node = - selection_state.node.children.find(n => - n.index <= index && n.index + n.length > index - ) - ?? - // cursor not inside child but in whitespace - selection_state.node + const leaf = find_leaf(selection_state.node, index) + next_node = ancestry_inc(leaf, selection_state.node) + .findLast(n => !n.not_evaluatable && n != selection_state.node) } else { // no children, cannot collapse next_node = selection_state.node diff --git a/test/test.js b/test/test.js index d3d8116..f514bdd 100644 --- a/test/test.js +++ b/test/test.js @@ -2130,6 +2130,17 @@ const y = x()` } ) }), + + test('eval_selection bug', () => { + const code = `{foo: 1}` + const i = test_initial_state(code) + const index = code.indexOf('1') + const moved = COMMANDS.move_cursor(i, index) + const selection = COMMANDS.eval_selection(moved, index, true).state + const selection2 = COMMANDS.eval_selection(selection, index, true).state + const selection3 = COMMANDS.eval_selection(selection2, index, false).state + assert_equal(selection3.selection_state.node.value, '1') + }), test('find_call', () => { const code = `