Skip to content

Commit

Permalink
feat: configureable MIX_ENV and MIX_TARGET (#246)
Browse files Browse the repository at this point in the history
Closes #246 

---------

Co-authored-by: Mitchell Hanberg <[email protected]>
  • Loading branch information
jjcarstens and mhanberg authored Oct 2, 2023
1 parent 7099370 commit c56518a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
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

0 comments on commit c56518a

Please sign in to comment.