0

I often find myself having to reformat C++ function arguments and template parameters to occupy individual lines rather than being on the same line. Here are a few examples.

Turn this

template <typename T>
std::enable_if_t<some_type_trait<T>, int> foo(const T&);

into this

template <typename T>
std::enable_if_t<
    some_type_trait<T>,
    int
> foo(const T&);

Turn this

double inner_product(const std::array<double,3>& a, const std::array<double,3>& b);

into this

double inner_product(
    const std::array<double,3>& a,
    const std::array<double,3>& b
);

Does a plugin that does that already exist? If not, can someone help me write a vim function for it? What I'd like to do is put the cursor inside the outer parentheses or angle brackets and press a key combination to reformat the arguments to individual lines. Not recursively. Just the top level. The number of arguments can be arbitrary. And, as I tried to illustrate in the second example, there may be additional commas that don't separate arguments. So, a bit similarly to how vim-surround works, except, instead of replacing the type of brackets, I want the code inside the brackets reformatted.

4
  • 1
    Using clang-format as an external formatting tools seems to become increasingly popular. Has the advantage that everybody working on a project will have the same formatting, regardless of their editor. Instead of wasting time tweaking your editor, you'll waste time tweaking clang-format.
    – Friedrich
    Oct 17, 2023 at 18:30
  • A comment on vim SO that advocates not doing something in vim. lol
    – SU3
    Oct 17, 2023 at 18:34
  • Glad my comment amused you. I wasn't so much advocating as mentioning what is - in my experience - a common setup. I don't even like it, if you must know.
    – Friedrich
    Oct 17, 2023 at 19:02
  • What did you try?
    – romainl
    Oct 18, 2023 at 5:10

2 Answers 2

2

With tpope/vim-surround, I believe cSbb and cS>> would do, since the S forces multi-line. You might have to touch up the insides (like :substitute/,/&\r/g) afterwards; I’m not able to test and confirm at the moment.

1
  • From what I can tell, the difference is that cSbb in vim-surrond autoindents the affected lines afterwards. I like what vim-argwrap does better. But thanks for chipping in. I didn't know vim-surrond basically solved the problem already.
    – SU3
    Oct 17, 2023 at 20:31
1

I found an existing plugin that does exactly what I wanted.

vim-argwrap

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.