Skip to content

Commit

Permalink
fix ignored space between words in light tables
Browse files Browse the repository at this point in the history
Only ignore space before and after actual content

Signed-off-by: Paul-Elliot <[email protected]>
  • Loading branch information
panglesd committed Mar 23, 2023
1 parent 46e8c19 commit 8937f76
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
18 changes: 11 additions & 7 deletions src/syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ and light_table ~parent_markup ~parent_markup_location input =

(* Consumes a table row that might start with [`Bar]. *)
and light_table_row ~parent_markup ~last_loc input =
let rec consume_row acc_row acc_cell ~new_line ~last_loc =
let rec consume_row acc_row acc_cell acc_space ~new_line ~last_loc =
let push_cells row cell =
match cell with [] -> row | _ -> List.rev cell :: row
in
Expand All @@ -1273,16 +1273,17 @@ and light_table_row ~parent_markup ~last_loc input =
| `Right_brace ->
junk input;
(`Stop, return acc_row acc_cell, next_token.location)
| `Space _ ->
| `Space _ as token ->
junk input;
consume_row acc_row acc_cell ~new_line ~last_loc
let i = Loc.at next_token.location token in
consume_row acc_row acc_cell (i :: acc_space) ~new_line ~last_loc
| `Single_newline _ | `Blank_line _ ->
junk input;
(`Continue, return acc_row acc_cell, last_loc)
| `Bar ->
junk input;
let acc_row = if new_line then [] else List.rev acc_cell :: acc_row in
consume_row acc_row [] ~new_line:false ~last_loc
consume_row acc_row [] [] ~new_line:false ~last_loc
| #token_that_always_begins_an_inline_element as token ->
let i = inline_element input next_token.location token in
if Loc.spans_multiple_lines i then
Expand All @@ -1291,17 +1292,20 @@ and light_table_row ~parent_markup ~last_loc input =
~in_what:(Token.describe `Begin_table_light)
i.location
|> add_warning input;
consume_row acc_row (i :: acc_cell) ~new_line:false
let acc_cell =
if acc_cell = [] then [ i ] else (i :: acc_space) @ acc_cell
in
consume_row acc_row acc_cell [] ~new_line:false
~last_loc:next_token.location
| other_token ->
Parse_error.not_allowed next_token.location
~what:(Token.describe other_token)
~in_what:(Token.describe parent_markup)
|> add_warning input;
junk input;
consume_row acc_row acc_cell ~new_line ~last_loc
consume_row acc_row acc_cell acc_space ~new_line ~last_loc
in
consume_row [] [] ~new_line:true ~last_loc
consume_row [] [] [] ~new_line:true ~last_loc

(* Consumes a sequence of table rows (starting with '{tr ...}', which are
represented by [`Begin_table_row] tokens).
Expand Down
19 changes: 16 additions & 3 deletions test/test_tables.ml
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,25 @@ let%expect_test _ =
(paragraph
(((f.ml (3 9) (3 14))
(italic (((f.ml (3 12) (3 13)) (word a)))))
((f.ml (3 14) (3 15)) space)
((f.ml (3 15) (3 28)) (google.com ()))
((f.ml (3 28) (3 29)) space)
((f.ml (3 29) (3 31)) (word "\\t")))))))
(header ())
(header
(((f.ml (3 36) (3 57))
(paragraph
(((f.ml (3 36) (3 41)) (math_span b))
((f.ml (3 41) (3 42)) space)
((f.ml (3 42) (3 47))
(emphasis (((f.ml (3 45) (3 46)) (word c)))))
((f.ml (3 47) (3 48)) space)
((f.ml (3 48) (3 57)) (raw_markup () " xyz ")))))))
(header
(((f.ml (3 60) (3 71))
(paragraph
(((f.ml (3 60) (3 65)) (bold (((f.ml (3 63) (3 64)) (word d)))))
((f.ml (3 65) (3 66)) space)
((f.ml (3 66) (3 71)) (code_span foo)))))))))))
(align (default default default default))))))
(warnings ())) |}]
Expand Down Expand Up @@ -537,6 +542,7 @@ let%expect_test _ =
(((f.ml (5 26) (5 37))
(paragraph
(((f.ml (5 26) (5 31)) (bold (((f.ml (5 29) (5 30)) (word d)))))
((f.ml (5 31) (5 32)) space)
((f.ml (5 32) (5 37)) (code_span foo)))))))))))
(align (default default))))))
(warnings
Expand Down Expand Up @@ -766,16 +772,19 @@ let%expect_test _ =
(((f.ml (3 4) (3 25))
(paragraph
(((f.ml (3 4) (3 10)) (word Header))
((f.ml (3 11) (3 14)) (word and))
((f.ml (3 10) (3 11)) space) ((f.ml (3 11) (3 14)) (word and))
((f.ml (3 14) (3 15)) space)
((f.ml (3 15) (3 20)) (word other))
((f.ml (3 20) (3 21)) space)
((f.ml (3 21) (3 25)) (word word)))))))))
(row
((data
(((f.ml (5 4) (5 24))
(paragraph
(((f.ml (5 4) (5 8)) (word cell))
((f.ml (5 9) (5 12)) (word and))
(((f.ml (5 4) (5 8)) (word cell)) ((f.ml (5 8) (5 9)) space)
((f.ml (5 9) (5 12)) (word and)) ((f.ml (5 12) (5 13)) space)
((f.ml (5 13) (5 18)) (word other))
((f.ml (5 18) (5 19)) space)
((f.ml (5 19) (5 24)) (word words)))))))))))
(align (default))))))
(warnings ())) |}]
Expand All @@ -800,14 +809,18 @@ let%expect_test _ =
(((f.ml (3 4) (3 21))
(paragraph
(((f.ml (3 4) (3 10)) (word Header))
((f.ml (3 10) (3 11)) space)
((f.ml (3 11) (3 16)) (word other))
((f.ml (3 16) (3 17)) space)
((f.ml (3 17) (3 21)) (word word)))))))))
(row
((data
(((f.ml (5 4) (5 21))
(paragraph
(((f.ml (5 4) (5 10)) (word Header))
((f.ml (5 10) (5 11)) space)
((f.ml (5 11) (5 16)) (word other))
((f.ml (5 16) (5 17)) space)
((f.ml (5 17) (5 21)) (word word)))))))))))
(align (default))))))
(warnings ())) |}]
Expand Down

0 comments on commit 8937f76

Please sign in to comment.