pseewald / vim-anyfold

Language agnostic vim plugin for folding and motion based on indentation.
MIT License
268 stars 11 forks source link

Question re: brace terminators (C/C++) #37

Open Falkgaard opened 3 years ago

Falkgaard commented 3 years ago

Is it possible to configure it so that the line with the terminating brace } and/or blank lines gets included in the fold?

If I have:

void f() {
   std::puts("foo");
}

void g() {
   std::puts("bar");
}

struct baz {
  int x;
  int y;
};

I'd like it to fold into:

void f() { ---------------------------------- 4 lines
void g() { ---------------------------------- 4 lines
struct baz { -------------------------------- 5 lines

instead of:

void f() { ---------------------------------- 2 lines
}

void g() { ---------------------------------- 2 lines
}

struct baz { -------------------------------- 3 lines
};

EDIT 1:

It seems like putting the opening brace on its own line might accomplish what I want, I'll report back here after further testing.


EDIT 2:

Never mind that... same results regardless, but in some cases it seems to fold as I want, and in others it doesn't. Not quite sure yet what's causing the difference in behaviour for me.

But one issues seems to be when the brace ends with a semi-colon such as in the case of struct, class, union, enum, or brace initialization. But then again, in some cases it works... for example:

In one file I have these (white space annotated for clarity):

// ...
<blank line>
// using aliases:
<tab> using U8  = uint8_t;
<tab> using U16 = uint16_t;
<tab> using U32 = uint32_t;
<tab> using U64 = uint64_t;
<tab> using I8  = int8_t;
<tab> using I16 = int16_t;
<tab> using I32 = int32_t;
<tab> using I64 = int64_t;
<tab> using F32 = float;
<tab> using F64 = double;
<blank line>
struct FG { // foreground color
<tab> U8 code = 255;
};
<blank line>
[[nodiscard]] inline auto to_string( FG const fg ) -> std::string {
<tab> return std::string("38;5;") + std::to_string(fg.code);
}
<blank line>
struct BG { // background color
<tab> U8 code = 238;
};
<blank line>
// ...

and I can fold all the using aliases into the comment, the function and BG properly, but not FG.

If I do try to fold them all it becomes:

// ...
// using aliases:                                                      12 lines
struct FG { // foreground color                                         2 lines
};
<blank line>
[[nodiscard]] inline auto to_string( FG const fg ) -> std::string {     4 lines
struct BG { // background color                                         4 lines
// ...

So in the above case all but FG work as desired. Super weird.

Falkgaard commented 3 years ago

I've found a hacky workaround that generally seems to give the result I'm looking for:

I insert a space between every }; so it becomes } ;, and I appent a space and comment after every free-standing } (i.e. } //) that I want to to terminate and foldーwhich generally are functions.

tejasvi commented 2 years ago

You can use Plug tejasvi/vim-anyfold meanwhile.