Skip to content

Support passing target and env options from LSP #246

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

Merged
merged 2 commits into from
Oct 2, 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
42 changes: 40 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,8 @@ defmodule NextLS do
%{name: Path.basename(root_uri), uri: root_uri}
end

{:ok, init_opts} = __MODULE__.InitOpts.validate(init_opts)

{:reply,
%InitializeResult{
capabilities: %ServerCapabilities{
Expand All @@ -124,7 +131,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: init_opts
)}
end

def handle_request(%TextDocumentDefinition{params: %{text_document: %{uri: uri}, position: position}}, lsp) do
Expand Down Expand Up @@ -555,6 +568,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 +683,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 @@ -985,4 +1002,25 @@ defmodule NextLS do

# penalty for unmatched letter
defp calc_unmatched_penalty(score, _traits), do: score - 1

defmodule InitOpts do
@moduledoc false
import Schematic

defstruct mix_target: "host", mix_env: "dev"

def validate(opts) do
schematic =
nullable(
schema(__MODULE__, %{
optional(:mix_target) => str(),
optional(:mix_env) => str()
})
)

with {:ok, nil} <- unify(schematic, opts) do
{:ok, %__MODULE__{}}
end
end
end
end
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
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ defmodule NextLS.MixProject do
{:exqlite, "~> 0.13.14"},
{:gen_lsp, "~> 0.6"},
{:req, "~> 0.3.11"},
{:schematic, "~> 0.2"},

{:burrito, github: "burrito-elixir/burrito", only: [:dev, :prod]},
{:bypass, "~> 2.1", only: :test},
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