@@ -27,7 +27,7 @@ defmodule ElixirLS.LanguageServer.Experimental.CodeMod.Format do
2727 project_path = Conversions . ensure_path ( project_path_or_uri )
2828
2929 with :ok <- check_current_directory ( document , project_path ) ,
30- { :ok , formatter , options } <- formatter_for ( document . path ) ,
30+ { :ok , formatter , options } <- formatter_for ( document . path , project_path ) ,
3131 :ok <-
3232 check_inputs_apply ( document , project_path , Keyword . get ( options , :inputs ) ) do
3333 document
@@ -44,15 +44,21 @@ defmodule ElixirLS.LanguageServer.Experimental.CodeMod.Format do
4444 |> formatter . ( )
4545 end
4646
47- @ spec formatter_for ( String . t ( ) ) ::
47+ @ spec formatter_for ( String . t ( ) , String . t ( ) | nil ) ::
4848 { :ok , formatter_function , keyword ( ) } | { :error , :no_formatter_available }
49- defp formatter_for ( uri_or_path ) do
49+ defp formatter_for ( uri_or_path , project_dir ) do
5050 path = Conversions . ensure_path ( uri_or_path )
5151
5252 try do
5353 true = Code . ensure_loaded? ( Mix.Tasks.Format )
5454
55- if function_exported? ( Mix.Tasks.Format , :formatter_for_file , 1 ) do
55+ if project_dir && Version . match? ( System . version ( ) , ">= 1.15.0-dev" ) do
56+ { formatter_function , options } = Mix.Tasks.Format . formatter_for_file ( path , root: project_dir )
57+
58+ wrapped_formatter_function = wrap_with_try_catch ( formatter_function )
59+
60+ { :ok , wrapped_formatter_function , options }
61+ else if Version . match? ( System . version ( ) , ">= 1.13.0" ) do
5662 { formatter_function , options } = Mix.Tasks.Format . formatter_for_file ( path )
5763
5864 wrapped_formatter_function = wrap_with_try_catch ( formatter_function )
@@ -63,6 +69,7 @@ defmodule ElixirLS.LanguageServer.Experimental.CodeMod.Format do
6369 formatter = build_formatter ( options )
6470 { :ok , formatter , Mix.Tasks.Format . formatter_opts_for_file ( path ) }
6571 end
72+ end
6673 rescue
6774 e ->
6875 message = Exception . message ( e )
0 commit comments