skip to Main Content

I want to use the same key binding to fold (editor.foldAll) and unfold (editor.unfoldAll) everything in the editor. Here’s my current settings:

{
    "key": "ctrl+m",
    "command": "editor.foldAll",
    "when": "editorTextFocus && foldingEnabled"
},
{
    "key": "ctrl+shift+m",
    "command": "editor.unfoldAll",
    "when": "editorTextFocus && foldingEnabled"
}

I want to change the ‘when’ statement to something like this:

{
    "key": "ctrl+m",
    "command": "editor.foldAll",
    "when": "editorTextFocus && foldingEnabled && !isFolded"
},
{
    "key": "ctrl+m",
    "command": "editor.unfoldAll",
    "when": "editorTextFocus && foldingEnabled && isFolded"
}

But, of course, isFolded and anything similar don’t appear to be an available ‘when’ clause according to the VS Code when clause context.

Is there something I’m missing or is there another way to accomplish this?

2

Answers


  1. I folded all in a file and then looked through all the context keys in the Developer Tools, and nothing looked helpful (I assume you know how to do that).

    Essentially, what you want is a toggleFoldAll command which doesn’t exist (toggleFold only works on the current fold, not all of them), but was requested some time ago, see fold-all-toggle command. It didn’t gain enough interest. I think if there was a simple way to accomplish the same thing with context keys, a dev would have mentioned it. I think you are out of luck.

    You could raise an issue on gitHub asking for such a context key (although I would say that would be very hard performance-wise for the editor to keep track of whether every single possible foldable range is indeed folded or not) or ask for the toggleFoldAll command again.

    Login or Signup to reply.
  2. You can create your own context variables and toggle them with the extension Extra Context

    You have to execute 2 commands for the key binding so you also need to use multi-command

    {
        "key": "ctrl+m",
        "when": "editorTextFocus && foldingEnabled && !extraContext:isFolded",
        "command": "extension.multiCommand.execute",
        "args": { 
            "sequence": [
                "editor.foldAll",
                { "command": "extra-context.toggleVariable",
                  "args": { "name": "isFolded" }
                }
            ]
        }
    },
    {
        "key": "ctrl+m",
        "when": "editorTextFocus && foldingEnabled && extraContext:isFolded",
        "command": "extension.multiCommand.execute",
        "args": { 
            "sequence": [
                "editor.unfoldAll",
                { "command": "extra-context.toggleVariable",
                  "args": { "name": "isFolded" }
                }
            ]
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search