0

What kind of plugin, cindent options, or alike, can I use to indent the code in two column, the first column indented based on block-depth, and the second based on the column of the assignators of the current block. So writing code like this is easy:

   left col    op   right col
|_____________|__|____________
let obj        = {
  somekey      : value,
  nested       : {
    akey       : value,
    ...
  }
  ...
},

At least I would like to cover the following two usages.

Case 1: automatically indent user-defined operators, like = and :, to the column of the current block. Once the operator is typed vim auto indents based (a) the previous block column in the case of the first item, (b) on the current block column, or (c) on the current cursor in the case where user has tab it manually.

a

let something_a     = {
    here:<-- cursor
}

a autoindents like

let something_a     = {
    first_item      : <-- cursor

b

let something_b     = {
    something_long_long     :
    second_item:<-- cursor

b autoindents like

let something_b     = {
    something_long_long     : value,
    second_item             : <-- cursor

c

let something_c      = {
    manual_indent                :<-- cursor

c autoindents like

let something_c      = {
    manual_indent                : <-- cursor

Case 2 indent back the whole block to the further right operator, either automatically or by issuing some command. So we go from here

let something       = {
    here                    : value,
    something_long_long     : value,
    short                   : value,
    something_long_long_even_longer :<-- cursor
}

To there

let something       = {
    here                            : value,
    something_long_long             : value
    short                           : value,
    something_long_long_even_longer : value,
}
``
5
  • check out the vim-easy-align plugin
    – husB
    Commented Apr 3 at 6:12
  • Ooh boy, indent scripts can be tricky. You might be better off finding a program that does the formatting you like and filtering your text through it
    – D. Ben Knoble
    Commented Apr 3 at 15:53
  • @D.BenKnoble from the documentation of 'indentexpr' The expression must return the number of spaces worth of indent. [...] The evaluation of the expression must not have side effects! It must not change the text [...] so I guess that by using the indent/*vim system you cannot achieve what I am describing, is that so? Your suggestion is not fit to be formatting while insert mode, right? So, if I were to resolve the problem as described in the question I'd need a overkill solution needing LSP or similar mechanism?
    – ax ax
    Commented Apr 3 at 16:06
  • @husB those are useful, but do not indent on the fly while in insert mode.
    – ax ax
    Commented Apr 3 at 16:06
  • LSP not required. See :help equalprg/formatprg/:range!. These are better especially when indent/ style scripts are challenging to write (though those can setup both insert-mode indents and others)
    – D. Ben Knoble
    Commented Apr 4 at 21:04

1 Answer 1

1

There's a pair of plugins, Align and AutoAlign, that cover most of the cases described in the question. They can be download from here: http://www.drchip.org/astronaut/vim/align.html

With the bare config it handles 1.a, 1.b, and 2, but not 1.c

It does not add an extra space, but this last issue which can be easily achieve via When AutoAlign is invoked and changes the line, an optional function (or functions) may be called via Funcref(s): let g:AutoAlign_funcref10= function("SomeFunctionName").

For achieving 1.c it seems some modification on the plugin is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.