Skip to content

Commit c80b72d

Browse files
authored
fix: update document store on didchange even without the engine running (#326)
We were dropping this event if the engine for that project wasn't running because it would attempt to trigger a document compilation. Skipping the document compilation is correct, but we should still be updating the document store with the new document contents.
1 parent 9e913f6 commit c80b72d

File tree

2 files changed

+76
-24
lines changed

2 files changed

+76
-24
lines changed

apps/expert/lib/expert/state.ex

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -128,30 +128,23 @@ defmodule Expert.State do
128128
projects = ActiveProjects.projects()
129129
project = Project.project_for_uri(projects, uri)
130130

131-
with true <- ActiveProjects.active?(project),
132-
{:ok, updated_source} <-
133-
Document.Store.get_and_update(
134-
uri,
135-
# TODO: this function needs to accept the GenLSP data structure
136-
&Document.apply_content_changes(&1, version, params.content_changes)
137-
) do
138-
updated_message =
139-
file_changed(
140-
uri: updated_source.uri,
141-
open?: true,
142-
from_version: version,
143-
to_version: updated_source.version
144-
)
145-
146-
EngineApi.broadcast(project, updated_message)
147-
EngineApi.compile_document(project, updated_source)
148-
{:ok, state}
149-
else
150-
false ->
151-
GenLSP.info(
152-
Expert.get_lsp(),
153-
"Received request textDocument/didChange before engine for #{Project.name(project)} was initialized. Ignoring."
154-
)
131+
case Document.Store.get_and_update(
132+
uri,
133+
&Document.apply_content_changes(&1, version, params.content_changes)
134+
) do
135+
{:ok, updated_source} ->
136+
updated_message =
137+
file_changed(
138+
uri: updated_source.uri,
139+
open?: true,
140+
from_version: version,
141+
to_version: updated_source.version
142+
)
143+
144+
if ActiveProjects.active?(project) do
145+
EngineApi.broadcast(project, updated_message)
146+
EngineApi.compile_document(project, updated_source)
147+
end
155148

156149
{:ok, state}
157150

apps/expert/test/expert/expert_test.exs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,4 +561,63 @@ defmodule ExpertTest do
561561
assert_project_alive?(nested_subproject)
562562
end
563563
end
564+
565+
describe "text document changes" do
566+
test "updates document store even when project engine is not active", %{
567+
client: client,
568+
project_root: project_root
569+
} do
570+
spy(Expert.EngineApi)
571+
572+
patch(Expert.Project.Supervisor, :ensure_node_started, fn _project ->
573+
{:ok, nil}
574+
end)
575+
576+
assert :ok = request(client, initialize_request(project_root, id: 1, projects: []))
577+
assert_result(1, _)
578+
579+
file_uri = Document.Path.to_uri(Path.join(project_root, "lib/test_file.ex"))
580+
initial_text = "defmodule Test do\nend"
581+
582+
assert :ok =
583+
notify(client, %{
584+
method: "textDocument/didOpen",
585+
jsonrpc: "2.0",
586+
params: %{
587+
textDocument: %{
588+
uri: file_uri,
589+
languageId: "elixir",
590+
version: 1,
591+
text: initial_text
592+
}
593+
}
594+
})
595+
596+
Process.sleep(50)
597+
598+
assert {:ok, doc} = Document.Store.fetch(file_uri)
599+
assert Document.to_string(doc) == initial_text
600+
601+
new_text = "defmodule Updated do\nend"
602+
603+
assert :ok =
604+
notify(client, %{
605+
method: "textDocument/didChange",
606+
jsonrpc: "2.0",
607+
params: %{
608+
textDocument: %{uri: file_uri, version: 2},
609+
contentChanges: [%{text: new_text}]
610+
}
611+
})
612+
613+
Process.sleep(50)
614+
615+
assert {:ok, updated_doc} = Document.Store.fetch(file_uri)
616+
assert updated_doc.version == 2
617+
assert Document.to_string(updated_doc) == new_text
618+
619+
refute_any_call(Expert.EngineApi.broadcast())
620+
refute_any_call(Expert.EngineApi.compile_document())
621+
end
622+
end
564623
end

0 commit comments

Comments
 (0)