Skip to content

Commit

Permalink
Support the scenario where descendants and the target have the same n…
Browse files Browse the repository at this point in the history
…ame.

When their names are the same, we can only judge when adjusting the range, and using a `match?` to filter would be safer and more accurate.
  • Loading branch information
scottming committed Dec 9, 2023
1 parent fb9ce6f commit 65c3550
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.Rename do
defp search_related_candidates(document, position, entity, range) do
cursor_entity_string = cursor_entity_string(range)

entities =
exacts(entity, cursor_entity_string)
entities = exacts(entity, cursor_entity_string)

# Users won't always want to rename descendants of a module.
# For instance, when there are no more submodules after the cursor.
Expand Down Expand Up @@ -96,7 +95,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.Rename do

prefix
|> Store.prefix(subject: [:definition, :reference])
|> Enum.filter(&entry_matching?(&1, cursor_entity_string))
|> Enum.filter(&(entry_matching?(&1, cursor_entity_string) and has_dots_in_range?(&1)))
|> adjust_range(entity)
end

Expand All @@ -109,14 +108,19 @@ defmodule Lexical.RemoteControl.CodeIntelligence.Rename do
end

defp entry_matching?(entry, cursor_entity_string) do
range_text = range_text(entry.range)
String.contains?(range_text, cursor_entity_string)
entry.range |> range_text() |> String.contains?(cursor_entity_string)
end

defp has_dots_in_range?(entry) do
entry.range |> range_text() |> String.contains?(".")
end

defp adjust_range(entries, entity) do
for entry <- entries,
uri = Document.Path.ensure_uri(entry.path),
{:ok, range} = resolve_entity_range(uri, entry.range.start, entity) do
range_result = resolve_entity_range(uri, entry.range.start, entity),
match?({:ok, _}, range_result) do
{_, range} = range_result
%{entry | range: range}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,25 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
assert result =~ ~S[defmodule TopLevel.Foo.Renamed do]
assert result =~ ~S[alias TopLevel.Foo.Renamed]
end

test "succeeds even if there are descendants with the same name" do
{:ok, result} =
~q[
defmodule TopLevel.Foo do
defmodule Foo do # skip this
end
end
defmodule TopLevel.Bar do
alias TopLevel.|Foo.Foo
end
]
|> rename("Renamed")

assert result =~ ~S[defmodule TopLevel.Renamed do]
assert result =~ ~S[defmodule Foo do # skip this]
assert result =~ ~S[alias TopLevel.Renamed.Foo]
end
end

describe "rename struct" do
Expand Down

0 comments on commit 65c3550

Please sign in to comment.