@@ -357,8 +357,12 @@ defmodule Mix.Tasks.Format do
357357  be used for the given file. 
358358
359359  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`. 
362366
363367  ## Options 
364368
@@ -390,7 +394,7 @@ defmodule Mix.Tasks.Format do
390394
391395    formatter_opts_and_subs  =  load_plugins ( formatter_opts_and_subs ,  opts ) 
392396
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 ) 
394398  end 
395399
396400  @ doc  false 
@@ -589,11 +593,11 @@ defmodule Mix.Tasks.Format do
589593        stdin_filename  =  Path . expand ( Keyword . get ( opts ,  :stdin_filename ,  "stdin.exs" ) ,  cwd ) 
590594
591595        { 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 } ) 
593597
594598        { file ,  formatter } 
595599      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 } ) 
597601        { file ,  formatter } 
598602      end 
599603    end 
@@ -659,19 +663,19 @@ defmodule Mix.Tasks.Format do
659663    if  plugins  !=  [ ] ,  do:  plugins ,  else:  nil 
660664  end 
661665
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 } 
665669  end 
666670
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 }  -> 
669673      size  =  byte_size ( sub ) 
670674
671675      case  file  do 
672676        << prefix :: binary - size ( size ) ,  dir_separator ,  _ :: binary >> 
673677        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 ) 
675679
676680        _  -> 
677681          nil 
0 commit comments