Indentation of line continuations in C++ braced initializer lists

115 views
Skip to first unread message

Antony Lee

unread,
Sep 17, 2017, 6:33:37 AM9/17/17
to vim_use
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

Erik Christiansen

unread,
Sep 17, 2017, 9:41:23 AM9/17/17
to vim_use
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
Reply all
Reply to author
Forward
0 new messages