-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extract function as an execute command
- Loading branch information
1 parent
fae8f09
commit 1e69ee4
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
apps/language_server/lib/language_server/experimental/code_mod/refactor_extract_function.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,18 @@ | ||
defmodule ElixirLS.LanguageServer.Experimental.CodeMod.RefactorExtractFunction do | ||
alias ElixirLS.LanguageServer.Experimental.CodeMod.Diff | ||
alias ElixirLS.LanguageServer.Experimental.CodeMod.ExtractFunction | ||
|
||
alias Sourceror.Zipper | ||
|
||
require Logger | ||
|
||
def text_edits(original_text, tree, start_line, end_line, new_function_name) do | ||
result = | ||
tree | ||
|> Zipper.zip() | ||
|> ExtractFunction.extract_function(start_line + 1, end_line + 1, new_function_name) | ||
|> Sourceror.to_string() | ||
|
||
{:ok, Diff.diff(original_text, result)} | ||
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
39 changes: 39 additions & 0 deletions
39
apps/language_server/lib/language_server/providers/execute_command/extract_function.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,39 @@ | ||
defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ExtractFunction do | ||
@moduledoc """ | ||
This module implements a custom command extract function. | ||
Sends applyEdit request. | ||
""" | ||
|
||
alias ElixirLS.LanguageServer.JsonRpc | ||
alias ElixirLS.LanguageServer.Server | ||
alias ElixirLS.LanguageServer.Experimental.CodeMod.Ast | ||
alias ElixirLS.LanguageServer.Experimental.CodeMod.RefactorExtractFunction | ||
|
||
require Logger | ||
|
||
@behaviour ElixirLS.LanguageServer.Providers.ExecuteCommand | ||
|
||
@impl ElixirLS.LanguageServer.Providers.ExecuteCommand | ||
def execute([uri, start_line, end_line, new_function_name], state) do | ||
with source_file <- Server.get_source_file(state, uri), | ||
{:ok, tree} <- Ast.from(source_file.text, include_comments: true), | ||
{:ok, text_edits} <- | ||
RefactorExtractFunction.text_edits( | ||
source_file.text, | ||
tree, | ||
start_line, | ||
end_line, | ||
new_function_name | ||
) do | ||
apply_edits(uri, text_edits) | ||
{:ok, nil} | ||
end | ||
end | ||
|
||
def apply_edits(uri, text_edits) do | ||
JsonRpc.send_request("workspace/applyEdit", %{ | ||
"label" => "Extract function", | ||
"edit" => %{"changes" => %{uri => text_edits}} | ||
}) |> IO.inspect(label: :rpc_response, limit: :infinity) | ||
end | ||
end |