@@ -357,8 +357,12 @@ defmodule Mix.Tasks.Format do
357
357
be used for the given file.
358
358
359
359
The function must be called with the contents of the file
360
- to be formatted. The options are returned for reflection
361
- purposes.
360
+ to be formatted. Keep in mind that a function is always
361
+ returned, even if it doesn't match any of the inputs
362
+ specified in the `formatter.exs`. You can retrieve the
363
+ `:inputs` from the returned options, alongside the `:root`
364
+ option, to validate if the returned file matches the given
365
+ `:root` and `:inputs`.
362
366
363
367
## Options
364
368
@@ -390,7 +394,7 @@ defmodule Mix.Tasks.Format do
390
394
391
395
formatter_opts_and_subs = load_plugins ( formatter_opts_and_subs , opts )
392
396
393
- find_formatter_and_opts_for_file ( Path . expand ( file , cwd ) , formatter_opts_and_subs )
397
+ find_formatter_and_opts_for_file ( Path . expand ( file , cwd ) , cwd , formatter_opts_and_subs )
394
398
end
395
399
396
400
@ doc false
@@ -589,11 +593,11 @@ defmodule Mix.Tasks.Format do
589
593
stdin_filename = Path . expand ( Keyword . get ( opts , :stdin_filename , "stdin.exs" ) , cwd )
590
594
591
595
{ formatter , _opts } =
592
- find_formatter_and_opts_for_file ( stdin_filename , { formatter_opts , subs } )
596
+ find_formatter_and_opts_for_file ( stdin_filename , cwd , { formatter_opts , subs } )
593
597
594
598
{ file , formatter }
595
599
else
596
- { formatter , _opts } = find_formatter_and_opts_for_file ( file , { formatter_opts , subs } )
600
+ { formatter , _opts } = find_formatter_and_opts_for_file ( file , cwd , { formatter_opts , subs } )
597
601
{ file , formatter }
598
602
end
599
603
end
@@ -659,19 +663,19 @@ defmodule Mix.Tasks.Format do
659
663
if plugins != [ ] , do: plugins , else: nil
660
664
end
661
665
662
- defp find_formatter_and_opts_for_file ( file , formatter_opts_and_subs ) do
663
- formatter_opts = recur_formatter_opts_for_file ( file , formatter_opts_and_subs )
664
- { find_formatter_for_file ( file , formatter_opts ) , formatter_opts }
666
+ defp find_formatter_and_opts_for_file ( file , root , formatter_opts_and_subs ) do
667
+ { formatter_opts , root } = recur_formatter_opts_for_file ( file , root , formatter_opts_and_subs )
668
+ { find_formatter_for_file ( file , formatter_opts ) , [ root: root ] ++ formatter_opts }
665
669
end
666
670
667
- defp recur_formatter_opts_for_file ( file , { formatter_opts , subs } ) do
668
- Enum . find_value ( subs , formatter_opts , fn { sub , formatter_opts_and_subs } ->
671
+ defp recur_formatter_opts_for_file ( file , root , { formatter_opts , subs } ) do
672
+ Enum . find_value ( subs , { formatter_opts , root } , fn { sub , formatter_opts_and_subs } ->
669
673
size = byte_size ( sub )
670
674
671
675
case file do
672
676
<< prefix :: binary - size ( size ) , dir_separator , _ :: binary >>
673
677
when prefix == sub and dir_separator in [ ?\\ , ?/ ] ->
674
- recur_formatter_opts_for_file ( file , formatter_opts_and_subs )
678
+ recur_formatter_opts_for_file ( file , sub , formatter_opts_and_subs )
675
679
676
680
_ ->
677
681
nil
0 commit comments