Skip to content

Commit

Permalink
fix #113
Browse files Browse the repository at this point in the history
  • Loading branch information
dm0n3y committed Feb 12, 2025
1 parent 8c095a6 commit 9894e70
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 35 deletions.
33 changes: 16 additions & 17 deletions src/core/editor/Modify.re
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ let add_edge = (~hand=Caret.Hand.Focus, side: Dir.t, tok: Token.t) =>
let delete_toks =
(d: Dir.t, toks: list(Token.t)): Chain.t(Cell.t, Token.Unmolded.t) => {
let n = List.length(toks);
let car = Cell.point(~dirty=true, Focus);
toks
// first, clear text of selected tokens within selection bounds and mark
// either the first or last token with the final cursor position
Expand Down Expand Up @@ -319,13 +320,19 @@ let delete_toks =
~f=(tok, c) => {
let (l, tok, r) = Token.pop_end_carets(tok);
c
|> Chain.map_hd(r == None ? Fun.id : Fun.const(Cell.point(Focus)))
|> Chain.map_hd(r == None ? Fun.id : Fun.const(car))
|> (
switch (tok.mtrl) {
| Space(_)
| Grout(_) when tok.text == "" =>
Chain.map_hd(l == None ? Fun.id : Fun.const(Cell.point(Focus)))
| _ => Chain.link(l == None ? Cell.dirty : Cell.point(Focus), tok)
Chain.map_hd(
l == None
? Fun.id
: car
|> Cell.map_marks(Cell.Marks.mark_degrouted([]))
|> Fun.const,
)
| _ => Chain.link(l == None ? Cell.dirty : car, tok)
}
);
},
Expand Down Expand Up @@ -371,7 +378,8 @@ let insert_toks =
// P.log("--- Modify.insert_toks/tok/Error removed");
// removed empty token
let next_fill =
Cell.mark_ends_dirty(Cell.Space.merge(fill, next_fill));
Cell.mark_ends_dirty(Cell.Space.merge(fill, next_fill))
|> Cell.mark_end_ungrouted(~side=R);
(ctx, next_fill);
}
},
Expand Down Expand Up @@ -539,6 +547,8 @@ let insert_remold =
// the case of tokens at the ends of the selection that are split by the selection
// boundaries, the selection-external affixes of those tokens are preserved.
let delete_sel = (d: Dir.t, z: Zipper.t): Zipper.t => {
// P.log("--- Modify.delete_sel");
Mode.set(Deleting(d));
switch (z.cur) {
| Point(_) => z
| Select(sel) =>
Expand Down Expand Up @@ -619,21 +629,9 @@ let try_truncate = (z: Zipper.t) => {

let delete = (d: Dir.t, z: Zipper.t) => {
open Options.Syntax;
// let/ () =
// // first try moving over space tokens.
// // need to refine this re: usr vs sys.
// switch (Ctx.face(~side=d, z.ctx)) {
// | Node({mtrl: Space(White(Sys)), text, _})
// when
// Cursor.is_point(z.cur)
// && text
// |> Dir.pick(d, (Strings.rev, Fun.id))
// |> String.starts_with(~prefix=" ") =>
// Move.perform(Step(H(d)), z)
// | _ => None
// };
// P.log("--- Modify.delete");
// P.show("z", Zipper.show(z));
Mode.set(Deleting(d));
let+ z =
Cursor.is_point(z.cur) ? Select.hstep(~char=true, d, z) : return(z);
let- () = try_truncate(z);
Expand All @@ -645,6 +643,7 @@ let insert = (s: string, z: Zipper.t) => {
open Options.Syntax;
let z = delete_sel(L, z);

Mode.set(Inserting(s));
// P.log("--- Modify.insert");
let- () = try_expand(s, z);
// P.log("didn't expand");
Expand Down
29 changes: 18 additions & 11 deletions src/core/editor/Move.re
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let map_focus =
open Options.Syntax;
let c = Zipper.zip(~save_cursor=true, z);
let* init = Option.bind(c.marks.cursor, Path.Cursor.get_focus);
let c = Cell.map_marks(Cell.Marks.mark_dirty(init), c);
let goal =
init
|> Layout.map(~round_tok?, ~tree=Layout.mk_cell(c), f)
Expand All @@ -34,6 +35,8 @@ let map_focus =
? None
: c
|> Cell.map_marks(Cell.Marks.put_focus(~save_anchor, goal))
|> Linter.repad
|> Cell.map_marks(Cell.Marks.flush)
|> Zipper.unzip;
};

Expand Down Expand Up @@ -102,21 +105,25 @@ let hstep = (d: Dir.t, z: Zipper.t): option(Zipper.t) => {
// P.show("stepped", Token.show(stepped));
// P.show("exited", string_of_bool(exited));
ctx
|> Ctx.push(~onto=b, stepped)
|> Ctx.push(~onto=b, ~fill=Cell.dirty, stepped)
|> (exited ? Fun.id : Ctx.push(~onto=d, stepped))
|> Zipper.mk;
};
Zipper.rebutton(z);
};
let rec hstep_n = (n: int, z: Zipper.t): Zipper.t => {
let step = (d, z) =>
hstep(d, z) |> Options.get_exn(Invalid_argument("Move.hstep_n"));
switch (n) {
| _ when n < 0 => z |> step(L) |> hstep_n(n + 1)
| _ when n > 0 => z |> step(R) |> hstep_n(n - 1)
| _zero => z
};
Zipper.rebutton(z)
|> Zipper.zip(~save_cursor=true)
|> Linter.repad
|> Cell.map_marks(Cell.Marks.flush)
|> Zipper.unzip_exn;
};
// let rec hstep_n = (n: int, z: Zipper.t): Zipper.t => {
// let step = (d, z) =>
// hstep(d, z) |> Options.get_exn(Invalid_argument("Move.hstep_n"));
// switch (n) {
// | _ when n < 0 => z |> step(L) |> hstep_n(n + 1)
// | _ when n > 0 => z |> step(R) |> hstep_n(n - 1)
// | _zero => z
// };
// };

let vstep = (~round_tok=?, ~save_anchor=false, d: Dir.t, z: Zipper.t) =>
switch (z.cur) {
Expand Down
5 changes: 5 additions & 0 deletions src/core/parser/Effects.re
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ let insert = tok => {
let remove = tok => perform(Remove(tok));
// let merge = (l, r) => perform(Merge(l, r));

let is_fresh_tile = (tok: Token.t) =>
Mtrl.is_tile(tok.mtrl)
&& !Token.is_empty(tok)
&& List.mem(Insert(tok), log^);

let perform_if = (eff: t, opt: option(_)) => {
if (Option.is_some(opt)) {
perform(eff);
Expand Down
15 changes: 11 additions & 4 deletions src/core/parser/Linter.re
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ let pad_wrap = (~break=false, c: Cell.t) => {
};

let rec repad = (~l=Delim.root, ~r=Delim.root, c: Cell.t) => {
// open Stds;
// P.log("--- Linter.repad");
// P.show("l", Delim.show(l));
// P.show("c", Cell.show(c));
// P.show("r", Delim.show(r));
let l_fresh =
l |> Bound.map(Effects.is_fresh_tile) |> Bound.get(~root=false);
let r_fresh =
r |> Bound.map(Effects.is_fresh_tile) |> Bound.get(~root=false);
switch (Cell.Space.get(c)) {
| None =>
let m = Option.get(Cell.get(c));
Expand All @@ -31,11 +40,9 @@ let rec repad = (~l=Delim.root, ~r=Delim.root, c: Cell.t) => {
|> Chain.map_link(Bound.get_exn)
|> Meld.of_chain
|> Cell.put;
| Some(_) when Cell.has_clean_cursor(c) => Cell.mark_clean(c)
| Some(_) when Cell.has_ungrouted_cursor(c) && !l_fresh && !r_fresh => c
| Some(_) =>
// open Stds;
// P.log("--- Linter.repad/Some/dirty or cursor");
// P.show("c", Cell.show(c));
let (_, spc_l) = Delim.padding(l).space;
let (_, brk_l) = Delim.padding(l).break;
let (spc_r, _) = Delim.padding(r).space;
Expand All @@ -62,7 +69,7 @@ let rec repad = (~l=Delim.root, ~r=Delim.root, c: Cell.t) => {
Chain.map_hd(Cell.Space.merge(c, ~fill=Cell.empty), acc);
switch (tok) {
| {mtrl: Space(_), text: " ", _} when height > 0 => drop()
| {mtrl: Space(White(Sys)), _}
| {mtrl: Space(White(_)), _}
when
no_pad || height == 0 && Result.is_ok(Chain.unlink(acc)) =>
drop()
Expand Down
18 changes: 15 additions & 3 deletions src/core/structure/Cell.re
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,26 @@ let mark_ends_dirty = (c: t) => {
let marks = {...c.marks, dirty};
{...c, marks};
};
let mark_end_ungrouted = (~side: Dir.t, c: t) => {
let p = end_path(~side, c);
let degrouted = c.marks.degrouted |> Path.Map.add(p, ());
{
...c,
marks: {
...c.marks,
degrouted,
},
};
};

let has_clean_cursor = (c: t) =>
let has_ungrouted_cursor = (c: t) =>
switch (c.marks.cursor) {
| None => false
| Some(Point(car)) => !Path.Map.mem(car.path, c.marks.dirty)
| Some(Point(car)) => !Path.Map.mem(car.path, c.marks.degrouted)

| Some(Select(sel)) =>
let (l, r) = sel.range;
Path.Map.(!mem(l, c.marks.dirty) && !mem(r, c.marks.dirty));
Path.Map.(!mem(l, c.marks.degrouted) && !mem(r, c.marks.degrouted));
};

let to_chain = (c: t) =>
Expand Down
9 changes: 9 additions & 0 deletions src/core/structure/marks/Marks.re
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,17 @@ module Cell = {
};

let dirty = mk(~dirty=Path.Map.singleton(Path.empty, ()), ());
let mark_dirty = (path, marks) => {
...marks,
dirty: Path.Map.add(path, (), marks.dirty),
};
let mark_clean = marks => {...marks, dirty: Path.Map.empty};

let mark_degrouted = (path, marks) => {
...marks,
degrouted: Path.Map.add(path, (), marks.degrouted),
};

// clear all temporary marks used by grouter/linter
let flush = marks => {
...marks,
Expand Down

0 comments on commit 9894e70

Please sign in to comment.