Skip to content

Commit aad9415

Browse files
committed
fix(elixir): format inside runtime
This ensures that formatter plugins can be utilized.
1 parent 36d8603 commit aad9415

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

Diff for: lib/next_ls.ex

+5-3
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ defmodule NextLS do
9797
def handle_request(%TextDocumentFormatting{params: %{text_document: %{uri: uri}}}, lsp) do
9898
document = lsp.assigns.documents[uri]
9999

100-
working_dir = URI.parse(lsp.assigns.root_uri).path
101-
{opts, _} = Code.eval_file(".formatter.exs", working_dir)
102-
new_document = Code.format_string!(Enum.join(document, "\n"), opts) |> IO.iodata_to_binary()
100+
{formatter, _} = Runtime.call(lsp.assigns.runtime, {Mix.Tasks.Format, :formatter_for_file, [".formatter.exs"]})
101+
102+
new_document =
103+
Runtime.call(lsp.assigns.runtime, {Kernel, :apply, [formatter, [Enum.join(document, "\n")]]})
104+
|> IO.iodata_to_binary()
103105

104106
{:reply,
105107
[

Diff for: lib/next_ls/runtime.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ defmodule NextLS.Runtime do
1010
GenServer.start_link(__MODULE__, opts, Keyword.take(opts, [:name]))
1111
end
1212

13-
@spec call(pid(), mfa()) :: any()
13+
@type mod_fun_arg :: {atom(), atom(), list()}
14+
15+
@spec call(pid(), mod_fun_arg()) :: any()
1416
def call(server, mfa), do: GenServer.call(server, {:call, mfa})
1517

1618
@spec ready?(pid()) :: boolean()

Diff for: test/next_ls_test.exs

+9-8
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ defmodule NextLSTest do
190190
params: %{}
191191
})
192192

193+
assert_notification "window/logMessage",
194+
%{"message" => "[NextLS] Runtime ready..."}
195+
193196
notify client, %{
194197
method: "textDocument/didOpen",
195198
jsonrpc: "2.0",
@@ -226,15 +229,13 @@ defmodule NextLSTest do
226229
}
227230
}
228231

229-
new_text =
230-
"""
231-
defmodule Foo.Bar do
232-
def run() do
233-
:ok
234-
end
232+
new_text = """
233+
defmodule Foo.Bar do
234+
def run() do
235+
:ok
235236
end
236-
"""
237-
|> String.trim()
237+
end
238+
"""
238239

239240
assert_result(
240241
2,

0 commit comments

Comments
 (0)