Skip to content

Bash: Add snippets for VS Code #664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vscode-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ We strongly recommend that you install [shellcheck][shellcheck] to enable lintin
- [x] Documentation for flags on hover
- [x] Workspace symbols
- [ ] Rename symbol
- [x] Snippets

## Configuration

Expand Down
6 changes: 6 additions & 0 deletions vscode-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
],
"main": "./out/extension",
"contributes": {
"snippets": [
{
"language": "shellscript",
"path": "./snippets/snippets.json"
}
],
"configuration": {
"type": "object",
"title": "Bash IDE configuration",
Expand Down
16 changes: 16 additions & 0 deletions vscode-client/snippets/convention.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Snippet naming convention
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice with this documentation. Once the snippets are in TypeScript, then we should change this markdown file to some unit tests to ensure that this is enforced.


- Snippet name always language keyword, builtin name or expansion symbol like `:-`.
- `description` always a short snippet explanation without leading articles and trailing punctuation.
Mnemonics with square brackets are always used to denote what chars are included in snippet `prefix`.
- `prefix` can be just a string or array containing two item. The string or first item always follow these rules:
- If a snippet is for language construct then first two letters of the first word from `description` are used
plus the first letter from the second one (if it's exist).
- If a snippet is for a builtin then builtin name is used.
- If a snippet is for expansion then expansion symbol is used.
- If a snippet is for a specific external program like **awk** then program name must be added to `prefix` like this:
`awk:{{snippet-prefix}}`.
The second one is always a noun existing to make snippets more memorizable.
- `body` is always array.
- If both short and long options available placeholder must be used to let user to chose the option's style
but the first alternative should always be the long option.
228 changes: 228 additions & 0 deletions vscode-client/snippets/snippets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
{
"shebang": {
"description": "[sh]hebang",
"prefix": "shebang",
"body": [
"#!/usr/bin/env ${1|bash,sh|}"
]
},
"if": {
"description": "[if]",
"prefix": "if",
"body": [
"if ${1:command}; then",
"\t$0",
"fi"
]
},
"if-else": {
"description": "[if] [e]lse",
"prefix": "if-else",
"body": [
"if ${1:command}; then",
"\t${2:echo}",
"else",
"\t$0",
"fi"
]
},
"while": {
"description": "[wh]ile",
"prefix": "while",
"body": [
"while ${1:command}; do",
"\t$0",
"done"
]
},
"until": {
"description": "[un]til",
"prefix": "until",
"body": [
"until ${1:command}; do",
"\t$0",
"done"
]
},
"for": {
"description": "[fo]r",
"prefix": "for",
"body": [
"for ${1:variable} in ${2:list}; do",
"\t$0",
"done"
]
},
"function": {
"description": "[fu]nction",
"prefix": "function",
"body": [
"${1:function_name}() {",
"\t$0",
"}"
]
},
"main": {
"description": "[ma]in",
"prefix": "main",
"body": [
"main() {",
"\t$0",
"}"
]
},
":-": {
"description": "[:-] expansion",
"prefix": ":-",
"body": [
"\"\\${${1:variable}:-${2:default}}\""
]
},
":=": {
"description": "[:=] expansion",
"prefix": ":=",
"body": [
"\"\\${${1:variable}:=${2:default}}\""
]
},
":?": {
"description": "[:?] expansion",
"prefix": ":?",
"body": [
"\"\\${${1:variable}:?${2:error_message}}\""
]
},
":+": {
"description": "[:+] expansion",
"prefix": ":+",
"body": [
"\"\\${${1:variable}:+${2:alternative}}\""
]
},
"#": {
"description": "[#] expansion",
"prefix": "#",
"body": [
"\"\\${${1:variable}#${2:pattern}}\""
]
},
"##": {
"description": "[##] expansion",
"prefix": "##",
"body": [
"\"\\${${1:variable}##${2:pattern}}\""
]
},
"%": {
"description": "[%] expansion",
"prefix": "%",
"body": [
"\"\\${${1:variable}%${2:pattern}}\""
]
},
"%%": {
"description": "[%%] expansion",
"prefix": "%%",
"body": [
"\"\\${${1:variable}%%${2:pattern}}\""
]
},
"..": {
"description": "brace expansion",
"prefix": "..",
"body": [
"{${1:from}..${2:to}}"
]
},
"echo": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how much value very basic snippets like echo and source provides.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to save some keystrokes and quote argument automatically. :)

"description": "[echo]",
"prefix": "echo",
"body": [
"echo \"${1:message}\""
]
},
"printf": {
"description": "[printf]",
"prefix": "printf",
"body": [
"printf '%s' \"${1:message}\""
]
},
"source": {
"description": "[source]",
"prefix": "source",
"body": [
"source \"${1:path/to/file}\""
]
},
"alias": {
"description": "[alias]",
"prefix": "alias",
"body": [
"alias ${1:name}=${2:value}"
]
},
"cd": {
"description": "[cd]",
"prefix": "cd",
"body": [
"cd \"${1:path/to/directory}\""
]
},
"getopts": {
"description": "[getopts]",
"prefix": "getopts",
"body": [
"getopts ${1:optstring} ${2:name}"
]
},
"jobs": {
"description": "[jobs]",
"prefix": "jobs",
"body": [
"jobs -x ${1:command}"
]
},
"kill": {
"description": "[kill]",
"prefix": "kill",
"body": [
"kill ${1|-l,-L|}"
]
},
"let": {
"description": "[let]",
"prefix": "let",
"body": [
"let ${1:argument}"
]
},
"test": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test seems really useful with all the different options here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=~ has been added.

"description": "[test]",
"prefix": "test",
"body": [
"[[ ${1:argument1} ${2|-ef,-nt,-ot,==,=,!=,=~,<,>,-eq,-ne,-lt,-le,-gt,-ge|} ${3:argument2} ]]"
]
},
"stream": {
"description": "[de]vice name",
"prefix": "dev",
"body": [
"/dev/${1|null,stdin,stdout,stderr|}"
]
},
"sed:filter-lines": {
"description": "[sed:filter-lines]",
"prefix": "sed:filter-lines",
"body": [
"sed ${1|--regexp-extended,-E|} ${2|--quiet,-n|} '/${3:pattern}/' ${4:path/to/file}"
]
},
"awk:filter-lines": {
"description": "[awk:filter-lines]",
"prefix": "awk:filter-lines",
"body": [
"awk '/${1:pattern}/' ${2:path/to/file}"
]
}
}