From da58dc6eeff11a70fa9096be041b8e2f48c092c1 Mon Sep 17 00:00:00 2001 From: David Moon Date: Tue, 12 Nov 2024 05:02:47 -0500 Subject: [PATCH] avoid leftover spaces after newlines --- src/core/parser/Labeler.re | 8 +++++++- src/core/parser/Linter.re | 20 ++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/core/parser/Labeler.re b/src/core/parser/Labeler.re index 443b3a90..39d776d0 100644 --- a/src/core/parser/Labeler.re +++ b/src/core/parser/Labeler.re @@ -55,7 +55,7 @@ let pop = buf => { // I'm guessing buf state is altered by this switch expression? // so I can't call lexeme(buf) before it? switch%sedlex (buf) { - | space => + | white_space => let text = lexeme(buf); Some(Token.Unmolded.mk(~text, Mtrl.Space(White(Usr)))); | int_lit => mk(lexeme(buf), Int_lit) @@ -93,3 +93,9 @@ let single = (s: string): option(Token.Unmolded.t) => | [tok] => Some(tok) | _ => None }; + +let starts_with_space = s => + switch (label(s)) { + | [] => false + | [tok, ..._] => Mtrl.is_space(tok.mtrl) + }; diff --git a/src/core/parser/Linter.re b/src/core/parser/Linter.re index 00e4a1c1..e68be705 100644 --- a/src/core/parser/Linter.re +++ b/src/core/parser/Linter.re @@ -5,8 +5,7 @@ let pad_wrap = (~break=false, c: Cell.t) => { // this choice only matters when c has caret let (l, r) = Meld.(mk(~l=c, w), mk(w, ~r=c)); switch (Mode.get()) { - | Inserting(" ") => r - | Inserting(_) => l + | Inserting(s) => Labeler.starts_with_space(s) ? r : l | Deleting(L) => l | Deleting(R) => r | Navigating => l @@ -44,14 +43,23 @@ let rec repad = (~l=Delim.root, ~r=Delim.root, c: Cell.t) => { | None when no_pad => c | None => pad_wrap(~break, c) | Some(m) => + let height = + Meld.to_chain(m) + |> Chain.links + |> List.map(Token.height) + |> List.fold_left((+), 0); let pruned = Meld.to_chain(m) |> Chain.fold_right( (c, tok: Token.t, acc) => { - let found_space = Result.is_ok(Chain.unlink(acc)); - switch (tok.mtrl) { - | Space(White(Sys)) when found_space || no_pad => - Chain.map_hd(Cell.Space.merge(c, ~fill=Cell.empty), acc) + let drop = () => + Chain.map_hd(Cell.Space.merge(c, ~fill=Cell.empty), acc); + switch (tok) { + | {mtrl: Space(_), text: " ", _} when height > 0 => drop() + | {mtrl: Space(White(Sys)), _} + when + no_pad || height == 0 && Result.is_ok(Chain.unlink(acc)) => + drop() | _ => Chain.link(c, tok, acc) }; },