Wrangler - an Erlang Refactoring Tool

Copyright © 2006-2012 Huiqing Li, Simon Thompson

Authors: Huiqing Li (H.Li@kent.ac.uk), Simon Thompson (s.j.thompson@kent.ac.uk) [web site: http://www.cs.kent.ac.uk/projects/wrangler].

Wrangler is an Erlang refactoring tool that supports interactive refactoring of Erlang programs.

Contents

  1. Introduction
  2. The Interface of Wrangler
  3. Support for Macros
  4. Refactoring Code with Syntax Errors
  5. Download
  6. How to Use Wrangler
  7. References
  8. Acknowledgments

Introduction

Refactoring is the process of improving the design of a program without changing its external behaviour. While refactoring by hand is possible, tool support for refactoring is invaluable as it is more reliable, and allows refactoring to be done (or undone) easily. Wrangler is such a tool that supports interactive refactoring of Erlang programs. Wrangler currently supports a number of basic Erlang refactorings, such as renaming of variable/function/module names, generalisation of a function definition, function extraction, folding against a function definition, duplicated code detection, etc.

Wrangler also provides a set of process-based refactorings, including the introduction of process naming, instead of providing access to a process only through a dynamically-generated process id Wrangler also contains a variety of refactorings which work with Erlang QuickCheck, introducing and merging constructs such as ?LET and ?FORALL.

Transforming data types in Erlang can be difficult, as Erlang functions can take values of any type, in principle. There are situations -- particularly in implementing an Erlang behaviour , which can be thought of as an interface for a set of callback functions -- where the types are more constrained, and where data transformations are possible. In particular we have implemented transformations for state data in various kinds of generic state machine in both the OTP library and QuickCheck.

In implementing Wrangler we have chosen to respect various features of the language and related tools. Wrangler is able to process modules which use macros, including the Erlang test frameworks that are in regular use. Wrangler also respects the naming conventions in those test frameworks.

Wrangler provides a portfolio of decision support tools. The code inspector highlights local code smells and a number of reports highlight issues in the module structure of projects, including circular inclusions and other potential faults. The code clone detection facilities can be used on large multi-module projects to report on code clones and how they can be removed; clone detection can be preformed incrementally on larger code bases, for example as part of a continuous integration approach to software construction.

Wrangler has recently been extended with a framework that allows users to define for themselves refactorings and code inspection functions that suit their needs. These are defined using a template- and rule-based program transformation and analysis API. Wrangler also supports a domain-specific language that allows users to script composite refactorings, test them and apply them on the fly. User-defined refactorings and scripts are not second-class citizens: like the existing Wrangler refactorings, user-defined refactorings benefit from features such as results preview, layout preservation, selective refactoring, undo and so on.

Built on top of the functionalities provided by the Erlang syntax-tools package, Wrangler is embedded in the Emacs/Xemacs editing environment, and makes use of the functionalities provided by Distel, an Emacs-based user interface toolkit for Erlang, to manage the communication between the refactoring tool and Emacs. Wrangler is also integrated within Eclipse, as a part of the ErlIDE plugin for Erlang.

The Interface of Wrangler

Wrangler in Emacs

Below is a snapshot of Wrangler embedded in Emacs, which shows a particular refactoring scenario. The user has selected the expression io:format("Hello") in the definition of repeat/1 , has chosen the Generalise Function Definition command from the Refactor sub-menu, and has just entered a new parameter name A in the mini-buffer.

selecting a refactoring from the Emacs menu

The result of this refactoring is shown in the snapshot below.

Emacs buffers after running the refactoring

Preview of the changes to be made by a refactoring is supported, and the user could choose to commit or abort the refactoring result after preview. Wrangler's preview uses the Ediff package for Emacs.

All the implemented refactorings are module-aware. In the case that a refactoring affects more than one module in the program, a message telling which files have been modified by the refactorer will be given after the refactoring has been successfully done. The Customize Wrangler command from the Refactor sub-menu allows the user to specify the boundary of the program, i.e., the directories that will be searched and analysed by the refactorer, as shown in the example snapshot below.

customizing Wrangler behaviour in Emacs

Undo is supported by the refactorer. Applying undo once will revert the program back to the status right before the last refactoring performed. However, PLEASE NOTE that the refactorer undo is currently separate from the Emacs undo, and undoing a refactoring will lose all the editing activities after the refactoring.

Wrangler in Eclipse

Below is a snapshot of Wrangler embedded in Eclipse + Erlide.

snapshot of Wrangler embedded in Eclipse with Erlide

Support for Macros

Wrangler makes use of functionalities provided by the epp_dodger module from the Erlang syntax_tools library to parse Erlang source code, and should be able to refactor Erlang modules containing preprocessor directives and macro applications, as long as the macro definitions are syntactically "well-behaved".

Refactoring Code with Syntax Errors

Wrangler accepts Erlang programs with syntax errors or macro definitions that cannot be parsed by epp_dodger. In the case that the program under consideration has syntax errors or unparsable macros, functions/attributes to which these syntax errors belong are not affected by the refactoring process; however, warning messages asking for manual inspection of those parts of the program are given after the refactoring.

Download

Wrangler is downloadable from Github: https://github.com/RefactoringTools, and also from http://www.cs.kent.ac.uk/projects/wrangler

How to Use Wrangler

  1. Open an Erlang source file in the Emacs editor. You should now have a menu called Erlang, if you have configured Emacs properly to use the Erlang-mode (see the file INSTALL otherwise).

  2. To start Wrangler, type: M-x erlang-wrangler-on, or alternatively Ctrl-c Ctrl-r. After that, new menu Wrangler should appear in the menu bar (see INSTALL otherwise).

  3. To stop Wrangler, type M-x erlang-wrangler-off, or alternatively use Ctrl-c Ctrl-r again. The Refactor and Inspector menus will disappear from the menu bar.

  4. You can use Ctrl-c Ctrl-r to toggle Wrangler on or off.

  5. For most refactorings, the editor interface will pass the current filename (should be the module name, as well), the refactoring command, and the current cursor position. For some refactorings, you'll also need to highlight an expression, or enter a new name.

  6. The Customize Wrangler menu in the Wrangler submenu allows you to specify the boundary of the system by giving the list of directories to search for Erlang source files/header files that could be affected by a refactoring.

  7. For most refactorings, the editor interface will pass the current filename (should be the module name, as well), the refactoring command, and the current cursor position. For some refactorings, you'll also need to highlight an expression, or enter a new name.

    How to use:
    Extensions:

References

Our Refactoring Functional Programs project website contains more information and references about refactoring functional, especially Haskell, programs; and our progress and ideas about refactoring Erlang programs are available at our Wrangler project webpage. Here are some publications related to Wrangler:

Acknowledgments

The work was initially supported by EPSRC in the UK, and is now by the European Commission. We are very grateful that we were able to build the tool on top of Distel and Erlang SyntaxTools, and make use some of the ideas from Dialyzer.

Generated by EDoc, Nov 5 2015, 16:30:22.