Search code examples
visual-studio-codenestedplaceholdercode-snippetsvscode-snippets

Snippets: nested placeholders not working


I'm trying to get nested placeholders to work in snippets, which should work according to the documentation. But when using it it's basically treated as a single tab stop.

Test snippet

{
    "nestedPlaceholders": {
        "prefix": "nestedPlaceholders",
        "body": ["<test ${1:first placeholder ${2:Second placeholder}} />"],
        "description": "Test for nested placeholders"
    }
}

Result video

Any ideas? Thanks in advance.


Solution

  • I would say that is expected behaviour. Here is what is happening. In this form:

    ${1:first placeholder ${2:Second placeholder}}
    

    The ${2:Second placeholder} is part of the first placeholder! So it all should appear selected on the first tabstop and when you type over that first placeholder default text, you also delete the second tabstop/placeholder. It works much better if the order is:

    ${2:second placeholder ${1:first placeholder}}
    

    Now accepting or typing over the first placeholder does not delete the second placeholder. But the first placeholder default text (first placeholder in my example) is still part of the second placeholder text and so will be eliminated if you type over it. It will remain if you accept the second placeholder text with a Tab.

    Obviously, if you want the two placeholders to operate independently, don't nest them:

    "<test ${1:first placeholder} ${2:Second placeholder} />",
    

    So I think the nested placeholder example in the docs is probably not helpful in very many situations because if you want the second placeholder default text but not the first - you lose the second text so what's the point. But I do think it is actually operating as it should - it just isn't very helpful in that form.