From c56518a297afd4bb3cd3ad86ebc32f3bf5a70ab4 Mon Sep 17 00:00:00 2001 From: Jon Carstens Date: Sun, 1 Oct 2023 21:11:50 -0600 Subject: [PATCH] feat: configureable MIX_ENV and MIX_TARGET (#246) Closes #246 --------- Co-authored-by: Mitchell Hanberg --- lib/next_ls.ex | 42 +++++++++++++++++++++++++++++++++-- lib/next_ls/runtime.ex | 5 ++++- mix.exs | 1 + test/next_ls/runtime_test.exs | 12 ++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/next_ls.ex b/lib/next_ls.ex index bf2745f7..6f8fd410 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -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 @@ -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{ @@ -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 @@ -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!") @@ -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!") @@ -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 diff --git a/lib/next_ls/runtime.ex b/lib/next_ls/runtime.ex index e34cbc7f..957d9fa9 100644 --- a/lib/next_ls/runtime.ex +++ b/lib/next_ls/runtime.ex @@ -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}) @@ -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}, diff --git a/mix.exs b/mix.exs index 62ba1f96..079b18f5 100644 --- a/mix.exs +++ b/mix.exs @@ -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}, diff --git a/test/next_ls/runtime_test.exs b/test/next_ls/runtime_test.exs index 885a82f7..310e6817 100644 --- a/test/next_ls/runtime_test.exs +++ b/test/next_ls/runtime_test.exs @@ -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 ) @@ -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 ) @@ -127,6 +131,8 @@ defmodule NextLs.RuntimeTest do parent: self(), logger: logger, db: :some_db, + mix_env: "dev", + mix_target: "host", registry: RuntimeTest.Registry} ) @@ -153,6 +159,8 @@ defmodule NextLs.RuntimeTest do parent: self(), logger: logger, db: :some_db, + mix_env: "dev", + mix_target: "host", registry: RuntimeTest.Registry} ) @@ -180,6 +188,8 @@ defmodule NextLs.RuntimeTest do parent: self(), logger: logger, db: :some_db, + mix_env: "dev", + mix_target: "host", registry: RuntimeTest.Registry} ) @@ -232,6 +242,8 @@ defmodule NextLs.RuntimeTest do parent: self(), logger: logger, db: :some_db, + mix_env: "dev", + mix_target: "host", registry: RuntimeTest.Registry} )