Skip to content

Commit

Permalink
fix: don't fail when document is missing (#266)
Browse files Browse the repository at this point in the history
There is a report of the document being nil, which is causing the server
to crash. The document should never be nil, but we can figure that out
later. The server not crashing should make that easier.
  • Loading branch information
mhanberg authored Oct 4, 2023
1 parent 2d3c4ed commit 8ec5c7b
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -451,41 +451,52 @@ defmodule NextLS do
document = lsp.assigns.documents[uri]

[resp] =
dispatch(lsp.assigns.registry, :runtimes, fn entries ->
for {runtime, %{uri: wuri}} <- entries, String.starts_with?(uri, wuri) do
with {:ok, {formatter, _}} <-
Runtime.call(runtime, {Mix.Tasks.Format, :formatter_for_file, [URI.parse(uri).path]}),
{:ok, response} when is_binary(response) or is_list(response) <-
Runtime.call(runtime, {Kernel, :apply, [formatter, [Enum.join(document, "\n")]]}) do
{:reply,
[
%TextEdit{
new_text: IO.iodata_to_binary(response),
range: %Range{
start: %Position{line: 0, character: 0},
end: %Position{
line: length(document),
character: document |> List.last() |> String.length() |> Kernel.-(1) |> max(0)
if is_list(document) do
dispatch(lsp.assigns.registry, :runtimes, fn entries ->
for {runtime, %{uri: wuri}} <- entries, String.starts_with?(uri, wuri) do
with {:ok, {formatter, _}} <-
Runtime.call(runtime, {Mix.Tasks.Format, :formatter_for_file, [URI.parse(uri).path]}),
{:ok, response} when is_binary(response) or is_list(response) <-
Runtime.call(runtime, {Kernel, :apply, [formatter, [Enum.join(document, "\n")]]}) do
{:reply,
[
%TextEdit{
new_text: IO.iodata_to_binary(response),
range: %Range{
start: %Position{line: 0, character: 0},
end: %Position{
line: length(document),
character: document |> List.last() |> String.length() |> Kernel.-(1) |> max(0)
}
}
}
}
], lsp}
else
{:error, :not_ready} ->
GenLSP.notify(lsp, %GenLSP.Notifications.WindowShowMessage{
params: %GenLSP.Structures.ShowMessageParams{
type: GenLSP.Enumerations.MessageType.info(),
message: "The NextLS runtime is still initializing!"
}
})
], lsp}
else
{:error, :not_ready} ->
GenLSP.notify(lsp, %GenLSP.Notifications.WindowShowMessage{
params: %GenLSP.Structures.ShowMessageParams{
type: GenLSP.Enumerations.MessageType.info(),
message: "The NextLS runtime is still initializing!"
}
})

{:reply, nil, lsp}

{:reply, nil, lsp}
_ ->
GenLSP.warning(lsp, "[Next LS] Failed to format the file: #{uri}")

_ ->
{:reply, nil, lsp}
{:reply, nil, lsp}
end
end
end
end)
end)
else
GenLSP.warning(
lsp,
"[Next LS] The file #{uri} was not found in the server's process state. Something must have gone wrong when opening, changing, or saving the file."
)

[{:reply, nil, lsp}]
end

resp
end
Expand Down

0 comments on commit 8ec5c7b

Please sign in to comment.