@@ -678,101 +678,33 @@ let extractEmbedded ~extensionPoints ~filename =
678678 |> List. rev |> array
679679
680680module FormatDocstrings = struct
681- let mapRescriptCodeBlocks ~colIndent ~(mapper : string -> int -> string )
682- (doc : string ) =
683- let indent = String. make colIndent ' ' in
684- let len = String. length doc in
685- let buf = Buffer. create len in
686- let addIndent () = Buffer. add_string buf indent in
687- let currentCodeBlockContents = ref None in
688- let lines = String. split_on_char '\n' doc in
689- let lineCount = ref (- 1 ) in
690- let rec processLines lines =
691- let currentLine = ! lineCount in
692- lineCount := currentLine + 1 ;
693- match (lines, ! currentCodeBlockContents) with
694- | l :: rest , None ->
695- if String. trim l = " ```rescript" then (
696- currentCodeBlockContents := Some [] ;
697- processLines rest)
698- else (
699- Buffer. add_string buf l;
700- Buffer. add_char buf '\n' ;
701- processLines rest)
702- | l :: rest , Some codeBlockContents ->
703- if String. trim l = " ```" then (
704- let codeBlockContents =
705- codeBlockContents |> List. rev |> String. concat " \n "
706- in
707- let mappedCodeBlockContents =
708- mapper codeBlockContents currentLine
709- |> String. split_on_char '\n'
710- |> List. map (fun line -> indent ^ line)
711- |> String. concat " \n "
712- in
713- addIndent () ;
714- Buffer. add_string buf " ```rescript\n " ;
715- Buffer. add_string buf mappedCodeBlockContents;
716- Buffer. add_char buf '\n' ;
717- addIndent () ;
718- Buffer. add_string buf " ```" ;
719- Buffer. add_char buf '\n' ;
720- currentCodeBlockContents := None ;
721- processLines rest)
722- else (
723- currentCodeBlockContents := Some (l :: codeBlockContents);
724- processLines rest)
725- | [] , Some codeBlockContents ->
726- (* EOF, broken, do not format*)
727- let codeBlockContents =
728- codeBlockContents |> List. rev |> String. concat " \n "
729- in
730- addIndent () ;
731- Buffer. add_string buf " ```rescript\n " ;
732- Buffer. add_string buf codeBlockContents
733- | [] , None -> ()
734- in
735- processLines lines;
736-
737- (* Normalize newlines at start/end of the content. *)
738- let initialWhitespace =
739- let rec findFirstNonWhitespace i =
740- if i > = String. length doc then " "
741- else if not (String. contains " \t\n\r " doc.[i]) then String. sub doc 0 i
742- else findFirstNonWhitespace (i + 1 )
743- in
744- findFirstNonWhitespace 0
745- in
746-
747- let endingWhitespace =
748- let rec findLastWhitespace i =
749- if i < 0 then " "
750- else if not (String. contains " \t\n\r " doc.[i]) then
751- String. sub doc (i + 1 ) (String. length doc - i - 1 )
752- else findLastWhitespace (i - 1 )
753- in
754- findLastWhitespace (String. length doc - 1 )
755- in
756-
757- initialWhitespace
758- ^ (buf |> Buffer. contents |> String. trim)
759- ^ endingWhitespace
760-
761681 let formatRescriptCodeBlocks content ~displayFilename ~addError
762682 ~(payloadLoc : Location.t ) =
683+ let open Cmarkit in
684+ (* Detect ReScript code blocks. *)
763685 let hadCodeBlocks = ref false in
764- let newContent =
765- mapRescriptCodeBlocks
766- ~col Indent:(payloadLoc.loc_start.pos_cnum - payloadLoc.loc_start.pos_bol)
767- ~mapper: ( fun code currentLine ->
686+ let block _m = function
687+ | Block. Code_block ( codeBlock , meta ) -> (
688+ match Block.Code_block. info_string codeBlock with
689+ | Some ((( "rescript" | "res" ), _ ) as info_string ) ->
768690 hadCodeBlocks := true ;
769- let codeLines = String. split_on_char '\n' code in
770- let n = List. length codeLines in
691+
692+ let currentLine =
693+ meta |> Meta. textloc |> Textloc. first_line |> fst |> Int. add 1
694+ in
695+ let layout = Block.Code_block. layout codeBlock in
696+ let code = Block.Code_block. code codeBlock in
697+ let codeText =
698+ code |> List. map Block_line. to_string |> String. concat " \n "
699+ in
700+
701+ let n = List. length code in
771702 let newlinesNeeded =
772703 max 0 (payloadLoc.loc_start.pos_lnum + currentLine - n)
773704 in
774- let codeWithOffset = String. make newlinesNeeded '\n' ^ code in
775- let formatted_code =
705+ let codeWithOffset = String. make newlinesNeeded '\n' ^ codeText in
706+
707+ let formattedCode =
776708 let {Res_driver. parsetree; comments; invalid; diagnostics} =
777709 Res_driver. parse_implementation_from_source ~for_printer: true
778710 ~display_filename: displayFilename ~source: codeWithOffset
@@ -788,10 +720,20 @@ module FormatDocstrings = struct
788720 else
789721 Res_printer. print_implementation
790722 ~width: Res_multi_printer. default_print_width parsetree ~comments
791- |> String. trim
723+ |> String. trim |> Block_line. list_of_string
724+ in
725+
726+ let mappedCodeBlock =
727+ Block.Code_block. make ~layout ~info_string formattedCode
792728 in
793- formatted_code)
794- content
729+ Mapper. ret (Block. Code_block (mappedCodeBlock, meta))
730+ | _ -> Mapper. default)
731+ | _ -> Mapper. default
732+ in
733+ let mapper = Mapper. make ~block () in
734+ let newContent =
735+ content |> Doc. of_string ~locs: true |> Mapper. map_doc mapper
736+ |> Cmarkit_commonmark. of_doc
795737 in
796738 (newContent, ! hadCodeBlocks)
797739
0 commit comments