The Nginx code style asks for a specific alignment of variables declarations that is strainuous to manually enforce:
size_t slen;
uint32_t hash;
ngx_int_t rc;
const u_char *p;
ngx_shm_zone_t *shm_zone;
ngx_slab_pool_t *shpool;
ngx_rbtree_node_t *node, *sentinel;
ngx_ssl_session_t *sess;
ngx_ssl_sess_id_t *sess_id;
ngx_ssl_session_cache_t *cache;
u_char buf[NGX_SSL_MAX_SESSION_SIZE];
ngx_connection_t *c;
Now, I make a point to respect a project's codestyle and architectural paradigms, but manually enforcing this style is particularly slow and tedious.
Here is a Tabular command I wrote to automate this task in Vim:
:Tabularize /\v\w\s*\zs(\s\|\*+)\ze\S+([,;]\|\s+\=)/l2r0l0
And here is a screencast showing it in action: https://terminalizer.com/view/48ca6d4c4091.
Personally, I mapped it to <leader>an
as in "align nginx-style":
nnoremap <leader>an :Tabularize /\v\w\s*\zs(\s\|\*+)\ze\S+([,;]\|\s+\=)/l2r0l0<cr>
The degree of extensibility and workflow optimization made possible by Vim and its third-paty plugins has never ceased to amaze me, and this is a small example of it.
Are there more formatting tools available to enforce such code style requirements? I could not find any!
That's a nice one! the [,;] is a cool trick! :)
GNU Emacs knows how to align those by default using
m-x align RET
. The only difference being the 2 spaces in between vs 1.The hand-made version with 2 spaces would be something like this:
m-x align-regexp RET \( \(\*\| \)[^ ]+[;,]\)
. (or modify the default used byalign
)