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: handle formatting files with syntax errors #26

Merged
merged 1 commit into from
Jun 24, 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
6 changes: 5 additions & 1 deletion lib/next_ls.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule NextLS do
@moduledoc false
use GenLSP

Check warning on line 3 in lib/next_ls.ex

View workflow job for this annotation

GitHub Actions / Test (1.15.x/24.x)

Logger.warn/1 is deprecated. Use Logger.warning/2 instead

Check warning on line 3 in lib/next_ls.ex

View workflow job for this annotation

GitHub Actions / Test (1.15.x/24.x)

Logger.warn/1 is deprecated. Use Logger.warning/2 instead

Check warning on line 3 in lib/next_ls.ex

View workflow job for this annotation

GitHub Actions / Test (1.15.x/25.x)

Logger.warn/1 is deprecated. Use Logger.warning/2 instead

Check warning on line 3 in lib/next_ls.ex

View workflow job for this annotation

GitHub Actions / Test (1.15.x/25.x)

Logger.warn/1 is deprecated. Use Logger.warning/2 instead

alias GenLSP.ErrorResponse

Expand Down Expand Up @@ -101,7 +101,8 @@
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 @@
})

{: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
Loading