0

At work I'm forced to work on custom mapping-files which map relations between objects of different systems.

These mappings, which are basically Jinja templates, are unfortunately stored in single lines, so that a simple mapping like (just made that up for illustration purposes):

{% for color in colors %}
    {% if color == 'white' or color == 'blue' or color == 'magenta' %}
        FF
    {% endif %}
{% endfor %}

Would be stored in the CSV, together with some additional attributes, like this:

Name,SomeFlag,Mapping
Mapping_Blue,TRUE,`{% for color in colors %}{% if color == 'white' or color == 'blue' or color == 'magenta' %}FF{% endif %}{% endfor %}`

As you can imagine, this is a pain to work with during day to day editing, for reviews, etc. In practice I typically find myself substituting } with line breaks and then do the indentation manually.

I wonder if there's a way to do this in vim, so that I could view the described CSV in a format similar to this:

Name,SomeFlag,Mapping
Mapping_Blue,TRUE,`
    {% for color in colors %}
        {% if color == 'white' or color == 'blue' or color == 'magenta' %}
            FF
        {% endif %}
    {% endfor %}`

Ideally, it would also collapse it back into one line when saving the file. Is there a way to achieve this? I'm using vim, but switching to neovim would be possible.

Best, Phil

3
  • 1
    You could probably write some autocommands, but there would be some edge cases. Might not be a bad idea to write a parser & formatter program in some language that you can use to switch between formats.
    – D. Ben Knoble
    Jul 7, 2021 at 12:51
  • 1
    @D.BenKnoble Interesting, didn't know about autocommands. Just looked into it and it seems like the right thing to do. Could just do the expanding / collapsing of lines with an autocommand on read or write, and then leave the formatting to an existing Jinja formatter. Thanks! Jul 8, 2021 at 6:24
  • If you get something working, please post an answer!
    – D. Ben Knoble
    Jul 8, 2021 at 12:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.