Skip to content

Commit 30f7931

Browse files
authored
Honor :inputs option in .formatter.exs (#315)
1 parent 7b43622 commit 30f7931

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

apps/language_server/lib/language_server/providers/formatting.ex

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
99
def format(source_file, uri, project_dir) do
1010
if can_format?(uri, project_dir) do
1111
opts = SourceFile.formatter_opts(uri)
12-
formatted = IO.iodata_to_binary([Code.format_string!(source_file.text, opts), ?\n])
1312

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])
1815

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
2025
else
2126
msg =
2227
"Cannot format file from current directory " <>
@@ -40,6 +45,19 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
4045
String.starts_with?(Path.absname(file_path), cwd)
4146
end
4247

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+
4361
defp myers_diff_to_text_edits(myers_diff, starting_pos \\ {0, 0}) do
4462
myers_diff_to_text_edits(myers_diff, starting_pos, [])
4563
end

apps/language_server/test/providers/formatting_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,19 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
180180
}
181181
]
182182
end
183+
184+
test "honors :inputs when deciding to format" do
185+
file = __ENV__.file
186+
uri = "file://" <> file
187+
project_dir = Path.dirname(file)
188+
189+
opts = []
190+
assert Formatting.should_format?(uri, project_dir, opts[:inputs])
191+
192+
opts = [inputs: ["*.exs"]]
193+
assert Formatting.should_format?(uri, project_dir, opts[:inputs])
194+
195+
opts = [inputs: ["*.ex"]]
196+
refute Formatting.should_format?(uri, project_dir, opts[:inputs])
197+
end
183198
end

0 commit comments

Comments
 (0)