Skip to content

Commit

Permalink
fix: handle formatting files with syntax errors (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg authored Jun 24, 2023
1 parent 8a5857b commit b124b16
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ defmodule NextLS do
runtime = lsp.assigns.runtime

with {:ok, {formatter, _}} <- Runtime.call(runtime, {Mix.Tasks.Format, :formatter_for_file, [".formatter.exs"]}),
{:ok, response} <- Runtime.call(runtime, {Kernel, :apply, [formatter, [Enum.join(document, "\n")]]}) do
{:ok, response} when is_binary(response) or is_list(response) <-
Runtime.call(runtime, {Kernel, :apply, [formatter, [Enum.join(document, "\n")]]}) do
{:reply,
[
%TextEdit{
Expand All @@ -125,6 +126,9 @@ defmodule NextLS do
})

{:reply, nil, lsp}

_ ->
{:reply, nil, lsp}
end
end

Expand Down
48 changes: 48 additions & 0 deletions test/next_ls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,52 @@ defmodule NextLSTest do
}
]
end

test "formatting gracefully handles files with syntax errors", %{client: client} do
assert :ok ==
notify(client, %{
method: "initialized",
jsonrpc: "2.0",
params: %{}
})

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: "file://lib/foo/bar.ex",
languageId: "elixir",
version: 1,
text: """
defmodule Foo.Bar do
def run() do
:ok
end
"""
}
}
}

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

request client, %{
method: "textDocument/formatting",
id: 2,
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: "file://lib/foo/bar.ex"
},
options: %{
insertSpaces: true,
tabSize: 2
}
}
}

assert_result 2, nil
end
end

0 comments on commit b124b16

Please sign in to comment.