prof-rossetti / intro-to-python

An Introduction to Programming in Python
Other
97 stars 244 forks source link

Customizing VS Code Snippets #32

Closed s2t2 closed 3 years ago

s2t2 commented 4 years ago

Use the command palette and starty typing "snippets" to find the "Preferences > Configure User Snippets" setting which should yield a snippets JSON file.

See below custom snippet for auto-generating a class constructor method. Todo: placeholder args perhaps.

{
    // Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
    // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
    // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
    // used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
    // Placeholders with the same ids are connected.
    // Example:
    // "Print to console": {
    //  "scope": "javascript,typescript",
    //  "prefix": "log",
    //  "body": [
    //      "console.log('$1');",
    //      "$2"
    //  ],
    //  "description": "Log output to console"
    // }

    "Python Function Definition": {
        "prefix": ["def"],
        "body": [
            "def my_func():",
            "    pass"
        ],
        "description": "A function definition in Python."
    },

    "Python Class Constructor": {
        "prefix": ["init", "__init", "defi"],
        "body": [
            "def __init__(self):",
            "    pass"
        ],
        "description": "A class initializer method for Python."
    },

    "Python Main Conditional": {
        "scope": "python",
        "prefix": ["main", "__main"],
        "body": [
            "if __name__ == '__main__':",
            "    pass"
        ],
        "description": "The main conditional for Python."
    }
}

https://code.visualstudio.com/docs/editor/userdefinedsnippets

s2t2 commented 3 years ago

They took away helpful Python snippets?

Restore them one at a time...

{
    // Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
    // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
    // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
    // used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
    // Placeholders with the same ids are connected.
    // Example:
    // "Print to console": {
    //  "scope": "javascript,typescript",
    //  "prefix": "log",
    //  "body": [
    //      "console.log('$1');",
    //      "$2"
    //  ],
    //  "description": "Log output to console"
    // }

    "Python Class Constructor": {
        "prefix": ["init", "__init", "defi"],
        "body": [
            "def __init__(self):",
            "    pass"
        ],
        "description": "A class initializer method, why was this not included?"
      },

    "Python Main Conditional": {
        "scope": "python",
        "prefix": ["main", "__main"],
        "body": [
            "if __name__ == '__main__':",
            "    pass"
        ],
        "description": "The main conditional, why was this taken away?"
      }
}