-
Notifications
You must be signed in to change notification settings - Fork 0
Additional Files
The additionalFiles setting lets you automatically create extra files when you make a package. You set up rules with conditions, and when those conditions match, the files get created.
{
"new-python-package.additionalFiles": [
{
"when": { /* optional conditions */ },
"files": [ /* files to create */ ]
}
]
}All conditions are optional. Skip the when block entirely and the rule always runs. If you specify multiple conditions, they all have to match.
Glob pattern for the package's path. This is the most useful one.
{
"when": {
"pathMatches": "**/modules/**"
}
}Some patterns:
| Pattern | What it matches |
|---|---|
**/modules/** |
Any package anywhere under a modules folder |
**/tests/** |
Any package under tests
|
**/modules/{cogs,events}/** |
Packages under modules/cogs or modules/events
|
src/modules/** |
Only packages under src/modules specifically |
So if you have modules/cogs/ and modules/events/, the pattern **/modules/** catches both.
Regex for the package name itself.
{
"when": {
"nameMatches": "^test_"
}
}| Pattern | Matches |
|---|---|
^test_ |
Names starting with test_
|
_test$ |
Names ending with _test
|
^[a-z]+$ |
Lowercase-only names |
Check if a file exists somewhere in the workspace.
{
"when": {
"workspaceContains": "pyproject.toml"
}
}Good for project-type detection.
{
"when": {
"pathMatches": "**/modules/cogs/**",
"workspaceContains": "**/discord/**"
}
}This only triggers when BOTH are true.
Three ways to specify content:
-
content- Inline string -
templateFile- Load from a file -
snippet- Insert a VS Code snippet
{
"files": [
{
"name": "${name}.py",
"content": "# ${name:pascal} module"
}
]
}Point to a template in your workspace:
{
"files": [
{
"name": "${name}.py",
"templateFile": ".vscode/templates/module.py"
}
]
}The template file can use all the same Variables. Put your templates in .vscode/templates/ or wherever makes sense.
Opens the file and inserts a VS Code snippet:
{
"files": [
{
"name": "${name}.py",
"snippet": "python-class"
}
]
}You get full snippet features: tabstops, placeholders, choices. Define your snippets in VS Code's user snippets.
You can combine them. Template content goes in first, then the snippet gets inserted:
{
"files": [
{
"name": "${name}.py",
"templateFile": ".vscode/templates/base.py",
"snippet": "python-method"
}
]
}Create several files at once:
{
"files": [
{
"name": "${name}.py",
"content": "class ${name:pascal}:\n pass"
},
{
"name": "test_${name}.py",
"content": "import pytest\nfrom .${name} import ${name:pascal}"
}
]
}{
"new-python-package.additionalFiles": [
{
"when": {
"pathMatches": "**/cogs/**"
},
"files": [
{
"name": "${name}.py",
"templateFile": ".vscode/templates/cog.py"
}
]
},
{
"when": {
"pathMatches": "**/tests/**"
},
"files": [
{
"name": "test_${name}.py",
"content": "import pytest\n\n\nclass Test${name:pascal}:\n pass"
}
]
},
{
"when": {
"nameMatches": "^api_"
},
"files": [
{
"name": "routes.py",
"content": "from fastapi import APIRouter\n\nrouter = APIRouter(prefix=\"/${name:slice:4:}\")"
}
]
}
]
}