Nico Win.
Nico Win.

Reputation: 23

How to write a multi-line description for user snippets in VS Code

I tried to create my own user snippet for pascal in VS Code. It worked fine and the multi-line descriptions I wrote were displayed correctly. But after a while, like a month, the descriptions with multiple lines aren't working anymore. The problem may be the code for the multi-line descriptions because descriptions with only one row still works and displays correct. However the multi-line descriptions aren't displayed correctly and get replaced with a {0} instead of the description I wrote.

This is how the multi-line descriptions are displayed:

https://i.sstatic.net/B61pT.png

Instead of my description there is a {0} and I don't know why because it worked fine a month ago.

This is the code I used:

{
    "SetValue":{
        "prefix": "SetValue",
        "body": "SetValue(${1:val:Integer}, ${2:id:Integer});",
        "description": [
            "Parameter:\r",
            "  val...desc\r",
            "  id....desc\r",
            "\r",

            "result:\r",
            "  0 : false desc\r",
            "  1 : true desc\r"
        ]
    }
}

I hope I have expressed myself understandably and you may can help me with this problem. Thanks for your attention!

Upvotes: 2

Views: 818

Answers (1)

Mark
Mark

Reputation: 181288

I can't explain why it changed but it appears to accept only one string (and not an array of strings). But you can still build up one string - a little ugly but it works:

"description":
          "Parameter:\rval...desc\r  id....desc\r\rresult:\r  0 : false desc\r  1 : true desc\r"

Now it will display in the suggestions panel as you expect.

Edit: v1.31 fixed this so you can use an array of strings rather than one long string. Snippet descriptions

When authoring snippets with long descriptions, in the past you were forced to write a long single string. There was no support for using an array as you could for body. This has now changed and long descriptions can be written using string arrays.

{
  "prefix": "happy",
    "body": "#Happy Coding!",
    "description": [
      " First Line",    // note the spaces after the opening parens
      " Second Line",
      " Third Line"
    ]
}

Thanks to @Ben questioning this, I found a bug and a fix.

    "description": [
      "First Line",    // won't work
      "Second Line",
      "Third Line"
    ]

    "description": [
      " First Line",    // will work
      " Second Line",
      " Third Line"
    ]

It won't work if the leading space is only on the "First Line", but will work if there is a leading space on " Second Line" or " Third Line". So I suggest just making a leading space on all the lines.

OR better this works:

 "description": [
    "First line",        // no leading spaces
    "Second line",
    "Third line",
    "\r"                 // just adding this makes it work, or \n
  ]

The OP's example works because there are some leading spaces and a "\r" in his description.

long description in a snippet


An issue, https://github.com/microsoft/vscode/issues/146493, has been filed on this.

Upvotes: 2

Related Questions