Skip to content

Commit a63d3e5

Browse files
committed
do not edit anything if we didnt have any code blocks
1 parent af1fb33 commit a63d3e5

File tree

1 file changed

+67
-75
lines changed

1 file changed

+67
-75
lines changed

tools/src/tools.ml

Lines changed: 67 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -686,94 +686,86 @@ module FormatDocstrings = struct
686686
let addIndent () = Buffer.add_string buf indent in
687687
let currentCodeBlockContents = ref None in
688688
let lines = String.split_on_char '\n' doc in
689-
let isSingleLine =
690-
match lines with
691-
| [_] -> true
692-
| _ -> false
693-
in
694-
if isSingleLine then
695-
(* No code blocks in single line comments... *)
696-
doc
697-
else
698-
let lineCount = ref (-1) in
699-
let rec processLines lines =
700-
let currentLine = !lineCount in
701-
lineCount := currentLine + 1;
702-
match (lines, !currentCodeBlockContents) with
703-
| l :: rest, None ->
704-
if String.trim l = "```rescript" then (
705-
currentCodeBlockContents := Some [];
706-
processLines rest)
707-
else (
708-
Buffer.add_string buf l;
709-
Buffer.add_char buf '\n';
710-
processLines rest)
711-
| l :: rest, Some codeBlockContents ->
712-
if String.trim l = "```" then (
713-
let codeBlockContents =
714-
codeBlockContents |> List.rev |> String.concat "\n"
715-
in
716-
let mappedCodeBlockContents =
717-
mapper codeBlockContents currentLine
718-
|> String.split_on_char '\n'
719-
|> List.map (fun line -> indent ^ line)
720-
|> String.concat "\n"
721-
in
722-
addIndent ();
723-
Buffer.add_string buf "```rescript\n";
724-
Buffer.add_string buf mappedCodeBlockContents;
725-
Buffer.add_char buf '\n';
726-
addIndent ();
727-
Buffer.add_string buf "```";
728-
Buffer.add_char buf '\n';
729-
currentCodeBlockContents := None;
730-
processLines rest)
731-
else (
732-
currentCodeBlockContents := Some (l :: codeBlockContents);
733-
processLines rest)
734-
| [], Some codeBlockContents ->
735-
(* EOF, broken, do not format*)
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 (
736704
let codeBlockContents =
737705
codeBlockContents |> List.rev |> String.concat "\n"
738706
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
739713
addIndent ();
740714
Buffer.add_string buf "```rescript\n";
741-
Buffer.add_string buf codeBlockContents
742-
| [], None -> ()
743-
in
744-
processLines lines;
745-
746-
(* Normalize newlines at start/end of the content. *)
747-
let initialWhitespace =
748-
let rec findFirstNonWhitespace i =
749-
if i >= String.length doc then ""
750-
else if not (String.contains " \t\n\r" doc.[i]) then
751-
String.sub doc 0 i
752-
else findFirstNonWhitespace (i + 1)
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"
753729
in
754-
findFirstNonWhitespace 0
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)
755743
in
744+
findFirstNonWhitespace 0
745+
in
756746

757-
let endingWhitespace =
758-
let rec findLastWhitespace i =
759-
if i < 0 then ""
760-
else if not (String.contains " \t\n\r" doc.[i]) then
761-
String.sub doc (i + 1) (String.length doc - i - 1)
762-
else findLastWhitespace (i - 1)
763-
in
764-
findLastWhitespace (String.length doc - 1)
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)
765753
in
754+
findLastWhitespace (String.length doc - 1)
755+
in
766756

767-
initialWhitespace
768-
^ (buf |> Buffer.contents |> String.trim)
769-
^ endingWhitespace
757+
initialWhitespace
758+
^ (buf |> Buffer.contents |> String.trim)
759+
^ endingWhitespace
770760

771761
let formatRescriptCodeBlocks content ~displayFilename ~addError
772762
~(payloadLoc : Location.t) =
763+
let hadCodeBlocks = ref false in
773764
let newContent =
774765
mapRescriptCodeBlocks
775766
~colIndent:(payloadLoc.loc_start.pos_cnum - payloadLoc.loc_start.pos_bol)
776767
~mapper:(fun code currentLine ->
768+
hadCodeBlocks := true;
777769
let codeLines = String.split_on_char '\n' code in
778770
let n = List.length codeLines in
779771
let newlinesNeeded =
@@ -801,7 +793,7 @@ module FormatDocstrings = struct
801793
formatted_code)
802794
content
803795
in
804-
newContent
796+
(newContent, !hadCodeBlocks)
805797

806798
let formatDocstrings ~outputMode ~entryPointFile =
807799
let path =
@@ -821,17 +813,17 @@ module FormatDocstrings = struct
821813
| ( {txt = "res.doc"},
822814
Some (contents, None),
823815
PStr [{pstr_desc = Pstr_eval ({pexp_loc}, _)}] ) ->
824-
let formatted_contents =
816+
let formattedContents, hadCodeBlocks =
825817
formatRescriptCodeBlocks ~addError ~displayFilename
826818
~payloadLoc:pexp_loc contents
827819
in
828-
if formatted_contents <> contents then
820+
if hadCodeBlocks && formattedContents <> contents then
829821
( name,
830822
PStr
831823
[
832824
Ast_helper.Str.eval
833825
(Ast_helper.Exp.constant
834-
(Pconst_string (formatted_contents, None)));
826+
(Pconst_string (formattedContents, None)));
835827
] )
836828
else attr
837829
| _ -> Ast_mapper.default_mapper.attribute mapper attr);

0 commit comments

Comments
 (0)