Skip to content

Commit

Permalink
fix(elixir): format inside runtime
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 e22aeaa commit fd2f638
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 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
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 fd2f638

Please sign in to comment.