From 5e60fa89830d9cc729db91e61df87c9a2d818a70 Mon Sep 17 00:00:00 2001 From: David Moon Date: Mon, 4 Nov 2024 19:00:46 -0500 Subject: [PATCH] fix #42 (hack? not sure) --- src/core/editor/Ctx.re | 11 +++++++++++ src/core/editor/Modify.re | 2 ++ src/stds/Strings.re | 2 ++ 3 files changed, 15 insertions(+) diff --git a/src/core/editor/Ctx.re b/src/core/editor/Ctx.re index 5506845f..04fead1a 100644 --- a/src/core/editor/Ctx.re +++ b/src/core/editor/Ctx.re @@ -167,6 +167,17 @@ let push_zigg = (~onto as d: Dir.t, zigg: Zigg.t, ~fill=Cell.empty, ctx: t) => { map_hd(Frame.Open.cat(rest), ctx); }; +let trim_space = (~side: Dir.t, ctx: t) => + switch (pull(~from=side, ctx)) { + | (Node(tok), ctx) when Mtrl.is_space(tok.mtrl) => + let tok = + Strings.chop_prefix(~prefix=" ", tok.text) + |> Option.map(text => {...tok, text}) + |> Option.value(~default=tok); + Token.is_empty(tok) ? ctx : push(~onto=side, tok, ctx); + | _ => ctx + }; + let zip_toks = (~save_cursor=false, ctx: t): option((Meld.t, t)) => { let (hd, tl) = uncons(ctx); Frame.Open.zip_toks(~save_cursor, hd) diff --git a/src/core/editor/Modify.re b/src/core/editor/Modify.re index 094739d0..47d418a7 100644 --- a/src/core/editor/Modify.re +++ b/src/core/editor/Modify.re @@ -295,6 +295,7 @@ let expand = (tok: Token.t) => let try_expand = (s: string, z: Zipper.t): option(Zipper.t) => { open Options.Syntax; let* () = Options.of_bool(String.starts_with(~prefix=" ", s)); + // todo: check if in middle of token let (face, rest) = Ctx.pull(~from=L, z.ctx); let* tok = Delim.is_tok(face); // if expandable, consider all expandable const labels @@ -314,6 +315,7 @@ let try_expand = (s: string, z: Zipper.t): option(Zipper.t) => { ) ) |> Ctx.push(~onto=L, Token.space()) + |> Ctx.trim_space(~side=R) |> finalize(~mode=Inserting(" "), ~fill=Cell.point(~dirty=true, Focus)) |> return; }; diff --git a/src/stds/Strings.re b/src/stds/Strings.re index 3c4edda3..9dd7c58a 100644 --- a/src/stds/Strings.re +++ b/src/stds/Strings.re @@ -14,3 +14,5 @@ let count = c => Base.String.count(~f=(==)(c)); let split = Base.String.split; let rev = Base.String.rev; + +let chop_prefix = Base.String.chop_prefix;