Indentation of line continuations in C++ braced initializer lists

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Indentation of line continuations in C++ braced initializer lists

Antony Lee
Hi,

In general I'm quite happy with vim's default C(++) indentation style (or rather, together with cinoptions=+l1 (indent just by 1sw after a case:) and +=h0 (don't indent after public:)). In particular, I like the fact that argument lists are indented by 2sw (the examples below use sw=2, just as a reference):

    // Case 1: 2-space indent.
    if (foo) {
      bar;
    }
    // Case 2: 4-space indent.
    some_func(
        arg_1,
        arg_2);

However, vim is by default unable to recognize a C++ braced-initializer list, which effectively have similar semantics as parentheses in a function call; it will indent

    // Case 3: oops, 2-space indent, but 4 would be preferred.
    some_type{
      arg1,
      arg2};

probably because, well, it does not distinguish this with Case 1 (and probably because braced initializer lists are a "relatively" recent addition to C++). In fact it "should" be easy to distinguish Case 1 from Case 3, as Case 1 always (AFAICT?) has a closing parenthesis just before the opening brace (excluding whitespace) (... and you may also want to check whether the word before the previous word is struct/class/union/namespace).

Does anyone know of a setting or a plugin which would solve this issue?

Just to be clear, I am not particularly interested in plugins that shell out to clang-format and other external tools, because these are far too opinionated and rigid IMO.

Thanks!

Antony

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Indentation of line continuations in C++ braced initializer lists

Erik Christiansen
On 17.09.17 03:33, Antony Lee wrote:
> However, vim is by default unable to recognize a C++
> braced-initializer list, which effectively have similar semantics as
> parentheses in a function call; it will indent

>     // Case 3: oops, 2-space indent, but 4 would be preferred.
>     some_type{
>       arg1,
>       arg2};

...
> In fact it "should" be easy to distinguish Case 1 from Case 3, as Case
> 1 always (AFAICT?) has a closing parenthesis just before the opening
> brace (excluding whitespace) (... and you may also want to check
> whether the word before the previous word is
> struct/class/union/namespace).

If you'd like all that, then it's DIY time, I suspect: :h indentexpr

Existing tweaking within braces is limited: :h cino-e , :h cino-^

As :h C-indenting says: "Vim is not a C compiler: it does not recognize
all syntax"

Admittedly, my vim version is not the very latest.

Erik

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.