Skip to content

Commit 8be709d

Browse files
committed
change printer to make width optional
1 parent 254ae5c commit 8be709d

File tree

8 files changed

+19
-31
lines changed

8 files changed

+19
-31
lines changed

analysis/src/Commands.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,13 @@ let format ~path =
282282
~filename:path
283283
in
284284
if List.length diagnostics > 0 then ""
285-
else
286-
Res_printer.print_implementation
287-
~width:Res_multi_printer.default_print_width ~comments structure
285+
else Res_printer.print_implementation ~comments structure
288286
else if Filename.check_suffix path ".resi" then
289287
let {Res_driver.parsetree = signature; comments; diagnostics} =
290288
Res_driver.parsing_engine.parse_interface ~for_printer:true ~filename:path
291289
in
292290
if List.length diagnostics > 0 then ""
293-
else
294-
Res_printer.print_interface ~width:Res_multi_printer.default_print_width
295-
~comments signature
291+
else Res_printer.print_interface ~comments signature
296292
else ""
297293

298294
let diagnosticSyntax ~path =

analysis/src/CreateInterface.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ let printSignature ~extractor ~signature =
102102
Printtyp.reset_names ();
103103
let sigItemToString (item : Outcometree.out_sig_item) =
104104
item |> Res_outcome_printer.print_out_sig_item_doc
105-
|> Res_doc.to_string ~width:Res_multi_printer.default_print_width
105+
|> Res_doc.to_string ~width:Res_printer.default_print_width
106106
in
107107

108108
let genSigStrForInlineAttr lines attributes id vd =

analysis/src/Xform.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,6 @@ let parseImplementation ~filename =
855855
let structure = [Ast_helper.Str.eval ~loc:expr.pexp_loc expr] in
856856
structure
857857
|> Res_printer.print_implementation
858-
~width:Res_multi_printer.default_print_width
859858
~comments:(comments |> filterComments ~loc:expr.pexp_loc)
860859
|> Utils.indent range.start.character
861860
in
@@ -864,14 +863,12 @@ let parseImplementation ~filename =
864863
let structure = [item] in
865864
structure
866865
|> Res_printer.print_implementation
867-
~width:Res_multi_printer.default_print_width
868866
~comments:(comments |> filterComments ~loc:item.pstr_loc)
869867
|> Utils.indent range.start.character
870868
in
871869
let printStandaloneStructure ~(loc : Location.t) structure =
872870
structure
873871
|> Res_printer.print_implementation
874-
~width:Res_multi_printer.default_print_width
875872
~comments:(comments |> filterComments ~loc)
876873
in
877874
(structure, printExpr, printStructureItem, printStandaloneStructure)
@@ -891,7 +888,7 @@ let parseInterface ~filename =
891888
(item : Parsetree.signature_item) =
892889
let signature_item = [item] in
893890
signature_item
894-
|> Res_printer.print_interface ~width:Res_multi_printer.default_print_width
891+
|> Res_printer.print_interface
895892
~comments:(comments |> filterComments ~loc:item.psig_loc)
896893
|> Utils.indent range.start.character
897894
in

compiler/syntax/src/res_multi_printer.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
let default_print_width = 100
2-
31
(* print res files to res syntax *)
42
let print_res ~ignore_parse_errors ~is_interface ~filename =
53
if is_interface then (
@@ -9,7 +7,7 @@ let print_res ~ignore_parse_errors ~is_interface ~filename =
97
if parse_result.invalid then (
108
Res_diagnostics.print_report parse_result.diagnostics parse_result.source;
119
if not ignore_parse_errors then exit 1);
12-
Res_printer.print_interface ~width:default_print_width
10+
Res_printer.print_interface ~width:Res_printer.default_print_width
1311
~comments:parse_result.comments parse_result.parsetree)
1412
else
1513
let parse_result =
@@ -18,7 +16,7 @@ let print_res ~ignore_parse_errors ~is_interface ~filename =
1816
if parse_result.invalid then (
1917
Res_diagnostics.print_report parse_result.diagnostics parse_result.source;
2018
if not ignore_parse_errors then exit 1);
21-
Res_printer.print_implementation ~width:default_print_width
19+
Res_printer.print_implementation ~width:Res_printer.default_print_width
2220
~comments:parse_result.comments parse_result.parsetree
2321
[@@raises exit]
2422

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
val default_print_width : int [@@live]
2-
31
(* Interface to print source code to res.
42
* Takes a filename called "input" and returns the corresponding formatted res syntax *)
53
val print : ?ignore_parse_errors:bool -> string -> string [@@dead "+print"]

compiler/syntax/src/res_printer.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Token = Res_token
55
module Parens = Res_parens
66
module ParsetreeViewer = Res_parsetree_viewer
77

8+
let default_print_width = 100
9+
810
type callback_style =
911
(* regular arrow function, example: `let f = x => x + 1` *)
1012
| NoCallback
@@ -6055,15 +6057,17 @@ let print_typ_expr t = print_typ_expr ~state:(State.init ()) t
60556057
let print_expression e = print_expression ~state:(State.init ()) e
60566058
let print_pattern p = print_pattern ~state:(State.init ()) p
60576059

6058-
let print_implementation ~width (s : Parsetree.structure) ~comments =
6060+
let print_implementation ?(width = default_print_width)
6061+
(s : Parsetree.structure) ~comments =
60596062
let cmt_tbl = CommentTable.make () in
60606063
CommentTable.walk_structure s cmt_tbl comments;
60616064
(* CommentTable.log cmt_tbl; *)
60626065
let doc = print_structure ~state:(State.init ()) s cmt_tbl in
60636066
(* Doc.debug doc; *)
60646067
Doc.to_string ~width doc ^ "\n"
60656068

6066-
let print_interface ~width (s : Parsetree.signature) ~comments =
6069+
let print_interface ?(width = default_print_width) (s : Parsetree.signature)
6070+
~comments =
60676071
let cmt_tbl = CommentTable.make () in
60686072
CommentTable.walk_signature s cmt_tbl comments;
60696073
Doc.to_string ~width (print_signature ~state:(State.init ()) s cmt_tbl) ^ "\n"

compiler/syntax/src/res_printer.mli

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
val default_print_width : int
2+
13
val print_type_params :
24
(Parsetree.core_type * Asttypes.variance) list ->
35
Res_comments_table.t ->
@@ -18,9 +20,9 @@ val print_structure : Parsetree.structure -> Res_comments_table.t -> Res_doc.t
1820
[@@live]
1921

2022
val print_implementation :
21-
width:int -> Parsetree.structure -> comments:Res_comment.t list -> string
23+
?width:int -> Parsetree.structure -> comments:Res_comment.t list -> string
2224
val print_interface :
23-
width:int -> Parsetree.signature -> comments:Res_comment.t list -> string
25+
?width:int -> Parsetree.signature -> comments:Res_comment.t list -> string
2426

2527
val print_ident_like :
2628
?allow_uident:bool -> ?allow_hyphen:bool -> string -> Res_doc.t

tools/src/tools.ml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,7 @@ module FormatCodeblocks = struct
772772
parsetree
773773
else parsetree
774774
in
775-
Res_printer.print_implementation
776-
~width:Res_multi_printer.default_print_width parsetree ~comments
775+
Res_printer.print_implementation parsetree ~comments
777776
|> String.trim |> Block_line.list_of_string
778777
in
779778

@@ -856,10 +855,7 @@ module FormatCodeblocks = struct
856855
makeMapper ~transformAssertEqual ~displayFilename:filename
857856
in
858857
let astMapped = mapper.structure mapper structure in
859-
Ok
860-
( Res_printer.print_implementation
861-
~width:Res_multi_printer.default_print_width astMapped ~comments,
862-
source )
858+
Ok (Res_printer.print_implementation astMapped ~comments, source)
863859
else if Filename.check_suffix path ".resi" then
864860
let parser =
865861
Res_driver.parsing_engine.parse_interface ~for_printer:true
@@ -871,10 +867,7 @@ module FormatCodeblocks = struct
871867
makeMapper ~transformAssertEqual ~displayFilename:filename
872868
in
873869
let astMapped = mapper.signature mapper signature in
874-
Ok
875-
( Res_printer.print_interface
876-
~width:Res_multi_printer.default_print_width astMapped ~comments,
877-
source )
870+
Ok (Res_printer.print_interface astMapped ~comments, source)
878871
else
879872
Error
880873
(Printf.sprintf

0 commit comments

Comments
 (0)