1

Is there a way how to use loops or conditionals when creating snippets in VS Code? I am trying to create a snippet that will generate a template for JSDoc documentation syntax for a function. Example (I am using coffeescript):

myFunction: (param1, param2): ->
  # some code
  return

And I would like a snippet that generates:

###*
 * @param {} param1
 * @param {} param2
 * @return {}
###
myFunction: (param1, param2): ->
  # some code
  return

I am able to create a snippet, that will simply generate:

###*
 * @return {}
###

using this snippet settings:

"JSDocs Template": {
    "prefix": "jsdoc",
    "body": [
        "###*",
        " * @return {}",
        "###"
    ],
    "description": "create template for JSDocs"
}

But to achieve want I need, I would have to use a loop to go through the param list and that is where I struggle...

2 Answers 2

1

I am not sure is that possible using snippets. You can achieve this by writing your own extension using VS Code API.

But you can use this extension https://marketplace.visualstudio.com/items?itemName=stevencl.addDocComments to achieve what you trying to achieve in your example.

Update: You have to modify this extension script a little bit.

  1. Go to C:\Users\%UserProfile%.vscode\extensions\stevencl.adddoccomments-0.0.8\out\
  2. Add this additional logic in the 'extension.js' file. enter image description here

Right now it only works for the ts and js file. Just added the coffeescript language type.

And it works!!! enter image description here

Mark it right ans if you agree.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for a tip, but it this extension doesn't work with CoffeeScript...
Check the update, I tested it and works fine with the change.
well, now it works for coffeescript but it is not generating the correct syntax, which is: ###* insted of /** and ### instead of */ . I am going to extend this extension or write my own, thanks for your help...
thanks for your effort, but I ended up creating my own extension, check my answer for details...
0

So after some research I found out that such a behaviour is not possible with snippets only, therefore I have create my own extension CoffeeScript JSDoc. Feel free to use it and extend it if necessary...

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.