Skip to content

Handle leading whitespace on code blocks #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
(library
((name parser_)
(preprocess (pps (bisect_ppx)))
(libraries (model))))
(libraries (model str))))
27 changes: 27 additions & 0 deletions src/parser/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@ let trim_trailing_blank_lines : string -> string = fun s ->
in
String.sub s 0 trim_from

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
else
match line.[index] with
| ' ' | '\t' -> count_leading_whitespace' (index + 1)
| _ -> index
in
count_leading_whitespace' 0
in
let lines = Str.(split (regexp "\n") s) in
let least_amount_of_whitespace =
lines
|> List.map count_leading_whitespace
|> List.fold_left min max_int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a comment, that using max_int is okay because if the list is empty, we will not use the result of the fold (in the map below).

in
let remove_whitespace : string -> string = fun line ->
String.sub line least_amount_of_whitespace (String.length line - least_amount_of_whitespace)
in
lines
|> List.map remove_whitespace
|> String.concat "\n"




module Location = Model.Location_
Expand Down Expand Up @@ -256,6 +282,7 @@ rule token input = parse
| "{[" (code_block_text as c) "]}"
{ let c = trim_leading_blank_lines c in
let c = trim_trailing_blank_lines c in
let c = trim_leading_whitespace c in
emit input (`Code_block c) }

| "{v" (verbatim_text as t) "v}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
((output (ok (((f.ml (1 0) (2 6)) (code_block " foo"))))) (warnings ()))
((output (ok (((f.ml (1 0) (2 6)) (code_block foo))))) (warnings ()))
2 changes: 1 addition & 1 deletion test/parser/expect/code-block/leading-tab.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
((output (ok (((f.ml (1 0) (1 8)) (code_block "\tfoo"))))) (warnings ()))
((output (ok (((f.ml (1 0) (1 8)) (code_block foo))))) (warnings ()))
2 changes: 1 addition & 1 deletion test/parser/expect/code-block/leading-whitespace.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
((output (ok (((f.ml (1 0) (1 8)) (code_block " foo"))))) (warnings ()))
((output (ok (((f.ml (1 0) (1 8)) (code_block foo))))) (warnings ()))