RefactorEx - Effortless Elixir Refactoring in VS Code ⚙️

Hello, Elixir enthusiasts! :wave:

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.

:movie_camera: Check it out in action:
RefactorEx Demo

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.

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. 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.
  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 :bulb: 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.

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.
80 Likes

Gods, this looks amazing!

Any chance to have it as a NeoVim plugin? :pray:

14 Likes

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!

7 Likes

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

12 Likes

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!

8 Likes

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. :handshake:

9 Likes

Congratulations! This has been a long time coming.

I am personally proud that GenLSP 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.

26 Likes

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

8 Likes

Amazing! :heart_eyes:

3 Likes

Hi, Gabriel. // fixed… sorry
Congrats!

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

[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.

1 Like

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.

17 Likes

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?

1 Like

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.

4 Likes

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.

4 Likes

Sorry.

I will check this.
Thanks.

1 Like

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.

1 Like

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

3 Likes

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)

1 Like

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

1 Like

For use in neovim (hacky),

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).

4 Likes