Skip to content

Commit af7da91

Browse files
committed
do not try to populate mix options from cache when the project dir is not mix project
Fixes #1022
1 parent d0f0db3 commit af7da91

File tree

5 files changed

+51
-42
lines changed

5 files changed

+51
-42
lines changed

apps/language_server/lib/language_server/providers/execute_command/apply_spec.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ApplySpec do
5656
formatted =
5757
try do
5858
target_line_length =
59-
case SourceFile.formatter_for(uri, state.project_dir) do
59+
case SourceFile.formatter_for(uri, state.project_dir, true) do
6060
{:ok, {_, opts, _formatter_exs_dir}} ->
6161
Keyword.get(opts, :line_length, @default_target_line_length)
6262

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
44
alias ElixirLS.LanguageServer.SourceFile
55
alias ElixirLS.LanguageServer.JsonRpc
66

7-
def format(%SourceFile{} = source_file, uri = "file:" <> _, project_dir)
7+
def format(%SourceFile{} = source_file, uri = "file:" <> _, project_dir, mix_project?)
88
when is_binary(project_dir) do
99
file_path = SourceFile.Path.absolute_from_uri(uri, project_dir)
1010
# file_path and project_dir are absolute paths with universal separators
1111
if SourceFile.Path.path_in_dir?(file_path, project_dir) do
1212
# file in project_dir we find formatter and options for file
13-
case SourceFile.formatter_for(uri, project_dir) do
13+
case SourceFile.formatter_for(uri, project_dir, mix_project?) do
1414
{:ok, {formatter, opts, formatter_exs_dir}} ->
1515
if should_format?(uri, formatter_exs_dir, opts[:inputs], project_dir) do
1616
do_format(source_file, formatter, opts)
@@ -46,7 +46,7 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
4646
end
4747

4848
# if project_dir is not set or schema is not file we format with default options
49-
def format(%SourceFile{} = source_file, _uri, _project_dir) do
49+
def format(%SourceFile{} = source_file, _uri, _project_dir, _mix_project?) do
5050
do_format(source_file, nil, [])
5151
end
5252

apps/language_server/lib/language_server/server.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ defmodule ElixirLS.LanguageServer.Server do
10011001
!!get_in(state.client_capabilities, ["textDocument", "signatureHelp"])
10021002

10031003
locals_without_parens =
1004-
case SourceFile.formatter_for(uri, state.project_dir) do
1004+
case SourceFile.formatter_for(uri, state.project_dir, state.mix_project?) do
10051005
{:ok, {_, opts, _formatter_exs_dir}} -> Keyword.get(opts, :locals_without_parens, [])
10061006
{:error, _} -> []
10071007
end
@@ -1034,7 +1034,7 @@ defmodule ElixirLS.LanguageServer.Server do
10341034

10351035
defp handle_request(formatting_req(_id, uri, _options), state = %__MODULE__{}) do
10361036
source_file = get_source_file(state, uri)
1037-
fun = fn -> Formatting.format(source_file, uri, state.project_dir) end
1037+
fun = fn -> Formatting.format(source_file, uri, state.project_dir, state.mix_project?) end
10381038
{:async, fun, state}
10391039
end
10401040

apps/language_server/lib/language_server/source_file.ex

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,26 +229,34 @@ defmodule ElixirLS.LanguageServer.SourceFile do
229229
"""
230230
end
231231

232-
@spec formatter_for(String.t(), String.t() | nil) ::
233-
{:ok, {function | nil, keyword(), String.t()}} | :error
234-
def formatter_for(uri = "file:" <> _, project_dir) when is_binary(project_dir) do
232+
@spec formatter_for(String.t(), String.t() | nil, boolean) ::
233+
{:ok, {function | nil, keyword(), String.t()}} | {:error, any}
234+
def formatter_for(uri = "file:" <> _, project_dir, mix_project?) when is_binary(project_dir) do
235235
path = __MODULE__.Path.from_uri(uri)
236236

237237
try do
238238
alias ElixirLS.LanguageServer.MixProject
239239

240-
if ElixirLS.LanguageServer.MixProject.loaded?() do
240+
if mix_project? do
241+
if ElixirLS.LanguageServer.MixProject.loaded?() do
242+
opts = [
243+
deps_paths: MixProject.deps_paths(),
244+
manifest_path: MixProject.manifest_path(),
245+
config_mtime: MixProject.config_mtime(),
246+
mix_project: MixProject.get(),
247+
root: project_dir
248+
]
249+
250+
{:ok, Mix.Tasks.ElixirLSFormat.formatter_for_file(path, opts)}
251+
else
252+
{:error, :project_not_loaded}
253+
end
254+
else
241255
opts = [
242-
deps_paths: MixProject.deps_paths(),
243-
manifest_path: MixProject.manifest_path(),
244-
config_mtime: MixProject.config_mtime(),
245-
mix_project: MixProject.get(),
246256
root: project_dir
247257
]
248258

249259
{:ok, Mix.Tasks.ElixirLSFormat.formatter_for_file(path, opts)}
250-
else
251-
{:error, :project_not_loaded}
252260
end
253261
catch
254262
kind, payload ->
@@ -261,7 +269,7 @@ defmodule ElixirLS.LanguageServer.SourceFile do
261269
end
262270
end
263271

264-
def formatter_for(_, _), do: {:error, :project_dir_not_set}
272+
def formatter_for(_, _, _), do: {:error, :project_dir_not_set}
265273

266274
defp format_code(code, opts) do
267275
try do

apps/language_server/test/providers/formatting_test.exs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
3636

3737
project_dir = maybe_convert_path_separators(FixtureHelpers.get_path("no_mixfile"))
3838

39-
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
39+
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir, false)
4040

4141
assert changes == [
4242
%TextEdit{
@@ -86,7 +86,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
8686

8787
project_dir = nil
8888

89-
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
89+
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir, false)
9090

9191
assert changes == [
9292
%TextEdit{
@@ -137,7 +137,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
137137

138138
project_dir = maybe_convert_path_separators(FixtureHelpers.get_path("formatter"))
139139

140-
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
140+
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir, true)
141141

142142
assert changes == [
143143
%TextEdit{
@@ -190,7 +190,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
190190

191191
project_dir = maybe_convert_path_separators(FixtureHelpers.get_path("formatter"))
192192

193-
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
193+
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir, true)
194194

195195
assert changes == [
196196
%TextEdit{
@@ -279,7 +279,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
279279
project_dir = maybe_convert_path_separators(FixtureHelpers.get_path("formatter"))
280280

281281
assert {:ok, []} =
282-
Formatting.format(source_file, uri, project_dir)
282+
Formatting.format(source_file, uri, project_dir, true)
283283
end)
284284
end
285285

@@ -310,7 +310,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
310310

311311
project_dir = maybe_convert_path_separators(FixtureHelpers.get_path("formatter"))
312312

313-
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
313+
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir, true)
314314

315315
assert changes == [
316316
%TextEdit{
@@ -365,7 +365,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
365365
project_dir = maybe_convert_path_separators(FixtureHelpers.get_path("formatter"))
366366

367367
assert {:ok, []} =
368-
Formatting.format(source_file, uri, project_dir)
368+
Formatting.format(source_file, uri, project_dir, true)
369369
end)
370370
end
371371

@@ -388,7 +388,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
388388

389389
project_dir = maybe_convert_path_separators("/project")
390390

391-
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
391+
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir, true)
392392

393393
assert changes == [
394394
%TextEdit{
@@ -428,7 +428,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
428428

429429
project_dir = maybe_convert_path_separators(FixtureHelpers.get_path("formatter"))
430430

431-
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
431+
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir, true)
432432

433433
assert changes == [
434434
%TextEdit{
@@ -468,7 +468,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
468468

469469
project_dir = maybe_convert_path_separators(FixtureHelpers.get_path("formatter"))
470470

471-
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
471+
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir, true)
472472

473473
assert changes == [
474474
%TextEdit{
@@ -495,40 +495,41 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
495495
store_mix_cache()
496496
project_dir = Path.expand(".")
497497

498-
assert_formatted("file.ex", project_dir)
498+
assert_formatted("file.ex", project_dir, true)
499499

500500
# test/.formatter.exs has [inputs: ["*.exs"]]
501-
assert_formatted("test/file.exs", project_dir)
502-
refute_formatted("test/file.ex", project_dir)
501+
assert_formatted("test/file.exs", project_dir, true)
502+
refute_formatted("test/file.ex", project_dir, true)
503503

504504
unless is_windows() do
505-
assert_formatted("symlink/file.exs", project_dir)
506-
refute_formatted("symlink/file.ex", project_dir)
505+
assert_formatted("symlink/file.exs", project_dir, true)
506+
refute_formatted("symlink/file.ex", project_dir, true)
507507
end
508508

509509
File.mkdir!("#{project_dir}/test/foo")
510-
refute_formatted("test/foo/file.ex", project_dir)
510+
refute_formatted("test/foo/file.ex", project_dir, true)
511511

512512
# apps/foo/bar/.formatter.exs has [inputs: ["foo.ex"]]
513-
assert_formatted("apps/foo/foo.ex", project_dir)
514-
refute_formatted("apps/foo/bar.ex", project_dir)
515-
refute_formatted("apps/foo.ex", project_dir)
513+
assert_formatted("apps/foo/foo.ex", project_dir, true)
514+
refute_formatted("apps/foo/bar.ex", project_dir, true)
515+
refute_formatted("apps/foo.ex", project_dir, true)
516516
end)
517517
end
518518

519-
def assert_formatted(path, project_dir) do
519+
def assert_formatted(path, project_dir, mix_file?) do
520520
assert match?(
521521
{:ok, [%ElixirLS.LanguageServer.Protocol.TextEdit{} | _]},
522-
format(path, project_dir)
522+
format(path, project_dir, mix_file?)
523523
),
524524
"expected '#{path}' to be formatted"
525525
end
526526

527-
def refute_formatted(path, project_dir) do
528-
assert match?({:ok, []}, format(path, project_dir)), "expected '#{path}' not to be formatted"
527+
def refute_formatted(path, project_dir, mix_file?) do
528+
assert match?({:ok, []}, format(path, project_dir, mix_file?)),
529+
"expected '#{path}' not to be formatted"
529530
end
530531

531-
defp format(path, project_dir) do
532+
defp format(path, project_dir, mix_project?) do
532533
project_dir =
533534
maybe_convert_path_separators(project_dir)
534535
|> Path.absname()
@@ -542,7 +543,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
542543
}
543544

544545
File.write!(path, " asd = 1")
545-
Formatting.format(source_file, SourceFile.Path.to_uri(path), project_dir)
546+
Formatting.format(source_file, SourceFile.Path.to_uri(path), project_dir, mix_project?)
546547
end
547548

548549
defp store_mix_cache() do

0 commit comments

Comments
 (0)