1

I created a vscode snippet for a a python function definiton and I am wondering if this is the only and/or recommended way of escaping the python block comments (""" """).

global snippets file

{   
    "def ": {
            "scope": "python",
            "prefix": "def func",
            "body": [
                "def $1 ():
                \"\"\"\" \"\"\"\    "
            ],
            "description": "Python function"
    }
}

output

def  ():  
""" """   
Mark
  • 143,421
  • 24
  • 428
  • 436
RSale
  • 463
  • 5
  • 14

2 Answers2

1

Specify each line in a separate string of the body:

You can choose the kind of doc string delimiters.

The doc string should be indented (\t) and the body.

{   
  "def": {
    "scope": "python",
    "prefix": "def func",
    "body": [
      "def $1 ():",
      "\t${2|\"\"\",''',\",'|} $3 $2",
      "\t$0"
    ],
    "description": "Python function"
  }
}
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • You are right, this should have been indented. But let's assume the indentation wasn't needed. How would you escape the block comment? – RSale Jan 02 '22 at 13:10
  • I just saw that you are writing the body as three seperate lines / comma seperated. Is that generally adviced when writing json code? – RSale Jan 02 '22 at 13:13
  • @RSale You can combine them in 1 string with `\n`, but separate strings make it a bit more readable if you have many lines – rioV8 Jan 02 '22 at 14:06
  • @RSale You can choose the kind of doc string delimiters, you don't have to use `"""` – rioV8 Jan 02 '22 at 14:14
1

I just saw that vscode has a built in variable for block comments. The code after the block comment should be indented, too.


"def": {
        "scope": "python",
        "prefix": "def func",
        "body": [
            "def $1 ():",
            "\t$BLOCK_COMMENT_START $2 $BLOCK_COMMENT_END",
            "\t$0"
        ],
        "description": "Python function"

RSale
  • 463
  • 5
  • 14