Skip to content

Commit 54d00d9

Browse files
committed
Add code action to export function
1 parent 51c877d commit 54d00d9

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

apps/els_lsp/src/els_code_action_provider.erl

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ make_code_actions(
6262
{"function (.*) undefined", fun els_code_actions:suggest_function/4},
6363
{"Cannot find definition for function (.*)", fun els_code_actions:suggest_function/4},
6464
{"Cannot find module (.*)", fun els_code_actions:suggest_module/4},
65+
{"Function (.+):(.+) is not exported.",
66+
fun els_code_actions:export_external_function/4},
6567
{"Unused file: (.*)", fun els_code_actions:remove_unused/4},
6668
{"Atom typo\\? Did you mean: (.*)", fun els_code_actions:fix_atom_typo/4},
6769
{"undefined callback function (.*) \\\(behaviour '(.*)'\\\)",

apps/els_lsp/src/els_code_actions.erl

+27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
extract_function/2,
44
create_function/4,
55
export_function/4,
6+
export_external_function/4,
67
fix_module_name/4,
78
ignore_variable/4,
89
remove_macro/4,
@@ -84,6 +85,32 @@ export_function(Uri, _Range, _Data, [UnusedFun]) ->
8485
]
8586
end.
8687

88+
-spec export_external_function(uri(), range(), binary(), [binary()]) -> [map()].
89+
export_external_function(_Uri, _Range, _Data, [ModBin, FABin]) ->
90+
Mod = binary_to_atom(ModBin, utf8),
91+
case els_utils:find_module(Mod) of
92+
{ok, Uri} ->
93+
{ok, Document} = els_utils:lookup_document(Uri),
94+
case els_poi:sort(els_dt_document:pois(Document, [module, export])) of
95+
[] ->
96+
[];
97+
POIs ->
98+
#{range := #{to := {Line, _Col}}} = lists:last(POIs),
99+
Pos = {Line + 1, 1},
100+
[
101+
make_edit_action(
102+
Uri,
103+
<<"Export ", FABin/binary>>,
104+
?CODE_ACTION_KIND_QUICKFIX,
105+
<<"-export([", FABin/binary, "]).\n">>,
106+
els_protocol:range(#{from => Pos, to => Pos})
107+
)
108+
]
109+
end;
110+
{error, not_found} ->
111+
[]
112+
end.
113+
87114
-spec ignore_variable(uri(), range(), binary(), [binary()]) -> [map()].
88115
ignore_variable(Uri, Range, _Data, [UnusedVariable]) ->
89116
{ok, Document} = els_utils:lookup_document(Uri),

apps/els_lsp/test/els_code_action_SUITE.erl

+49
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
-export([
1515
add_underscore_to_unused_var/1,
1616
export_unused_function/1,
17+
export_external_function/1,
1718
suggest_variable/1,
1819
fix_module_name/1,
1920
remove_unused_macro/1,
@@ -158,6 +159,54 @@ export_unused_function(Config) ->
158159
?assertEqual(Expected, Result),
159160
ok.
160161

162+
-spec export_external_function(config()) -> ok.
163+
export_external_function(Config) ->
164+
Uri = ?config(code_navigation_uri, Config),
165+
%% Don't care
166+
Range = els_protocol:range(#{
167+
from => {1, 1},
168+
to => {1, 2}
169+
}),
170+
Diag = #{
171+
message => <<"Function code_action:function_c/0 is not exported.">>,
172+
range => Range,
173+
severity => 2,
174+
source => <<"CrossRef">>
175+
},
176+
#{result := Result} = els_client:document_codeaction(Uri, Range, [Diag]),
177+
TargetUri = ?config(code_action_uri, Config),
178+
Expected =
179+
[
180+
#{
181+
edit => #{
182+
changes =>
183+
#{
184+
binary_to_atom(TargetUri, utf8) =>
185+
[
186+
#{
187+
range =>
188+
#{
189+
'end' => #{
190+
character => 0,
191+
line => ?COMMENTS_LINES + 3
192+
},
193+
start => #{
194+
character => 0,
195+
line => ?COMMENTS_LINES + 3
196+
}
197+
},
198+
newText => <<"-export([function_c/0]).\n">>
199+
}
200+
]
201+
}
202+
},
203+
kind => <<"quickfix">>,
204+
title => <<"Export function_c/0">>
205+
}
206+
],
207+
?assertEqual(Expected, Result),
208+
ok.
209+
161210
-spec suggest_variable(config()) -> ok.
162211
suggest_variable(Config) ->
163212
Uri = ?config(code_action_uri, Config),

0 commit comments

Comments
 (0)