Skip to content

Move snippets from vscode extension to server #683

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 3 commits into from
Jan 17, 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
4 changes: 4 additions & 0 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Bash Language Server

## 4.5.0

- Include 30 snippets for language constructs (e.g. `if`), builtins (e.g. `test`), expansions (e.g. `[##]`), and external programs (e.g. `sed`) https://github.com/bash-lsp/bash-language-server/pull/683

## 4.4.0

- Improve source command parser and include diagnostics when parser fails https://github.com/bash-lsp/bash-language-server/pull/673
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A language server for Bash",
"author": "Mads Hartmann",
"license": "MIT",
"version": "4.4.0",
"version": "4.5.0",
"main": "./out/server.js",
"typings": "./out/server.d.ts",
"bin": {
Expand Down
9 changes: 0 additions & 9 deletions server/src/__tests__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ describe('server', () => {
expect.arrayContaining([
{
data: {
name: 'rm',
type: CompletionItemDataType.Executable,
},
kind: expect.any(Number),
Expand Down Expand Up @@ -634,7 +633,6 @@ describe('server', () => {
Array [
Object {
"data": Object {
"name": "BLUE",
"type": 3,
},
"documentation": Object {
Expand Down Expand Up @@ -666,7 +664,6 @@ describe('server', () => {
Array [
Object {
"data": Object {
"name": "add_a_user",
"type": 3,
},
"documentation": Object {
Expand Down Expand Up @@ -708,7 +705,6 @@ describe('server', () => {
Array [
Object {
"data": Object {
"name": "BOLD",
"type": 3,
},
"documentation": undefined,
Expand Down Expand Up @@ -747,7 +743,6 @@ describe('server', () => {
Array [
Object {
"data": Object {
"name": "BOLD",
"type": 3,
},
"documentation": undefined,
Expand All @@ -756,7 +751,6 @@ describe('server', () => {
},
Object {
"data": Object {
"name": "RED",
"type": 3,
},
"documentation": Object {
Expand All @@ -768,7 +762,6 @@ describe('server', () => {
},
Object {
"data": Object {
"name": "GREEN",
"type": 3,
},
"documentation": Object {
Expand All @@ -780,7 +773,6 @@ describe('server', () => {
},
Object {
"data": Object {
"name": "BLUE",
"type": 3,
},
"documentation": Object {
Expand All @@ -792,7 +784,6 @@ describe('server', () => {
},
Object {
"data": Object {
"name": "RESET",
"type": 3,
},
"documentation": Object {
Expand Down
31 changes: 31 additions & 0 deletions server/src/__tests__/snippets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { MarkupContent } from 'vscode-languageserver'

import { SNIPPETS } from '../snippets'

describe('snippets', () => {
it('should have unique labels', () => {
const labels = SNIPPETS.map((snippet) => snippet.label)
const uniqueLabels = new Set(labels)
expect(labels.length).toBe(uniqueLabels.size)
})

SNIPPETS.forEach(({ label, documentation }) => {
it(`contains the label in the documentation for "${label}"`, () => {
const stringDocumentation = (documentation as MarkupContent)?.value
expect(stringDocumentation).toBeDefined()
if (stringDocumentation) {
expect(stringDocumentation).toContain(label)
const secondLine = stringDocumentation.split('\n')[1]
try {
expect(
secondLine.startsWith(label) || secondLine.startsWith(`[${label}]`),
).toBe(true)
} catch (error) {
// eslint-disable-next-line no-console
console.error(`Did not start with label: ${label}`, secondLine)
throw error
}
}
})
})
})
14 changes: 6 additions & 8 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Executables from './executables'
import { initializeParser } from './parser'
import * as ReservedWords from './reserved-words'
import { Linter } from './shellcheck'
import { SNIPPETS } from './snippets'
import { BashCompletionItem, CompletionItemDataType } from './types'
import { uniqueBasedOnHash } from './util/array'
import { logger, setLogConnection, setLogLevel } from './util/logger'
Expand Down Expand Up @@ -344,7 +345,6 @@ export default class BashServer {
label: symbol.name,
kind: symbolKindToCompletionKind(symbol.kind),
data: {
name: symbol.name,
type: CompletionItemDataType.Symbol,
},
documentation:
Expand Down Expand Up @@ -503,7 +503,6 @@ export default class BashServer {
label: reservedWord,
kind: LSP.CompletionItemKind.Keyword,
data: {
name: reservedWord,
type: CompletionItemDataType.ReservedWord,
},
}))
Expand All @@ -516,7 +515,6 @@ export default class BashServer {
label: executable,
kind: LSP.CompletionItemKind.Function,
data: {
name: executable,
type: CompletionItemDataType.Executable,
},
}
Expand All @@ -526,7 +524,6 @@ export default class BashServer {
label: builtin,
kind: LSP.CompletionItemKind.Function,
data: {
name: builtin,
type: CompletionItemDataType.Builtin,
},
}))
Expand All @@ -535,7 +532,6 @@ export default class BashServer {
label: option,
kind: LSP.CompletionItemKind.Constant,
data: {
name: option,
type: CompletionItemDataType.Symbol,
},
}))
Expand All @@ -546,6 +542,7 @@ export default class BashServer {
...programCompletions,
...builtinsCompletions,
...optionsCompletions,
...SNIPPETS,
]

if (word) {
Expand All @@ -560,10 +557,11 @@ export default class BashServer {
item: LSP.CompletionItem,
): Promise<LSP.CompletionItem> {
const {
data: { name, type },
label,
data: { type },
} = item as BashCompletionItem

logger.debug(`onCompletionResolve name=${name} type=${type}`)
logger.debug(`onCompletionResolve label=${label} type=${type}`)

try {
let documentation = null
Expand All @@ -573,7 +571,7 @@ export default class BashServer {
type === CompletionItemDataType.Builtin ||
type === CompletionItemDataType.ReservedWord
) {
documentation = await getShellDocumentation({ word: name })
documentation = await getShellDocumentation({ word: label })
}

return documentation
Expand Down
168 changes: 168 additions & 0 deletions server/src/snippets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/**
* Naming convention for `label`:
* - is always a language keyword, builtin name or expansion symbol like `:-`.
* - 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}}`.
*/
import { CompletionItemKind, InsertTextFormat, MarkupKind } from 'vscode-languageserver'

import { BashCompletionItem } from './types'

export const SNIPPETS: BashCompletionItem[] = [
{
label: 'shebang',
insertText: '#!/usr/bin/env ${1|bash,sh|}',
},
{
label: 'if',
insertText: ['if ${1:command}; then', '\t$0', 'fi'].join('\n'),
},
{
label: 'if-else',
insertText: ['if ${1:command}; then', '\t${2:echo}', 'else', '\t$0', 'fi'].join('\n'),
},
{
label: 'while',
insertText: ['while ${1:command}; do', '\t$0', 'done'].join('\n'),
},
{
label: 'until',
insertText: ['until ${1:command}; do', '\t$0', 'done'].join('\n'),
},
{
label: 'for',
insertText: ['for ${1:variable} in ${2:list}; do', '\t$0', 'done'].join('\n'),
},
{
label: 'function',
insertText: ['${1:function_name}() {', '\t$0', '}'].join('\n'),
},
{
label: 'main',
insertText: ['main() {', '\t$0', '}'].join('\n'),
},
{
documentation: '[:-] expansion',
label: ':-',
insertText: '"\\${${1:variable}:-${2:default}}"',
},
{
documentation: '[:=] expansion',
label: ':=',
insertText: '"\\${${1:variable}:=${2:default}}"',
},
{
documentation: '[:?] expansion',
label: ':?',
insertText: '"\\${${1:variable}:?${2:error_message}}"',
},
{
documentation: '[:+] expansion',
label: ':+',
insertText: '"\\${${1:variable}:+${2:alternative}}"',
},
{
documentation: '[#] expansion',
label: '#',
insertText: '"\\${${1:variable}#${2:pattern}}"',
},
{
documentation: '[##] expansion',
label: '##',
insertText: '"\\${${1:variable}##${2:pattern}}"',
},
{
documentation: '[%] expansion',
label: '%',
insertText: '"\\${${1:variable}%${2:pattern}}"',
},
{
documentation: '[%%] expansion',
label: '%%',
insertText: '"\\${${1:variable}%%${2:pattern}}"',
},
{
documentation: '[..] brace expansion',
label: '..',
insertText: '{${1:from}..${2:to}}',
},
{
label: 'echo',
insertText: 'echo "${1:message}"',
},
{
label: 'printf',
insertText: 'printf \'%s\' "${1:message}"',
},
{
label: 'source',
insertText: 'source "${1:path/to/file}"',
},
{
label: 'alias',
insertText: 'alias ${1:name}=${2:value}',
},
{
label: 'cd',
insertText: 'cd "${1:path/to/directory}"',
},
{
label: 'getopts',
insertText: 'getopts ${1:optstring} ${2:name}',
},
{
label: 'jobs',
insertText: 'jobs -x ${1:command}',
},
{
label: 'kill',
insertText: 'kill ${1|-l,-L|}',
},
{
label: 'let',
insertText: 'let ${1:argument}',
},
{
label: 'test',
insertText:
'[[ ${1:argument1} ${2|-ef,-nt,-ot,==,=,!=,=~,<,>,-eq,-ne,-lt,-le,-gt,-ge|} ${3:argument2} ]]',
},
{
documentation: '[dev]ice name',
label: 'dev',
insertText: '/dev/${1|null,stdin,stdout,stderr|}',
},
{
label: 'sed:filter-lines',
insertText:
"sed ${1|--regexp-extended,-E|} ${2|--quiet,-n|} '/${3:pattern}/' ${4:path/to/file}",
},
{
label: 'awk:filter-lines',
insertText: "awk '/${1:pattern}/' ${2:path/to/file}",
},
].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: CompletionItemKind.Snippet,
},
}))

function markdownBlock(text: string, language: string): string {
const tripleQoute = '```'
return [tripleQoute + language, text, tripleQoute].join('\n')
}
2 changes: 1 addition & 1 deletion server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export enum CompletionItemDataType {
Executable,
ReservedWord,
Symbol,
Snippet,
}

export interface BashCompletionItem extends LSP.CompletionItem {
data: {
type: CompletionItemDataType
name: string
}
}
6 changes: 0 additions & 6 deletions vscode-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
],
"main": "./out/extension",
"contributes": {
"snippets": [
{
"language": "shellscript",
"path": "./snippets/snippets.json"
}
],
"configuration": {
"type": "object",
"title": "Bash IDE configuration",
Expand Down
Loading