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: respect client capabilities #469

Merged
merged 2 commits into from
May 13, 2024
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
43 changes: 24 additions & 19 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -558,16 +558,16 @@ defmodule NextLS do
{:reply, nil, lsp}

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

{:reply, nil, lsp}
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."
NextLS.Logger.warning(
lsp.assigns.logger,
"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}]
Expand Down Expand Up @@ -717,9 +717,9 @@ defmodule NextLS do
{:reply, results, lsp}
rescue
e ->
GenLSP.warning(
lsp,
"[Next LS] Failed to run completion request: #{Exception.format(:error, e, __STACKTRACE__)}"
NextLS.Logger.warning(
lsp.assigns.logger,
"Failed to run completion request: #{Exception.format(:error, e, __STACKTRACE__)}"
)

{:reply, [], lsp}
Expand Down Expand Up @@ -812,7 +812,7 @@ defmodule NextLS do
end

def handle_request(%{method: method}, lsp) do
GenLSP.warning(lsp, "[Next LS] Method Not Found: #{method}")
NextLS.Logger.warning(lsp.assigns.logger, "Method Not Found: #{method}")

{:reply,
%ErrorResponse{
Expand All @@ -823,7 +823,8 @@ defmodule NextLS do

@impl true
def handle_notification(%Initialized{}, lsp) do
GenLSP.log(lsp, "[Next LS] NextLS v#{version()} has initialized!")
NextLS.Logger.log(lsp.assigns.logger, "NextLS v#{version()} has initialized!")
NextLS.Logger.log(lsp.assigns.logger, "Log file located at #{Path.join(File.cwd!(), ".elixir-tools/next-ls.log")}")

with opts when is_list(opts) <- lsp.assigns.auto_update do
{:ok, _} =
Expand Down Expand Up @@ -875,7 +876,7 @@ defmodule NextLS do
end

NextLS.Runtime.BundledElixir.install(lsp.assigns.bundle_base, lsp.assigns.logger)
GenLSP.log(lsp, "[Next LS] Booting runtimes...")
NextLS.Logger.log(lsp.assigns.logger, "Booting runtimes...")

parent = self()

Expand Down Expand Up @@ -917,7 +918,7 @@ defmodule NextLS do
on_initialized: fn status ->
if status == :ready do
Progress.stop(lsp, token, "NextLS runtime for folder #{name} has initialized!")
GenLSP.log(lsp, "[Next LS] Runtime for folder #{name} is ready...")
NextLS.Logger.log(lsp.assigns.logger, "Runtime for folder #{name} is ready...")

msg = {:runtime_ready, name, self()}

Expand All @@ -931,7 +932,7 @@ defmodule NextLS do

send(parent, {:runtime_failed, name, status})

GenLSP.error(lsp, "[Next LS] Runtime for folder #{name} failed to initialize")
NextLS.Logger.error(lsp.assigns.logger, "Runtime for folder #{name} failed to initialize")
end
end,
logger: lsp.assigns.logger
Expand Down Expand Up @@ -1015,7 +1016,7 @@ defmodule NextLS do
names = Enum.map(entries, fn {_, %{name: name}} -> name end)

for %{name: name, uri: uri} <- added, name not in names do
GenLSP.log(lsp, "[Next LS] Adding workspace folder #{name}")
NextLS.Logger.log(lsp.assigns.logger, "Adding workspace folder #{name}")
token = Progress.token()
Progress.start(lsp, token, "Initializing NextLS runtime for folder #{name}...")
parent = self()
Expand All @@ -1039,7 +1040,7 @@ defmodule NextLS do
on_initialized: fn status ->
if status == :ready do
Progress.stop(lsp, token, "NextLS runtime for folder #{name} has initialized!")
GenLSP.log(lsp, "[Next LS] Runtime for folder #{name} is ready...")
NextLS.Logger.log(lsp.assigns.logger, "Runtime for folder #{name} is ready...")

msg = {:runtime_ready, name, self()}

Expand All @@ -1053,7 +1054,7 @@ defmodule NextLS do

send(parent, {:runtime_failed, name, status})

GenLSP.error(lsp, "[Next LS] Runtime for folder #{name} failed to initialize")
NextLS.Logger.error(lsp.assigns.logger, "Runtime for folder #{name} failed to initialize")
end
end,
logger: lsp.assigns.logger
Expand All @@ -1064,7 +1065,7 @@ defmodule NextLS do
names = Enum.map(removed, & &1.name)

for {pid, %{name: name}} <- entries, name in names do
GenLSP.log(lsp, "[Next LS] Removing workspace folder #{name}")
NextLS.Logger.log(lsp.assigns.logger, "Removing workspace folder #{name}")
NextLS.Runtime.stop(lsp.assigns.dynamic_supervisor, pid)
end
end)
Expand Down Expand Up @@ -1224,7 +1225,7 @@ defmodule NextLS do

:ok = DynamicSupervisor.terminate_child(lsp.assigns.dynamic_supervisor, pid)

if status == {:error, :deps} do
if status == {:error, :deps} && lsp.assigns.client_capabilities.window.show_message do
resp =
GenLSP.request(
lsp,
Expand Down Expand Up @@ -1281,6 +1282,10 @@ defmodule NextLS do
_ ->
NextLS.Logger.info(lsp.assigns.logger, "Not running `mix deps.get`")
end
else
unless lsp.assigns.client_capabilities.window.show_message do
NextLS.Logger.info(lsp.assigns.logger, "Client does not support window/showMessageRequest")
end
end

{:noreply, lsp}
Expand All @@ -1298,8 +1303,8 @@ defmodule NextLS do
end

def handle_info(message, lsp) do
GenLSP.log(lsp, "[Next LS] Unhandled message: #{inspect(message)}")
GenLSP.log(lsp, "[Next LS] process assigns=#{inspect(lsp.assigns)}")
NextLS.Logger.log(lsp.assigns.logger, "Unhandled message: #{inspect(message)}")
NextLS.Logger.log(lsp.assigns.logger, "process assigns=#{inspect(lsp.assigns)}")
{:noreply, lsp}
end

Expand Down
6 changes: 2 additions & 4 deletions lib/next_ls/progress.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
defmodule NextLS.Progress do
@moduledoc false
@env Mix.env()

def start(lsp, token, msg) do
Task.start(fn ->
# FIXME: gen_lsp should allow stubbing requests so we don't have to
# set this in every test. For now, don't send it in the test env
if @env != :test do
if lsp.assigns.client_capabilities.window.work_done_progress do
GenLSP.request(lsp, %GenLSP.Requests.WindowWorkDoneProgressCreate{
id: System.unique_integer([:positive]),
params: %GenLSP.Structures.WorkDoneProgressCreateParams{
Expand Down
12 changes: 10 additions & 2 deletions lib/next_ls/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,17 @@ defmodule NextLS.Runtime do
|> String.to_charlist()

bindir = System.get_env("BINDIR")
NextLS.Logger.log(logger, "BINDIR=#{bindir}")
path = System.get_env("PATH")
new_path = String.replace(path, bindir <> ":", "")
new_path = elixir_bin_path <> ":" <> new_path
NextLS.Logger.log(logger, "before PATH=#{path}")
path_minus_bindir = String.replace(path, bindir <> ":", "")
NextLS.Logger.log(logger, "after1 PATH=#{path_minus_bindir}")

path_minus_bindir2 = path_minus_bindir |> String.split(":") |> List.delete(bindir) |> Enum.join(":")
NextLS.Logger.log(logger, "after2 PATH=#{path_minus_bindir2}")

new_path = elixir_bin_path <> ":" <> path_minus_bindir2
NextLS.Logger.log(logger, "after3 PATH=#{new_path}")

with dir when is_list(dir) <- :code.priv_dir(:next_ls) do
exe =
Expand Down
4 changes: 4 additions & 0 deletions test/support/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ defmodule NextLS.Support.Utils do
capabilities: %{
workspace: %{
workspaceFolders: true
},
window: %{
work_done_progress: false,
showMessage: %{}
}
},
workspaceFolders:
Expand Down