Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(elixir): format inside runtime #13

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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