Skip to content

Commit

Permalink
fix(elixir): format inside runtime (#13)
Browse files Browse the repository at this point in the history
This ensures that formatter plugins can be utilized.
  • Loading branch information
mhanberg committed Jun 20, 2023
1 parent 36d8603 commit 99965f1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
8 changes: 5 additions & 3 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
[
Expand Down
4 changes: 3 additions & 1 deletion lib/next_ls/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 9 additions & 8 deletions test/next_ls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ defmodule NextLSTest do
params: %{}
})

assert_notification "window/logMessage",
%{"message" => "[NextLS] Runtime ready..."}

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 99965f1

Please sign in to comment.