Skip to content

Commit

Permalink
fix #51
Browse files Browse the repository at this point in the history
  • Loading branch information
dm0n3y committed Feb 10, 2025
1 parent 57a7785 commit a714342
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 316 deletions.
66 changes: 47 additions & 19 deletions src/core/layout/Layout.re
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,25 @@ module State = {

let map = (f, s: t) => {...s, loc: f(s.loc)};

// let indent = (n: int, s: t) => {
// ind: s.ind,
// rel: s.rel + n,
// loc: Loc.shift(n, s.loc),
// };
let push_ind = (s: t) => {...s, ind: Indent.push(s.ind)};
let pop_ind = (s: t) => {...s, ind: Indent.pop(s.ind)};

// pairs of states recorded immediately before and after newlines.
// used to calculate selection silhouettes.
let newline_log: ref(list((t, t))) = ref([]);
let clear_log = () => newline_log := [];
let get_log = () => List.rev(newline_log^);

let return = (s: t, rel: int) => {
loc: Loc.return(s.loc, ~ind=Indent.peek(s.ind) + rel),
ind: {
...s.ind,
uni: rel,
},
let end_state = {
loc: Loc.return(s.loc, ~ind=Indent.peek(s.ind) + rel),
ind: {
...s.ind,
uni: rel,
},
};
newline_log := [(s, end_state), ...newline_log^];
end_state;
};

let rec jump_block = (s: t, ~over as B(b): Block.t) =>
Expand All @@ -89,24 +94,44 @@ module State = {
};
let jump_tok = jump_block;

let jump_terr_l = (~closed=false, s: t, ~over: LTerr.t) => {
let is_space = Mtrl.is_space(LTerr.sort(over));
let (ts, cs) = LTerr.unmk(over);
let n = List.length(ts);
List.combine(ts, cs)
|> List.mapi((i, tc) => (i, tc))
|> Lists.fold_left(~init=s, ~f=(s, (i, (b_tok, cell))) =>
s
|> (i == 0 && !is_space ? push_ind : Fun.id)
|> jump_tok(~over=b_tok)
|> (i == n - 1 && closed && !is_space ? Fun.id : pop_ind)
|> jump_cell(~over=cell)
);
};

// assuming terrace wald faces right
let jump_terr = (~closed=false, s: t, ~over: LTerr.t) => {
let is_space = Mtrl.is_space(LTerr.sort(over));
// let (ind, rel) = (s.ind, s.rel);
let (ts, cs) = LTerr.unmk(over);
List.combine(cs, ts)
|> List.rev
|> List.mapi((i, tc) => (i, tc))
|> List.mapi((i, ct) => (i, ct))
|> Lists.fold_left(~init=s, ~f=(s, (i, (cell, b_tok))) =>
s
|> jump_cell(~over=cell)
|> (i == 0 && !Mtrl.is_space(LTerr.sort(over)) ? push_ind : Fun.id)
|> (i == 0 && !is_space ? push_ind : Fun.id)
|> jump_tok(~over=b_tok)
)
|> (closed ? Fun.id : pop_ind);
|> (closed || is_space ? Fun.id : pop_ind);
};
// assuming dn slope
let jump_slope = (s: t, ~over: LSlope.t) =>
List.fold_right((terr, s) => jump_terr(s, ~over=terr), over, s);
let jump_slope = (~eqs=[], s: t, ~over: LSlope.t) =>
List.rev(over)
|> List.mapi((i, terr) => (i, terr))
|> Lists.fold_left(~init=s, ~f=(s, (i, terr)) =>
jump_terr(s, ~over=terr, ~closed=List.mem(i, eqs))
);
};

let max_path = _ => failwith("todo");
Expand Down Expand Up @@ -532,12 +557,15 @@ and unzip_select = (~ctx, sel: Path.Selection.t, meld: LMeld.t) => {
LZipper.mk(~eqs, Select(zigg), ctx);
};

let state_of_ctx = (ctx: LCtx.t) =>
let state_of_ctx = (~eqs=[], ctx: LCtx.t) => {
ctx
|> Chain.mapi_loop((i, frame) => (i, frame))
|> Chain.fold_right(
((pre: LSlope.t, _), (l: LTerr.t, _), state) =>
((i, (pre: LSlope.t, _)), (l: LTerr.t, _), state) =>
state
|> State.jump_terr(~over=l, ~closed=true)
|> State.jump_slope(~over=pre),
((pre, _)) => State.jump_slope(State.init, ~over=pre),
|> State.jump_slope(~eqs=i == 0 ? eqs : [], ~over=pre),
((i, (pre, _))) =>
State.jump_slope(~eqs=i == 0 ? eqs : [], State.init, ~over=pre),
);
};
7 changes: 7 additions & 0 deletions src/stds/Lists.re
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,10 @@ let rec disjoint_pairs = (xs: list('x)): list(('x, 'x)) =>
| [_] => []
| [x1, x2, ...xs] => [(x1, x2), ...disjoint_pairs(xs)]
};

let rec consnoc_pairs =
(init: 'x, xxs: list(('x, 'x)), final: 'x): list(('x, 'x)) =>
switch (xxs) {
| [] => [(init, final)]
| [(x1, x2), ...xxs] => [(init, x1), ...consnoc_pairs(x2, xxs, final)]
};
8 changes: 8 additions & 0 deletions src/web/util/Svgs.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Point = {
x: float,
y: float,
};
let mk_ = (~x, ~y) => {x: Float.of_int(x), y: Float.of_int(y)};
let move = (~x=0., ~y=0., p: t) => {x: p.x +. x, y: p.y +. y};
};

Expand All @@ -27,6 +28,11 @@ module Rect = {
width: float,
height: float,
};
let mk_ = (~min, ~width, ~height) => {
min,
width: Float.of_int(width),
height: Float.of_int(height),
};

let pad = (~x=0., ~y=0., {min, width, height}: t): t => {
min: Point.{x: min.x -. x, y: min.y -. y},
Expand Down Expand Up @@ -435,6 +441,8 @@ module OrthogonalPolygon = {

path
|> round_corners(corner_radii)
// start in middle of first edge bc round_corners cuts each edge in the middle
// and rotates the path by a single half-edge while processing
|> List.cons(
Path.M({
x: (start.src.x +. start.dst.x) *. 0.5,
Expand Down
3 changes: 2 additions & 1 deletion src/web/view/Code.re
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ let cursor = (~font, z: Zipper.t) => {
Fun.id,
);
let ind_lz = Layout.unzip(ind_cur, lc);
let state = Layout.state_of_ctx(ind_lz.ctx);
let state =
Layout.state_of_ctx(~eqs=fst(List.split(fst(ind_lz.eqs))), ind_lz.ctx);

// P.log("--- Code.cursor");
// P.show("c", Cell.show(c));
Expand Down
30 changes: 26 additions & 4 deletions src/web/view/dec/Child.re
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,34 @@ module Profile = {
sil: bool,
};

// ind is the indentation of delimiting token
// sil indicates whether the child border itself should be silhouetted
let mk =
(~sil=false, ~whole, ~ind, ~loc: Loc.t, ~null as (l, r), lc: LCell.t) => {
(
~sil=false,
~whole,
~ind,
~state: L.State.t,
~null as (l, r),
lc: LCell.t,
) => {
// todo: fix awkward conceptual overlap between state logging and dims
// calculation
let dims = Dims.of_block(LCell.flatten(lc));
let l = l ? Some(L.nth_line(whole, loc.row)) : None;
let r = r ? Some(L.nth_line(whole, loc.row + dims.height)) : None;
{ind, loc, dims, sort: LCell.sort(lc), no_delim: (l, r), sil};
let l = l ? Some(L.nth_line(whole, state.loc.row)) : None;
let r = r ? Some(L.nth_line(whole, state.loc.row + dims.height)) : None;
let p = {
ind,
loc: state.loc,
dims,
sort: LCell.sort(lc),
no_delim: (l, r),
sil,
};
L.State.clear_log();
let final = state |> L.State.jump_cell(~over=lc);
let ns = L.State.get_log();
((final, ns), p);
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/web/view/dec/Filters.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let all =
~attrs=[Attr.id("filters")],
[
Silhouette.Outer.blur,
Silhouette.Inner.blur,
// Silhouette.Inner.blur,
...List.map(Token.drop_shadow, Tylr_core.Sort.all),
],
);
68 changes: 30 additions & 38 deletions src/web/view/dec/Meld.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
open Sexplib.Std;
open Ppx_yojson_conv_lib.Yojson_conv.Primitives;

// to keep a reference to token dec
module T = Token;
module W = Wald;
Expand All @@ -11,20 +8,7 @@ module L = Layout;

module Profile = {
[@deriving (show({with_path: false}), sexp, yojson)]
type t = {
chain: Chain.t(Child.Profile.t, T.Profile.t),
sil: option(Silhouette.Inner.Profile.t),
};
exception No_tokens;

let cells = p => Chain.loops(p.chain);
let tokens = p => Chain.links(p.chain);

let sort = (p: t) =>
switch (tokens(p)) {
| [] => raise(No_tokens)
| [hd, ..._] => hd.style |> Option.map((style: T.Style.t) => style.sort)
};
type t = Layers.t;

let mk = (~sil=false, ~whole: LCell.t, ~state: L.State.t, lm: LMeld.t) => {
let M(lc_l, lw, lc_r) = lm;
Expand All @@ -34,45 +18,53 @@ module Profile = {
let null =
Mtrl.(is_space(LCell.sort(lc_l)), is_space(LCell.sort(lc_r)));

L.State.clear_log();
let s_init = state |> L.State.jump_cell(~over=p_l);
let s_tok = L.State.jump_cell(s_init, ~over=lc_l);
let p_l_ns = L.State.get_log();

let inner =
sil
? [
Silhouette.Inner.Profile.mk(
~is_space=Mtrl.is_space(LMeld.sort(lm)),
~state=s_init,
LMeld.flatten(~flatten=LCell.flatten, lm),
),
]
: [];
let s_tok = L.State.jump_cell(s_init, ~over=lc_l);

let l =
let ((state, l_ns), l) =
Child.Profile.mk(
~sil,
~whole,
~ind=L.Indent.curr(s_tok.ind),
~loc=s_init.loc,
~state=s_init,
~null=(true, false),
lc_l,
);
let state = L.State.jump_cell(s_init, ~over=lc_l);
let (state, w) =
let ((state, w_ns), w) =
W.Profile.mk(~sil, ~whole, ~state, ~null, ~eq=(false, false), lw);
let r =
let ((state, r_ns), r) =
Child.Profile.mk(
~sil,
~whole,
~ind=L.Indent.curr(state.ind),
~loc=state.loc,
~state,
~null=(false, true),
lc_r,
);
let state =
state |> L.State.jump_cell(~over=lc_r) |> L.State.jump_cell(~over=p_r);
let inner =
sil
? [
Silhouette.Inner.Profile.mk(
~is_space=Mtrl.is_space(LMeld.sort(lm)),
Stds.Lists.consnoc_pairs(
s_init,
List.concat([l_ns, w_ns, r_ns]),
state,
),
),
]
: [];

L.State.clear_log();
let final = state |> L.State.jump_cell(~over=p_r);
let p_r_ns = L.State.get_log();

let ns = List.concat([p_l_ns, l_ns, w_ns, r_ns, p_r_ns]);

let p = {...w, inner, cells: [l] @ w.cells @ [r]};
// let p = {chain: Chain.consnoc(~hd=l, w, ~ft=r), sil: silhouette};
(state, p);
((final, ns), p);
};
};
Loading

0 comments on commit a714342

Please sign in to comment.