Skip to content

Commit 7cb5574

Browse files
committed
make markdown code block formatter work on markdown files as well
1 parent 2b9e2df commit 7cb5574

File tree

7 files changed

+76
-14
lines changed

7 files changed

+76
-14
lines changed

tests/tools_tests/src/docstrings-format/FormatDocstringsTest1.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Nested = {
3030
let getName = user=>user.name
3131
}
3232
```
33-
*/
33+
*/
3434
let testFunction2 = () => "test2"
3535
}
3636

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Format ReScript code blocks
2+
3+
This markdown file should be formatted.
4+
5+
This is the first docstring with unformatted ReScript code.
6+
7+
```rescript
8+
let badly_formatted=(x,y)=>{
9+
let result=x+y
10+
if result>0{Console.log("positive")}else{Console.log("negative")}
11+
result
12+
}
13+
```
14+
15+
And another code block in the same docstring:
16+
17+
```res
18+
type user={name:string,age:int,active:bool}
19+
let createUser=(name,age)=>{name:name,age:age,active:true}
20+
```

tests/tools_tests/src/expected/FormatDocstringsTest1.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module Nested = {
3434
let getName = user => user.name
3535
}
3636
```
37-
*/
37+
*/
3838
let testFunction2 = () => "test2"
3939
}
4040

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Format ReScript code blocks
2+
3+
This markdown file should be formatted.
4+
5+
This is the first docstring with unformatted ReScript code.
6+
7+
```rescript
8+
let badly_formatted = (x, y) => {
9+
let result = x + y
10+
if result > 0 {
11+
Console.log("positive")
12+
} else {
13+
Console.log("negative")
14+
}
15+
result
16+
}
17+
```
18+
19+
And another code block in the same docstring:
20+
21+
```res
22+
type user = {name: string, age: int, active: bool}
23+
let createUser = (name, age) => {name, age, active: true}
24+
```
25+

tests/tools_tests/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ for file in ppx/*.res; do
1717
done
1818

1919
# Test format-docstrings command
20-
for file in src/docstrings-format/*.{res,resi}; do
20+
for file in src/docstrings-format/*.{res,resi,md}; do
2121
output="src/expected/$(basename $file).expected"
22-
../../_build/install/default/bin/rescript-tools format-docstrings "$file" --stdout > $output
22+
../../_build/install/default/bin/rescript-tools format-codeblocks "$file" --stdout > $output
2323
# # CI. We use LF, and the CI OCaml fork prints CRLF. Convert.
2424
if [ "$RUNNER_OS" == "Windows" ]; then
2525
perl -pi -e 's/\r\n/\n/g' -- $output

tools/bin/main.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Usage: rescript-tools [command]
2424
Commands:
2525

2626
doc <file> Generate documentation
27-
format-docstrings <file> [--stdout] Format ReScript code blocks in docstrings
27+
format-codeblocks <file> [--stdout] Format ReScript code blocks
2828
reanalyze Reanalyze
2929
-v, --version Print version
3030
-h, --help Print help|}
@@ -53,19 +53,19 @@ let main () =
5353
in
5454
logAndExit (Tools.extractDocs ~entryPointFile:path ~debug:false)
5555
| _ -> logAndExit (Error docHelp))
56-
| "format-docstrings" :: rest -> (
56+
| "format-codeblocks" :: rest -> (
5757
match rest with
5858
| ["-h"] | ["--help"] -> logAndExit (Ok formatDocstringsHelp)
5959
| [path; "--stdout"] -> (
6060
Clflags.color := Some Misc.Color.Never;
6161
match
62-
Tools.FormatDocstrings.formatDocstrings ~outputMode:`Stdout
62+
Tools.FormatCodeblocks.formatCodeBlocksInFile ~outputMode:`Stdout
6363
~entryPointFile:path
6464
with
6565
| Ok content -> print_endline content
6666
| Error e -> logAndExit (Error e))
6767
| [path] ->
68-
Tools.FormatDocstrings.formatDocstrings ~outputMode:`File
68+
Tools.FormatCodeblocks.formatCodeBlocksInFile ~outputMode:`File
6969
~entryPointFile:path
7070
|> logAndExit
7171
| _ -> logAndExit (Error formatDocstringsHelp))

tools/src/tools.ml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,9 @@ let extractEmbedded ~extensionPoints ~filename =
677677
])
678678
|> List.rev |> array
679679

680-
module FormatDocstrings = struct
680+
module FormatCodeblocks = struct
681681
let formatRescriptCodeBlocks content ~displayFilename ~addError
682-
~(payloadLoc : Location.t) =
682+
~markdownBlockStartLine =
683683
let open Cmarkit in
684684
(* Detect ReScript code blocks. *)
685685
let hadCodeBlocks = ref false in
@@ -700,7 +700,7 @@ module FormatDocstrings = struct
700700

701701
let n = List.length code in
702702
let newlinesNeeded =
703-
max 0 (payloadLoc.loc_start.pos_lnum + currentLine - n)
703+
max 0 (markdownBlockStartLine + currentLine - n)
704704
in
705705
let codeWithOffset = String.make newlinesNeeded '\n' ^ codeText in
706706

@@ -737,7 +737,7 @@ module FormatDocstrings = struct
737737
in
738738
(newContent, !hadCodeBlocks)
739739

740-
let formatDocstrings ~outputMode ~entryPointFile =
740+
let formatCodeBlocksInFile ~outputMode ~entryPointFile =
741741
let path =
742742
match Filename.is_relative entryPointFile with
743743
| true -> Unix.realpath entryPointFile
@@ -757,7 +757,7 @@ module FormatDocstrings = struct
757757
PStr [{pstr_desc = Pstr_eval ({pexp_loc}, _)}] ) ->
758758
let formattedContents, hadCodeBlocks =
759759
formatRescriptCodeBlocks ~addError ~displayFilename
760-
~payloadLoc:pexp_loc contents
760+
~markdownBlockStartLine:pexp_loc.loc_start.pos_lnum contents
761761
in
762762
if hadCodeBlocks && formattedContents <> contents then
763763
( name,
@@ -772,7 +772,24 @@ module FormatDocstrings = struct
772772
}
773773
in
774774
let formatted_content, source =
775-
if Filename.check_suffix path ".res" then
775+
if Filename.check_suffix path ".md" then
776+
let content =
777+
let ic = open_in path in
778+
let n = in_channel_length ic in
779+
let s = Bytes.create n in
780+
really_input ic s 0 n;
781+
close_in ic;
782+
Bytes.to_string s
783+
in
784+
let displayFilename = Filename.basename path in
785+
let formattedContents, hadCodeBlocks =
786+
formatRescriptCodeBlocks ~addError ~displayFilename
787+
~markdownBlockStartLine:1 content
788+
in
789+
if hadCodeBlocks && formattedContents <> content then
790+
(formattedContents, content)
791+
else (content, content)
792+
else if Filename.check_suffix path ".res" then
776793
let parser =
777794
Res_driver.parsing_engine.parse_implementation ~for_printer:true
778795
in

0 commit comments

Comments
 (0)