@@ -9,14 +9,19 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
9
9
def format ( source_file , uri , project_dir ) do
10
10
if can_format? ( uri , project_dir ) do
11
11
opts = SourceFile . formatter_opts ( uri )
12
- formatted = IO . iodata_to_binary ( [ Code . format_string! ( source_file . text , opts ) , ?\n ] )
13
12
14
- response =
15
- source_file . text
16
- |> String . myers_difference ( formatted )
17
- |> myers_diff_to_text_edits ( )
13
+ if should_format? ( uri , project_dir , opts [ :inputs ] ) do
14
+ formatted = IO . iodata_to_binary ( [ Code . format_string! ( source_file . text , opts ) , ?\n ] )
18
15
19
- { :ok , response }
16
+ response =
17
+ source_file . text
18
+ |> String . myers_difference ( formatted )
19
+ |> myers_diff_to_text_edits ( )
20
+
21
+ { :ok , response }
22
+ else
23
+ { :ok , [ ] }
24
+ end
20
25
else
21
26
msg =
22
27
"Cannot format file from current directory " <>
@@ -40,6 +45,19 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
40
45
String . starts_with? ( Path . absname ( file_path ) , cwd )
41
46
end
42
47
48
+ def should_format? ( file_uri , project_dir , inputs ) when is_list ( inputs ) do
49
+ file = String . trim_leading ( file_uri , "file://" )
50
+
51
+ Enum . any? ( inputs , fn glob ->
52
+ project_dir
53
+ |> Path . join ( glob )
54
+ |> Path . wildcard ( match_dot: true )
55
+ |> Enum . any? ( & ( file == & 1 ) )
56
+ end )
57
+ end
58
+
59
+ def should_format? ( _file_uri , _project_dir , _inputs ) , do: true
60
+
43
61
defp myers_diff_to_text_edits ( myers_diff , starting_pos \\ { 0 , 0 } ) do
44
62
myers_diff_to_text_edits ( myers_diff , starting_pos , [ ] )
45
63
end
0 commit comments