Skip to content

Commit d9dc4cd

Browse files
authored
Merge pull request #664 from emilyseville7cfg-plugins/feature/add-snippets
Bash: Add snippets for VS Code
2 parents b6ece7d + 1e17a6d commit d9dc4cd

File tree

4 files changed

+251
-0
lines changed

4 files changed

+251
-0
lines changed

vscode-client/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ We strongly recommend that you install [shellcheck][shellcheck] to enable lintin
1919
- [x] Documentation for flags on hover
2020
- [x] Workspace symbols
2121
- [ ] Rename symbol
22+
- [x] Snippets
2223

2324
## Configuration
2425

vscode-client/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
],
2828
"main": "./out/extension",
2929
"contributes": {
30+
"snippets": [
31+
{
32+
"language": "shellscript",
33+
"path": "./snippets/snippets.json"
34+
}
35+
],
3036
"configuration": {
3137
"type": "object",
3238
"title": "Bash IDE configuration",

vscode-client/snippets/convention.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Snippet naming convention
2+
3+
- Snippet name always language keyword, builtin name or expansion symbol like `:-`.
4+
- `description` always a short snippet explanation without leading articles and trailing punctuation.
5+
Mnemonics with square brackets are always used to denote what chars are included in snippet `prefix`.
6+
- `prefix` can be just a string or array containing two item. The string or first item always follow these rules:
7+
- If a snippet is for language construct then first two letters of the first word from `description` are used
8+
plus the first letter from the second one (if it's exist).
9+
- If a snippet is for a builtin then builtin name is used.
10+
- If a snippet is for expansion then expansion symbol is used.
11+
- If a snippet is for a specific external program like **awk** then program name must be added to `prefix` like this:
12+
`awk:{{snippet-prefix}}`.
13+
The second one is always a noun existing to make snippets more memorizable.
14+
- `body` is always array.
15+
- If both short and long options available placeholder must be used to let user to chose the option's style
16+
but the first alternative should always be the long option.

vscode-client/snippets/snippets.json

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
{
2+
"shebang": {
3+
"description": "[sh]hebang",
4+
"prefix": "shebang",
5+
"body": [
6+
"#!/usr/bin/env ${1|bash,sh|}"
7+
]
8+
},
9+
"if": {
10+
"description": "[if]",
11+
"prefix": "if",
12+
"body": [
13+
"if ${1:command}; then",
14+
"\t$0",
15+
"fi"
16+
]
17+
},
18+
"if-else": {
19+
"description": "[if] [e]lse",
20+
"prefix": "if-else",
21+
"body": [
22+
"if ${1:command}; then",
23+
"\t${2:echo}",
24+
"else",
25+
"\t$0",
26+
"fi"
27+
]
28+
},
29+
"while": {
30+
"description": "[wh]ile",
31+
"prefix": "while",
32+
"body": [
33+
"while ${1:command}; do",
34+
"\t$0",
35+
"done"
36+
]
37+
},
38+
"until": {
39+
"description": "[un]til",
40+
"prefix": "until",
41+
"body": [
42+
"until ${1:command}; do",
43+
"\t$0",
44+
"done"
45+
]
46+
},
47+
"for": {
48+
"description": "[fo]r",
49+
"prefix": "for",
50+
"body": [
51+
"for ${1:variable} in ${2:list}; do",
52+
"\t$0",
53+
"done"
54+
]
55+
},
56+
"function": {
57+
"description": "[fu]nction",
58+
"prefix": "function",
59+
"body": [
60+
"${1:function_name}() {",
61+
"\t$0",
62+
"}"
63+
]
64+
},
65+
"main": {
66+
"description": "[ma]in",
67+
"prefix": "main",
68+
"body": [
69+
"main() {",
70+
"\t$0",
71+
"}"
72+
]
73+
},
74+
":-": {
75+
"description": "[:-] expansion",
76+
"prefix": ":-",
77+
"body": [
78+
"\"\\${${1:variable}:-${2:default}}\""
79+
]
80+
},
81+
":=": {
82+
"description": "[:=] expansion",
83+
"prefix": ":=",
84+
"body": [
85+
"\"\\${${1:variable}:=${2:default}}\""
86+
]
87+
},
88+
":?": {
89+
"description": "[:?] expansion",
90+
"prefix": ":?",
91+
"body": [
92+
"\"\\${${1:variable}:?${2:error_message}}\""
93+
]
94+
},
95+
":+": {
96+
"description": "[:+] expansion",
97+
"prefix": ":+",
98+
"body": [
99+
"\"\\${${1:variable}:+${2:alternative}}\""
100+
]
101+
},
102+
"#": {
103+
"description": "[#] expansion",
104+
"prefix": "#",
105+
"body": [
106+
"\"\\${${1:variable}#${2:pattern}}\""
107+
]
108+
},
109+
"##": {
110+
"description": "[##] expansion",
111+
"prefix": "##",
112+
"body": [
113+
"\"\\${${1:variable}##${2:pattern}}\""
114+
]
115+
},
116+
"%": {
117+
"description": "[%] expansion",
118+
"prefix": "%",
119+
"body": [
120+
"\"\\${${1:variable}%${2:pattern}}\""
121+
]
122+
},
123+
"%%": {
124+
"description": "[%%] expansion",
125+
"prefix": "%%",
126+
"body": [
127+
"\"\\${${1:variable}%%${2:pattern}}\""
128+
]
129+
},
130+
"..": {
131+
"description": "brace expansion",
132+
"prefix": "..",
133+
"body": [
134+
"{${1:from}..${2:to}}"
135+
]
136+
},
137+
"echo": {
138+
"description": "[echo]",
139+
"prefix": "echo",
140+
"body": [
141+
"echo \"${1:message}\""
142+
]
143+
},
144+
"printf": {
145+
"description": "[printf]",
146+
"prefix": "printf",
147+
"body": [
148+
"printf '%s' \"${1:message}\""
149+
]
150+
},
151+
"source": {
152+
"description": "[source]",
153+
"prefix": "source",
154+
"body": [
155+
"source \"${1:path/to/file}\""
156+
]
157+
},
158+
"alias": {
159+
"description": "[alias]",
160+
"prefix": "alias",
161+
"body": [
162+
"alias ${1:name}=${2:value}"
163+
]
164+
},
165+
"cd": {
166+
"description": "[cd]",
167+
"prefix": "cd",
168+
"body": [
169+
"cd \"${1:path/to/directory}\""
170+
]
171+
},
172+
"getopts": {
173+
"description": "[getopts]",
174+
"prefix": "getopts",
175+
"body": [
176+
"getopts ${1:optstring} ${2:name}"
177+
]
178+
},
179+
"jobs": {
180+
"description": "[jobs]",
181+
"prefix": "jobs",
182+
"body": [
183+
"jobs -x ${1:command}"
184+
]
185+
},
186+
"kill": {
187+
"description": "[kill]",
188+
"prefix": "kill",
189+
"body": [
190+
"kill ${1|-l,-L|}"
191+
]
192+
},
193+
"let": {
194+
"description": "[let]",
195+
"prefix": "let",
196+
"body": [
197+
"let ${1:argument}"
198+
]
199+
},
200+
"test": {
201+
"description": "[test]",
202+
"prefix": "test",
203+
"body": [
204+
"[[ ${1:argument1} ${2|-ef,-nt,-ot,==,=,!=,=~,<,>,-eq,-ne,-lt,-le,-gt,-ge|} ${3:argument2} ]]"
205+
]
206+
},
207+
"stream": {
208+
"description": "[de]vice name",
209+
"prefix": "dev",
210+
"body": [
211+
"/dev/${1|null,stdin,stdout,stderr|}"
212+
]
213+
},
214+
"sed:filter-lines": {
215+
"description": "[sed:filter-lines]",
216+
"prefix": "sed:filter-lines",
217+
"body": [
218+
"sed ${1|--regexp-extended,-E|} ${2|--quiet,-n|} '/${3:pattern}/' ${4:path/to/file}"
219+
]
220+
},
221+
"awk:filter-lines": {
222+
"description": "[awk:filter-lines]",
223+
"prefix": "awk:filter-lines",
224+
"body": [
225+
"awk '/${1:pattern}/' ${2:path/to/file}"
226+
]
227+
}
228+
}

0 commit comments

Comments
 (0)