@@ -63,20 +63,17 @@ init(State) ->
6363
6464% % API entry point
6565-spec format_file (file :name_all () | stdin , config ()) ->
66- {ok , [error_info ()]} | {error , error_info ()}.
66+ {ok , [error_info ()]} | skip | {error , error_info ()}.
6767format_file (FileName , {Pragma , Out }) ->
6868 try
69- % % TODO: fix for stdin
70- ShouldFormat = (Pragma == ignore ) orelse contains_pragma_file (FileName ),
71- case ShouldFormat of
72- true ->
73- {ok , Nodes , Warnings } = file_read_nodes (FileName ),
69+ case file_read_nodes (FileName , Pragma ) of
70+ {ok , Nodes , Warnings } ->
7471 [$\n | Formatted ] = format_nodes (Nodes ),
7572 verify_nodes (FileName , Nodes , Formatted ),
7673 write_formatted (FileName , Formatted , Out ),
7774 {ok , Warnings };
78- false ->
79- { ok , []}
75+ skip ->
76+ skip
8077 end
8178 catch
8279 {error , Error } -> {error , Error }
@@ -119,7 +116,7 @@ contains_pragma_comment(_) ->
119116 {options , [{erlfmt_scan :location (), erlfmt_scan :location ()}]}.
120117format_range (FileName , StartLocation , EndLocation ) ->
121118 try
122- {ok , Nodes , Warnings } = file_read_nodes (FileName ),
119+ {ok , Nodes , Warnings } = file_read_nodes (FileName , ignore ),
123120 case verify_ranges (Nodes , StartLocation , EndLocation ) of
124121 {ok , NodesInRange } ->
125122 [$\n | Result ] = format_nodes (NodesInRange ),
@@ -136,14 +133,14 @@ format_range(FileName, StartLocation, EndLocation) ->
136133-spec read_nodes (file :name_all ()) ->
137134 {ok , [erlfmt_parse :abstract_form ()], [error_info ()]} | {error , error_info ()}.
138135read_nodes (FileName ) ->
139- try file_read_nodes (FileName )
136+ try file_read_nodes (FileName , ignore )
140137 catch
141138 {error , Error } -> {error , Error }
142139 end .
143140
144- file_read_nodes (FileName ) ->
141+ file_read_nodes (FileName , Pragma ) ->
145142 read_file (FileName , fun (File ) ->
146- read_nodes (erlfmt_scan :io_node (File ), FileName , [], [] )
143+ read_nodes (erlfmt_scan :io_node (File ), FileName , Pragma )
147144 end ).
148145
149146read_file (stdin , Action ) ->
@@ -162,17 +159,28 @@ read_file(FileName, Action) ->
162159-spec read_nodes_string (file :name_all (), string ()) ->
163160 {ok , [erlfmt_parse :abstract_form ()], [error_info ()]} | {error , error_info ()}.
164161read_nodes_string (FileName , String ) ->
165- try read_nodes (erlfmt_scan :string_node (String ), FileName , [], [] )
162+ try read_nodes (erlfmt_scan :string_node (String ), FileName , ignore )
166163 catch
167164 {error , Error } -> {error , Error }
168165 end .
169166
170- read_nodes ({ok , Tokens , Comments , Cont }, FileName , Acc , Warnings0 ) ->
167+ read_nodes ({ok , Tokens , Comments , Cont }, FileName , Pragma ) ->
168+ {Node , Warnings } = parse_nodes (Tokens , Comments , FileName , Cont , []),
169+ case contains_pragma_node (Node ) of
170+ false when Pragma =:= require ->
171+ skip ;
172+ _ ->
173+ read_nodes_loop (erlfmt_scan :continue (Cont ), FileName , [Node ], Warnings )
174+ end ;
175+ read_nodes (Other , FileName , _Pragma ) ->
176+ read_nodes_loop (Other , FileName , [], []).
177+
178+ read_nodes_loop ({ok , Tokens , Comments , Cont }, FileName , Acc , Warnings0 ) ->
171179 {Node , Warnings } = parse_nodes (Tokens , Comments , FileName , Cont , Warnings0 ),
172- read_nodes (erlfmt_scan :continue (Cont ), FileName , [Node | Acc ], Warnings );
173- read_nodes ({eof , _Loc }, _FileName , Acc , Warnings ) ->
180+ read_nodes_loop (erlfmt_scan :continue (Cont ), FileName , [Node | Acc ], Warnings );
181+ read_nodes_loop ({eof , _Loc }, _FileName , Acc , Warnings ) ->
174182 {ok , lists :reverse (Acc ), lists :reverse (Warnings )};
175- read_nodes ({error , {ErrLoc , Mod , Reason }, _Loc }, FileName , _Acc , _Warnings ) ->
183+ read_nodes_loop ({error , {ErrLoc , Mod , Reason }, _Loc }, FileName , _Acc , _Warnings ) ->
176184 throw ({error , {FileName , ErrLoc , Mod , Reason }}).
177185
178186parse_nodes ([], _Comments , _FileName , Cont , Warnings ) ->
0 commit comments