-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#597] Refactor code lenses, making them customizable
- Loading branch information
1 parent
ad26c42
commit efd2a30
Showing
15 changed files
with
254 additions
and
103 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
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,86 @@ | ||
%%============================================================================== | ||
%% Code Lens: Behaviour and API | ||
%%============================================================================== | ||
|
||
-module(els_code_lens). | ||
|
||
%%============================================================================== | ||
%% Callback Functions | ||
%%============================================================================== | ||
|
||
-callback command() -> els_command:command_id(). | ||
-callback is_default() -> boolean(). | ||
-callback lenses(els_dt_document:item()) -> [lens()]. | ||
|
||
%%============================================================================== | ||
%% API | ||
%%============================================================================== | ||
|
||
-export([ available_lenses/0 | ||
, default_lenses/0 | ||
, enabled_lenses/0 | ||
, lenses/2 | ||
]). | ||
|
||
%%============================================================================== | ||
%% Constructors | ||
%%============================================================================== | ||
|
||
-export([ make_lens/3 ]). | ||
|
||
%%============================================================================== | ||
%% Includes | ||
%%============================================================================== | ||
|
||
-include("erlang_ls.hrl"). | ||
|
||
%%============================================================================== | ||
%% Type Definitions | ||
%%============================================================================== | ||
|
||
-type lens() :: #{ range := range() | ||
, command => els_command:command() | ||
, data => any() | ||
}. | ||
-type lens_id() :: binary(). | ||
-export_type([ lens/0 | ||
, lens_id/0 | ||
]). | ||
|
||
%%============================================================================== | ||
%% API | ||
%%============================================================================== | ||
|
||
-spec available_lenses() -> [lens_id()]. | ||
available_lenses() -> | ||
[<<"server-info">>]. | ||
|
||
-spec default_lenses() -> [lens_id()]. | ||
default_lenses() -> | ||
[Id || Id <- available_lenses(), (cb_module(Id)):is_default()]. | ||
|
||
-spec enabled_lenses() -> [lens_id()]. | ||
enabled_lenses() -> | ||
els_config:get(code_lenses). | ||
|
||
-spec lenses(lens_id(), els_dt_document:item()) -> [lens()]. | ||
lenses(Id, Document) -> | ||
CbModule = cb_module(Id), | ||
CbModule:lenses(Document). | ||
|
||
%%============================================================================== | ||
%% Constructors | ||
%%============================================================================== | ||
|
||
-spec make_lens(range(), els_command:command(), any()) -> lens(). | ||
make_lens(Range, Command, Data) -> | ||
#{ range => Range | ||
, command => Command | ||
, data => Data | ||
}. | ||
|
||
%% @doc Return the callback module for a given Code Lens Identifier | ||
-spec cb_module(els_code_lens:lens_id()) -> module(). | ||
cb_module(Id0) -> | ||
Id = re:replace(Id0, "-", "_", [global, {return, binary}]), | ||
binary_to_atom(<<"els_code_lens_", Id/binary>>, utf8). |
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,29 @@ | ||
%%============================================================================== | ||
%% Code Lens: server_info | ||
%%============================================================================== | ||
|
||
-module(els_code_lens_server_info). | ||
|
||
-export([ command/0 | ||
, is_default/0 | ||
, lenses/1 | ||
]). | ||
|
||
-behaviour(els_code_lens). | ||
|
||
-spec command() -> els_command:command_id(). | ||
command() -> | ||
<<"server-info">>. | ||
|
||
-spec is_default() -> boolean(). | ||
is_default() -> | ||
false. | ||
|
||
%% @doc Given a Document, returns the available lenses | ||
-spec lenses(els_dt_document:item()) -> [els_code_lens:lens()]. | ||
lenses(_Document) -> | ||
Root = filename:basename(els_uri:path(els_config:get(root_uri))), | ||
Title = <<"Erlang LS (in ", Root/binary, ") info">>, | ||
Range = els_range:line(1), | ||
Command = els_command:make_command(Title, command(), []), | ||
[ els_code_lens:make_lens(Range, Command, []) ]. |
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,72 @@ | ||
%%============================================================================== | ||
%% Command | ||
%%============================================================================== | ||
-module(els_command). | ||
|
||
%%============================================================================== | ||
%% API | ||
%%============================================================================== | ||
-export([ with_prefix/1 | ||
, without_prefix/1 | ||
]). | ||
|
||
%%============================================================================== | ||
%% Constructors | ||
%%============================================================================== | ||
|
||
-export([ make_command/3 ]). | ||
|
||
%%============================================================================== | ||
%% Type Definitions | ||
%%============================================================================== | ||
|
||
-type command() :: #{ title := binary() | ||
, command := command_id() | ||
, arguments => [any()] | ||
}. | ||
-type command_id() :: binary(). | ||
-export_type([ command/0 | ||
, command_id/0 | ||
]). | ||
|
||
%%============================================================================== | ||
%% API | ||
%%============================================================================== | ||
|
||
%% @doc Add a server-unique prefix to a command. | ||
-spec with_prefix(command_id()) -> command_id(). | ||
with_prefix(Id) -> | ||
Prefix = server_prefix(), | ||
<<Prefix/binary, ":", Id/binary>>. | ||
|
||
%% @doc Strip a server-unique prefix from a command. | ||
-spec without_prefix(command_id()) -> command_id(). | ||
without_prefix(Id0) -> | ||
case binary:split(Id0, <<":">>) of | ||
[_, Id] -> Id; | ||
[Id] -> Id | ||
end. | ||
|
||
%%============================================================================== | ||
%% Constructors | ||
%%============================================================================== | ||
|
||
-spec make_command(binary(), command_id(), [any()]) -> command(). | ||
make_command(Title, CommandId, Args) -> | ||
#{ title => Title | ||
, command => with_prefix(CommandId) | ||
, arguments => Args | ||
}. | ||
|
||
%%============================================================================== | ||
%% Internal Functions | ||
%%============================================================================== | ||
|
||
%% @doc Generate a prefix unique to this running erlang_ls server. | ||
%% | ||
%% This is needed because some clients have a global namespace for all | ||
%% registered commands, and we need to be able to run multiple | ||
%% erlang_ls instances at the same time against a single client. | ||
-spec server_prefix() -> binary(). | ||
server_prefix() -> | ||
els_utils:to_binary(os:getpid()). |
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
Oops, something went wrong.