Skip to content

Commit 7b8e146

Browse files
committed
normalize whitespace better
1 parent 75c70ff commit 7b8e146

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

tools/src/tools.ml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,16 @@ module FormatDocstrings = struct
735735
processLines lines;
736736

737737
(* Normalize newlines at start/end of the content. *)
738-
"\n" ^ indent ^ (buf |> Buffer.contents |> String.trim) ^ indent ^ "\n"
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+
initialWhitespace ^ (buf |> Buffer.contents |> String.trim) ^ indent ^ "\n"
739748

740749
let formatRescriptCodeBlocks content ~displayFilename ~addError
741750
~(payloadLoc : Location.t) =
@@ -763,7 +772,8 @@ module FormatDocstrings = struct
763772
addError (Buffer.contents buf);
764773
code)
765774
else
766-
Res_printer.print_implementation ~width:80 parsetree ~comments
775+
Res_printer.print_implementation
776+
~width:Res_multi_printer.default_print_width parsetree ~comments
767777
|> String.trim
768778
in
769779
formatted_code)
@@ -816,7 +826,9 @@ module FormatDocstrings = struct
816826

817827
let mapper = makeMapper ~displayFilename:filename in
818828
let astMapped = mapper.structure mapper structure in
819-
(Res_printer.print_implementation ~width:80 astMapped ~comments, source)
829+
( Res_printer.print_implementation
830+
~width:Res_multi_printer.default_print_width astMapped ~comments,
831+
source )
820832
else
821833
let parser =
822834
Res_driver.parsing_engine.parse_interface ~for_printer:true
@@ -826,7 +838,9 @@ module FormatDocstrings = struct
826838
in
827839
let mapper = makeMapper ~displayFilename:filename in
828840
let astMapped = mapper.signature mapper signature in
829-
(Res_printer.print_interface ~width:80 astMapped ~comments, source)
841+
( Res_printer.print_interface
842+
~width:Res_multi_printer.default_print_width astMapped ~comments,
843+
source )
830844
in
831845
let errors = !errors in
832846
if not (List.is_empty errors) then (

0 commit comments

Comments
 (0)