-
-
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.
- Loading branch information
Showing
6 changed files
with
251 additions
and
34 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
12 changes: 12 additions & 0 deletions
12
apps/remote_control/lib/lexical/remote_control/code_mod/rename/document_changes.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,12 @@ | ||
defmodule Lexical.RemoteControl.CodeMod.Rename.DocumentChanges do | ||
defstruct [:uri, :edits, :rename_file] | ||
|
||
@type t :: %__MODULE__{ | ||
uri: Lexical.uri(), | ||
edits: [Lexical.Document.Edit.t()], | ||
rename_file: {Lexical.uri(), Lexical.uri()} | nil | ||
} | ||
def new(uri, edits, rename_file \\ nil) do | ||
%__MODULE__{uri: uri, edits: edits, rename_file: rename_file} | ||
end | ||
end |
99 changes: 99 additions & 0 deletions
99
apps/remote_control/lib/lexical/remote_control/code_mod/rename/file.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,99 @@ | ||
defmodule Lexical.RemoteControl.CodeMod.Rename.File do | ||
alias Lexical.Ast | ||
alias Lexical.Document | ||
alias Lexical.RemoteControl.Search.Store | ||
alias Lexical.RemoteControl | ||
|
||
def maybe_rename(entry, new_suffix) do | ||
with false <- has_parent?(entry), | ||
false <- has_any_siblings?(entry) do | ||
rename_path(entry, new_suffix) | ||
else | ||
_ -> nil | ||
end | ||
end | ||
|
||
defp has_parent?(entry) do | ||
case Store.parent(entry) do | ||
{:ok, _} -> true | ||
_ -> false | ||
end | ||
end | ||
|
||
defp has_any_siblings?(entry) do | ||
case Store.siblings(entry) do | ||
{:ok, [_]} -> false | ||
{:ok, [_ | _]} -> true | ||
_ -> false | ||
end | ||
end | ||
|
||
defp fetch_new_name(entry, new_suffix) do | ||
uri = Document.Path.ensure_uri(entry.path) | ||
text_edits = [Document.Edit.new(new_suffix, entry.range)] | ||
|
||
with {:ok, document} <- Document.Store.open_temporary(uri), | ||
{:ok, edited_document} = | ||
Document.apply_content_changes(document, document.version + 1, text_edits), | ||
{:ok, %{context: {:alias, alias}}} <- | ||
Ast.surround_context(edited_document, entry.range.start) do | ||
{:ok, to_string(alias)} | ||
else | ||
_ -> :error | ||
end | ||
end | ||
|
||
defp rename_path(entry, new_suffix) do | ||
relative_path = relative_path(entry.path) | ||
|
||
with {:ok, prefix} <- fetch_conventional_prefix(relative_path), | ||
{:ok, new_name} <- fetch_new_name(entry, new_suffix) do | ||
extname = Path.extname(entry.path) | ||
suffix = Macro.underscore(new_name) | ||
new_path = Path.join(prefix, "#{suffix}#{extname}") | ||
|
||
{Document.Path.ensure_uri(entry.path), Document.Path.ensure_uri(new_path)} | ||
else | ||
_ -> nil | ||
end | ||
end | ||
|
||
defp relative_path(path) do | ||
Path.relative_to(path, root_path()) | ||
end | ||
|
||
defp root_path do | ||
RemoteControl.get_project() | ||
|> Map.get(:root_uri) | ||
|> Document.Path.ensure_path() | ||
end | ||
|
||
defp fetch_conventional_prefix(path) do | ||
result = | ||
path | ||
|> Path.split() | ||
|> Enum.chunk_every(2, 2) | ||
|> Enum.reduce({[], []}, fn | ||
["apps", app_name], _ -> | ||
{[], [app_name, "apps"]} | ||
|
||
["lib", follow_element], {elements, prefix} -> | ||
{[follow_element | elements], ["lib" | prefix]} | ||
|
||
["test", follow_element], {elements, prefix} -> | ||
{[follow_element | elements], ["test" | prefix]} | ||
|
||
remain, {elements, prefix} -> | ||
{remain ++ elements, prefix} | ||
end) | ||
|
||
case result do | ||
{_, []} -> | ||
:error | ||
|
||
{_module_path, prefix} -> | ||
prefix = prefix |> Enum.reverse() |> Enum.join("/") | ||
{:ok, prefix} | ||
end | ||
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
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