Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8a8a922

Browse files
Merge pull request #33 from mads-hartmann/builtins-support
Builtins support
2 parents 991715f + bf4129d commit 8a8a922

File tree

4 files changed

+140
-19
lines changed

4 files changed

+140
-19
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as Builtins from '../builtins'
2+
3+
describe('documentation', () => {
4+
it('returns an error string an unknown builtin', async () => {
5+
const result = await Builtins.documentation('foobar')
6+
expect(result).toEqual('No help page for foobar')
7+
})
8+
9+
it('returns documentation string an known builtin', async () => {
10+
const result = await Builtins.documentation('exit')
11+
const firstLine = result.split('\n')[0]
12+
expect(firstLine).toEqual('exit: exit [n]')
13+
})
14+
})

‎server/src/builtins.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as ShUtil from './util/sh'
2+
3+
// You can generate this list by running `compgen -b` in a bash session
4+
export const LIST = [
5+
'.',
6+
':',
7+
'[',
8+
'alias',
9+
'bg',
10+
'bind',
11+
'break',
12+
'builtin',
13+
'caller',
14+
'cd',
15+
'command',
16+
'compgen',
17+
'complete',
18+
'continue',
19+
'declare',
20+
'dirs',
21+
'disown',
22+
'echo',
23+
'enable',
24+
'eval',
25+
'exec',
26+
'exit',
27+
'export',
28+
'false',
29+
'fc',
30+
'fg',
31+
'getopts',
32+
'hash',
33+
'help',
34+
'history',
35+
'jobs',
36+
'kill',
37+
'let',
38+
'local',
39+
'logout',
40+
'popd',
41+
'printf',
42+
'pushd',
43+
'pwd',
44+
'read',
45+
'readonly',
46+
'return',
47+
'set',
48+
'shift',
49+
'shopt',
50+
'source',
51+
'suspend',
52+
'test',
53+
'times',
54+
'trap',
55+
'true',
56+
'type',
57+
'typeset',
58+
'ulimit',
59+
'umask',
60+
'unalias',
61+
'unset',
62+
'wait',
63+
]
64+
65+
export function isBuiltin(word: string): boolean {
66+
return LIST.find(builtin => builtin === word) !== undefined
67+
}
68+
69+
export async function documentation(builtin: string): Promise<string> {
70+
const errorMessage = `No help page for ${builtin}`
71+
try {
72+
const doc = await ShUtil.execShellScript(`help ${builtin}`)
73+
return doc || errorMessage
74+
} catch (error) {
75+
return errorMessage
76+
}
77+
}

‎server/src/server.ts‎

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as LSP from 'vscode-languageserver'
22

33
import Analyzer from './analyser'
4+
import * as Builtins from './builtins'
45
import Executables from './executables'
56

67
/**
@@ -106,14 +107,23 @@ export default class BashServer {
106107

107108
const word = this.getWordAtPoint(pos)
108109

109-
return this.executables.isExecutableOnPATH(word)
110-
? this.executables.documentation(word).then(doc => ({
111-
contents: {
112-
language: 'plaintext',
113-
value: doc,
114-
},
115-
}))
116-
: null
110+
if (Builtins.isBuiltin(word)) {
111+
return Builtins.documentation(word).then(doc => ({
112+
contents: {
113+
language: 'plaintext',
114+
value: doc,
115+
},
116+
}))
117+
} else if (this.executables.isExecutableOnPATH(word)) {
118+
return this.executables.documentation(word).then(doc => ({
119+
contents: {
120+
language: 'plaintext',
121+
value: doc,
122+
},
123+
}))
124+
} else {
125+
return null
126+
}
117127
}
118128

119129
private onDefinition(pos: LSP.TextDocumentPositionParams): LSP.Definition {
@@ -159,20 +169,40 @@ export default class BashServer {
159169
}
160170
})
161171

162-
return symbolCompletions.concat(programCompletions)
172+
const builtinsCompletions = Builtins.LIST.map(builtin => ({
173+
label: builtin,
174+
kind: LSP.SymbolKind.Method, // ??
175+
data: {
176+
name: builtin,
177+
type: 'builtin',
178+
},
179+
}))
180+
181+
return [...symbolCompletions, ...programCompletions, ...builtinsCompletions]
163182
}
164183

165-
private onCompletionResolve(item: LSP.CompletionItem): Promise<LSP.CompletionItem> {
166-
if (item.data.type === 'executable') {
167-
return this.executables
168-
.documentation(item.data.name)
169-
.then(doc => ({
184+
private async onCompletionResolve(
185+
item: LSP.CompletionItem,
186+
): Promise<LSP.CompletionItem> {
187+
const { data: { name, type } } = item
188+
try {
189+
if (type === 'executable') {
190+
const doc = await this.executables.documentation(name)
191+
return {
170192
...item,
171193
detail: doc,
172-
}))
173-
.catch(() => item)
174-
} else {
175-
return Promise.resolve(item)
194+
}
195+
} else if (type === 'builtin') {
196+
const doc = await Builtins.documentation(name)
197+
return {
198+
...item,
199+
detail: doc,
200+
}
201+
} else {
202+
return item
203+
}
204+
} catch (error) {
205+
return item
176206
}
177207
}
178208
}

‎server/src/util/sh.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as ChildProcess from 'child_process'
55
*/
66
export function execShellScript(body: string): Promise<string> {
77
const args = ['-c', body]
8-
const process = ChildProcess.spawn('sh', args)
8+
const process = ChildProcess.spawn('bash', args)
99

1010
return new Promise((resolve, reject) => {
1111
let output = ''

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /