contact Me

Use the form on the right to contact me.

You are welcome, to contact me regarding the topics of this page, my open source projects, or my work. Please use the contact form and leave a valid email address for me to respond to.

Thank you.

Egidestr. 9
44892 Bochum
Germany

/brain/dump

Random thoughts, bright ideas and interesting experiments. In short the ramblings of a fulltime nerd.

 

ArgumentRewrap - A Vim plugin to automatically split argument lists

Jakob Westhoff

I have been using Vim as my primary PHP development environment for several years by now. Inspired by two blog articles (Coming Home To Vim, Vim Toolbox, 2010 Edition) I recently read I decided to cleanup my Vim configurations and create certain new macros for recurring tasks. One of these macros is the ArgumentRewrap plugin, which instantly saves me a lot of time.

What does this plugin do exactly?

In a lot of situations wrapping arguments of a function call or an array definition is the only suitable way to produce readable source code, which uses an acceptable maximal line length. The plugin does rewrap a list of arguments to multiple lines with correct indentation.

Example

Just to make sure everybody is on the same page, take a look at the following example:

$foo( "bar", array( 1, 2, 3 ), "baz" );

Even though a line like this isn't that long it still is kind of hard to read. A rewrapped and splitted version provides better readability:

$foo(
    "bar",
    array( 1, 2, 3 ),
    "baz"
);

Supported parenthesis

In different languages all kinds of parenthesis are allowed for housing lists of arguments. Therefore all this different possibilities are supported ("()", "[]", "{}"). It is automatically detected which type needs to be handled.

Multiple parenthesis groups and levels

Inside one line of source code multiple groups of different or even equal parenthesis are quite usual. Think about adding an element to an array:

$foo[] = array( "baz" => "bar", "blub" => "blam" );

ArgumentRewrap searches for the first parenthesis group which does contain an argument list, more precisely a comma. Therefore the given example is automatically wrapped as expected:

$foo[] = array(
    "baz" => "bar",
    "blub" => "blam"
);

Multiple levels of nesting need to be handled as well, as they occur quite often as well:

$foo = array( "one" => array( 1, 2, 3 ), "two" => array( 4, 5, 6 ) );

ArgumentRewrap does handle this situation by just rewrapping the outer argument list. Inner lists are just copied over as one argument:

$foo = array(
    "one" => array( 1, 2, 3 ),
    "two" => array( 4, 5, 6 )
);

If you want to further split these arguments, simply select the lines containing them and call ArgumentRewrap again:

$foo = array(
    "one" => array(
        1,
        2,
        3
    ),
    "two" => array(
        4,
        5,
        6
    )
);

Download and Availability

The plugin is hosted on github. Installation instructions as well as the latest information about ArgumentRewrap can always be found there.