ArgumentRewrap - A Vim plugin to automatically split argument lists
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.
Bruce Weirdan on Wed, 22 Dec 2010 18:34:20 +0100
Does it support different code styles? For example, ZF style guide forbids line breaks after opening or before closing brace - would it be easy to adapt/configure your plugin to follow ZF coding style instead of what you shown in this post?
Link to commentJakob on Wed, 22 Dec 2010 22:20:40 +0100
Hi Bruce,
Link to commentI added this as a feature request to the project at github [1]. If I have got some spare time during the holidays I will implement some sort of configuration options.
Cheers,
Jakob
[1] https://github.com/jakobwesthoff/argumentrewrap/issues/issue/2