forked from erlang-ls/erlang_ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[erlang-ls#108] Add codeLens, codeAction and executeCommand plumbing.
- Loading branch information
Showing
8 changed files
with
249 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
-module(els_code_action_provider). | ||
|
||
-behaviour(els_provider). | ||
|
||
-export([ handle_request/2 | ||
, is_enabled/0 | ||
, options/0 | ||
]). | ||
|
||
-include("erlang_ls.hrl"). | ||
|
||
%%============================================================================== | ||
%% els_provider functions | ||
%%============================================================================== | ||
|
||
-spec is_enabled() -> boolean(). | ||
is_enabled() -> true. | ||
|
||
%% /** | ||
%% * The server provides code actions. The `CodeActionOptions` return type is | ||
%% * only valid if the client signals code action literal support via the | ||
%% * property `textDocument.codeAction.codeActionLiteralSupport`. | ||
%% */ | ||
%% codeActionProvider?: boolean | CodeActionOptions; | ||
|
||
-spec options() -> boolean() | map(). | ||
options() -> | ||
Capabilities = els_config:get(capabilities), | ||
lager:info("code_actions: [Capabilities=~p]", [Capabilities]), | ||
true. | ||
|
||
-spec handle_request(any(), els_provider:state()) -> | ||
{any(), els_provider:state()}. | ||
handle_request({document_codeaction, Params}, State) -> | ||
#{ <<"textDocument">> := #{ <<"uri">> := Uri} | ||
, <<"range">> := RangeLSP | ||
, <<"context">> := Context } = Params, | ||
Result = code_actions(Uri, RangeLSP, Context), | ||
{Result, State}. | ||
|
||
%%============================================================================== | ||
%% Internal Functions | ||
%%============================================================================== | ||
|
||
|
||
%% result: (Command | CodeAction)[] | null where CodeAction is defined | ||
%% as follows: | ||
-spec code_actions(uri(), range(), code_action_context()) -> [map()]. | ||
code_actions(_Uri, Range, _Context) -> | ||
#{ <<"start">> := #{ <<"character">> := _StartCol | ||
, <<"line">> := StartLine } | ||
, <<"end">> := _End | ||
} = Range, | ||
%% {ok, _Document} = els_utils:lookup_document(Uri), | ||
Actions = [add_lines_action( <<"Add TODO comment">> | ||
, <<"%% TODO: something\n">> | ||
, StartLine)], | ||
lager:info("actions: [Actions=~p]", [Actions]), | ||
Actions. | ||
|
||
|
||
%% TODO: either provide a literal workspaceedit, or a command, based | ||
%% on the `textDocument.codeAction.codeActionLiteralSupport` client | ||
%% capability. | ||
-spec add_lines_action(binary(), binary(), number()) -> code_action(). | ||
add_lines_action(Title, Lines, Before) -> | ||
#{ title => Title | ||
, command => | ||
els_utils:make_command( Title | ||
, <<"add-lines">> | ||
, [#{ lines => Lines | ||
, before => Before }]) | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
-module(els_code_lens_provider). | ||
|
||
-behaviour(els_provider). | ||
|
||
-export([ handle_request/2 | ||
, is_enabled/0 | ||
, options/0 | ||
]). | ||
|
||
-include("erlang_ls.hrl"). | ||
|
||
%%============================================================================== | ||
%% els_provider functions | ||
%%============================================================================== | ||
|
||
-spec is_enabled() -> boolean(). | ||
is_enabled() -> true. | ||
|
||
-spec options() -> map(). | ||
options() -> | ||
#{ resolveProvider => false }. | ||
|
||
-spec handle_request(any(), els_provider:state()) -> | ||
{any(), els_provider:state()}. | ||
handle_request({document_codelens, Params}, State) -> | ||
#{ <<"textDocument">> := #{ <<"uri">> := Uri}} = Params, | ||
Lenses = lenses(Uri), | ||
{Lenses, State}. | ||
|
||
%%============================================================================== | ||
%% Internal Functions | ||
%%============================================================================== | ||
-spec lenses(uri()) -> [map()]. | ||
lenses(Uri) -> | ||
{ok, _Document} = els_utils:lookup_document(Uri), | ||
Lenses = [#{ range => one_line_range(3) | ||
, command => | ||
els_utils:make_command(<<"Foo Title">> | ||
, <<"foo command">>, [])}], | ||
lager:info("lenses: [Lenses=~p]", [Lenses]), | ||
Lenses. | ||
|
||
-spec one_line_range(non_neg_integer()) -> range(). | ||
one_line_range(Line) -> | ||
Range = #{from => {Line, 1}, to => {Line + 1, 1}}, | ||
els_protocol:range(Range). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
-module(els_execute_command_provider). | ||
|
||
-behaviour(els_provider). | ||
|
||
-export([ handle_request/2 | ||
, is_enabled/0 | ||
, options/0 | ||
]). | ||
|
||
-include("erlang_ls.hrl"). | ||
|
||
%%============================================================================== | ||
%% els_provider functions | ||
%%============================================================================== | ||
|
||
-spec is_enabled() -> boolean(). | ||
is_enabled() -> true. | ||
|
||
-spec options() -> map(). | ||
options() -> | ||
#{ commands => ["foo"] }. | ||
|
||
-spec handle_request(any(), els_provider:state()) -> | ||
{any(), els_provider:state()}. | ||
handle_request({workspace_executecommand, Params}, State) -> | ||
#{ <<"command">> := Command } = Params, | ||
Arguments = maps:get(<<"arguments">>, Params, []), | ||
Result = execute_command(Command, Arguments), | ||
{Result, State}. | ||
|
||
%%============================================================================== | ||
%% Internal Functions | ||
%%============================================================================== | ||
|
||
%% TODO: the actual commands should be in some sort of registry, | ||
%% together with their handlers, so that we guarantee that any command | ||
%% sent to the client is actually valid. | ||
|
||
-spec execute_command(string(), [any()]) -> [map()]. | ||
execute_command(<<"add-lines">>, [#{ <<"lines">> := _Lines | ||
, <<"before">> := _Before }] = Arguments) -> | ||
lager:info("execute_command:add-lines [Arguments=~p]", [Arguments]), | ||
[]; | ||
execute_command(Command, Arguments) -> | ||
lager:info("execute_command: [Command=~p] [Arguments=~p]" | ||
, [Command, Arguments]), | ||
[]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters