Skip to content

Commit

Permalink
full term decorations using generic child border dec (need to trim wh…
Browse files Browse the repository at this point in the history
…itespace at the ends)
  • Loading branch information
dm0n3y committed Sep 6, 2024
1 parent 361b11f commit b00eb9a
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 175 deletions.
150 changes: 150 additions & 0 deletions src/core/layout/Dims.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
open Sexplib.Std;
open Ppx_yojson_conv_lib.Yojson_conv.Primitives;
open Stds;

// module Width = {
// [@deriving (show({with_path: false}), sexp, yojson)]
// type t = {
// body: int, // count of last-line chars up to trailing whitespace
// foot: int // count of last-line chars in trailing whitespace
// };
// let mk = (~foot=0, body) => {body, foot};
// let zero = mk(0);
// let total = w => w.body + w.foot;
// let add = (l, r) =>
// l.body == 0 && r.body == 0
// ? mk(0, ~foot=l.foot + r.foot)
// : {body: total(l) + r.body, foot: r.foot};
// // last line indented only if nonempty body
// let indent = (w: t) => {...w, body: w.body + (w.body > 0 ? 2 : 0)};
// };

// [@deriving (show({with_path: false}), sexp, yojson)]
// type t = {
// height: int, // number of newlines
// width: Width.t // number of characters in last line
// };

// let mk = (~height=0, width) => {height, width};
// let zero = mk(Width.zero);

// let indent = ({height, width}: t) => {
// height,
// width: (height > 0 ? Width.indent : Fun.id)(width),
// };

// let of_space = (spc: string) => {
// let lines = String.split_on_char('\n', spc);
// let width = Width.mk(0, ~foot=Utf8.length(Lists.ft_exn(lines)));
// mk(~height=List.length(lines) - 1, width);
// };

// let of_tok = (tok: Token.t) =>
// switch (tok.mtrl) {
// | Space(_) => of_space(tok.text)
// | Grout(_) => mk(Width.mk(1))
// | Tile(_) => mk(Width.mk(Token.length(tok)))
// };

// let rec of_cell = (c: Cell.t): t =>
// switch (Cell.get(c)) {
// | None => zero
// | Some(m) => of_meld(m)
// }
// and of_meld = (m: Meld.t) =>
// m
// |> Meld.fold(of_cell, (dims, tok, cell) =>
// sum([
// dims,
// of_tok(tok),
// of_cell(cell) |> (Token.indent(tok) ? indent : Fun.id),
// ])
// );

module Width = {
// metrics describing the first/last line of a block
[@deriving (show({with_path: false}), sexp, yojson)]
type t = {
// column count of leading/trailing whitespace
pad: int,
// column count of the rest of the line
rest: int,
};
let mk = (~pad=0, rest) => {pad, rest};
let zero = {pad: 0, rest: 0};
let total = ({pad, rest}: t) => pad + rest;
let add_same = (outer: t, inner: t) =>
outer.rest == 0
? {pad: outer.pad + inner.pad, rest: inner.rest}
: {pad: outer.pad, rest: outer.rest + total(inner)};
};

[@deriving (show({with_path: false}), sexp, yojson)]
type t = {
height: int,
// if height == 0, then
// Width.total(fst(widths)) == Width.total(snd(widths))
widths: (Width.t, Width.t),
};

let zero = {height: 0, widths: Width.(zero, zero)};
let newline = {height: 1, widths: Width.(zero, zero)};
let indent = (n: int, {height, widths: (top, bot)}: t) => {
height,
widths: (
{...top, pad: n + top.pad},
bot.rest == 0
? {...bot, pad: n + bot.pad} : {...bot, rest: n + bot.rest},
),
};

// // associative, not commutative
let add = (l: t, r: t) => {
height: l.height + r.height,
widths: (
l.height == 0
? Width.add_same(fst(l.widths), fst(r.widths)) : fst(l.widths),
r.height == 0
? Width.add_same(snd(r.widths), snd(l.widths)) : snd(r.widths),
),
};
let sum = List.fold_left(add, zero);

let tok = (~height=0, width: Width.t) => {height, widths: (width, width)};

let of_space = (spc: string) => {
let lines = String.split_on_char('\n', spc);
let width = Width.mk(0, ~pad=Utf8.length(Lists.ft_exn(lines)));
tok(~height=List.length(lines) - 1, width);
};

let of_tok = (t: Token.t) => {
switch (t.mtrl) {
| Space(_) => of_space(t.text)
| Grout(_) => tok(Width.mk(1))
| Tile(_) => tok(Width.mk(Token.length(t)))
};
};

let rec of_block = (B(b): Block.t) =>
b
|> Chain.fold_left_map(
sec => ((), of_sec(sec)),
((), ind, sec) => {
let sec = of_sec(sec) |> add(newline) |> indent(ind);
((), (), sec);
},
)
|> snd
|> Chain.loops
|> sum
and of_sec =
fun
| Block.Section.Line(line) => sum(List.map(of_tok, line))
| Block(b) => of_block(b);

let skip = (loc: Loc.t, ~over: t, ~ind: Loc.Col.t) =>
Loc.{
row: loc.row + over.height,
col: (over.height > 0 ? ind : loc.col) + Width.total(snd(over.widths)),
};
14 changes: 14 additions & 0 deletions src/core/layout/Layout.re
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,17 @@ let states = (~init: State.t, m: Tree.meld) =>
(s_end, state, s_mid);
},
);

let row_ends = (~tree: Tree.t, row: Loc.Row.t): (Loc.Col.t, Loc.Col.t) => {
let (l, _) =
Loc.{row, col: 0}
|> path_of_loc(~tree)
|> Stds.Result.get_fail("unexpected")
|> state_of_path(~tree);
let (r, _) =
Loc.{row, col: Int.max_int}
|> path_of_loc(~tree)
|> Stds.Result.value(~default=Fun.const(Tree.end_path(tree, ~side=R)))
|> state_of_path(~tree);
(l.loc.col, r.loc.col);
};
10 changes: 0 additions & 10 deletions src/core/layout/Loc.re
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ module Base = {
let shift = (n, loc: t) => {...loc, col: loc.col + n};

let return = (loc: t, ~ind: Col.t) => {row: loc.row + 1, col: ind};
// let skip = (pos: t, ~over: Dims.t, ~return: Col.t) => {
// {
// // let h = Dims.Height.total(over.height);
// // let w = Dims.Width.total(over.width);
// row: pos.row + over.height,
// col:
// (over.height > 0 ? return : pos.col) + Dims.Width.total(over.width),
// };
// p;
// };
};
include Base;

Expand Down
4 changes: 4 additions & 0 deletions src/core/material/Mtrl.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
open Sexplib.Std;
open Ppx_yojson_conv_lib.Yojson_conv.Primitives;

module Base = {
[@deriving (show({with_path: false}), sexp, yojson, ord)]
type t('s, 'g, 't) =
Expand Down Expand Up @@ -34,6 +37,7 @@ let map = (~space, ~grout, ~tile) =>
| Tile(t) => Tile(tile(t));

module Sorted = {
[@deriving (show({with_path: false}), sexp, yojson)]
type t = Base.t(unit, Sort.t, Sort.t);
// currently used specifically for walk comparison based on
// stance sorted mtrl at each prec level
Expand Down
69 changes: 0 additions & 69 deletions src/core/structure/Dims.re

This file was deleted.

Loading

0 comments on commit b00eb9a

Please sign in to comment.