Skip to content

Commit 977c559

Browse files
committed
support resi code blocks as well
1 parent be8657f commit 977c559

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,17 @@ Console.log(processed)
4040
```
4141
*/
4242
let testPipes = () => "pipes test"
43+
44+
/**
45+
Testing resi code blocks.
46+
47+
```resi
48+
type x=int
49+
let x:int =>
50+
string
51+
module M:{let ff: string => int
52+
}
53+
54+
```
55+
*/
56+
let testResi = () => true

tests/tools_tests/src/expected/FormatDocstringsTest2.res.expected

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,16 @@ let asyncExample = async () => {
4343
*/
4444
let testPipes = () => "pipes test"
4545

46+
/**
47+
Testing resi code blocks.
48+
49+
```resi
50+
type x = int
51+
let x: int => string
52+
module M: {
53+
let ff: string => int
54+
}
55+
```
56+
*/
57+
let testResi = () => true
58+

tools/src/tools.ml

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -762,29 +762,43 @@ module FormatCodeblocks = struct
762762
max 0 (markdownBlockStartLine + currentLine - n)
763763
in
764764
let codeWithOffset = String.make newlinesNeeded '\n' ^ codeText in
765-
765+
let reportParseError diagnostics =
766+
let buf = Buffer.create 1000 in
767+
let formatter = Format.formatter_of_buffer buf in
768+
Res_diagnostics.print_report ~formatter
769+
~custom_intro:(Some "Syntax error in code block in docstring")
770+
diagnostics codeWithOffset;
771+
addError (Buffer.contents buf)
772+
in
766773
let formattedCode =
767-
let {Res_driver.parsetree; comments; invalid; diagnostics} =
768-
Res_driver.parse_implementation_from_source ~for_printer:true
769-
~display_filename:displayFilename ~source:codeWithOffset
770-
in
771-
if invalid then (
772-
let buf = Buffer.create 1000 in
773-
let formatter = Format.formatter_of_buffer buf in
774-
Res_diagnostics.print_report ~formatter
775-
~custom_intro:(Some "Syntax error in code block in docstring")
776-
diagnostics codeWithOffset;
777-
addError (Buffer.contents buf);
778-
code)
774+
if lang |> String.split_on_char ' ' |> List.hd = "resi" then
775+
let {Res_driver.parsetree; comments; invalid; diagnostics} =
776+
Res_driver.parse_interface_from_source ~for_printer:true
777+
~display_filename:displayFilename ~source:codeWithOffset
778+
in
779+
if invalid then (
780+
reportParseError diagnostics;
781+
code)
782+
else
783+
Res_printer.print_interface parsetree ~comments
784+
|> String.trim |> Cmarkit.Block_line.list_of_string
779785
else
780-
let parsetree =
781-
if transformAssertEqual then
782-
Transform.transform ~transforms:[AssertEqualFnToEquals]
783-
parsetree
784-
else parsetree
786+
let {Res_driver.parsetree; comments; invalid; diagnostics} =
787+
Res_driver.parse_implementation_from_source ~for_printer:true
788+
~display_filename:displayFilename ~source:codeWithOffset
785789
in
786-
Res_printer.print_implementation parsetree ~comments
787-
|> String.trim |> Cmarkit.Block_line.list_of_string
790+
if invalid then (
791+
reportParseError diagnostics;
792+
code)
793+
else
794+
let parsetree =
795+
if transformAssertEqual then
796+
Transform.transform ~transforms:[AssertEqualFnToEquals]
797+
parsetree
798+
else parsetree
799+
in
800+
Res_printer.print_implementation parsetree ~comments
801+
|> String.trim |> Cmarkit.Block_line.list_of_string
788802
in
789803

790804
let mappedCodeBlock =

0 commit comments

Comments
 (0)