Skip to content

Commit

Permalink
Moved Api.Local functions to RemoteControl directly
Browse files Browse the repository at this point in the history
  • Loading branch information
scohen committed May 21, 2024
1 parent 355e046 commit 4c93891
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 103 deletions.
62 changes: 56 additions & 6 deletions apps/remote_control/lib/lexical/remote_control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}
Expand All @@ -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 != ".",
Expand Down
48 changes: 26 additions & 22 deletions apps/remote_control/lib/lexical/remote_control/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -25,19 +24,19 @@ 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
])
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(
Expand All @@ -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(
Expand All @@ -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?
Expand All @@ -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
51 changes: 0 additions & 51 deletions apps/remote_control/lib/lexical/remote_control/api/local.ex

This file was deleted.

13 changes: 6 additions & 7 deletions apps/remote_control/lib/lexical/remote_control/build/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
Loading

0 comments on commit 4c93891

Please sign in to comment.