|
| 1 | +/** |
| 2 | + * Naming convention for `label`: |
| 3 | + * - is always a language keyword, builtin name or expansion symbol like `:-`. |
| 4 | + * - If a snippet is for a builtin then builtin name is used. |
| 5 | + * - If a snippet is for expansion then expansion symbol is used. |
| 6 | + * - If a snippet is for a specific external program like **awk** then program name must be added to `prefix` like this: |
| 7 | + * `awk:{{snippet-prefix}}`. |
| 8 | + */ |
| 9 | +import { CompletionItemKind, InsertTextFormat, MarkupKind } from 'vscode-languageserver' |
| 10 | + |
| 11 | +import { BashCompletionItem } from './types' |
| 12 | + |
| 13 | +export const SNIPPETS: BashCompletionItem[] = [ |
| 14 | + { |
| 15 | + label: 'shebang', |
| 16 | + insertText: '#!/usr/bin/env ${1|bash,sh|}', |
| 17 | + }, |
| 18 | + { |
| 19 | + label: 'if', |
| 20 | + insertText: ['if ${1:command}; then', '\t$0', 'fi'].join('\n'), |
| 21 | + }, |
| 22 | + { |
| 23 | + label: 'if-else', |
| 24 | + insertText: ['if ${1:command}; then', '\t${2:echo}', 'else', '\t$0', 'fi'].join('\n'), |
| 25 | + }, |
| 26 | + { |
| 27 | + label: 'while', |
| 28 | + insertText: ['while ${1:command}; do', '\t$0', 'done'].join('\n'), |
| 29 | + }, |
| 30 | + { |
| 31 | + label: 'until', |
| 32 | + insertText: ['until ${1:command}; do', '\t$0', 'done'].join('\n'), |
| 33 | + }, |
| 34 | + { |
| 35 | + label: 'for', |
| 36 | + insertText: ['for ${1:variable} in ${2:list}; do', '\t$0', 'done'].join('\n'), |
| 37 | + }, |
| 38 | + { |
| 39 | + label: 'function', |
| 40 | + insertText: ['${1:function_name}() {', '\t$0', '}'].join('\n'), |
| 41 | + }, |
| 42 | + { |
| 43 | + label: 'main', |
| 44 | + insertText: ['main() {', '\t$0', '}'].join('\n'), |
| 45 | + }, |
| 46 | + { |
| 47 | + documentation: '[:-] expansion', |
| 48 | + label: ':-', |
| 49 | + insertText: '"\\${${1:variable}:-${2:default}}"', |
| 50 | + }, |
| 51 | + { |
| 52 | + documentation: '[:=] expansion', |
| 53 | + label: ':=', |
| 54 | + insertText: '"\\${${1:variable}:=${2:default}}"', |
| 55 | + }, |
| 56 | + { |
| 57 | + documentation: '[:?] expansion', |
| 58 | + label: ':?', |
| 59 | + insertText: '"\\${${1:variable}:?${2:error_message}}"', |
| 60 | + }, |
| 61 | + { |
| 62 | + documentation: '[:+] expansion', |
| 63 | + label: ':+', |
| 64 | + insertText: '"\\${${1:variable}:+${2:alternative}}"', |
| 65 | + }, |
| 66 | + { |
| 67 | + documentation: '[#] expansion', |
| 68 | + label: '#', |
| 69 | + insertText: '"\\${${1:variable}#${2:pattern}}"', |
| 70 | + }, |
| 71 | + { |
| 72 | + documentation: '[##] expansion', |
| 73 | + label: '##', |
| 74 | + insertText: '"\\${${1:variable}##${2:pattern}}"', |
| 75 | + }, |
| 76 | + { |
| 77 | + documentation: '[%] expansion', |
| 78 | + label: '%', |
| 79 | + insertText: '"\\${${1:variable}%${2:pattern}}"', |
| 80 | + }, |
| 81 | + { |
| 82 | + documentation: '[%%] expansion', |
| 83 | + label: '%%', |
| 84 | + insertText: '"\\${${1:variable}%%${2:pattern}}"', |
| 85 | + }, |
| 86 | + { |
| 87 | + documentation: '[..] brace expansion', |
| 88 | + label: '..', |
| 89 | + insertText: '{${1:from}..${2:to}}', |
| 90 | + }, |
| 91 | + { |
| 92 | + label: 'echo', |
| 93 | + insertText: 'echo "${1:message}"', |
| 94 | + }, |
| 95 | + { |
| 96 | + label: 'printf', |
| 97 | + insertText: 'printf \'%s\' "${1:message}"', |
| 98 | + }, |
| 99 | + { |
| 100 | + label: 'source', |
| 101 | + insertText: 'source "${1:path/to/file}"', |
| 102 | + }, |
| 103 | + { |
| 104 | + label: 'alias', |
| 105 | + insertText: 'alias ${1:name}=${2:value}', |
| 106 | + }, |
| 107 | + { |
| 108 | + label: 'cd', |
| 109 | + insertText: 'cd "${1:path/to/directory}"', |
| 110 | + }, |
| 111 | + { |
| 112 | + label: 'getopts', |
| 113 | + insertText: 'getopts ${1:optstring} ${2:name}', |
| 114 | + }, |
| 115 | + { |
| 116 | + label: 'jobs', |
| 117 | + insertText: 'jobs -x ${1:command}', |
| 118 | + }, |
| 119 | + { |
| 120 | + label: 'kill', |
| 121 | + insertText: 'kill ${1|-l,-L|}', |
| 122 | + }, |
| 123 | + { |
| 124 | + label: 'let', |
| 125 | + insertText: 'let ${1:argument}', |
| 126 | + }, |
| 127 | + { |
| 128 | + label: 'test', |
| 129 | + insertText: |
| 130 | + '[[ ${1:argument1} ${2|-ef,-nt,-ot,==,=,!=,=~,<,>,-eq,-ne,-lt,-le,-gt,-ge|} ${3:argument2} ]]', |
| 131 | + }, |
| 132 | + { |
| 133 | + documentation: '[dev]ice name', |
| 134 | + label: 'dev', |
| 135 | + insertText: '/dev/${1|null,stdin,stdout,stderr|}', |
| 136 | + }, |
| 137 | + { |
| 138 | + label: 'sed:filter-lines', |
| 139 | + insertText: |
| 140 | + "sed ${1|--regexp-extended,-E|} ${2|--quiet,-n|} '/${3:pattern}/' ${4:path/to/file}", |
| 141 | + }, |
| 142 | + { |
| 143 | + label: 'awk:filter-lines', |
| 144 | + insertText: "awk '/${1:pattern}/' ${2:path/to/file}", |
| 145 | + }, |
| 146 | +].map((item) => ({ |
| 147 | + ...item, |
| 148 | + documentation: { |
| 149 | + value: [ |
| 150 | + markdownBlock( |
| 151 | + `${item.documentation || item.label} (bash-language-server)\n\n`, |
| 152 | + 'man', |
| 153 | + ), |
| 154 | + markdownBlock(item.insertText, 'bash'), |
| 155 | + ].join('\n'), |
| 156 | + kind: MarkupKind.Markdown, |
| 157 | + }, |
| 158 | + |
| 159 | + insertTextFormat: InsertTextFormat.Snippet, |
| 160 | + data: { |
| 161 | + type: CompletionItemKind.Snippet, |
| 162 | + }, |
| 163 | +})) |
| 164 | + |
| 165 | +function markdownBlock(text: string, language: string): string { |
| 166 | + const tripleQoute = '```' |
| 167 | + return [tripleQoute + language, text, tripleQoute].join('\n') |
| 168 | +} |
0 commit comments