Skip to content

Commit

Permalink
Fix empty lines handling on code blocks (#135)
Browse files Browse the repository at this point in the history
Fixes #132.
Fixes #133.
  • Loading branch information
bobbypriam authored and aantron committed Apr 25, 2018
1 parent c1cdfb6 commit 1140ce8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/parser/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ let trim_leading_whitespace : string -> string = fun s ->
let count_leading_whitespace : string -> int = fun line ->
let rec count_leading_whitespace' : int -> int = fun index ->
if index >= String.length line then
index
(* Ignore empty and whitespace-only lines. *)
max_int
else
match line.[index] with
| ' ' | '\t' -> count_leading_whitespace' (index + 1)
Expand All @@ -92,10 +93,13 @@ let trim_leading_whitespace : string -> string = fun s ->
|> List.fold_left min max_int
in
let remove_whitespace : string -> string = fun line ->
String.sub
if String.length line < least_amount_of_whitespace then
line
least_amount_of_whitespace
(String.length line - least_amount_of_whitespace)
else
String.sub
line
least_amount_of_whitespace
(String.length line - least_amount_of_whitespace)
in
lines
|> List.map remove_whitespace
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
((output (ok (((f.ml (1 0) (3 6)) (code_block "foo\
\n\
\nbar")))))
(warnings ()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
((output (ok (((f.ml (1 0) (3 6)) (code_block "foo\
\n \
\nbar")))))
(warnings ()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
((output (ok (((f.ml (1 0) (3 7)) (code_block "foo\
\n \
\nbar")))))
(warnings ()))
3 changes: 3 additions & 0 deletions test/parser/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ let tests : test_suite list = [
t "leading-whitespace-two-cr-lf" "{[ foo\r\n bar]}";
t "leading-whitespace-two-different-indent" "{[ foo\n bar]}";
t "leading-whitespace-two-different-indent-rev" "{[ foo\n bar]}";
t "leading-whitespace-with-empty-line" "{[ foo\n\n bar]}";
t "leading-whitespace-with-whitespace-line-short" "{[ foo\n \n bar]}";
t "leading-whitespace-with-whitespace-line-long" "{[ foo\n \n bar]}";
t "leading-tab" "{[\tfoo]}";
t "leading-tab-two" "{[\tfoo\n\tbar]}";
t "leading-tab-two-different-indent" "{[\tfoo\n\t\tbar]}";
Expand Down

0 comments on commit 1140ce8

Please sign in to comment.