Skip to content

Commit f42fa99

Browse files
committed
improve error messages
use default formatter options for files outside project dir instead of erroring
1 parent fdb49e8 commit f42fa99

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,29 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
77
def format(%SourceFile{} = source_file, uri = "file:" <> _, project_dir)
88
when is_binary(project_dir) do
99
file_path = SourceFile.Path.absolute_from_uri(uri)
10+
1011
if SourceFile.Path.path_in_dir?(file_path, project_dir) do
12+
# file in project_dir we find formatter and options for file
1113
case SourceFile.formatter_for(uri, project_dir) do
1214
{:ok, {formatter, opts, formatter_exs_dir}} ->
1315
if should_format?(uri, formatter_exs_dir, opts[:inputs]) do
1416
do_format(source_file, formatter, opts)
1517
else
16-
JsonRpc.show_message(:info, "formatter.exs not found for #{file_path}")
18+
JsonRpc.show_message(:info, "File #{file_path} not included in #{Path.join(formatter_exs_dir, ".formatter.exs")}")
1719
{:ok, []}
1820
end
1921

2022
{:error, message} ->
21-
JsonRpc.show_message(:error, "Unable to fetch formatter options for #{file_path}")
23+
JsonRpc.show_message(:error, "Unable to find formatter for #{file_path}")
2224
{:error, :internal_error, message, true}
2325
end
2426
else
25-
JsonRpc.show_message(:warning, "Cannot format file #{file_path} outside of project dir #{project_dir}")
26-
27-
{:ok, []}
27+
# if file is outside project_dir we format with default options
28+
do_format(source_file, nil, [])
2829
end
2930
end
3031

31-
# if project_dir is not set or schema is not file: we format with default options
32+
# if project_dir is not set or schema is not file we format with default options
3233
def format(%SourceFile{} = source_file, _uri, _project_dir) do
3334
do_format(source_file, nil, [])
3435
end
@@ -43,8 +44,9 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
4344

4445
{:ok, response}
4546
rescue
46-
_e in [TokenMissingError, SyntaxError] ->
47-
{:error, :internal_error, "Unable to format due to syntax error", false}
47+
e ->
48+
JsonRpc.show_message(:error, "Unable to format:\n#{Exception.message(e)}")
49+
{:ok, []}
4850
end
4951

5052
defp get_formatted(text, formatter, _) when is_function(formatter) do

0 commit comments

Comments
 (0)