Skip to content

Commit

Permalink
PR Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
scohen committed Sep 6, 2024
1 parent 95a03e3 commit 8227486
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
36 changes: 17 additions & 19 deletions apps/remote_control/lib/lexical/remote_control/build/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@ defmodule Lexical.RemoteControl.Build.State do
end

def on_timeout(%__MODULE__{} = state) do
case state.project_compile do
:none -> :ok
:force -> compile_project(state, true)
:normal -> compile_project(state, false)
end
new_state =
case state.project_compile do
:none -> state
:force -> compile_project(state, true)
:normal -> compile_project(state, false)
end

# We need to compile the individual documents even after the project is
# compiled because they might have unsaved changes, and we want that state
# to be the latest state of the project.
new_state =
state.uri_to_document
|> Map.values()
|> Enum.reduce(
state,
fn document, state ->
new_state = increment_build_number(state)
compile_file(new_state, document)
new_state
end
)
Enum.reduce(new_state.uri_to_document, state, fn {_uri, document}, state ->
compile_file(state, document)
end)

%__MODULE__{new_state | uri_to_document: %{}, project_compile: :none}
end
Expand All @@ -60,10 +57,6 @@ defmodule Lexical.RemoteControl.Build.State do
end
end

def compile_scheduled?(%__MODULE__{} = state, uri) do
Map.has_key?(state.uri_to_document, uri)
end

def ensure_build_directory(%__MODULE__{} = state) do
# If the project directory isn't there, for some reason the main build fails, so we create it here
# to ensure that the build will succeed.
Expand Down Expand Up @@ -127,9 +120,12 @@ defmodule Lexical.RemoteControl.Build.State do
RemoteControl.broadcast(diagnostics_message)
Plugin.diagnose(project, state.build_number)
end)

state
end

def compile_file(%__MODULE__{} = state, %Document{} = document) do
state = increment_build_number(state)
project = state.project

Build.with_lock(fn ->
Expand Down Expand Up @@ -182,6 +178,8 @@ defmodule Lexical.RemoteControl.Build.State do
RemoteControl.broadcast(diagnostics)
Plugin.diagnose(project, state.build_number, document)
end)

state
end

def set_compiler_options do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ defmodule Lexical.RemoteControl.Build.StateTest do
setup [:with_metadata_project, :with_a_valid_document, :with_patched_compilation]

test "it doesn't compile immediately", %{state: state, document: document} do
new_state = State.on_file_compile(state, document)
State.on_file_compile(state, document)

assert State.compile_scheduled?(new_state, document.uri)
refute_called(Build.Document.compile(document))
refute_called(Build.Project.compile(_, _))
end
Expand Down

0 comments on commit 8227486

Please sign in to comment.