Skip to content

Commit

Permalink
Respect appdirs when prioritizing module candidates. (#1418)
Browse files Browse the repository at this point in the history
This might be helpful in situations when tooling (e.g. `rebar3`) makes
"shadow copies" in the indexable project directories. Before this
change files having `_build/` in filepath were preferred over those
having `apps/` or `src/` for example.
  • Loading branch information
keynslug committed Dec 21, 2023
1 parent 7163398 commit a4047b7
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions apps/els_core/src/els_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,30 @@ cmd_receive(Port) ->
-spec prioritize_uris([uri()]) -> [uri()].
prioritize_uris(Uris) ->
Root = els_config:get(root_uri),
Order = fun
(nomatch) ->
3;
(Cont) ->
case string:find(Cont, "/src/") of
nomatch -> 1;
_ -> 0
end
end,
Prio = [{Order(string:prefix(Uri, Root)), Uri} || Uri <- Uris],
AppsPaths = els_config:get(apps_paths),
Prio = [{score_uri(Uri, Root, AppsPaths), Uri} || Uri <- Uris],
[Uri || {_, Uri} <- lists:sort(Prio)].

-spec score_uri(uri(), uri(), [file:name()]) -> tuple().
score_uri(Uri, RootUri, AppsPaths) ->
Path = els_uri:path(Uri),
Prefix = string:prefix(Uri, RootUri),
%% prefer files under project root
S1 =
case Prefix of
nomatch -> 1;
_Rest -> 0
end,
%% among those, prefer files under some project app directory (e.g.
%% deprioritize dependencies and shadow copies)
S2 = length([
AP
|| S1 == 0,
AP <- AppsPaths,
string:prefix(Path, AP) /= nomatch
]),
{S1, -S2}.

%%==============================================================================
%% This section excerpted from the rebar3 sources, rebar_dir.erl
%% Pending resolution of https://github.com/erlang/rebar3/issues/2223
Expand Down

0 comments on commit a4047b7

Please sign in to comment.