Skip to content

Commit

Permalink
tab complete ghosts
Browse files Browse the repository at this point in the history
  • Loading branch information
dm0n3y committed Nov 8, 2024
1 parent e0c1861 commit d4cc817
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/core/editor/Move.re
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,53 @@ let skip = (d2: Dir2.t) =>

let jump = loc => map_focus(Fun.const(loc));

let complete_face = (site: Zipper.Site.t, ctx: Ctx.t) =>
switch (site) {
| Within(tok) =>
Token.complete(tok)
|> Option.map(tok =>
ctx |> Ctx.push(~onto=L, tok) |> Ctx.push(~onto=R, tok)
)
|> Option.value(~default=ctx)
| Between =>
open Options.Syntax;
let complete = side => {
let (face, ctx) = Ctx.pull(~from=side, ctx);
let* tok = Delim.to_opt(face);
let+ tok = Token.complete(tok);
Ctx.push(~onto=side, tok, ctx);
};
// assuming this is only called when tabbing forward
let- () = complete(L);
let- () = complete(R);
ctx;
};

let hole = (d: Dir.t, z: Zipper.t): option(Zipper.t) => {
open Options.Syntax;
let c = Zipper.zip(~save_cursor=true, z);
let normal = Zipper.normalize(~cell=c);
switch (Options.get_exn(Zipper.Bug__lost_cursor, c.marks.cursor)) {
| Select(_) => hstep(d, z)
| Point({path, _}) =>
switch (Zipper.cursor_site(z)) {
| (Select(_), _) => hstep(d, z)
| (Point(site), ctx) =>
open Options.Syntax;
let z =
switch (d) {
| L => z
| R => Zipper.mk(complete_face(site, ctx))
};
let c = Zipper.zip(~save_cursor=true, z);
let normal = Zipper.normalize(~cell=c);
let car =
c.marks.cursor
|> Options.get_exn(Zipper.Bug__lost_cursor)
|> Path.Cursor.get_point
|> Option.get;
let+ (path, _) =
c.marks.obligs
|> Path.Map.filter((_, mtrl: Mtrl.T.t) => mtrl != Space(Unmolded))
|> Dir.pick(
d,
(
Path.Map.find_last_opt(p => Path.lt(normal(p), path)),
Path.Map.find_first_opt(p => Path.gt(normal(p), path)),
Path.Map.find_last_opt(p => Path.lt(normal(p), car.path)),
Path.Map.find_first_opt(p => Path.gt(normal(p), car.path)),
),
);
c |> Cell.put_cursor(Point(Caret.focus(path))) |> Zipper.unzip_exn;
Expand Down
8 changes: 8 additions & 0 deletions src/core/grammar/Label.re
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ let is_complete = text =>
true
| Const(_, c) => String.equal(c, text);

let complete =
fun
| Id_lower
| Id_upper
| Int_lit
| Float_lit => None
| Const(_, c) => Some(c);

// beware calling this with the text of partial tokens
let oblig = text =>
fun
Expand Down
1 change: 1 addition & 0 deletions src/core/structure/Delim.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
type t = Bound.t(Token.t);
let root: t = Bound.Root;
let tok = t => Bound.Node(t);
let to_opt = Bound.to_opt;
let is_tok: t => _ = Bound.to_opt;
let unwrap = delim => Option.get(is_tok(delim));
let indent =
Expand Down
7 changes: 7 additions & 0 deletions src/core/structure/Token.re
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ module Molded = {
| Grout(_) => true
| Tile((lbl, _)) => Label.is_complete(tok.text, lbl)
};
let complete = (tok: t) =>
switch (tok.mtrl) {
| Space(_)
| Grout(_) => None
| Tile((lbl, _)) =>
Label.complete(lbl) |> Option.map(text => {...tok, text})
};

let cat = (l: t, ~caret=?, r: t) => {
let n = Utf8.length(l.text);
Expand Down

0 comments on commit d4cc817

Please sign in to comment.