Skip to content

Commit 2898cdd

Browse files
gp-pereirascohen
andauthored
[feat] integrate refactorex (#5)
* temp: commented formatter dep imports * feat: added refactorex as a dependency * feat: added refactorex as a code action handler * temp: skipping broken remote control tests * refactor: less restrict refactorex dependency version Co-authored-by: Steve Cohen <scohen@scohen.org> * refactor: removed useless test * feat: range subtree (selection) extraction algorithm - inlined from refactorex * Revert "feat: range subtree (selection) extraction algorithm - inlined from refactorex" This reverts commit a362383. * refactor: using Document.fragment/3 to find the selection, then positioning it * refactor: improved test infrastructure and multiline selection test * fix: fixed the formatter import deps problem * Revert "temp: commented formatter dep imports" This reverts commit b4ad1ce. * feat: introducing context.trigger_kind to Code Actions so they can decide when they are triggered * refactor: adding more supported code actions * refactor: removed Logger used for debugging * [bugfix] Only index if there's stuff to index A colleague found a fun edge case. He added three new empty files to a project, and Expert wouldn't start. This is because of a divide by zero error when there are no bytes to index, which causes progress updates to fail (indexing completed successfully). This fix checks for this condition and no-ops if there's nothing to index. * refactor: sort aliases * refactor: remove unnecessary changes. * refactor: better naming the test cases * Revert "temp: skipping broken remote control tests" This reverts commit 389afbba063341ccde90bad72d0aa1246c9c3a5a. * Update Nix hash of Mix deps * fix: removed GenLSP from refactorex so it is not imported into remote_control * Update Nix hash of Mix deps * fix: setting up Project before tests * refactor: bump Sourceror and using updates on Refactorex * Update Nix hash of Mix deps --------- Co-authored-by: Steve Cohen <scohen@scohen.org>
1 parent f3dc077 commit 2898cdd

File tree

25 files changed

+237
-52
lines changed

25 files changed

+237
-52
lines changed

apps/common/mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule Common.MixProject do
3535
Mix.Credo.dependency(),
3636
Mix.Dialyzer.dependency(),
3737
{:snowflake, "~> 1.0"},
38-
{:sourceror, "~> 1.7"},
38+
{:sourceror, "~> 1.9"},
3939
{:stream_data, "~> 1.1", only: [:test], runtime: false},
4040
{:patch, "~> 0.15", only: [:test], optional: true, runtime: false}
4141
]

apps/common/mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
1010
"patch": {:hex, :patch, "0.15.0", "947dd6a8b24a2d2d1137721f20bb96a8feb4f83248e7b4ad88b4871d52807af5", [:mix], [], "hexpm", "e8dadf9b57b30e92f6b2b1ce2f7f57700d14c66d4ed56ee27777eb73fb77e58d"},
1111
"snowflake": {:hex, :snowflake, "1.0.4", "8433b4e04fbed19272c55e1b7de0f7a1ee1230b3ae31a813b616fd6ef279e87a", [:mix], [], "hexpm", "badb07ebb089a5cff737738297513db3962760b10fe2b158ae3bebf0b4d5be13"},
12-
"sourceror": {:hex, :sourceror, "1.7.1", "599d78f4cc2be7d55c9c4fd0a8d772fd0478e3a50e726697c20d13d02aa056d4", [:mix], [], "hexpm", "cd6f268fe29fa00afbc535e215158680a0662b357dc784646d7dff28ac65a0fc"},
12+
"sourceror": {:hex, :sourceror, "1.9.0", "3bf5fe2d017aaabe3866d8a6da097dd7c331e0d2d54e59e21c2b066d47f1e08e", [:mix], [], "hexpm", "d20a9dd5efe162f0d75a307146faa2e17b823ea4f134f662358d70f0332fed82"},
1313
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
1414
"stream_data": {:hex, :stream_data, "1.1.3", "15fdb14c64e84437901258bb56fc7d80aaf6ceaf85b9324f359e219241353bfb", [:mix], [], "hexpm", "859eb2be72d74be26c1c4f272905667672a52e44f743839c57c7ee73a1a66420"},
1515
}

apps/lexical_credo/mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"makeup_erlang": {:hex, :makeup_erlang, "1.0.2", "03e1804074b3aa64d5fad7aa64601ed0fb395337b982d9bcf04029d68d51b6a7", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "af33ff7ef368d5893e4a267933e7744e46ce3cf1f61e2dccf53a111ed3aa3727"},
1515
"nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"},
1616
"snowflake": {:hex, :snowflake, "1.0.4", "8433b4e04fbed19272c55e1b7de0f7a1ee1230b3ae31a813b616fd6ef279e87a", [:mix], [], "hexpm", "badb07ebb089a5cff737738297513db3962760b10fe2b158ae3bebf0b4d5be13"},
17-
"sourceror": {:hex, :sourceror, "1.7.1", "599d78f4cc2be7d55c9c4fd0a8d772fd0478e3a50e726697c20d13d02aa056d4", [:mix], [], "hexpm", "cd6f268fe29fa00afbc535e215158680a0662b357dc784646d7dff28ac65a0fc"},
17+
"sourceror": {:hex, :sourceror, "1.9.0", "3bf5fe2d017aaabe3866d8a6da097dd7c331e0d2d54e59e21c2b066d47f1e08e", [:mix], [], "hexpm", "d20a9dd5efe162f0d75a307146faa2e17b823ea4f134f662358d70f0332fed82"},
1818
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
1919
"stream_data": {:hex, :stream_data, "1.1.3", "15fdb14c64e84437901258bb56fc7d80aaf6ceaf85b9324f359e219241353bfb", [:mix], [], "hexpm", "859eb2be72d74be26c1c4f272905667672a52e44f743839c57c7ee73a1a66420"},
2020
}

apps/proto/mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"file_system": {:hex, :file_system, "1.1.0", "08d232062284546c6c34426997dd7ef6ec9f8bbd090eb91780283c9016840e8f", [:mix], [], "hexpm", "bfcf81244f416871f2a2e15c1b515287faa5db9c6bcf290222206d120b3d43f6"},
99
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
1010
"snowflake": {:hex, :snowflake, "1.0.4", "8433b4e04fbed19272c55e1b7de0f7a1ee1230b3ae31a813b616fd6ef279e87a", [:mix], [], "hexpm", "badb07ebb089a5cff737738297513db3962760b10fe2b158ae3bebf0b4d5be13"},
11-
"sourceror": {:hex, :sourceror, "1.7.1", "599d78f4cc2be7d55c9c4fd0a8d772fd0478e3a50e726697c20d13d02aa056d4", [:mix], [], "hexpm", "cd6f268fe29fa00afbc535e215158680a0662b357dc784646d7dff28ac65a0fc"},
11+
"sourceror": {:hex, :sourceror, "1.9.0", "3bf5fe2d017aaabe3866d8a6da097dd7c331e0d2d54e59e21c2b066d47f1e08e", [:mix], [], "hexpm", "d20a9dd5efe162f0d75a307146faa2e17b823ea4f134f662358d70f0332fed82"},
1212
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
1313
"stream_data": {:hex, :stream_data, "1.1.3", "15fdb14c64e84437901258bb56fc7d80aaf6ceaf85b9324f359e219241353bfb", [:mix], [], "hexpm", "859eb2be72d74be26c1c4f272905667672a52e44f743839c57c7ee73a1a66420"},
1414
}

apps/protocol/mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
1010
"patch": {:hex, :patch, "0.15.0", "947dd6a8b24a2d2d1137721f20bb96a8feb4f83248e7b4ad88b4871d52807af5", [:mix], [], "hexpm", "e8dadf9b57b30e92f6b2b1ce2f7f57700d14c66d4ed56ee27777eb73fb77e58d"},
1111
"snowflake": {:hex, :snowflake, "1.0.4", "8433b4e04fbed19272c55e1b7de0f7a1ee1230b3ae31a813b616fd6ef279e87a", [:mix], [], "hexpm", "badb07ebb089a5cff737738297513db3962760b10fe2b158ae3bebf0b4d5be13"},
12-
"sourceror": {:hex, :sourceror, "1.7.1", "599d78f4cc2be7d55c9c4fd0a8d772fd0478e3a50e726697c20d13d02aa056d4", [:mix], [], "hexpm", "cd6f268fe29fa00afbc535e215158680a0662b357dc784646d7dff28ac65a0fc"},
12+
"sourceror": {:hex, :sourceror, "1.9.0", "3bf5fe2d017aaabe3866d8a6da097dd7c331e0d2d54e59e21c2b066d47f1e08e", [:mix], [], "hexpm", "d20a9dd5efe162f0d75a307146faa2e17b823ea4f134f662358d70f0332fed82"},
1313
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
1414
"stream_data": {:hex, :stream_data, "1.1.3", "15fdb14c64e84437901258bb56fc7d80aaf6ceaf85b9324f359e219241353bfb", [:mix], [], "hexpm", "859eb2be72d74be26c1c4f272905667672a52e44f743839c57c7ee73a1a66420"},
1515
}

apps/remote_control/lib/lexical/remote_control.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ defmodule Lexical.RemoteControl do
3333

3434
defdelegate list_modules, to: :code, as: :all_available
3535

36-
defdelegate code_actions(document, range, diagnostics, kinds), to: CodeAction, as: :for_range
36+
defdelegate code_actions(document, range, diagnostics, kinds, trigger_kind),
37+
to: CodeAction,
38+
as: :for_range
3739

3840
defdelegate complete(env), to: RemoteControl.Completion, as: :elixir_sense_expand
3941

apps/remote_control/lib/lexical/remote_control/api.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ defmodule Lexical.RemoteControl.Api do
4444
%Document{} = document,
4545
%Range{} = range,
4646
diagnostics,
47-
kinds
47+
kinds,
48+
trigger_kind
4849
) do
4950
RemoteControl.call(project, RemoteControl, :code_actions, [
5051
document,
5152
range,
5253
diagnostics,
53-
kinds
54+
kinds,
55+
trigger_kind
5456
])
5557
end
5658

apps/remote_control/lib/lexical/remote_control/code_action.ex

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ defmodule Lexical.RemoteControl.CodeAction do
1818
| :source_organize_imports
1919
| :source_fix_all
2020

21+
@type trigger_kind :: :invoked | :automatic
22+
2123
@type t :: %__MODULE__{
2224
title: String.t(),
2325
kind: code_action_kind,
@@ -30,33 +32,35 @@ defmodule Lexical.RemoteControl.CodeAction do
3032
Handlers.ReplaceWithUnderscore,
3133
Handlers.OrganizeAliases,
3234
Handlers.AddAlias,
33-
Handlers.RemoveUnusedAlias
35+
Handlers.RemoveUnusedAlias,
36+
Handlers.Refactorex
3437
]
3538

3639
@spec new(Lexical.uri(), String.t(), code_action_kind(), Changes.t()) :: t()
3740
def new(uri, title, kind, changes) do
3841
%__MODULE__{uri: uri, title: title, changes: changes, kind: kind}
3942
end
4043

41-
@spec for_range(Document.t(), Range.t(), [Diagnostic.t()], [code_action_kind] | :all) :: [t()]
42-
def for_range(%Document{} = doc, %Range{} = range, diagnostics, kinds) do
43-
results =
44-
Enum.flat_map(@handlers, fn handler ->
45-
if applies?(kinds, handler) do
46-
handler.actions(doc, range, diagnostics)
47-
else
48-
[]
49-
end
50-
end)
51-
52-
results
44+
@spec for_range(
45+
Document.t(),
46+
Range.t(),
47+
[Diagnostic.t()],
48+
[code_action_kind] | :all,
49+
trigger_kind
50+
) :: [t()]
51+
def for_range(%Document{} = doc, %Range{} = range, diagnostics, kinds, trigger_kind) do
52+
Enum.flat_map(@handlers, fn handler ->
53+
if handle_kinds?(handler, kinds) and handle_trigger_kind?(handler, trigger_kind) do
54+
handler.actions(doc, range, diagnostics)
55+
else
56+
[]
57+
end
58+
end)
5359
end
5460

55-
defp applies?(:all, _handler_module) do
56-
true
57-
end
61+
defp handle_kinds?(_handler, :all), do: true
62+
defp handle_kinds?(handler, kinds), do: kinds -- handler.kinds() != kinds
5863

59-
defp applies?(kinds, handler_module) do
60-
kinds -- handler_module.kinds() != kinds
61-
end
64+
defp handle_trigger_kind?(handler, trigger_kind),
65+
do: handler.trigger_kind() in [trigger_kind, :all]
6266
end

apps/remote_control/lib/lexical/remote_control/code_action/handler.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ defmodule Lexical.RemoteControl.CodeAction.Handler do
66

77
@callback actions(Document.t(), Range.t(), [Diagnostic.t()]) :: [CodeAction.t()]
88
@callback kinds() :: [CodeAction.code_action_kind()]
9+
@callback trigger_kind() :: CodeAction.trigger_kind() | :all
910
end

apps/remote_control/lib/lexical/remote_control/code_action/handlers/add_alias.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ defmodule Lexical.RemoteControl.CodeAction.Handlers.AddAlias do
4444
[:quick_fix]
4545
end
4646

47+
@impl CodeAction.Handler
48+
def trigger_kind, do: :all
49+
4750
defp build_code_action(%Analysis{} = analysis, range, current_aliases, potential_alias_module) do
4851
case Ast.Module.safe_split(potential_alias_module, as: :atoms) do
4952
{:erlang, _} ->

0 commit comments

Comments
 (0)