Skip to content

Commit

Permalink
fix broken up/down selection
Browse files Browse the repository at this point in the history
  • Loading branch information
dm0n3y committed Nov 10, 2024
1 parent f151bd0 commit 69dbe28
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
21 changes: 14 additions & 7 deletions src/core/editor/Move.re
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Expand All @@ -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 =
Expand Down
6 changes: 3 additions & 3 deletions src/core/editor/Select.re
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
};
22 changes: 20 additions & 2 deletions src/core/structure/marks/Marks.re
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
6 changes: 3 additions & 3 deletions src/web/view/Code.re
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 69dbe28

Please sign in to comment.