Skip to content

Commit

Permalink
Try integrate proxy instead of multiple maybe_xxx method
Browse files Browse the repository at this point in the history
  • Loading branch information
scottming committed May 25, 2024
1 parent 4d9b00f commit 66591aa
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 77 deletions.
4 changes: 4 additions & 0 deletions apps/remote_control/lib/lexical/remote_control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ defmodule Lexical.RemoteControl do

defdelegate workspace_symbols(query), to: CodeIntelligence.Symbols, as: :for_workspace

defdelegate maybe_update_rename_progress(triggered_message),
to: RemoteControl.Commands.Rename,
as: :update_progress

def start_link(%Project{} = project) do
:ok = ensure_epmd_started()
start_net_kernel(project)
Expand Down
17 changes: 5 additions & 12 deletions apps/remote_control/lib/lexical/remote_control/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Lexical.RemoteControl.Api do
alias Lexical.Project
alias Lexical.RemoteControl
alias Lexical.RemoteControl.CodeIntelligence
alias Lexical.RemoteControl.CodeMod

require Logger

Expand All @@ -18,18 +19,6 @@ defmodule Lexical.RemoteControl.Api do
RemoteControl.call(project, RemoteControl, :compile_document, [document])
end

def maybe_schedule_compile(project, triggered_message) do
RemoteControl.call(project, Build, :maybe_schedule_compile, [triggered_message])
end

def maybe_compile_document(project, document, updated_message) do
RemoteControl.call(project, Build, :maybe_compile_document, [
project,
document,
updated_message
])
end

def expand_alias(
%Project{} = project,
segments_or_module,
Expand Down Expand Up @@ -89,6 +78,10 @@ defmodule Lexical.RemoteControl.Api do
])
end

def maybe_update_rename_progress(project, updated_message) do
RemoteControl.call(project, RemoteControl, :maybe_update_rename_progress, [updated_message])
end

def complete(%Project{} = project, %Env{} = env) do
Logger.info("Completion for #{inspect(env.position)}")
RemoteControl.call(project, RemoteControl, :complete, [env])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule Lexical.RemoteControl.Application do
[
RemoteControl.Api.Proxy,
{RemoteControl.Commands.Reindex, nil},
RemoteControl.Commands.Rename,
RemoteControl.Module.Loader,
{RemoteControl.Dispatch, progress: true},
RemoteControl.ModuleMappings,
Expand Down
17 changes: 0 additions & 17 deletions apps/remote_control/lib/lexical/remote_control/build.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule Lexical.RemoteControl.Build do
alias Lexical.RemoteControl
alias Lexical.RemoteControl.Build.Document.Compilers.HEEx
alias Lexical.RemoteControl.Build.State
alias Lexical.RemoteControl.Commands.Rename
alias Lexical.VM.Versions

require Logger
Expand Down Expand Up @@ -36,22 +35,6 @@ defmodule Lexical.RemoteControl.Build do
:ok
end

def maybe_schedule_compile(message) do
if Rename.in_progress?() do
Rename.update_progress(message)
else
GenServer.cast(__MODULE__, {:compile, false})
end
end

def maybe_compile_document(%Project{} = project, %Document{} = document, message) do
if Rename.in_progress?() do
Rename.update_progress(message)
else
compile_document(project, document)
end
end

# this is for testing
def force_compile_document(%Project{} = project, %Document{} = document) do
with false <- Path.absname(document.path) == "mix.exs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule Lexical.RemoteControl.CodeMod.Format do
alias Lexical.RemoteControl
alias Lexical.RemoteControl.Build
alias Lexical.RemoteControl.CodeMod.Diff
alias Lexical.RemoteControl.Commands.Rename

require Logger

Expand Down Expand Up @@ -100,22 +99,13 @@ defmodule Lexical.RemoteControl.CodeMod.Format do
def edits(%Document{} = document) do
project = RemoteControl.get_project()

with :ok <- ensure_not_renaming(),
:ok <- Build.compile_document(project, document),
with :ok <- Build.compile_document(project, document),
{:ok, formatted} <- do_format(project, document) do
edits = Diff.diff(document, formatted)
{:ok, Changes.new(document, edits)}
end
end

defp ensure_not_renaming do
if Rename.in_progress?() do
{:error, :rename_in_progress}
else
:ok
end
end

defp do_format(%Project{} = project, %Document{} = document) do
project_path = Project.project_path(project)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ defmodule Lexical.RemoteControl.CodeMod.Rename do
progress_notification_functions =
Progress.begin_percent("Renaming", Enum.count(uri_with_expected_operation))

Commands.Rename.set_rename_progress(
uri_with_expected_operation,
progress_notification_functions
)
Commands.Rename.start_link([uri_with_expected_operation, progress_notification_functions])
end

defp uri_with_expected_operation(client_name, document_changes_list)
Expand Down
50 changes: 30 additions & 20 deletions apps/remote_control/lib/lexical/remote_control/commands/rename.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,17 @@ defmodule Lexical.RemoteControl.Commands.Rename do
end
end

alias Lexical.RemoteControl.Api.Proxy
use GenServer

def start_link(_) do
GenServer.start_link(__MODULE__, %State{}, name: __MODULE__)
def start_link([uri_with_expected_operation, progress_functions]) do
state = State.new(uri_with_expected_operation, progress_functions)
GenServer.start_link(__MODULE__, state, name: __MODULE__)
end

@impl true
def init(state) do
{:ok, state}
end

@spec set_rename_progress(
%{Lexical.uri() => Messages.file_changed() | Messages.file_saved()},
{function(), function()}
) :: :ok
def set_rename_progress(uri_with_expected_operation, progress_functions) do
GenServer.cast(
__MODULE__,
{:set_rename_progress, uri_with_expected_operation, progress_functions}
)
end

def in_progress? do
GenServer.call(__MODULE__, :in_progress?)
{:ok, state, {:continue, :start_buffering}}
end

@spec update_progress(Messages.file_changed() | Messages.file_saved()) :: :ok
Expand All @@ -92,7 +79,25 @@ defmodule Lexical.RemoteControl.Commands.Rename do
# Instead, it should call this function to synchronously update the status,
# thus preventing failures due to latency issues.
def update_progress(message) do
GenServer.cast(__MODULE__, {:update_progress, message})
if in_progress?() do
GenServer.cast(__MODULE__, {:update_progress, message})
else
:ok
end
end

def in_progress? do
if Process.whereis(__MODULE__) do
GenServer.call(__MODULE__, :in_progress?)
else
false
end
end

@impl true
def handle_continue(:start_buffering, state) do
Proxy.start_buffering(self())
{:noreply, state}
end

@impl true
Expand All @@ -103,7 +108,12 @@ defmodule Lexical.RemoteControl.Commands.Rename do
@impl true
def handle_cast({:update_progress, message}, state) do
new_state = State.update_progress(state, message)
{:noreply, new_state}

if State.in_progress?(new_state) do
{:noreply, new_state}
else
{:stop, :normal, new_state}
end
end

@impl true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Lexical.RemoteControl.Commands.RenameTest do
alias Lexical.RemoteControl.Api.Proxy
alias Lexical.RemoteControl.Commands.Rename

import Lexical.RemoteControl.Api.Messages
Expand All @@ -7,36 +8,42 @@ defmodule Lexical.RemoteControl.Commands.RenameTest do
use Patch

setup do
start_supervised!(Rename)
pid = self()

progress_funcs =
{fn delta, message -> update_progress(pid, delta, message) end,
fn -> complete_progress(pid) end}

patch(Proxy, :start_buffering, :ok)
%{progress_funcs: progress_funcs}
end

test "it should mark the `in_progress` as `true` when a rename is in progress.", %{
progress_funcs: progress_funcs
} do
uri = "file://file.ex"
assert :ok = Rename.set_rename_progress(%{uri => file_changed(uri: uri)}, progress_funcs)
init_opts = [%{uri => file_changed(uri: uri)}, progress_funcs]
pid = start_supervised!({Rename, init_opts})

assert Rename.in_progress?()
assert_called(Proxy.start_buffering(^pid))
end

test "it should mark the `in_progress` as false when a rename is done", %{
progress_funcs: progress_funcs
} do
test "it should mark the `in_progress` as false and shutdown the process when a rename is done",
%{
progress_funcs: progress_funcs
} do
file_uri = "file://file.ex"
init_opts = [%{file_uri => file_saved(uri: file_uri)}, progress_funcs]
start_supervised!({Rename, init_opts})

Rename.set_rename_progress(%{file_uri => file_saved(uri: file_uri)}, progress_funcs)
Rename.update_progress(file_saved(uri: file_uri))

assert_receive {:update_progress, 1, ""}
assert_receive :complete_progress

refute Rename.in_progress?()
refute Process.whereis(Rename)
end

test "it should still in progress if there are files yet to be saved.", %{
Expand All @@ -45,19 +52,23 @@ defmodule Lexical.RemoteControl.Commands.RenameTest do
uri1 = "file://file1.ex"
uri2 = "file://file2.ex"

Rename.set_rename_progress(
init_opts = [
%{uri1 => file_changed(uri: uri1), uri2 => file_saved(uri: uri2)},
progress_funcs
)
]

Rename.update_progress(file_changed(uri: uri1))
start_supervised!({Rename, init_opts})

Rename.update_progress(file_changed(uri: uri1))
assert_receive {:update_progress, 1, ""}

refute_receive :complete_progress
assert Rename.in_progress?()
end

test "it should return `:ok` when updating the progress if the process is not alive" do
assert :ok = Rename.update_progress(file_changed(uri: "file://file.ex"))
end

defp update_progress(pid, delta, message) do
send(pid, {:update_progress, delta, message})
end
Expand Down
6 changes: 4 additions & 2 deletions apps/server/lib/lexical/server/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ defmodule Lexical.Server.State do
to_version: updated_source.version
)

Api.maybe_update_rename_progress(state.configuration.project, updated_message)
Api.compile_document(state.configuration.project, updated_source)
Api.broadcast(project, updated_message)
Api.maybe_compile_document(state.configuration.project, updated_source, updated_message)
{:ok, state}

error ->
Expand Down Expand Up @@ -213,7 +214,8 @@ defmodule Lexical.Server.State do

case Document.Store.save(uri) do
:ok ->
Api.maybe_schedule_compile(state.configuration.project, file_saved(uri: uri))
Api.maybe_update_rename_progress(state.configuration.project, file_saved(uri: uri))
Api.schedule_compile(state.configuration.project, false)

{:ok, state}

Expand Down

0 comments on commit 66591aa

Please sign in to comment.