Skip to content

Commit af1fb33

Browse files
committed
more tweaks to not make more changes to docstrings than needed
1 parent 02eb87b commit af1fb33

File tree

1 file changed

+79
-55
lines changed

1 file changed

+79
-55
lines changed

tools/src/tools.ml

Lines changed: 79 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -686,65 +686,87 @@ 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 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 (
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*)
704736
let codeBlockContents =
705737
codeBlockContents |> List.rev |> String.concat "\n"
706738
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
713739
addIndent ();
714740
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"
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)
729753
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)
754+
findFirstNonWhitespace 0
755+
in
756+
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)
743765
in
744-
findFirstNonWhitespace 0
745-
in
746766

747-
initialWhitespace ^ (buf |> Buffer.contents |> String.trim) ^ indent ^ "\n"
767+
initialWhitespace
768+
^ (buf |> Buffer.contents |> String.trim)
769+
^ endingWhitespace
748770

749771
let formatRescriptCodeBlocks content ~displayFilename ~addError
750772
~(payloadLoc : Location.t) =
@@ -844,15 +866,17 @@ module FormatDocstrings = struct
844866
in
845867
let errors = !errors in
846868
if List.length errors > 0 then (
847-
errors |> String.concat "\n" |> print_endline;
848-
Error (Printf.sprintf "Error formatting docstrings."))
869+
errors |> List.rev |> String.concat "\n" |> print_endline;
870+
Error
871+
(Printf.sprintf "%s: Error formatting docstrings."
872+
(Filename.basename path)))
849873
else if formatted_content <> source then (
850874
match outputMode with
851875
| `Stdout -> Ok formatted_content
852876
| `File ->
853877
let oc = open_out path in
854878
Printf.fprintf oc "%s" formatted_content;
855879
close_out oc;
856-
Ok "Formatted docstrings successfully")
857-
else Ok "No formatting needed"
880+
Ok (Filename.basename path ^ ": formatted successfully"))
881+
else Ok (Filename.basename path ^ ": needed no formatting")
858882
end

0 commit comments

Comments
 (0)