Skip to content

Commit 65a2576

Browse files
committed
fixed newline hole bug
1 parent 66767fb commit 65a2576

File tree

4 files changed

+125
-12
lines changed

4 files changed

+125
-12
lines changed

src/core/editor/Modify.re

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,9 @@ let apply_changes =
591591

592592
let apply_remold = (changes, ctx) => {
593593
open Choice.Syntax;
594-
// P.log("--- Modify.apply_remold");
595-
// P.show("changes", Changes.show(changes));
596-
// P.show("ctx", Ctx.show(ctx));
594+
P.log("--- Modify.apply_remold");
595+
P.show("changes", Changes.show(changes));
596+
P.show("ctx", Ctx.show(ctx));
597597
let+ changed = apply_changes(changes, ctx);
598598
() => {
599599
open Options.Syntax;

src/core/parser/Grouter.re

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@ let rec split_cell_padding = (~side: Dir.t, c: Cell.t) =>
2929
}
3030
};
3131

32+
let rec extract_cell_padding = (~side: Dir.t, c: Cell.t) =>
33+
switch (Cell.get(c)) {
34+
| None => (Cell.empty, c)
35+
| Some(m) when Option.is_some(Meld.Space.get(m)) =>
36+
P.show("hit extract_cell_padding.space", Cell.show(c));
37+
switch (Cell.Space.split_cursor(c)) {
38+
| Some(s) =>
39+
P.show(
40+
"hit extract_cell_padding.split_cursor some fst: ",
41+
Cell.show(fst(s)),
42+
);
43+
P.show("snd: ", Cell.show(snd(s)));
44+
s;
45+
| None => (c, Cell.empty)
46+
};
47+
| Some(M(l, w, r)) =>
48+
switch (side) {
49+
| L =>
50+
let (p_l, l) = extract_cell_padding(~side=L, l);
51+
Cell.(p_l, put(M(l, w, r)));
52+
| R =>
53+
let (p_r, r) = extract_cell_padding(r, ~side=R);
54+
Cell.(p_r, put(M(l, w, r)));
55+
}
56+
};
57+
3258
module Cells = {
3359
[@deriving (show({with_path: false}), sexp, yojson)]
3460
type t = list(Cell.t);
@@ -160,6 +186,26 @@ let rec degrout = (c: Cell.t): Cells.t =>
160186
| _ => [c]
161187
};
162188

189+
let extract_newline = (~from: Dir.t, cs: Cells.t) => {
190+
switch (cs) {
191+
| [c] =>
192+
switch (from) {
193+
| L =>
194+
let (pad, c) = extract_cell_padding(~side=R, c);
195+
([c], pad);
196+
| R => (cs, Cell.empty)
197+
}
198+
| _ => (cs, Cell.empty)
199+
};
200+
};
201+
202+
let reinsert_newline = (~from: Dir.t, grouted: Grouted.t, nl: Cell.t) => {
203+
switch (from) {
204+
| L => Chain.map_hd(Tuples.map_snd(Cell.pad(~r=nl)), grouted)
205+
| R => grouted
206+
};
207+
};
208+
163209
let fill_default =
164210
fun
165211
| Mtrl.Space(_) => Cell.dirty
@@ -266,16 +312,26 @@ let fill = (~repair, ~from, cs, (swings, stances): Walk.t) => {
266312
// obligation delta. the given cells are expected to be oriented the same way as the
267313
// given walks according to from.
268314
let pick = (~repair=false, ~from: Dir.t, cs: list(Cell.t), ws: list(Walk.t)) => {
315+
open Options.Syntax;
269316
// if (dbg^) {
270-
// P.log("--- Grouter.pick");
271-
// P.show("from", Dir.show(from));
272-
// P.show("cs", Cells.show(cs));
317+
P.log("--- Grouter.pick");
318+
P.show("from", Dir.show(from));
319+
P.show("cs", Cells.show(cs));
273320
// P.log("ws");
274321
// ws |> List.iter(w => P.show("w", Walk.show(w)));
275322
// };
276-
Oblig.Delta.minimize(
277-
~to_zero=!repair,
278-
fill(~repair, ~from, cs),
279-
ws,
280-
);
323+
324+
let (cs, nl) = extract_newline(~from, cs);
325+
326+
P.show("cs post newline extract", Cells.show(cs));
327+
P.show("nl", Cell.show(nl));
328+
329+
let+ grouted =
330+
Oblig.Delta.minimize(~to_zero=!repair, fill(~repair, ~from, cs), ws);
331+
332+
P.show("grouted no nl", Grouted.show(grouted));
333+
334+
let reinsert = reinsert_newline(~from, grouted, nl);
335+
P.show("grouted with nl ", Grouted.show(reinsert));
336+
reinsert;
281337
};

src/core/structure/Cell.re

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,63 @@ module Space = {
296296
| _ => put(Meld.of_chain((cs, ts)))
297297
};
298298

299+
let split_cursor = (c: t) => {
300+
open Options.Syntax;
301+
assert(is_space(c));
302+
//g = general get
303+
//calling get on a space cell (spaces separated by empty cells with potential metadata ie degrout)
304+
let* m = g(c);
305+
//cd is now reversed to be from the right
306+
let (cs, ts) = m |> Meld.rev |> Meld.to_chain;
307+
cs |> List.iter(c => P.show("split_cursor_rev cs", show(c)));
308+
ts |> List.iter(t => P.show("split_cursor_rev ts", Token.show(t)));
309+
//traverses cells and searches for 1st cell with cursor mark
310+
let (cs_r, cs_l) =
311+
cs |> Lists.split_while(~f=(c: t) => Option.is_none(c.marks.cursor));
312+
313+
cs_r |> List.iter(c => P.show("split_cursor_cursor cs_r", show(c)));
314+
cs_l |> List.iter(c => P.show("split_cursor_cursor cs_l", show(c)));
315+
316+
switch (cs_r, cs_l) {
317+
//we failed to find a cursor mark
318+
| (_, []) => None
319+
//immediately encounter cursor on the "d" side
320+
| ([], [l_hd, ...l_tl]) =>
321+
//ensuring no cursor duplication in cells with cursor mark
322+
let l_hd_dup = {
323+
...l_hd,
324+
marks: {
325+
...l_hd.marks,
326+
cursor: None,
327+
},
328+
};
329+
330+
P.show("l_hd_dup", show(l_hd_dup));
331+
332+
let rest = Meld.of_chain(([l_hd, ...l_tl], ts)) |> Meld.rev |> put;
333+
Some((l_hd_dup, rest));
334+
| ([_, ..._], [l_hd, ...l_tl]) =>
335+
let (ts_r, ts_l) = Lists.split_n(ts, List.length(cs_r));
336+
let l_hd_dup = {
337+
...l_hd,
338+
marks: {
339+
...l_hd.marks,
340+
cursor: None,
341+
},
342+
};
343+
344+
let cs_r = cs_r @ [l_hd_dup];
345+
let cs_l = [l_hd, ...l_tl];
346+
cs_r |> List.iter(c => P.show("cs_r", show(c)));
347+
cs_l |> List.iter(c => P.show("cs_l", show(c)));
348+
let split = (cs_r, ts_r) |> (c => Chain.rev(c)) |> Funs.uncurry(mk);
349+
let rest = (cs_l, ts_l) |> (c => Chain.rev(c)) |> Funs.uncurry(mk);
350+
ts_r |> List.iter(t => P.show("ts_r", Token.show(t)));
351+
ts_l |> List.iter(t => P.show("ts_l", Token.show(t)));
352+
Some((split, rest));
353+
};
354+
};
355+
299356
// returns split-off cell first (regardless of side), rest of cell second
300357
// if i find a degrout mark within the space cell, split the cell into two at that point
301358
let split = (~side as d: Dir.t, c: t) => {

src/web/store/Store.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let save_syntax_key: int => string =
2525
let save_syntax = (save_idx: int, z: Zipper.t) =>
2626
LocalStorage.set(save_syntax_key(save_idx), z |> serialize);
2727

28-
let _tasks = [];
28+
// let tasks = [];
2929
let tasks =
3030
Data.[
3131
safe_div,

0 commit comments

Comments
 (0)