# RefactorEx - Effortless Elixir Refactoring in VS Code

**URL:** <https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759>\
**Category:** Announcing\
**Tags:** library, vs-code, vscode, refactoring\
**Created:** [November 29, 2024, 3:33pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759 "2024-11-29T15:33:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gpereira](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gpereira/32/26636_2.png) [@gpereira](https://forum.elixirforum.com/u/gpereira)\
**Post date:** [November 29, 2024, 3:33pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/1 "2024-11-29T15:33:59Z")

</div>

Hello, Elixir enthusiasts! 👋

I’m thrilled to introduce **RefactorEx** , a new Visual Studio Code extension designed to make refactoring your Elixir projects smoother and more efficient than ever.

### What is RefactorEx?

RefactorEx simplifies your Elixir development by offering intuitive code actions for a wide range of refactorings. Whether you’re extracting functions, renaming variables, or tweaking pipelines, RefactorEx lets you refactor with confidence and speed—all without leaving your editor.

🎥 **Check it out in action:**  
 ![RefactorEx Demo](https://github.com/gp-pereira/refactorex/blob/main/assets/examples/demo.gif?raw=true)

### Key Features

Here are some of the many refactorings you can perform with RefactorEx:

- **Functions** : Extract, inline, rename, or even convert anonymous functions to regular ones.
- **Variables & Constants** : Extract, inline, or rename with ease.
- **Aliases & Guards** : Expand, merge, sort, or inline them.
- **Pipelines** : Introduce or remove pipes, and even add `IO.inspect` for debugging.

A complete list of available refactorings can be found in the [documentation](https://github.com/gp-pereira/refactorex#available-refactorings).

### Why RefactorEx?

Refactoring is a core part of maintaining clean, readable, and maintainable code. RefactorEx began as part of my academic journey—specifically, my undergraduate thesis project (_TCC_), extending the [research of my professor](https://forum.elixirforum.com/t/towards-a-catalog-of-refactorings-for-elixir/58855). My goal was to create something practical and valuable for the Elixir community, and I’m excited to share it with you now!

### Get Started

1. Install RefactorEx from the [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=gp-pereira.refatorex).
2. Open your Elixir project in VS Code.
3. Highlight a code snippet or place your cursor where a refactoring is needed.
4. Hit the lightbulb icon 💡 or use the quick actions menu (`Ctrl+.` or `Cmd+.`).

### Feedback and Contributions

Your feedback is invaluable! If you have suggestions, encounter issues, or want to contribute, please head over to the [GitHub repository](https://github.com/gp-pereira/refactorex).

RefactorEx is a work of love, and I’d love to hear your thoughts as we continue to improve and expand it. Let me know how it works for your projects or if there’s a refactoring you’d love to see implemented next!

Happy coding!  
**gp-pereira**

#### Notes

1. I know there is official LSP on its way and I’m already looking into how to merge these refactorings into it.
2. Although it was developed for VS Code, I think it should be pretty straight forward to connect it to other editors since it’s a LSP.

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [November 29, 2024, 3:51pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/2 "2024-11-29T15:51:48Z")

</div>

Gods, this looks amazing!

Any chance to have it as a NeoVim plugin? 🙏

---

<div class="post-metadata">

**Author:** ![aifrak](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aifrak/32/25463_2.png) [@aifrak](https://forum.elixirforum.com/u/aifrak)\
**Post date:** [November 29, 2024, 4:08pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/3 "2024-11-29T16:08:27Z")

</div>

I just gave it a quick try. It is amazing!

At the first try, it did not work and I had to reload the window.

I would say RefactorEx should be a must have with ElixirLS for VSCode users.

Thank you for this VSCode extension!

---

<div class="post-metadata">

**Author:** ![gpereira](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gpereira/32/26636_2.png) [@gpereira](https://forum.elixirforum.com/u/gpereira)\
**Post date:** [November 29, 2024, 4:50pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/4 "2024-11-29T16:50:32Z")

</div>

Yeah, I intend to work on it in the next weeks

---

<div class="post-metadata">

**Author:** ![lucasvegi](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lucasvegi/32/30901_2.png) [@lucasvegi](https://forum.elixirforum.com/u/lucasvegi)\
**Post date:** [November 29, 2024, 4:56pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/5 "2024-11-29T16:56:15Z")

</div>

Excellent work @gpereira! It is a great satisfaction to see the work I have been doing over the last few years turning into a practical application for the community!

---

<div class="post-metadata">

**Author:** ![gpereira](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gpereira/32/26636_2.png) [@gpereira](https://forum.elixirforum.com/u/gpereira)\
**Post date:** [November 29, 2024, 5:26pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/6 "2024-11-29T17:26:40Z")

</div>

You’re one of the main inspirations for this project and without your groundwork it would have taken way longer to build it, so I can’t thank you enough. 🤝

---

<div class="post-metadata">

**Author:** ![mhanberg](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mhanberg/32/17162_2.png) [@mhanberg](https://forum.elixirforum.com/u/mhanberg)\
**Post date:** [November 29, 2024, 5:27pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/7 "2024-11-29T17:27:49Z")

</div>

Congratulations! This has been a long time coming.

I am personally proud that [GenLSP](https://github.com/elixir-tools/gen_lsp) was able to be used for another language server!

As we move forward with the new official LSP, I plan on working with Gabriel to integrate his refactorings into it.

Congrats again! Awesome project.

---

<div class="post-metadata">

**Author:** ![gpereira](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gpereira/32/26636_2.png) [@gpereira](https://forum.elixirforum.com/u/gpereira)\
**Post date:** [November 29, 2024, 5:38pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/8 "2024-11-29T17:38:29Z")

</div>

Oh thanks, that means a lot coming from you and I’m super excited to work with the LSP team!

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [November 29, 2024, 5:38pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/9 "2024-11-29T17:38:42Z")

</div>

Amazing! 😍

---

<div class="post-metadata">

**Author:** ![adolfont](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/adolfont/32/6057_2.png) [@adolfont](https://forum.elixirforum.com/u/adolfont)\
**Post date:** [November 29, 2024, 7:16pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/10 "2024-11-29T19:16:42Z")

</div>

Hi, Gabriel. // fixed.. sorry  
Congrats!

I wasn’t able to try RecaftorEx.  
Maybe you can help me:

```elixir
[Info - 4:13:40 PM] Client is waiting server
[Error - 4:13:40 PM] Compilation error: /home/adolfo/.asdf/lib/commands/command-exec.bash: line 31: with_shim_executable: command not found

[Error - 4:13:40 PM] Refactorex client: couldn't create connection to server.
Error: Compilation error: /home/adolfo/.asdf/lib/commands/command-exec.bash: line 31: with_shim_executable: command not found

	at /mnt/windows/__LINUX/home/adolfo/.vscode/extensions/gp-pereira.refatorex-0.1.24/out/extension.js:62:20
	at ChildProcess.exithandler (node:child_process:415:7)
	at ChildProcess.emit (node:events:519:28)
	at maybeClose (node:internal/child_process:1105:16)
	at Socket.<anonymous> (node:internal/child_process:457:11)
	at Socket.emit (node:events:519:28)
	at Pipe.<anonymous> (node:net:339:12)

```

I am not sure it is RefactorEx fault.

---

<div class="post-metadata">

**Author:** ![zachdaniel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/32/31980_2.png) [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Post date:** [November 29, 2024, 7:37pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/11 "2024-11-29T19:37:33Z")

</div>

Woah! This is awesome! We’ve built some refactor tools into igniter, our generator and project patching tool, I’m wondering if there is an opportunity to collaborate here? There is a huge intersection in what the two libraries are doing, although ours works on a sort of “virtual” file system via rewrite. Igniter is also based on sourceror.

One of our goals with igniter is to make these accessible _outside_ of the context of an editor, so that they can be scripted, shared and standardized.

---

<div class="post-metadata">

**Author:** ![gpereira](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gpereira/32/26636_2.png) [@gpereira](https://forum.elixirforum.com/u/gpereira)\
**Post date:** [November 29, 2024, 8:03pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/12 "2024-11-29T20:03:41Z")

</div>

Hello, I’m actually Gabriel

The server process is a mix run --halt elixir app. In your case, it seems there is a problem in the ASDF elixir installation. Do other elixir programs run normally?

---

<div class="post-metadata">

**Author:** ![gpereira](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gpereira/32/26636_2.png) [@gpereira](https://forum.elixirforum.com/u/gpereira)\
**Post date:** [November 29, 2024, 8:22pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/13 "2024-11-29T20:22:04Z")

</div>

That seems pretty interesting. I liked how your rename\_function works in all files, I know the effort to build that so congrats!

Based on what I saw, the Igniter project works on a “higher” level of abstraction than mine (whole projects) and refactoring is not the core of it. So there is definitely an opportunity here, for example using the refactoring of my project as features instead of working directly on the zipper.

Send me a message so we can continue this conversation elsewhere.

---

<div class="post-metadata">

**Author:** ![Sorc96](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sorc96/32/28361_2.png) [@Sorc96](https://forum.elixirforum.com/u/Sorc96)\
**Post date:** [November 29, 2024, 8:25pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/14 "2024-11-29T20:25:44Z")

</div>

This is absolutely amazing! I’m super excited to see this evolve even more, but it’s already so exciting! You’ve done an incredible job.

---

<div class="post-metadata">

**Author:** ![adolfont](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/adolfont/32/6057_2.png) [@adolfont](https://forum.elixirforum.com/u/adolfont)\
**Post date:** [November 29, 2024, 10:28pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/15 "2024-11-29T22:28:22Z")

</div>

Sorry.

I will check this.  
Thanks.

---

<div class="post-metadata">

**Author:** ![andzdroid](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/andzdroid/32/31831_2.png) [@andzdroid](https://forum.elixirforum.com/u/andzdroid)\
**Post date:** [November 30, 2024, 11:52am UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/16 "2024-11-30T11:52:25Z")

</div>

Thanks so much for building this!

I installed this in cursor and I’m unable to get any of the rename code actions to appear. Other code actions seem to work but not the rename. I’ve tried putting the cursor at the function name, at the start of the line, selecting the function name, selecting the whole line.

---

<div class="post-metadata">

**Author:** ![iurimateus](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@iurimateus](https://forum.elixirforum.com/u/iurimateus)\
**Post date:** [November 30, 2024, 1:23pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/17 "2024-11-30T13:23:29Z")

</div>

> [@andzdroid](#):
>
> rename code actions to appear

It’s just the LSP rename (F2 in vscode, `vim.lsp.buf.rename` in nvim)

---

<div class="post-metadata">

**Author:** ![gpereira](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gpereira/32/26636_2.png) [@gpereira](https://forum.elixirforum.com/u/gpereira)\
**Post date:** [November 30, 2024, 1:25pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/18 "2024-11-30T13:25:19Z")

</div>

Hi, at least for vs code the rename functionality stays on a different keybinding than the refactorings. Can you check if cursor does the same? In vs code is F2 (+ctrl in iOS)

---

<div class="post-metadata">

**Author:** ![r8code](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@r8code](https://forum.elixirforum.com/u/r8code)\
**Post date:** [November 30, 2024, 3:36pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/19 "2024-11-30T15:36:17Z")

</div>

It also working with vscode + neovim ext (with lazyvim) ,  
btw Ctrl + Shift + r to trigger Refactor and F2 to rename  
Thank you very much

---

<div class="post-metadata">

**Author:** ![iurimateus](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@iurimateus](https://forum.elixirforum.com/u/iurimateus)\
**Post date:** [November 30, 2024, 3:46pm UTC](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759/20 "2024-11-30T15:46:40Z")

</div>

For use in neovim (hacky),

```lua
local function find_available_port()
  local tcp = vim.uv.new_tcp()
  if tcp:bind("127.0.0.1", 0) then
    local port = tcp:getsockname().port
    tcp:close()

    return port
  end
end

local function start_refactorex(port, bufnr)
  vim.lsp.start({
    name = "refactorex",
    cmd = vim.lsp.rpc.connect("127.0.0.1", port),
    root_dir = vim.fs.root(bufnr, { "mix.exs" }),
  }, {
    bufnr = bufnr,
  })
end

vim.api.nvim_create_autocmd("Filetype", {
  pattern = { "elixir", "eelixir", "heex" },
  callback = function(args)
    local spawned = (vim.fn.system "pgrep -fl refactorex"):match "beam%.smp" ~= nil
    local port = (
      spawned and vim.fn.system("pgrep -fla refactorex"):match "beam%.smp.*--port (%d+)"
    )
      or find_available_port()

    if not spawned then
      vim.uv.spawn(
        "/path/to/refactorex/bin/start",
        { args = { "--port", port } }
      )
    end

    vim.defer_fn(function()
      start_refactorex(port, args.buf)
    end, 3000)
  end,
})

```

* * *

EDIT: i believe it’s unnecessary to grep processes for the port, and the current approach is probably buggy for multiple projects; note that `vim.lsp.start` will reuse the client if root\_dir matches (can also pass a function).

[Next page](https://forum.elixirforum.com/t/refactorex-effortless-elixir-refactoring-in-vs-code/67759.md?page=2)
