From f98cfe32d8efbdddbc8cd0cda3b0125e2e3684fd Mon Sep 17 00:00:00 2001 From: Paul-Elliot Date: Tue, 21 Mar 2023 15:21:18 +0100 Subject: [PATCH] fix ignored space between words in light tables Only ignore space before and after actual content Signed-off-by: Paul-Elliot --- src/syntax.ml | 18 +++++++++++------- test/test_tables.ml | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/syntax.ml b/src/syntax.ml index cadc3440..114211af 100644 --- a/src/syntax.ml +++ b/src/syntax.ml @@ -1264,7 +1264,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 @@ -1274,16 +1274,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 @@ -1292,7 +1293,10 @@ 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 @@ -1300,9 +1304,9 @@ and light_table_row ~parent_markup ~last_loc input = ~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). diff --git a/test/test_tables.ml b/test/test_tables.ml index 3a97164e..32876485 100644 --- a/test/test_tables.ml +++ b/test/test_tables.ml @@ -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 ())) |}] @@ -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 @@ -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 ())) |}] @@ -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 ())) |}]