-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a brand new diff strategy and apply some special handling for ren…
…aming non-alias reference modules. This can fix some errors that occur when renaming without changing the local name.
- Loading branch information
Showing
6 changed files
with
207 additions
and
58 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
apps/remote_control/lib/lexical/remote_control/code_mod/rename/entry.ex
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,38 @@ | ||
defmodule Lexical.RemoteControl.CodeMod.Rename.Entry do | ||
@moduledoc """ | ||
""" | ||
alias Lexical.RemoteControl.Search.Indexer, as: Indexer | ||
|
||
# When renaming, we rely on the `Indexer.Entry`, | ||
# and we also need some other fields used exclusively for renaming, such as `edit_range`. | ||
@type t :: %__MODULE__{ | ||
id: Indexer.Entry.entry_id(), | ||
path: Lexical.path(), | ||
subject: Indexer.Entry.subject(), | ||
block_range: Lexical.Document.Range.t() | nil, | ||
range: Lexical.Document.Range.t(), | ||
edit_range: Lexical.Document.Range.t(), | ||
subtype: Indexer.Entry.entry_subtype() | ||
} | ||
defstruct [ | ||
:id, | ||
:path, | ||
:subject, | ||
:block_range, | ||
:range, | ||
:edit_range, | ||
:subtype | ||
] | ||
|
||
def new(%Indexer.Entry{} = indexer_entry) do | ||
%__MODULE__{ | ||
id: indexer_entry.id, | ||
path: indexer_entry.path, | ||
subject: indexer_entry.subject, | ||
subtype: indexer_entry.subtype, | ||
block_range: indexer_entry.block_range, | ||
range: indexer_entry.range, | ||
edit_range: indexer_entry.range | ||
} | ||
end | ||
end |
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
27 changes: 27 additions & 0 deletions
27
apps/remote_control/lib/lexical/remote_control/code_mod/rename/module/diff.ex
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,27 @@ | ||
defmodule Lexical.RemoteControl.CodeMod.Rename.Module.Diff do | ||
def diff(old_name, new_name) do | ||
with [{:eq, eq} | _] <- String.myers_difference(old_name, new_name), | ||
equal_segment <- trim_last_dot_part(eq), | ||
true <- not is_nil(equal_segment) do | ||
to_be_renamed = replace_leading_eq(old_name, equal_segment) | ||
replacement = replace_leading_eq(new_name, equal_segment) | ||
{to_be_renamed, replacement} | ||
else | ||
_ -> | ||
{old_name, new_name} | ||
end | ||
end | ||
|
||
defp trim_last_dot_part(module) do | ||
split = module |> String.reverse() |> String.split(".", parts: 2) | ||
|
||
if length(split) == 2 do | ||
[_, rest] = split | ||
rest |> String.reverse() | ||
end | ||
end | ||
|
||
defp replace_leading_eq(module, eq) do | ||
module |> String.replace(~r"^#{eq}", "") |> String.trim_leading(".") | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
apps/remote_control/test/lexical/remote_control/code_mod/rename/module/diff_test.exs
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,27 @@ | ||
defmodule Lexical.RemoteControl.CodeMod.Rename.Module.DiffTest do | ||
alias Lexical.RemoteControl.CodeMod.Rename.Module.Diff | ||
|
||
use ExUnit.Case, async: true | ||
|
||
describe "diff/2" do | ||
test "returns the local module pair if only the local name is changed" do | ||
assert Diff.diff("A.B.C", "A.B.D") == {"C", "D"} | ||
end | ||
|
||
test "returns the local module pair even if the part of local name is changed" do | ||
assert Diff.diff("A.B.CD", "A.B.CC") == {"CD", "CC"} | ||
end | ||
|
||
test "returns the suffix when extending the middle part" do | ||
assert Diff.diff("Foo.Bar", "Foo.Baz.Bar") == {"Bar", "Baz.Bar"} | ||
end | ||
|
||
test "returns the suffix if the middle part is removed" do | ||
assert Diff.diff("Foo.Baz.Bar", "Foo.Bar") == {"Baz.Bar", "Bar"} | ||
end | ||
|
||
test "returns the entire module pair if the change starts from the first module" do | ||
assert Diff.diff("Foo.Bar", "Foa.Bar") == {"Foo.Bar", "Foa.Bar"} | ||
end | ||
end | ||
end |
Oops, something went wrong.