Skip to content

Commit

Permalink
avoid leftover spaces after newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
dm0n3y committed Nov 12, 2024
1 parent ae62cde commit da58dc6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/core/parser/Labeler.re
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
};
20 changes: 14 additions & 6 deletions src/core/parser/Linter.re
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
};
},
Expand Down

0 comments on commit da58dc6

Please sign in to comment.