-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the prepare logic to a individual module
- Loading branch information
Showing
3 changed files
with
53 additions
and
45 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
apps/remote_control/lib/lexical/remote_control/code_mod/rename.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,2 @@ | ||
defmodule Lexical.RemoteControl.CodeMod.Rename do | ||
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
43 changes: 43 additions & 0 deletions
43
apps/remote_control/lib/lexical/remote_control/code_mod/rename/prepare.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,43 @@ | ||
defmodule Lexical.RemoteControl.CodeMod.Rename.Prepare do | ||
alias Lexical.Ast | ||
alias Lexical.Ast.Analysis | ||
alias Lexical.Document.Position | ||
alias Lexical.Document.Line | ||
alias Lexical.Document.Range | ||
alias Lexical.RemoteControl.CodeIntelligence.Entity | ||
|
||
import Line | ||
|
||
@spec prepare(Analysis.t(), Position.t()) :: {:ok, String.t(), Range.t()} | {:error, term()} | ||
def prepare(%Analysis{} = analysis, %Position{} = position) do | ||
case resolve_module(analysis, position) do | ||
{:ok, _, range} -> | ||
{:ok, local_module_name(range), range} | ||
|
||
{:error, _} -> | ||
{:error, :unsupported_entity} | ||
end | ||
end | ||
|
||
def resolve_module(analysis, position) do | ||
case Entity.resolve(analysis, position) do | ||
{:ok, {module_or_struct, module}, range} when module_or_struct in [:struct, :module] -> | ||
{:ok, module, range} | ||
|
||
{:ok, other, _} -> | ||
{:error, {:unsupported_entity, other}} | ||
|
||
{:error, reason} -> | ||
{:error, reason} | ||
end | ||
end | ||
|
||
def range_text(range) do | ||
line(text: text) = range.end.context_line | ||
String.slice(text, range.start.character - 1, range.end.character - range.start.character) | ||
end | ||
|
||
def local_module_name(%Range{} = range) do | ||
range |> range_text() |> Ast.Module.local_name() | ||
end | ||
end |