abonec
abonec

Reputation: 1431

Support in vim for specific types of comments

In ruby methaprogramming we have special types of comments within multiline string that will be evaluate. They looks like this: https://github.com/rails/rails/blob/8775ffa37231d381cba34f0ecacb8a7bbcf0573f/activesupport/lib/active_support/memoizable.rb#L77

This string divided line by line on two parts: string with interpolate that will be evaluate on the left side and example of code on the right side. Manually type this comments is a hard work. It requires many manual indentation.

Is there some plugins or tools in vim that help with code this type of comments?

Upvotes: 1

Views: 173

Answers (1)

sehe
sehe

Reputation: 392833

First of all, get to know the features that make life easier for stuff like this in vim:

Here are two approaches:

Assuming that the vertical split is always in a fixed column (like 84 in this sample):

/\%84v#\zs

will locate the vertical divider bar. Now you can operate on that, e.g.

Quick and dirty: Fold comments pairwise

This results in the comment lines being folded right after the 'template' line:

:g//s//\r         /

Note: the (9) spaces have been chosen to match the starting indent level of the OP's sample.
IRL, you could use indent('.') to figure out how many spaces programmatically

Presto:

enter image description here

To recombine:

:g//join!

Possibly combined with something to 'eat' the redundant indent (9 spaces)

:g//j!|norm! n9x

Alternatives:

I'd usually opt to split the blocks into physical files instead. Recombining them will take more effort, but editing is much more comfortable and you can leverage vim's diffmode.

Just a quick starter:

ggn<C-v>ND
:tabnew  | 0put
:tabprev
gvVxgvVd
:tabnext | vert new | 0put

put the windows in diffmode:

:windo diffthis

Now you can edit both windows independently, with live diff highlighting.

enter image description here

Let me know if you would like more input on this strategy. I might try my hand at recombining from the split temp-window configuration.

Upvotes: 5

Related Questions