Skip to content

Commit

Permalink
[#22] Do not expose Uri to code navigation module
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoaloi committed Aug 6, 2019
1 parent d4a0539 commit a346a6b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
27 changes: 12 additions & 15 deletions src/erlang_ls_code_navigation.erl
Expand Up @@ -34,9 +34,8 @@
%%==============================================================================

%% TODO: Specify type instead of generic map
%% TODO: goto_definition/2 should probably not take the Uri, but a path.
-spec goto_definition(uri(), erlang_ls_poi:poi()) -> null | map().
goto_definition(_Uri, #{ info := {application, {M, _F, _A}} = Info }) ->
-spec goto_definition(binary(), erlang_ls_poi:poi()) -> null | map().
goto_definition(_Filename, #{ info := {application, {M, _F, _A}} = Info }) ->
case erlang_ls_tree:annotate_file(erlang_ls_uri:filename(M), full_path()) of
{ok, Uri, AnnotatedTree} ->
case erlang_ls_poi:match(AnnotatedTree, definition(Info)) of
Expand All @@ -51,8 +50,8 @@ goto_definition(_Uri, #{ info := {application, {M, _F, _A}} = Info }) ->
{error, _Error} ->
null
end;
goto_definition(Uri, #{ info := {application, {_F, _A}} = Info }) ->
case erlang_ls_tree:annotate_file(erlang_ls_uri:basename(Uri), full_path()) of
goto_definition(Filename, #{ info := {application, {_F, _A}} = Info }) ->
case erlang_ls_tree:annotate_file(filename:basename(Filename), full_path()) of
{ok, Uri, AnnotatedTree} ->
case erlang_ls_poi:match(AnnotatedTree, definition(Info)) of
[#{ range := Range }] ->
Expand All @@ -66,17 +65,15 @@ goto_definition(Uri, #{ info := {application, {_F, _A}} = Info }) ->
{error, _Error} ->
null
end;
goto_definition(_Uri, #{ info := {behaviour, Behaviour} = Info }) ->
goto_definition(_Filename, #{ info := {behaviour, Behaviour} = Info }) ->
Filename = erlang_ls_uri:filename(Behaviour),
search(Filename, full_path(), definition(Info));
%% TODO: Eventually search everywhere and suggest a code lens to include a file
goto_definition(Uri, #{ info := {macro, _Define} = Info }) ->
Filename = erlang_ls_uri:basename(Uri),
search(Filename, app_path(), definition(Info));
goto_definition(Uri, #{ info := {record_expr, _Record} = Info }) ->
Filename = erlang_ls_uri:basename(Uri),
search(Filename, app_path(), Info);
goto_definition(_Uri, #{ info := {include, Include0} }) ->
goto_definition(Filename, #{ info := {macro, _Define} = Info }) ->
search(filename:basename(Filename), app_path(), definition(Info));
goto_definition(Filename, #{ info := {record_expr, _Record} = Info }) ->
search(filename:basename(Filename), app_path(), Info);
goto_definition(_Filename, #{ info := {include, Include0} }) ->
Include = list_to_binary(string:trim(Include0, both, [$"])),
case erlang_ls_tree:annotate_file(Include, full_path()) of
{ok, Uri, _AnnotatedTree} ->
Expand All @@ -89,7 +86,7 @@ goto_definition(_Uri, #{ info := {include, Include0} }) ->
{error, _Error} ->
null
end;
goto_definition(_Uri, #{ info := {include_lib, Include0} }) ->
goto_definition(_Filename, #{ info := {include_lib, Include0} }) ->
Include = list_to_binary(lists:last(filename:split(string:trim(Include0, both, [$"])))),
case erlang_ls_tree:annotate_file(Include, full_path()) of
{ok, Uri, _AnnotatedTree} ->
Expand All @@ -102,7 +99,7 @@ goto_definition(_Uri, #{ info := {include_lib, Include0} }) ->
{error, _Error} ->
null
end;
goto_definition(_Uri, _) ->
goto_definition(_Filename, _) ->
null.

-spec definition({atom(), any()}) -> {atom(), any()}.
Expand Down
3 changes: 2 additions & 1 deletion src/erlang_ls_server.erl
Expand Up @@ -177,7 +177,8 @@ handle_method(<<"textDocument/definition">>, Params) ->
{ok, Buffer} = erlang_ls_buffer_server:get_buffer(Uri),
case erlang_ls_buffer:get_element_at_pos(Buffer, Line + 1, Character + 1) of
[POI|_] ->
{response, erlang_ls_code_navigation:goto_definition(Uri, POI)};
Filename = erlang_ls_uri:path(Uri),
{response, erlang_ls_code_navigation:goto_definition(Filename, POI)};
[] ->
{response, null}
end;
Expand Down
9 changes: 2 additions & 7 deletions src/erlang_ls_uri.erl
Expand Up @@ -8,8 +8,7 @@
%%==============================================================================
%% Exports
%%==============================================================================
-export([ basename/1
, filename/1
-export([ filename/1
, module/1
, path/1
, uri/1
Expand All @@ -20,17 +19,13 @@
%%==============================================================================
-include("erlang_ls.hrl").

-spec basename(uri()) -> binary().
basename(Uri) ->
filename:basename(path(Uri)).

-spec filename(atom()) -> binary().
filename(Module) ->
list_to_binary(atom_to_list(Module) ++ ".erl").

-spec module(uri()) -> atom().
module(Uri) ->
binary_to_atom(filename:basename(basename(Uri), <<".erl">>), utf8).
binary_to_atom(filename:basename(path(Uri), <<".erl">>), utf8).

-spec path(uri()) -> uri_path().
path(<<"file://", Path/binary>>) ->
Expand Down

0 comments on commit a346a6b

Please sign in to comment.