diff --git a/lib/next_ls.ex b/lib/next_ls.ex index 74197430..52115e5f 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -97,9 +97,11 @@ defmodule NextLS do def handle_request(%TextDocumentFormatting{params: %{text_document: %{uri: uri}}}, lsp) do document = lsp.assigns.documents[uri] - working_dir = URI.parse(lsp.assigns.root_uri).path - {opts, _} = Code.eval_file(".formatter.exs", working_dir) - new_document = Code.format_string!(Enum.join(document, "\n"), opts) |> IO.iodata_to_binary() + {formatter, _} = Runtime.call(lsp.assigns.runtime, {Mix.Tasks.Format, :formatter_for_file, [".formatter.exs"]}) + + new_document = + Runtime.call(lsp.assigns.runtime, {Kernel, :apply, [formatter, [Enum.join(document, "\n")]]}) + |> IO.iodata_to_binary() {:reply, [ diff --git a/lib/next_ls/runtime.ex b/lib/next_ls/runtime.ex index 03612c66..a613879b 100644 --- a/lib/next_ls/runtime.ex +++ b/lib/next_ls/runtime.ex @@ -10,7 +10,9 @@ defmodule NextLS.Runtime do GenServer.start_link(__MODULE__, opts, Keyword.take(opts, [:name])) end - @spec call(pid(), mfa()) :: any() + @type mod_fun_arg :: {atom(), atom(), list()} + + @spec call(pid(), mod_fun_arg()) :: any() def call(server, mfa), do: GenServer.call(server, {:call, mfa}) @spec ready?(pid()) :: boolean() diff --git a/test/next_ls_test.exs b/test/next_ls_test.exs index 548bd149..db6f9c43 100644 --- a/test/next_ls_test.exs +++ b/test/next_ls_test.exs @@ -190,6 +190,9 @@ defmodule NextLSTest do params: %{} }) + assert_notification "window/logMessage", + %{"message" => "[NextLS] Runtime ready..."} + notify client, %{ method: "textDocument/didOpen", jsonrpc: "2.0", @@ -226,15 +229,13 @@ defmodule NextLSTest do } } - new_text = - """ - defmodule Foo.Bar do - def run() do - :ok - end + new_text = """ + defmodule Foo.Bar do + def run() do + :ok end - """ - |> String.trim() + end + """ assert_result( 2,