Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safer incremental sync #1222

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions apps/els_lsp/src/els_index_buffer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
, stop/0
, apply_edits_async/2
, flush/1
, load/2
]).

-include_lib("kernel/include/logger.hrl").
Expand Down Expand Up @@ -42,6 +43,15 @@ apply_edits_async(Uri, Edits) ->
?SERVER ! {apply_edits, Uri, Edits},
ok.

-spec load(uri(), binary()) -> ok.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow up, we should probably convert this to a gen server.

load(Uri, Text) ->
Ref = make_ref(),
?SERVER ! {load, self(), Ref, Uri, Text},
receive
{Ref, done} ->
ok
end.

-spec flush(uri()) -> ok.
flush(Uri) ->
Ref = make_ref(),
Expand Down Expand Up @@ -71,6 +81,15 @@ loop() ->
[?SERVER, Uri, {E, R, St}])
end,
Pid ! {Ref, done},
loop();
{load, Pid, Ref, Uri, Text} ->
try
do_load(Uri, Text)
catch E:R:St ->
?LOG_ERROR("[~p] Crashed while loading ~p: ~p",
[?SERVER, Uri, {E, R, St}])
end,
Pid ! {Ref, done},
loop()
end.

Expand All @@ -97,6 +116,17 @@ do_flush(Uri) ->
[?SERVER, Uri, Duration div 1000]),
ok.

-spec do_load(uri(), binary()) -> ok.
do_load(Uri, Text) ->
?LOG_DEBUG("[~p] Loading ~p", [?SERVER, Uri]),
{Duration, ok} =
timer:tc(fun() ->
els_indexing:index(Uri, Text, 'deep')
end),
?LOG_DEBUG("[~p] Done load ~p [duration: ~pms]",
[?SERVER, Uri, Duration div 1000]),
ok.

-spec receive_all(uri(), binary()) -> binary().
receive_all(Uri, Text0) ->
receive
Expand Down
13 changes: 7 additions & 6 deletions apps/els_lsp/src/els_text_synchronization.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ did_change(Params) ->

-spec did_open(map()) -> ok.
did_open(Params) ->
TextDocument = maps:get(<<"textDocument">>, Params),
Uri = maps:get(<<"uri">> , TextDocument),
Text = maps:get(<<"text">> , TextDocument),
ok = els_indexing:index(Uri, Text, 'deep'),
#{<<"textDocument">> := #{ <<"uri">> := Uri
, <<"text">> := Text}} = Params,
ok = els_index_buffer:load(Uri, Text),
ok = els_index_buffer:flush(Uri),
Provider = els_diagnostics_provider,
els_provider:handle_request(Provider, {run_diagnostics, Params}),
ok.

-spec did_save(map()) -> ok.
did_save(Params) ->
TextDocument = maps:get(<<"textDocument">>, Params),
Uri = maps:get(<<"uri">> , TextDocument),
#{<<"textDocument">> := #{<<"uri">> := Uri}} = Params,
{ok, Text} = file:read_file(els_uri:path(Uri)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how this play with different editors. Both in Emacs and VS Code, if the current version of the "buffer" is different from disk, you get a warning. Maybe it doesn't matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be fine as if we get didSave my assumption is that it must mean that the file has been written to disk.

ok = els_index_buffer:load(Uri, Text),
ok = els_index_buffer:flush(Uri),
Provider = els_diagnostics_provider,
els_provider:handle_request(Provider, {run_diagnostics, Params}),
Expand Down