Skip to content

Commit

Permalink
Support passing target and env options from LSP
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcarstens authored and mhanberg committed Oct 2, 2023
1 parent 7099370 commit 8f8f276
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
28 changes: 26 additions & 2 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ defmodule NextLS do
@impl true
def handle_request(
%Initialize{
params: %InitializeParams{root_uri: root_uri, workspace_folders: workspace_folders, capabilities: caps}
params: %InitializeParams{
root_uri: root_uri,
workspace_folders: workspace_folders,
capabilities: caps,
initialization_options: init_opts
}
},
lsp
) do
Expand All @@ -102,6 +107,15 @@ defmodule NextLS do
%{name: Path.basename(root_uri), uri: root_uri}
end

safe_opt = fn opts, key, default ->
if val = opts[key] in ["", nil], do: default, else: val
end

safe_init_opts = %{
mix_env: safe_opt.(init_opts, "mix_env", "dev"),
mix_target: safe_opt.(init_opts, "mix_target", "host")
}

{:reply,
%InitializeResult{
capabilities: %ServerCapabilities{
Expand All @@ -124,7 +138,13 @@ defmodule NextLS do
}
},
server_info: %{name: "Next LS"}
}, assign(lsp, root_uri: root_uri, workspace_folders: workspace_folders, client_capabilities: caps)}
},
assign(lsp,
root_uri: root_uri,
workspace_folders: workspace_folders,
client_capabilities: caps,
init_opts: safe_init_opts
)}
end

def handle_request(%TextDocumentDefinition{params: %{text_document: %{uri: uri}, position: position}}, lsp) do
Expand Down Expand Up @@ -555,6 +575,8 @@ defmodule NextLS do
task_supervisor: lsp.assigns.runtime_task_supervisor,
working_dir: working_dir,
uri: uri,
mix_env: lsp.assigns.init_opts.mix_env,
mix_target: lsp.assigns.init_opts.mix_target,
on_initialized: fn status ->
if status == :ready do
Progress.stop(lsp, token, "NextLS runtime for folder #{name} has initialized!")
Expand Down Expand Up @@ -668,6 +690,8 @@ defmodule NextLS do
task_supervisor: lsp.assigns.runtime_task_supervisor,
working_dir: working_dir,
uri: uri,
mix_env: lsp.assigns.init_opts.mix_env,
mix_target: lsp.assigns.init_opts.mix_target,
on_initialized: fn status ->
if status == :ready do
Progress.stop(lsp, token, "NextLS runtime for folder #{name} has initialized!")
Expand Down
5 changes: 4 additions & 1 deletion lib/next_ls/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ defmodule NextLS.Runtime do
registry = Keyword.fetch!(opts, :registry)
on_initialized = Keyword.fetch!(opts, :on_initialized)
db = Keyword.fetch!(opts, :db)
mix_env = Keyword.fetch!(opts, :mix_env)
mix_target = Keyword.fetch!(opts, :mix_target)

Registry.register(registry, :runtimes, %{name: name, uri: uri, path: working_dir, db: db})

Expand Down Expand Up @@ -86,7 +88,8 @@ defmodule NextLS.Runtime do
env: [
{~c"LSP", ~c"nextls"},
{~c"NEXTLS_PARENT_PID", parent},
{~c"MIX_ENV", ~c"dev"},
{~c"MIX_ENV", ~c"#{mix_env}"},
{~c"MIX_TARGET", ~c"#{mix_target}"},
{~c"MIX_BUILD_ROOT", ~c".elixir-tools/_build"},
{~c"ROOTDIR", false},
{~c"BINDIR", false},
Expand Down
12 changes: 12 additions & 0 deletions test/next_ls/runtime_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ defmodule NextLs.RuntimeTest do
parent: self(),
logger: logger,
db: :some_db,
mix_env: "dev",
mix_target: "host",
registry: RuntimeTest.Registry},
restart: :temporary
)
Expand Down Expand Up @@ -97,6 +99,8 @@ defmodule NextLs.RuntimeTest do
parent: self(),
logger: logger,
db: :some_db,
mix_env: "dev",
mix_target: "host",
registry: RuntimeTest.Registry},
restart: :temporary
)
Expand Down Expand Up @@ -127,6 +131,8 @@ defmodule NextLs.RuntimeTest do
parent: self(),
logger: logger,
db: :some_db,
mix_env: "dev",
mix_target: "host",
registry: RuntimeTest.Registry}
)

Expand All @@ -153,6 +159,8 @@ defmodule NextLs.RuntimeTest do
parent: self(),
logger: logger,
db: :some_db,
mix_env: "dev",
mix_target: "host",
registry: RuntimeTest.Registry}
)

Expand Down Expand Up @@ -180,6 +188,8 @@ defmodule NextLs.RuntimeTest do
parent: self(),
logger: logger,
db: :some_db,
mix_env: "dev",
mix_target: "host",
registry: RuntimeTest.Registry}
)

Expand Down Expand Up @@ -232,6 +242,8 @@ defmodule NextLs.RuntimeTest do
parent: self(),
logger: logger,
db: :some_db,
mix_env: "dev",
mix_target: "host",
registry: RuntimeTest.Registry}
)

Expand Down

0 comments on commit 8f8f276

Please sign in to comment.