From 4c93891d6d63bb3556e6490a05251f2320ea2f73 Mon Sep 17 00:00:00 2001 From: Steve Cohen Date: Tue, 21 May 2024 11:36:47 -0700 Subject: [PATCH] Moved Api.Local functions to RemoteControl directly --- .../lib/lexical/remote_control.ex | 62 +++++++++++++++++-- .../lib/lexical/remote_control/api.ex | 48 +++++++------- .../lib/lexical/remote_control/api/local.ex | 51 --------------- .../lib/lexical/remote_control/build/state.ex | 13 ++-- .../remote_control/commands/reindex.ex | 4 +- .../remote_control/compilation/tracer.ex | 5 +- .../lexical/remote_control/module_mappings.ex | 4 +- .../lib/lexical/remote_control/plugin.ex | 6 +- .../lib/lexical/remote_control/progress.ex | 12 ++-- .../lexical/remote_control/search/store.ex | 3 +- 10 files changed, 105 insertions(+), 103 deletions(-) delete mode 100644 apps/remote_control/lib/lexical/remote_control/api/local.ex diff --git a/apps/remote_control/lib/lexical/remote_control.ex b/apps/remote_control/lib/lexical/remote_control.ex index 9462ad2e6..f1ae9739e 100644 --- a/apps/remote_control/lib/lexical/remote_control.ex +++ b/apps/remote_control/lib/lexical/remote_control.ex @@ -6,12 +6,62 @@ defmodule Lexical.RemoteControl do """ alias Lexical.Project + alias Lexical.RemoteControl + alias Lexical.RemoteControl.Api.Proxy + alias Lexical.RemoteControl.CodeAction + alias Lexical.RemoteControl.CodeIntelligence alias Lexical.RemoteControl.ProjectNode + require Logger @excluded_apps [:patch, :nimble_parsec] @allowed_apps [:remote_control | Mix.Project.deps_apps()] -- @excluded_apps + defdelegate schedule_compile(force?), to: Proxy + + defdelegate compile_document(document), to: Proxy + + defdelegate format(document), to: Proxy + + defdelegate reindex, to: Proxy + + defdelegate index_running?, to: Proxy + + defdelegate expand_alias(segments_or_module, analysis, position), to: RemoteControl.Analyzer + + defdelegate list_modules, to: :code, as: :all_available + + defdelegate code_actions(document, range, diagnostics, kinds), to: CodeAction, as: :for_range + + defdelegate complete(env), to: RemoteControl.Completion, as: :elixir_sense_expand + + defdelegate complete_struct_fields(analysis, position), + to: RemoteControl.Completion, + as: :struct_fields + + defdelegate definition(document, position), to: CodeIntelligence.Definition + + defdelegate references(analysis, position, include_definitions?), + to: CodeIntelligence.References + + defdelegate modules_with_prefix(prefix), to: RemoteControl.Modules, as: :with_prefix + + defdelegate modules_with_prefix(prefix, predicate), to: RemoteControl.Modules, as: :with_prefix + + defdelegate docs(module, opts \\ []), to: CodeIntelligence.Docs, as: :for_module + + defdelegate register_listener(listener_pid, message_types), to: RemoteControl.Dispatch + + defdelegate broadcast(message), to: RemoteControl.Dispatch + + defdelegate resolve_entity(analysis, position), to: CodeIntelligence.Entity, as: :resolve + + defdelegate struct_definitions, to: CodeIntelligence.Structs, as: :for_project + + defdelegate document_symbols(document), to: CodeIntelligence.Symbols, as: :for_document + + defdelegate workspace_symbols(query), to: CodeIntelligence.Symbols, as: :for_workspace + def start_link(%Project{} = project) do :ok = ensure_epmd_started() start_net_kernel(project) @@ -49,15 +99,15 @@ defmodule Lexical.RemoteControl do |> :erpc.call(m, f, a) end - defp start_net_kernel(%Project{} = project) do - :net_kernel.start([manager_node_name(project)]) - end - def manager_node_name(%Project{} = project) do :"manager-#{Project.name(project)}-#{Project.entropy(project)}@127.0.0.1" end - def ensure_apps_started(node, app_names) do + defp start_net_kernel(%Project{} = project) do + :net_kernel.start([manager_node_name(project)]) + end + + defp ensure_apps_started(node, app_names) do Enum.reduce_while(app_names, :ok, fn app_name, _ -> case :rpc.call(node, :application, :ensure_all_started, [app_name]) do {:ok, _} -> {:cont, :ok} @@ -66,7 +116,7 @@ defmodule Lexical.RemoteControl do end) end - def glob_paths do + defp glob_paths do for entry <- :code.get_path(), entry_string = List.to_string(entry), entry_string != ".", diff --git a/apps/remote_control/lib/lexical/remote_control/api.ex b/apps/remote_control/lib/lexical/remote_control/api.ex index 52eaecdfd..ffb723188 100644 --- a/apps/remote_control/lib/lexical/remote_control/api.ex +++ b/apps/remote_control/lib/lexical/remote_control/api.ex @@ -6,17 +6,16 @@ defmodule Lexical.RemoteControl.Api do alias Lexical.Document.Range alias Lexical.Project alias Lexical.RemoteControl - alias Lexical.RemoteControl.Api alias Lexical.RemoteControl.CodeIntelligence require Logger def schedule_compile(%Project{} = project, force?) do - RemoteControl.call(project, Api.Local, :schedule_compile, [force?]) + RemoteControl.call(project, RemoteControl, :schedule_compile, [force?]) end def compile_document(%Project{} = project, %Document{} = document) do - RemoteControl.call(project, Api.Local, :compile_document, [document]) + RemoteControl.call(project, RemoteControl, :compile_document, [document]) end def expand_alias( @@ -25,7 +24,7 @@ defmodule Lexical.RemoteControl.Api do %Analysis{} = analysis, %Position{} = position ) do - RemoteControl.call(project, Api.Local, :expand_alias, [ + RemoteControl.call(project, RemoteControl, :expand_alias, [ segments_or_module, analysis, position @@ -33,11 +32,11 @@ defmodule Lexical.RemoteControl.Api do end def list_modules(%Project{} = project) do - RemoteControl.call(project, Api.Local, :list_modules) + RemoteControl.call(project, RemoteControl, :list_modules) end def format(%Project{} = project, %Document{} = document) do - RemoteControl.call(project, Api.Local, :format, [document]) + RemoteControl.call(project, RemoteControl, :format, [document]) end def code_actions( @@ -47,23 +46,28 @@ defmodule Lexical.RemoteControl.Api do diagnostics, kinds ) do - RemoteControl.call(project, Api.Local, :code_actions, [document, range, diagnostics, kinds]) + RemoteControl.call(project, RemoteControl, :code_actions, [ + document, + range, + diagnostics, + kinds + ]) end def complete(%Project{} = project, %Env{} = env) do Logger.info("Completion for #{inspect(env.position)}") - RemoteControl.call(project, Api.Local, :complete, [env]) + RemoteControl.call(project, RemoteControl, :complete, [env]) end def complete_struct_fields(%Project{} = project, %Analysis{} = analysis, %Position{} = position) do - RemoteControl.call(project, Api.Local, :complete_struct_fields, [ + RemoteControl.call(project, RemoteControl, :complete_struct_fields, [ analysis, position ]) end def definition(%Project{} = project, %Document{} = document, %Position{} = position) do - RemoteControl.call(project, Api.Local, :definition, [document, position]) + RemoteControl.call(project, RemoteControl, :definition, [document, position]) end def references( @@ -72,7 +76,7 @@ defmodule Lexical.RemoteControl.Api do %Position{} = position, include_definitions? ) do - RemoteControl.call(project, Api.Local, :references, [ + RemoteControl.call(project, RemoteControl, :references, [ analysis, position, include_definitions? @@ -81,52 +85,52 @@ defmodule Lexical.RemoteControl.Api do def modules_with_prefix(%Project{} = project, prefix) when is_binary(prefix) or is_atom(prefix) do - RemoteControl.call(project, Api.Local, :modules_with_prefix, [prefix]) + RemoteControl.call(project, RemoteControl, :modules_with_prefix, [prefix]) end def modules_with_prefix(%Project{} = project, prefix, predicate) when is_binary(prefix) or is_atom(prefix) do - RemoteControl.call(project, Api.Local, :modules_with_prefix, [prefix, predicate]) + RemoteControl.call(project, RemoteControl, :modules_with_prefix, [prefix, predicate]) end @spec docs(Project.t(), module()) :: {:ok, CodeIntelligence.Docs.t()} | {:error, any()} def docs(%Project{} = project, module, opts \\ []) when is_atom(module) do - RemoteControl.call(project, Api.Local, :docs, [module, opts]) + RemoteControl.call(project, RemoteControl, :docs, [module, opts]) end def register_listener(%Project{} = project, listener_pid, message_types) when is_pid(listener_pid) and is_list(message_types) do - RemoteControl.call(project, Api.Local, :register_listener, [ + RemoteControl.call(project, RemoteControl, :register_listener, [ listener_pid, message_types ]) end def broadcast(%Project{} = project, message) do - RemoteControl.call(project, Api.Local, :broadcast, [message]) + RemoteControl.call(project, RemoteControl, :broadcast, [message]) end def reindex(%Project{} = project) do - RemoteControl.call(project, Api.Local, :reindex, []) + RemoteControl.call(project, RemoteControl, :reindex, []) end def index_running?(%Project{} = project) do - RemoteControl.call(project, Api.Local, :index_running?, []) + RemoteControl.call(project, RemoteControl, :index_running?, []) end def resolve_entity(%Project{} = project, %Analysis{} = analysis, %Position{} = position) do - RemoteControl.call(project, Api.Local, :resolve_entity, [analysis, position]) + RemoteControl.call(project, RemoteControl, :resolve_entity, [analysis, position]) end def struct_definitions(%Project{} = project) do - RemoteControl.call(project, Api.Local, :struct_definitions, []) + RemoteControl.call(project, RemoteControl, :struct_definitions, []) end def document_symbols(%Project{} = project, %Document{} = document) do - RemoteControl.call(project, Api.Local, :document_symbols, [document]) + RemoteControl.call(project, RemoteControl, :document_symbols, [document]) end def workspace_symbols(%Project{} = project, query) do - RemoteControl.call(project, Api.Local, :workspace_symbols, [query]) + RemoteControl.call(project, RemoteControl, :workspace_symbols, [query]) end end diff --git a/apps/remote_control/lib/lexical/remote_control/api/local.ex b/apps/remote_control/lib/lexical/remote_control/api/local.ex deleted file mode 100644 index bb1fd4a63..000000000 --- a/apps/remote_control/lib/lexical/remote_control/api/local.ex +++ /dev/null @@ -1,51 +0,0 @@ -defmodule Lexical.RemoteControl.Api.Local do - alias Lexical.RemoteControl - alias Lexical.RemoteControl.Api.Proxy - alias Lexical.RemoteControl.CodeAction - alias Lexical.RemoteControl.CodeIntelligence - - defdelegate schedule_compile(force?), to: Proxy - - defdelegate compile_document(document), to: Proxy - - defdelegate format(document), to: Proxy - - defdelegate reindex, to: Proxy - - defdelegate index_running?, to: Proxy - - defdelegate expand_alias(segments_or_module, analysis, position), to: RemoteControl.Analyzer - - defdelegate list_modules, to: :code, as: :all_available - - defdelegate code_actions(document, range, diagnostics, kinds), to: CodeAction, as: :for_range - - defdelegate complete(env), to: RemoteControl.Completion, as: :elixir_sense_expand - - defdelegate complete_struct_fields(analysis, position), - to: RemoteControl.Completion, - as: :struct_fields - - defdelegate definition(document, position), to: CodeIntelligence.Definition - - defdelegate references(analysis, position, include_definitions?), - to: CodeIntelligence.References - - defdelegate modules_with_prefix(prefix), to: RemoteControl.Modules, as: :with_prefix - - defdelegate modules_with_prefix(prefix, predicate), to: RemoteControl.Modules, as: :with_prefix - - defdelegate docs(module, opts \\ []), to: CodeIntelligence.Docs, as: :for_module - - defdelegate register_listener(listener_pid, message_types), to: RemoteControl.Dispatch - - defdelegate broadcast(message), to: RemoteControl.Dispatch - - defdelegate resolve_entity(analysis, position), to: CodeIntelligence.Entity, as: :resolve - - defdelegate struct_definitions, to: CodeIntelligence.Structs, as: :for_project - - defdelegate document_symbols(document), to: CodeIntelligence.Symbols, as: :for_document - - defdelegate workspace_symbols(query), to: CodeIntelligence.Symbols, as: :for_workspace -end diff --git a/apps/remote_control/lib/lexical/remote_control/build/state.ex b/apps/remote_control/lib/lexical/remote_control/build/state.ex index 97dbaa0c5..44883068b 100644 --- a/apps/remote_control/lib/lexical/remote_control/build/state.ex +++ b/apps/remote_control/lib/lexical/remote_control/build/state.ex @@ -3,7 +3,6 @@ defmodule Lexical.RemoteControl.Build.State do alias Lexical.Document alias Lexical.Project alias Lexical.RemoteControl - alias Lexical.RemoteControl.Api alias Lexical.RemoteControl.Api.Messages alias Lexical.RemoteControl.Build alias Lexical.RemoteControl.Plugin @@ -74,7 +73,7 @@ defmodule Lexical.RemoteControl.Build.State do compile_requested_message = project_compile_requested(project: project, build_number: state.build_number) - Api.Local.broadcast(compile_requested_message) + RemoteControl.broadcast(compile_requested_message) {elapsed_us, result} = :timer.tc(fn -> Build.Project.compile(project, initial?) end) elapsed_ms = to_ms(elapsed_us) @@ -103,8 +102,8 @@ defmodule Lexical.RemoteControl.Build.State do diagnostics: diagnostics ) - Api.Local.broadcast(compile_message) - Api.Local.broadcast(diagnostics_message) + RemoteControl.broadcast(compile_message) + RemoteControl.broadcast(diagnostics_message) Plugin.diagnose(project, state.build_number) end) end @@ -121,7 +120,7 @@ defmodule Lexical.RemoteControl.Build.State do project = state.project Build.with_lock(fn -> - Api.Local.broadcast(file_compile_requested(uri: document.uri)) + RemoteControl.broadcast(file_compile_requested(uri: document.uri)) safe_compile_func = fn -> RemoteControl.Mix.in_project(fn _ -> Build.Document.compile(document) end) @@ -166,8 +165,8 @@ defmodule Lexical.RemoteControl.Build.State do diagnostics: List.wrap(diagnostics) ) - Api.Local.broadcast(compile_message) - Api.Local.broadcast(diagnostics) + RemoteControl.broadcast(compile_message) + RemoteControl.broadcast(diagnostics) Plugin.diagnose(project, state.build_number, document) end) end diff --git a/apps/remote_control/lib/lexical/remote_control/commands/reindex.ex b/apps/remote_control/lib/lexical/remote_control/commands/reindex.ex index 308602b88..45798e5d9 100644 --- a/apps/remote_control/lib/lexical/remote_control/commands/reindex.ex +++ b/apps/remote_control/lib/lexical/remote_control/commands/reindex.ex @@ -147,7 +147,7 @@ defmodule Lexical.RemoteControl.Commands.Reindex do end defp do_reindex(%Project{} = project) do - Api.Local.broadcast(project_reindex_requested(project: project)) + RemoteControl.broadcast(project_reindex_requested(project: project)) {elapsed_us, result} = :timer.tc(fn -> @@ -156,7 +156,7 @@ defmodule Lexical.RemoteControl.Commands.Reindex do end end) - Api.Local.broadcast( + RemoteControl.broadcast( project_reindexed(project: project, elapsed_ms: round(elapsed_us / 1000), status: :success) ) diff --git a/apps/remote_control/lib/lexical/remote_control/compilation/tracer.ex b/apps/remote_control/lib/lexical/remote_control/compilation/tracer.ex index 5d30afe64..e779be3d5 100644 --- a/apps/remote_control/lib/lexical/remote_control/compilation/tracer.ex +++ b/apps/remote_control/lib/lexical/remote_control/compilation/tracer.ex @@ -1,6 +1,5 @@ defmodule Lexical.RemoteControl.Compilation.Tracer do alias Lexical.RemoteControl - alias Lexical.RemoteControl.Api alias Lexical.RemoteControl.Build alias Lexical.RemoteControl.Module.Loader @@ -9,7 +8,7 @@ defmodule Lexical.RemoteControl.Compilation.Tracer do def trace({:on_module, module_binary, _filename}, %Macro.Env{} = env) do message = extract_module_updated(env.module, module_binary, env.file) maybe_report_progress(env.file) - Api.Local.broadcast(message) + RemoteControl.broadcast(message) :ok end @@ -61,7 +60,7 @@ defmodule Lexical.RemoteControl.Compilation.Tracer do if Path.extname(file) == ".ex" do file |> progress_message() - |> Api.Local.broadcast() + |> RemoteControl.broadcast() end end diff --git a/apps/remote_control/lib/lexical/remote_control/module_mappings.ex b/apps/remote_control/lib/lexical/remote_control/module_mappings.ex index c64c2cb4e..9a3218b2b 100644 --- a/apps/remote_control/lib/lexical/remote_control/module_mappings.ex +++ b/apps/remote_control/lib/lexical/remote_control/module_mappings.ex @@ -39,7 +39,7 @@ defmodule Lexical.RemoteControl.ModuleMappings do end end - alias Lexical.RemoteControl.Api + alias Lexical.RemoteControl alias Lexical.RemoteControl.Api.Messages use GenServer @@ -67,7 +67,7 @@ defmodule Lexical.RemoteControl.ModuleMappings do @impl GenServer def init(_) do - Api.Local.register_listener(self(), [module_updated()]) + RemoteControl.register_listener(self(), [module_updated()]) {:ok, State.new()} end diff --git a/apps/remote_control/lib/lexical/remote_control/plugin.ex b/apps/remote_control/lib/lexical/remote_control/plugin.ex index de7d2afc1..f99fdba5f 100644 --- a/apps/remote_control/lib/lexical/remote_control/plugin.ex +++ b/apps/remote_control/lib/lexical/remote_control/plugin.ex @@ -1,11 +1,11 @@ defmodule Lexical.RemoteControl.Plugin do alias Lexical.Document alias Lexical.Project + alias Lexical.RemoteControl alias Lexical.RemoteControl.Api.Messages alias Lexical.RemoteControl.Plugin.Runner import Messages - alias Lexical.RemoteControl.Api def diagnose(%Project{} = project, build_number) do on_complete = fn @@ -20,7 +20,7 @@ defmodule Lexical.RemoteControl.Plugin do diagnostics: diagnostics ) - Api.Local.broadcast(message) + RemoteControl.broadcast(message) end Runner.diagnose(project, on_complete) @@ -40,7 +40,7 @@ defmodule Lexical.RemoteControl.Plugin do diagnostics: diagnostics ) - Api.Local.broadcast(message) + RemoteControl.broadcast(message) end Runner.diagnose(document, on_complete) diff --git a/apps/remote_control/lib/lexical/remote_control/progress.ex b/apps/remote_control/lib/lexical/remote_control/progress.ex index 1ecae009d..d64c4df08 100644 --- a/apps/remote_control/lib/lexical/remote_control/progress.ex +++ b/apps/remote_control/lib/lexical/remote_control/progress.ex @@ -1,5 +1,5 @@ defmodule Lexical.RemoteControl.Progress do - alias Lexical.RemoteControl.Api + alias Lexical.RemoteControl import Lexical.RemoteControl.Api.Messages @@ -41,26 +41,26 @@ defmodule Lexical.RemoteControl.Progress do @spec begin_progress(label :: label()) :: on_complete_callback() def begin_progress(label) do - Api.Local.broadcast(project_progress(label: label, stage: :begin)) + RemoteControl.broadcast(project_progress(label: label, stage: :begin)) fn -> - Api.Local.broadcast(project_progress(label: label, stage: :complete)) + RemoteControl.broadcast(project_progress(label: label, stage: :complete)) end end @spec begin_percent(label(), pos_integer()) :: {report_progress_callback(), on_complete_callback()} def begin_percent(label, max) do - Api.Local.broadcast(percent_progress(label: label, max: max, stage: :begin)) + RemoteControl.broadcast(percent_progress(label: label, max: max, stage: :begin)) report_progress = fn delta, message -> - Api.Local.broadcast( + RemoteControl.broadcast( percent_progress(label: label, message: message, delta: delta, stage: :report) ) end complete = fn -> - Api.Local.broadcast(percent_progress(label: label, stage: :complete)) + RemoteControl.broadcast(percent_progress(label: label, stage: :complete)) end {report_progress, complete} diff --git a/apps/remote_control/lib/lexical/remote_control/search/store.ex b/apps/remote_control/lib/lexical/remote_control/search/store.ex index e0c9686ec..0ad31fb2f 100644 --- a/apps/remote_control/lib/lexical/remote_control/search/store.ex +++ b/apps/remote_control/lib/lexical/remote_control/search/store.ex @@ -4,6 +4,7 @@ defmodule Lexical.RemoteControl.Search.Store do """ alias Lexical.Project + alias Lexical.RemoteControl alias Lexical.RemoteControl.Api alias Lexical.RemoteControl.Search.Indexer.Entry alias Lexical.RemoteControl.Search.Store @@ -129,7 +130,7 @@ defmodule Lexical.RemoteControl.Search.Store do # we have this bandaid. We wait for the first compilation to complete, and then # the search store enables itself, at which point we index the code. - Api.Local.register_listener(self(), project_compiled()) + RemoteControl.register_listener(self(), project_compiled()) state = State.new(project, create_index, update_index, backend) {:ok, state} end