From 69dbe28ef36dd31a7c3eaa24c7b224353c3ab47e Mon Sep 17 00:00:00 2001 From: David Moon Date: Sun, 10 Nov 2024 13:10:39 -0500 Subject: [PATCH] fix broken up/down selection --- src/core/editor/Move.re | 21 ++++++++++++++------- src/core/editor/Select.re | 6 +++--- src/core/structure/marks/Marks.re | 22 ++++++++++++++++++++-- src/web/view/Code.re | 6 +++--- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/core/editor/Move.re b/src/core/editor/Move.re index 24b9fe3c..8c6df999 100644 --- a/src/core/editor/Move.re +++ b/src/core/editor/Move.re @@ -12,13 +12,17 @@ type t = // bounds goal pos to within start/end pos of program. // returns none if the resulting goal pos is same as start pos. -let map_focus = (f: Loc.t => Loc.t, z: Zipper.t): option(Zipper.t) => { +let map_focus = + (~drop_anchor=false, f: Loc.t => Loc.t, z: Zipper.t): option(Zipper.t) => { open Options.Syntax; let c = Zipper.zip(~save_cursor=true, z); let* init = Option.bind(c.marks.cursor, Path.Cursor.get_focus); let goal = Layout.map(~tree=Layout.mk_cell(c), f, init); goal == init - ? None : c |> Cell.map_marks(Cell.Marks.put_focus(goal)) |> Zipper.unzip; + ? None + : c + |> Cell.map_marks(Cell.Marks.put_focus(~drop_anchor, goal)) + |> Zipper.unzip; }; // returns token with updated cursor after moving in direction d. @@ -97,11 +101,13 @@ let rec hstep_n = (n: int, z: Zipper.t): Zipper.t => { }; }; -let vstep = (d: Dir.t) => - map_focus(loc => {...loc, row: loc.row + Dir.pick(d, ((-1), 1))}); +let vstep = (~drop_anchor=false, d: Dir.t) => + map_focus(~drop_anchor, loc => + {...loc, row: loc.row + Dir.pick(d, ((-1), 1))} + ); -let skip = (d2: Dir2.t) => - map_focus(loc => +let skip = (~drop_anchor=false, d2: Dir2.t) => + map_focus(~drop_anchor, loc => switch (d2) { | H(L) => {...loc, col: 0} | H(R) => {...loc, col: Int.max_int} @@ -110,7 +116,8 @@ let skip = (d2: Dir2.t) => } ); -let jump = loc => map_focus(Fun.const(loc)); +let jump = (~drop_anchor=false, loc) => + map_focus(~drop_anchor, Fun.const(loc)); // todo: need to return none in some more cases when no visible movement occurs let perform = diff --git a/src/core/editor/Select.re b/src/core/editor/Select.re index 5fc5d8c5..3d259e24 100644 --- a/src/core/editor/Select.re +++ b/src/core/editor/Select.re @@ -167,8 +167,8 @@ let perform = (a: t, z: Zipper.t): option(Zipper.t) => | Move(a) => switch (a) { | Step(H(d)) => hstep(d, z) - | Step(V(d)) => Move.vstep(d, z) - | Skip(d2) => Move.skip(d2, z) - | Jump(pos) => Move.jump(pos, z) + | Step(V(d)) => Move.vstep(~drop_anchor=true, d, z) + | Skip(d2) => Move.skip(~drop_anchor=true, d2, z) + | Jump(pos) => Move.jump(~drop_anchor=true, pos, z) } }; diff --git a/src/core/structure/marks/Marks.re b/src/core/structure/marks/Marks.re index 52b8ae5a..4ee4ad19 100644 --- a/src/core/structure/marks/Marks.re +++ b/src/core/structure/marks/Marks.re @@ -84,9 +84,27 @@ module Cell = { let put_cursor = (cur, marks) => {...marks, cursor: Some(cur)}; let get_focus = (marks: t) => Option.bind(marks.cursor, Path.Cursor.get_focus); - let put_focus = (path: Path.t, marks: t) => { + // let map_focus = (marks: t) => Path.Cursor.get_focus(marks.cursor); + let put_focus = (~drop_anchor=false, foc: Path.t, marks: t) => { ...marks, - cursor: Path.Cursor.put_focus(path, marks.cursor), + cursor: { + open Options.Syntax; + let/ () = { + let* () = Options.of_bool(drop_anchor); + let* cur = marks.cursor; + // only drop anchor if cursor was pointing + let* car = Cursor.get_point(cur); + let+ sel = + Result.to_option( + Path.Selection.of_carets( + Caret.focus(foc), + Caret.anchor(car.path), + ), + ); + Path.Cursor.select(sel); + }; + Path.Cursor.put_focus(foc, marks.cursor); + }, }; let add_oblig = (~path=Path.empty, t: Mtrl.T.t, marks: t) => { diff --git a/src/web/view/Code.re b/src/web/view/Code.re index 5ee768b1..34422156 100644 --- a/src/web/view/Code.re +++ b/src/web/view/Code.re @@ -73,10 +73,10 @@ let cursor = (~font, z: Zipper.t) => { }; let view = (~font: Model.Font.t, ~zipper: Zipper.t): Node.t => { - // print_endline("--- Code.view ---"); - // print_endline("z = " ++ Zipper.show(zipper)); + // P.log("--- Code.view ---"); + // P.show("z", Zipper.show(zipper)); let c = Zipper.zip(~save_cursor=true, zipper); - // print_endline("c = " ++ Cell.show(c)); + // P.show("c", Cell.show(c)); // let t = Layout.mk_cell(c); // print_endline("t = " ++ LCell.show(t)); // let b = LCell.flatten(t);