forked from bash-lsp/bash-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets.ts
686 lines (682 loc) · 18.2 KB
/
snippets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/**
* Naming convention for `documentation`:
* - for Bash operators it's '<operator> operator'
* - for Bash documentation it's 'documentation definition' or '"<documentation>" documentation definition'
* - for Bash functions it's 'function definition' or '"<function>" function definition'
* - for Bash character classes it's any string with optional mnemonics depicted via square brackets
* - for shell shebang it's 'shebang'
* - for anything else it's any string
*
* Naming convention for `label`:
* - for shell shebang it's 'shebang' or 'shebang-with-arguments'
* - for Bash operators it's '<operator>[<nested-operator>]', where:
* - <operator> is Bash operator
* - <nested-operator> is 'test'
* used when [[ command is contained in <operator> condition
* - term delimiter: dash, like 'if-test'
* - for Bash parameter expansions it's '[<prefix>]<expression>', where:
* - <prefix> is one of 'set'/'error'
* used when expansion modifies variable or prints error to stderr
* - <expession> is 'if-(set|unset)[-or-[not-]null]'
* - term delimiter: dash, like 'set-if-unset-or-null'
* - for Bash documentation it's one of 'documentation'/'<documentation>'
* - for Bash functions it's one of 'function'/'<function>'
* - for anything else it's any string
*/
import { CompletionItemKind, InsertTextFormat, MarkupKind } from 'vscode-languageserver'
import { BashCompletionItem, CompletionItemDataType } from './types'
export const SNIPPETS: BashCompletionItem[] = [
{
documentation: 'shebang',
label: 'shebang',
insertText: '#!/usr/bin/env ${1|bash,sh|}',
},
{
documentation: 'shebang-with-arguments',
label: 'shebang-with-arguments',
insertText: '#!/usr/bin/env ${1|-S,--split-string|} ${2|bash,sh|} ${3|argument ...|}',
},
{
label: 'and',
documentation: 'and operator',
insertText: '${1:first-expression} && ${2:second-expression}',
},
{
label: 'or',
documentation: 'or operator',
insertText: '${1:first-expression} || ${2:second-expression}',
},
{
label: 'if',
documentation: 'if operator',
insertText: ['if ${1:condition}; then', '\t${2:command ...}', 'fi'].join('\n'),
},
{
label: 'if-else',
documentation: 'if-else operator',
insertText: [
'if ${1:condition}; then',
'\t${2:command ...}',
'else',
'\t${3:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-less',
documentation: 'if with number comparison',
insertText: [
'if (( "${1:first-expression}" < "${2:second-expression}" )); then',
'\t${3:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-greater',
documentation: 'if with number comparison',
insertText: [
'if (( "${1:first-expression}" > "${2:second-expression}" )); then',
'\t${3:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-less-or-equal',
documentation: 'if with number comparison',
insertText: [
'if (( "${1:first-expression}" <= "${2:second-expression}" )); then',
'\t${3:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-greater-or-equal',
documentation: 'if with number comparison',
insertText: [
'if (( "${1:first-expression}" >= "${2:second-expression}" )); then',
'\t${3:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-equal',
documentation: 'if with number comparison',
insertText: [
'if (( "${1:first-expression}" == "${2:second-expression}" )); then',
'\t${3:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-not-equal',
documentation: 'if with number comparison',
insertText: [
'if (( "${1:first-expression}" != "${2:second-expression}" )); then',
'\t${3:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-string-equal',
documentation: 'if with string comparison',
insertText: [
'if [[ "${1:first-expression}" == "${2:second-expression}" ]]; then',
'\t${3:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-string-not-equal',
documentation: 'if with string comparison',
insertText: [
'if [[ "${1:first-expression}" != "${2:second-expression}" ]]; then',
'\t${3:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-string-empty',
documentation: 'if with string comparison (has [z]ero length)',
insertText: ['if [[ -z "${1:expression}" ]]; then', '\t${2:command ...}', 'fi'].join(
'\n',
),
},
{
label: 'if-string-not-empty',
documentation: 'if with string comparison ([n]ot empty)',
insertText: ['if [[ -n "${1:expression}" ]]; then', '\t${2:command ...}', 'fi'].join(
'\n',
),
},
{
label: 'if-defined',
documentation: 'if with variable existence check',
insertText: [
'if [[ -n "\\${${1:variable}+x}" ]]; then',
'\t${2:command ...}',
'fi',
].join('\n'),
},
{
label: 'if-not-defined',
documentation: 'if with variable existence check',
insertText: [
'if [[ -z "\\${${1:variable}+x}" ]]; then',
'\t${2:command ...}',
'fi',
].join('\n'),
},
{
label: 'while',
documentation: 'while operator',
insertText: ['while ${1:condition}; do', '\t${2:command ...}', 'done'].join('\n'),
},
{
label: 'while-else',
documentation: 'while-else operator',
insertText: [
'while ${1:condition}; do',
'\t${2:command ...}',
'else',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'while-less',
documentation: 'while with number comparison',
insertText: [
'while (( "${1:first-expression}" < "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'while-greater',
documentation: 'while with number comparison',
insertText: [
'while (( "${1:first-expression}" > "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'while-less-or-equal',
documentation: 'while with number comparison',
insertText: [
'while (( "${1:first-expression}" <= "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'while-greater-or-equal',
documentation: 'while with number comparison',
insertText: [
'while (( "${1:first-expression}" >= "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'while-equal',
documentation: 'while with number comparison',
insertText: [
'while (( "${1:first-expression}" == "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'while-not-equal',
documentation: 'while with number comparison',
insertText: [
'while (( "${1:first-expression}" != "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'while-string-equal',
documentation: 'while with string comparison',
insertText: [
'while [[ "${1:first-expression}" == "${2:second-expression}" ]]; do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'while-string-not-equal',
documentation: 'while with string comparison',
insertText: [
'while [[ "${1:first-expression}" != "${2:second-expression}" ]]; do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'while-string-empty',
documentation: 'while with string comparison (has [z]ero length)',
insertText: [
'while [[ -z "${1:expression}" ]]; do',
'\t${2:command ...}',
'done',
].join('\n'),
},
{
label: 'while-string-not-empty',
documentation: 'while with string comparison ([n]ot empty)',
insertText: [
'while [[ -n "${1:expression}" ]]; do',
'\t${2:command ...}',
'done',
].join('\n'),
},
{
label: 'while-defined',
documentation: 'while with variable existence check',
insertText: [
'while [[ -n "\\${${1:variable}+x}" ]]',
'\t${2:command ...}',
'done',
].join('\n'),
},
{
label: 'while-not-defined',
documentation: 'while with variable existence check',
insertText: [
'while [[ -z "\\${${1:variable}+x}" ]]',
'\t${2:command ...}',
'done',
].join('\n'),
},
{
label: 'until',
documentation: 'until operator',
insertText: ['until ${1:condition}; do', '\t${2:command ...}', 'done'].join('\n'),
},
{
label: 'until-else',
documentation: 'until-else operator',
insertText: [
'until ${1:condition}; do',
'\t${2:command ...}',
'else',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'until-less',
documentation: 'until with number comparison',
insertText: [
'until (( "${1:first-expression}" < "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'until-greater',
documentation: 'until with number comparison',
insertText: [
'until (( "${1:first-expression}" > "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'until-less-or-equal',
documentation: 'until with number comparison',
insertText: [
'until (( "${1:first-expression}" <= "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'until-greater-or-equal',
documentation: 'until with number comparison',
insertText: [
'until (( "${1:first-expression}" >= "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'until-equal',
documentation: 'until with number comparison',
insertText: [
'until (( "${1:first-expression}" == "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'until-not-equal',
documentation: 'until with number comparison',
insertText: [
'until (( "${1:first-expression}" != "${2:second-expression}" )); do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'until-string-equal',
documentation: 'until with string comparison',
insertText: [
'until [[ "${1:first-expression}" == "${2:second-expression}" ]]; do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'until-string-not-equal',
documentation: 'until with string comparison',
insertText: [
'until [[ "${1:first-expression}" != "${2:second-expression}" ]]; do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'until-string-empty',
documentation: 'until with string comparison (has [z]ero length)',
insertText: [
'until [[ -z "${1:expression}" ]]; do',
'\t${2:command ...}',
'done',
].join('\n'),
},
{
label: 'until-string-not-empty',
documentation: 'until with string comparison ([n]ot empty)',
insertText: [
'until [[ -n "${1:expression}" ]]; do',
'\t${2:command ...}',
'done',
].join('\n'),
},
{
label: 'until-defined',
documentation: 'until with variable existence check',
insertText: [
'until [[ -n "\\${${1:variable}+x}" ]]',
'\t${2:command ...}',
'done',
].join('\n'),
},
{
label: 'until-not-defined',
documentation: 'until with variable existence check',
insertText: [
'until [[ -z "\\${${1:variable}+x}" ]]',
'\t${2:command ...}',
'done',
].join('\n'),
},
{
label: 'for',
documentation: 'for operator',
insertText: [
'for ${1:item} in ${2:expression}; do',
'\t${3:command ...}',
'done',
].join('\n'),
},
{
label: 'for-range',
documentation: 'for with range',
insertText: [
'for ${1:item} in \\$(seq ${2:from} ${3:to}); do',
'\t${4:command ...}',
'done',
].join('\n'),
},
{
label: 'for-stepped-range',
documentation: 'for with stepped range',
insertText: [
'for ${1:item} in \\$(seq ${2:from} ${3:step} ${4:to}); do',
'\t${5:command ...}',
'done',
].join('\n'),
},
{
label: 'for-files',
documentation: 'for with files',
insertText: [
'for ${1:item} in *.${2:extension}; do',
'\t${4:command ...}',
'done',
].join('\n'),
},
{
label: 'case',
documentation: 'case operator',
insertText: [
'case "${1:expression}" in',
'\t${2:pattern})',
'\t\t${3:command ...}',
'\t\t;;',
'\t*)',
'\t\t${4:command ...}',
'\t\t;;',
'esac',
].join('\n'),
},
{
label: 'function',
documentation: 'function definition',
insertText: ['${1:name}() {', '\t${2:command ...}', '}'].join('\n'),
},
{
documentation: 'documentation definition',
label: 'documentation',
insertText: [
'# ${1:function_name} ${2:function_parameters}',
'# ${3:function_description}',
'#',
'# Output:',
'# ${4:function_output}',
'#',
'# Return:',
'# - ${5:0} when ${6:all parameters are correct}',
'# - ${7:1} ${8:otherwise}',
].join('\n'),
},
{
documentation: 'block',
label: 'block',
insertText: ['{', '\t${1:command ...}', '}'].join('\n'),
},
{
documentation: 'block redirected',
label: 'block-redirected',
insertText: ['{', '\t${1:command ...}', '} > ${2:file}'].join('\n'),
},
{
documentation: 'block stderr redirected',
label: 'block-stderr-redirected',
insertText: ['{', '\t${1:command ...}', '} 2> ${2:file}'].join('\n'),
},
{
documentation: 'variable',
label: 'variable',
insertText: 'declare ${1:variable}=${2:value}',
},
{
documentation: 'variable index',
label: 'variable-index',
insertText: '${1:variable}[${2:index}]=${3:value}',
},
{
documentation: 'variable append',
label: 'variable-append',
insertText: '${1:variable}+=${2:value}',
},
{
documentation: 'variable-prepend',
label: 'variable-prepend',
insertText: '${1:variable}=${2:value}\\$$1',
},
{
documentation: 'if unset or null',
label: 'if-unset-or-null',
insertText: '"\\${${1:variable}:-${2:default}}"',
},
{
documentation: 'if unset',
label: 'if-unset',
insertText: '"\\${${1:variable}-${2:default}}"',
},
{
documentation: 'set if unset or null',
label: 'set-if-unset-or-null',
insertText: '"\\${${1:variable}:=${2:default}}"',
},
{
documentation: 'set if unset',
label: 'set-if-unset',
insertText: '"\\${${1:variable}=${2:default}}"',
},
{
documentation: 'error if unset or null',
label: 'error-if-unset-or-null',
insertText: '"\\${${1:variable}:?${2:error_message}}"',
},
{
documentation: 'error if unset',
label: 'error-if-unset',
insertText: '"\\${${1:variable}?${2:error_message}}"',
},
{
documentation: 'if set or not null',
label: 'if-set-or-not-null',
insertText: '"\\${${1:variable}:+${2:alternative}}"',
},
{
documentation: 'if set',
label: 'if-set',
insertText: '"\\${${1:variable}+${2:alternative}}"',
},
{
documentation: 'string shortest leading replacement',
label: 'string-remove-leading',
insertText: '"\\${${1:variable}#${2:pattern}}"',
},
{
documentation: 'string shortest trailing replacement',
label: 'string-remove-trailing',
insertText: '"\\${${1:variable}%${2:pattern}}"',
},
{
documentation: 'string filtering',
label: 'string-match',
insertText: "sed ${1|-E -n,--regexp-extended --quiet|} '/${2:pattern}/p'",
},
{
documentation: 'string replacement',
label: 'string-replace',
insertText: "sed ${1|-E,--regexp-extended|} 's/${2:pattern}/${3:replacement}/'",
},
{
documentation: 'string replacement',
label: 'string-replace-all',
insertText: "sed ${1|-E,--regexp-extended|} 's/${2:pattern}/${3:replacement}/g'",
},
{
documentation: 'string transliterate',
label: 'string-transliterate',
insertText:
"sed ${1|-E,--regexp-extended|} 'y/${2:source-characters}/${3:replacement-characters}/g'",
},
{
documentation: 'file print',
label: 'file-print',
insertText: "sed '' ${1:file}",
},
{
documentation: 'file read',
label: 'file-read',
insertText:
"sed ${1|-E,--regexp-extended|} ':${2:x} N $! b$2 ${3:command}' ${4:file}",
},
{
documentation: 'skip first',
label: 'skip-first',
insertText: 'tail ${1|-n,-c,--lines,--bytes|} +${2:count}',
},
{
documentation: 'skip last',
label: 'skip-last',
insertText: 'head ${1|-n,-c,--lines,--bytes|} -${2:count}',
},
{
documentation: 'take first',
label: 'take-first',
insertText: 'head ${1|-n,-c,--lines,--bytes|} ${2:count}',
},
{
documentation: 'take last',
label: 'take-last',
insertText: 'tail ${1|-n,-c,--lines,--bytes|} ${2:count}',
},
{
documentation: 'take range',
label: 'take-range',
insertText: "sed ${1|-n,--quiet|} '${2:from},${3:to}p'",
},
{
documentation: 'take stepped range',
label: 'take-stepped-range',
insertText: "sed ${1|-n,--quiet|} '${2:from},${3:to}p' | sed $1 '1~${4:step}p'",
},
{
documentation: 'json print',
label: 'json-print',
insertText: "jq '.${1:node}' ${2:file}",
},
{
documentation: 'device',
label: 'device',
insertText: '/dev/${1|null,stdin,stdout,stderr|}',
},
{
documentation: 'completion',
label: 'completion definition',
insertText: [
'_$1_completions()',
'{',
'\treadarray -t COMPREPLY < <(compgen -W "-h --help -v --version" "\\${COMP_WORDS[1]}")',
'}',
'',
'complete -F _$1_completions ${1:command}',
].join('\n'),
},
{
documentation: 'comment',
label: 'comment definition',
insertText: '# ${1:description}',
},
].map((item) => ({
...item,
documentation: {
value: [
markdownBlock(
`${item.documentation || item.label} (bash-language-server)\n\n`,
'man',
),
markdownBlock(item.insertText, 'bash'),
].join('\n'),
kind: MarkupKind.Markdown,
},
insertTextFormat: InsertTextFormat.Snippet,
data: {
type: CompletionItemDataType.Snippet,
},
kind: CompletionItemKind.Snippet,
}))
function markdownBlock(text: string, language: string): string {
const tripleQoute = '```'
return [tripleQoute + language, text, tripleQoute].join('\n')
}