@@ -267,6 +267,14 @@ defmodule Mix.Tasks.ElixirLSFormat do
267
267
Mix . raise ( "--no-exit can only be used together with --check-formatted" )
268
268
end
269
269
270
+ opts =
271
+ Keyword . merge ( opts ,
272
+ deps_paths: Mix.Project . deps_paths ( ) ,
273
+ manifest_path: Mix.Project . manifest_path ( ) ,
274
+ config_mtime: Mix.Project . config_mtime ( ) ,
275
+ mix_project: Mix.Project . get ( )
276
+ )
277
+
270
278
{ formatter_opts_and_subs , _sources } =
271
279
eval_deps_and_subdirectories ( cwd , dot_formatter , formatter_opts , [ dot_formatter ] , opts )
272
280
@@ -411,6 +419,15 @@ defmodule Mix.Tasks.ElixirLSFormat do
411
419
@ doc since: "1.13.0"
412
420
def formatter_for_file ( file , opts \\ [ ] ) do
413
421
cwd = Keyword . get_lazy ( opts , :root , & File . cwd! / 0 )
422
+
423
+ opts =
424
+ Keyword . merge ( opts ,
425
+ deps_paths: Keyword . get_lazy ( opts , :deps_paths , & Mix.Project . deps_paths / 0 ) ,
426
+ manifest_path: Keyword . get_lazy ( opts , :manifest_path , & Mix.Project . manifest_path / 0 ) ,
427
+ config_mtime: Keyword . get_lazy ( opts , :config_mtime , & Mix.Project . config_mtime / 0 ) ,
428
+ mix_project: Keyword . get_lazy ( opts , :mix_project , & Mix.Project . get / 0 )
429
+ )
430
+
414
431
{ dot_formatter , formatter_opts } = eval_dot_formatter ( cwd , opts )
415
432
416
433
{ formatter_opts_and_subs , _sources } =
@@ -464,13 +481,19 @@ defmodule Mix.Tasks.ElixirLSFormat do
464
481
if deps == [ ] and subs == [ ] do
465
482
{ { formatter_opts , [ ] } , sources }
466
483
else
467
- manifest = Path . join ( Mix.Project . manifest_path ( ) , @ manifest_dot_formatter )
484
+ manifest = Path . join ( opts [ : manifest_path] , @ manifest_dot_formatter )
468
485
469
486
{ { locals_without_parens , subdirectories } , sources } =
470
- maybe_cache_in_manifest ( dot_formatter , manifest , fn ->
471
- { subdirectories , sources } = eval_subs_opts ( subs , cwd , sources , opts )
472
- { { eval_deps_opts ( deps , opts ) , subdirectories } , sources }
473
- end )
487
+ maybe_cache_in_manifest (
488
+ dot_formatter ,
489
+ opts [ :mix_project ] ,
490
+ manifest ,
491
+ opts [ :config_mtime ] ,
492
+ fn ->
493
+ { subdirectories , sources } = eval_subs_opts ( subs , cwd , sources , opts )
494
+ { { eval_deps_opts ( deps , opts ) , subdirectories } , sources }
495
+ end
496
+ )
474
497
475
498
formatter_opts =
476
499
Keyword . update (
@@ -484,19 +507,19 @@ defmodule Mix.Tasks.ElixirLSFormat do
484
507
end
485
508
end
486
509
487
- defp maybe_cache_in_manifest ( dot_formatter , manifest , fun ) do
510
+ defp maybe_cache_in_manifest ( dot_formatter , mix_project , manifest , config_mtime , fun ) do
488
511
cond do
489
- is_nil ( Mix.Project . get ( ) ) or dot_formatter != ".formatter.exs" -> fun . ( )
490
- entry = read_manifest ( manifest ) -> entry
512
+ is_nil ( mix_project ) or dot_formatter != ".formatter.exs" -> fun . ( )
513
+ entry = read_manifest ( manifest , config_mtime ) -> entry
491
514
true -> write_manifest! ( manifest , fun . ( ) )
492
515
end
493
516
end
494
517
495
- defp read_manifest ( manifest ) do
518
+ defp read_manifest ( manifest , config_mtime ) do
496
519
with { :ok , binary } <- File . read ( manifest ) ,
497
520
{ :ok , { @ manifest_vsn , entry , sources } } <- safe_binary_to_term ( binary ) ,
498
521
expanded_sources = Enum . flat_map ( sources , & Path . wildcard ( & 1 , match_dot: true ) ) ,
499
- false <- Mix.Utils . stale? ( [ Mix.Project . config_mtime ( ) | expanded_sources ] , [ manifest ] ) do
522
+ false <- Mix.Utils . stale? ( [ config_mtime | expanded_sources ] , [ manifest ] ) do
500
523
{ entry , sources }
501
524
else
502
525
_ -> nil
@@ -1069,4 +1092,3 @@ defmodule Mix.Tasks.ElixirLSFormat do
1069
1092
end
1070
1093
end
1071
1094
end
1072
-
0 commit comments