15

I am dealing with json files with multiple repeated blocks with just one field value(integer) changed - in fact, every changed value is just the previous corresponding value incremented. To make such files easy to read, I would like to be able to fold this entire set of repeated blocks into just one block with some indication of what the ranges of the changing value across these blocks is. Searching for code folding methods in emacs, I found hs-minor-mode and outline-mode. However, I don't see how to go from the functionality they provide to the one I desire. Could someone provide a rough overview of how I could achieve this?

EDIT : An example of what I am trying to do:

Given a json file that looks like this:

{
    "SomeArray": [
        {
        "ChangingField": 1,
        "ConstantField" : "X"
        },
        {
        "ChangingField": 2,
        "ConstantField" : "X"
        },
        {
        "ChangingField": 3,
        "ConstantField" : "X"
        },
        {
        "ChangingField": 4,
        "ConstantField" : "X"
        },
        {
        "ChangingField": 5,
        "ConstantField" : "X"
        }
    ]
}

I would like to have it displayed like this:

{
    "SomeArray": [
        {
        "ChangingField": 1...5,
        "ConstantField" : "X"
        }
    ]
}

Apologies for the very open-ended question.

  • 1
    Good question - that would be quite useful. – Drew Feb 13 '15 at 14:59
8

There are more folding options for Emacs: http://wikemacs.org/wiki/Folding#Yafolding and Yafolding looks like the best for your case since it is based on indentation. It also includes a nice interface à-la Magit to discover the commands.

However, that doesn't fully answer your question since it won't show the range of the fields out of the box.

Yafolding is in MELPA:

M-x package-install yafolding
1

Use emacs' hideshow. You can keep your cursor anywhere & to hide blocks 'n' level below it, use the command

C-c @ C-l

Here, n is a numeric prefix argument that gets set by pressing

C-u 1 RET

Read more about hsmode here, hs-mode. More about prefix arguments here, prefix args hs mode doesn't work on Default mode. You need to have js2-mode (or) js-mode in place for this to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.