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:
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!
Highlight a code snippet or place your cursor where a refactoring is needed.
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.
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
I know there is official LSP on its way and I’m already looking into how to merge these refactorings into it.
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.
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!
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.
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)
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.
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?
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.
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.
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)
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).