Emacs Refactoring: Intial Release

by Chris Barrett

I’ve spent the couple of weeks on the beginnings of a refactoring toolset I’m calling Emacs Refactor (EMR). My goal is to have a simple, unified UI for refactoring in Emacs that I can easily extend for any language mode that has syntax transformation tools.

EMR

I’ve made an initial release that comes bundled with refactoring commands for Emacs Lisp. Get it from GitHub: https://github.com/chrisbarrett/emacs-refactor

Motivation

Since I’ve left the Visual Studio toolchain I’ve been yearning for visual refactoring tools like ReSharper. There’s a lot of value in features like variable inlining and function extraction, even in languages that aren’t as verbose as C#.

The feature that I love most about ReSharper is the context-awareness of its UI. Your main refactoring interface is a popup menu with smart refactoring options. When you’re editing, you move the cursor to the thing you want to refactor and hammer alt+enter until your code looks right.

EMR provides a unified UI for code transformations. Rather than bind different refactoring commands to different keys or M-x-ing all over the place, I should be able to put my cursor somewhere and let Emacs show me some things I can do. Context sensitivity is where it’s at.

Extensibility

EMR lets you add commands to its popup menu. Here’s an example from the Elisp refactoring commands:

This snippet enables the emr-extract-to-let command in Emacs Lisp mode. It will not be shown if the predicate is not satisfied.

Future

Many of the Elisp refactorings I’ve implemented are available in Redshank, which I’ve only just heard of (hat-tip Steve Purcell). I’ll be switching to Redshank commands where suitable.

My next port of call will be adding EMR commands to Clojure Mode. I’m looking forward to using Overtone with this stuff.

Advertisements