Skip to content

Commit

Permalink
[#60] Use OTP path from server
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoaloi committed Sep 20, 2019
1 parent 65c64bd commit f0455f5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 57 deletions.
4 changes: 1 addition & 3 deletions src/erlang_ls_buffer_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
%%==============================================================================
-type state() :: #state{}.
-type buffer() :: pid().
-type path() :: filename:filename().
-type path() :: binary().

%%%=============================================================================
%%% API
Expand Down Expand Up @@ -121,8 +121,6 @@ handle_call({set_root_uri, Uri}, _From, State) ->
handle_call({set_otp_path, Path}, _From, State) ->
{reply, ok, State#state{otp_path = Path}}.



-spec handle_cast(any(), state()) -> {noreply, state()}.
handle_cast(_Msg, State) -> {noreply, State}.

Expand Down
10 changes: 4 additions & 6 deletions src/erlang_ls_code_navigation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
%%==============================================================================

%% API
-export([ goto_definition/2
, goto_definition/3
]).
-export([ goto_definition/2 ]).

-export([ otp_path/0
]).
Expand All @@ -27,7 +25,7 @@
-spec goto_definition(binary(), erlang_ls_poi:poi()) ->
{ok, binary(), erlang_ls_poi:range()} | {error, any()}.
goto_definition(Filename, POI) ->
goto_definition(Filename, POI, full_path()).
goto_definition(Filename, POI, include_path()).

%% TODO: Abstract pattern
-spec goto_definition(binary(), erlang_ls_poi:poi(), [string()]) ->
Expand Down Expand Up @@ -140,8 +138,8 @@ app_path() ->
, filename:join([RootPath, "include"])
].

-spec full_path() -> [string()].
full_path() ->
-spec include_path() -> [string()].
include_path() ->
lists:append( [ app_path(), otp_path() ]).

%% Look for a definition recursively in a file and its includes.
Expand Down
77 changes: 29 additions & 48 deletions test/erlang_ls_code_navigation_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ suite() ->

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
{ok, Started} = application:ensure_all_started(erlang_ls),
RootDir = code:priv_dir(erlang_ls),
AppDir = filename:join([list_to_binary(RootDir), ?TEST_APP]),
OtpPath = erlang_ls_code_navigation:otp_path(),
erlang_ls_buffer_server:set_otp_path(code:root_dir()),
erlang_ls_buffer_server:set_root_uri(erlang_ls_uri:uri(AppDir)),
[ {app_dir, AppDir}
, {include_path, [ filename:join([AppDir, "src"])
, filename:join([AppDir, "include"])
| OtpPath
]}
, {started, Started}
|Config].

-spec end_per_suite(config()) -> ok.
end_per_suite(_Config) ->
end_per_suite(Config) ->
[application:stop(App) || App <- ?config(started, Config)],
ok.

-spec init_per_testcase(atom(), config()) -> config().
Expand Down Expand Up @@ -110,143 +110,127 @@ all() ->
-spec application_local(config()) -> ok.
application_local(Config) ->
Thing = #{info => {application, {function_b, 0}}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation.erl">>, Config), FullName),
?assertEqual(#{from => {24, 0}, to => {24, 10}}, Range),
ok.

-spec application_remote(config()) -> ok.
application_remote(Config) ->
Thing = #{info => {application, {code_navigation_extra, do, 1}}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation_extra.erl">>, Config), FullName),
?assertEqual(#{from => {4, 0}, to => {4, 2}}, Range),
ok.

-spec behaviour(config()) -> ok.
behaviour(Config) ->
Thing = #{info => {behaviour, 'behaviour_a'}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"behaviour_a.erl">>, Config), FullName),
?assertEqual(#{from => {0, 1}, to => {0, 1}}, Range),
ok.

-spec export_entry(config()) -> ok.
export_entry(Config) ->
Thing = #{info => {exports_entry, {callback_a, 0}}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation.erl">>, Config), FullName),
?assertEqual(#{from => {27, 0}, to => {27, 10}}, Range),
ok.

-spec fun_local(config()) -> ok.
fun_local(Config) ->
Thing = #{info => {implicit_fun, {function_b, 0}}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation.erl">>, Config), FullName),
?assertEqual(#{from => {24, 0}, to => {24, 10}}, Range),
ok.

-spec fun_remote(config()) -> ok.
fun_remote(Config) ->
Thing = #{info => {implicit_fun, {code_navigation_extra, do, 1}}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation_extra.erl">>, Config), FullName),
?assertEqual(#{from => {4, 0}, to => {4, 2}}, Range),
ok.

-spec import_entry(config()) -> ok.
import_entry(Config) ->
Thing = #{info => {import_entry, {code_navigation_extra, do, 1}}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation_extra.erl">>, Config), FullName),
?assertEqual(#{from => {4, 0}, to => {4, 2}}, Range),
ok.

-spec include(config()) -> ok.
include(Config) ->
Thing = #{info => {include, "code_navigation.hrl"}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(include, <<"code_navigation.hrl">>, Config), FullName),
?assertEqual(#{from => {0, 0}, to => {0, 0}}, Range),
ok.

-spec include_lib(config()) -> ok.
include_lib(Config) ->
Thing = #{info => {include_lib, "code_navigation/include/code_navigation.hrl"}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(include, <<"code_navigation.hrl">>, Config), FullName),
?assertEqual(#{from => {0, 0}, to => {0, 0}}, Range),
ok.

-spec macro(config()) -> ok.
macro(Config) ->
Thing = #{info => {macro, 'MACRO_A'}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation.erl">>, Config), FullName),
?assertEqual(#{from => {17, 0}, to => {17, 0}}, Range),
ok.

-spec macro_lowercase(config()) -> ok.
macro_lowercase(Config) ->
Thing = #{info => {macro, 'macro_A'}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation.erl">>, Config), FullName),
?assertEqual(#{from => {44, 0}, to => {44, 0}}, Range),
ok.

-spec macro_included(config()) -> ok.
macro_included(Config) ->
Thing = #{info => {macro, 'INCLUDED_MACRO_A'}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(include, <<"code_navigation.hrl">>, Config), FullName),
?assertEqual(#{from => {2, 0}, to => {2, 0}}, Range),
ok.

-spec macro_with_args(config()) -> ok.
macro_with_args(Config) ->
Thing = #{info => {macro, 'MACRO_WITH_ARGS'}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation.erl">>, Config), FullName),
?assertEqual(#{from => {18, 0}, to => {18, 0}}, Range),
ok.

-spec macro_with_args_included(config()) -> ok.
macro_with_args_included(Config) ->
macro_with_args_included(_Config) ->
Thing = #{info => {macro, 'assertEqual'}},
Path = ?config(include_path, Config),
{ok, FullName, _Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, _Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(otp_app_path("stdlib", "include", "assert.hrl"), FullName),
%% Do not assert on line number to avoid binding to a specific OTP version
ok.

-spec record_access(config()) -> ok.
record_access(Config) ->
Thing = #{info => {record_access, {"record_a", "_X"}}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation.erl">>, Config), FullName),
?assertEqual(#{from => {15, 0}, to => {15, 0}}, Range),
ok.

-spec record_access_included(config()) -> ok.
record_access_included(Config) ->
Thing = #{info => {record_access, {"included_record_a", "_X"}}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(include, <<"code_navigation.hrl">>, Config), FullName),
?assertEqual(#{from => {0, 0}, to => {0, 0}}, Range),
ok.
Expand All @@ -256,26 +240,23 @@ record_access_included(Config) ->
-spec record_expr(config()) -> ok.
record_expr(Config) ->
Thing = #{info => {record_expr, "record_a"}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(src, <<"code_navigation.erl">>, Config), FullName),
?assertEqual(#{from => {15, 0}, to => {15, 0}}, Range),
ok.

-spec record_expr_included(config()) -> ok.
record_expr_included(Config) ->
Thing = #{info => {record_expr, "included_record_a"}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual(full_path(include, <<"code_navigation.hrl">>, Config), FullName),
?assertEqual(#{from => {0, 0}, to => {0, 0}}, Range),
ok.

-spec type_application(config()) -> ok.
type_application(Config) ->
Thing = #{info => {type_application, {'type_a', undefined}}},
Path = ?config(include_path, Config),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing, Path),
{ok, FullName, Range} = goto_def(<<"code_navigation.erl">>, Thing),
?assertEqual( full_path(src, <<"code_navigation.erl">>, Config)
, FullName),
?assertEqual(#{from => {36, 1}, to => {36, 1}}, Range),
Expand All @@ -300,7 +281,7 @@ otp_app_path(App, Dir, FileName) ->
, FileName
]))).

-spec goto_def(binary(), erlang_ls_poi:poi(), [string()]) ->
-spec goto_def(binary(), erlang_ls_poi:poi()) ->
{ok, binary(), erlang_ls_poi:range()} | {error, any()}.
goto_def(FileName, Thing, Path) ->
erlang_ls_code_navigation:goto_definition(FileName, Thing, Path).
goto_def(FileName, Thing) ->
erlang_ls_code_navigation:goto_definition(FileName, Thing).

0 comments on commit f0455f5

Please sign in to comment.