Skip to content

Commit

Permalink
fix #105
Browse files Browse the repository at this point in the history
  • Loading branch information
dm0n3y committed Jan 31, 2025
1 parent 89b5148 commit 79f81ad
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
8 changes: 5 additions & 3 deletions src/core/editor/Modify.re
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ let relabel = (s: string, z: Zipper.t): list((to_be_inserted, Ctx.t)) => {
let merged_l =
switch (l) {
| Root
| Node({mtrl: Space(White(_)) | Grout(_), _}) => []
| Node({mtrl: Space(White(_)) | Grout(_), _})
| Node({mtrl: Tile(_), text: "", _}) => []
| Node({mtrl: Space(Unmolded) | Tile(_), text: l, _}) =>
switch (Labeler.single(l ++ s)) {
| None => []
Expand All @@ -75,7 +76,8 @@ let relabel = (s: string, z: Zipper.t): list((to_be_inserted, Ctx.t)) => {
let merged_r =
switch (r) {
| Root
| Node({mtrl: Space(White(_)) | Grout(_), _}) => []
| Node({mtrl: Space(White(_)) | Grout(_), _})
| Node({mtrl: Tile(_), text: "", _}) => []
| Node({mtrl: Space(Unmolded) | Tile(_), text: r, _}) =>
switch (Labeler.single(s ++ r)) {
| None => []
Expand Down Expand Up @@ -322,7 +324,7 @@ let delete_toks =
},
)
// finally, unmold the tokens (only relabeling the last token)
|> Chain.mapi_link(i => Token.unmold(~relabel=i - 1 / 2 == n - 1));
|> Chain.mapi_link(i => Labeler.unmold(~relabel=i - 1 / 2 == n - 1));
};

// mold each token against the ctx, using each preceding cell as its fill, and
Expand Down
21 changes: 21 additions & 0 deletions src/core/parser/Labeler.re
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,24 @@ let starts_with_space = s =>
| [] => false
| [tok, ..._] => Mtrl.is_space(tok.mtrl)
};

let unmold = (~relabel=true, tok: Token.t): Token.Unmolded.t => {
let mtrl =
switch (tok.mtrl) {
| Space(White(a)) => Mtrl.Space(Space.T.White(a))
| Space(Unmolded) =>
switch (single(~id=tok.id, tok.text)) {
| Some(tok) when relabel => tok.mtrl
| _ => Space(Unmolded)
}
| Grout(_) => Grout()
| Tile((lbl, _)) =>
Tile(
Token.is_empty(tok) || !relabel
? [lbl] : [lbl, ...Labels.completions(tok.text)],
)
};
// todo: may need to fix token marks if marks happen to have caret at right end
// of incomplete tile
Token.Unmolded.mk(~id=tok.id, ~marks=?tok.marks, ~text=tok.text, mtrl);
};
21 changes: 0 additions & 21 deletions src/core/structure/Token.re
Original file line number Diff line number Diff line change
Expand Up @@ -309,27 +309,6 @@ module Unmolded = {
};
};

let unmold = (~relabel=true, tok: Molded.t): Unmolded.t => {
let mtrl =
switch (tok.mtrl) {
| Space(White(a)) => Mtrl.Space(Space.T.White(a))
| Space(Unmolded) =>
switch (Labels.completions(tok.text)) {
| [] => Space(Unmolded)
| [_, ..._] as lbls => Tile(lbls)
}
| Grout(_) => Grout()
| Tile((lbl, _)) =>
Tile(
is_empty(tok) || !relabel
? [lbl] : [lbl, ...Labels.completions(tok.text)],
)
};
// todo: may need to fix token marks if marks happen to have caret at right end
// of incomplete tile
Unmolded.mk(~id=tok.id, ~marks=?tok.marks, ~text=tok.text, mtrl);
};

module Space = {
let is = (tok: Molded.t) => Mtrl.is_space(tok.mtrl);
let mk = (~id=?, ~text="", ~marks=?, t: Space.T.t) =>
Expand Down
17 changes: 16 additions & 1 deletion test/test_suite.re
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ let tab_tests = (
],
);

let modify_tests = (
"Modify",
[
test_case(
"consecutive unmolded tokens get molded once the proper left hand context is inserted",
`Quick,
check_edit(
(Path.Cursor.point(Caret.focus([2, 0, 2])), "let a,b)"),
[Edit.Insert("(")],
(Path.Cursor.point(Caret.focus([2, 2, 0])), "let (a,b)"),
),
),
],
);

let () = {
run("tylr", [move_tests, tab_tests]);
run("tylr", [move_tests, tab_tests, modify_tests]);
};

0 comments on commit 79f81ad

Please sign in to comment.