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

Builtins support #33

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
mads-hartmann merged 6 commits into master from builtins-support
Apr 21, 2018
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
14 changes: 14 additions & 0 deletions server/src/__tests__/builtins.test.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Builtins from '../builtins'

describe('documentation', () => {
it('returns an error string an unknown builtin', async () => {
const result = await Builtins.documentation('foobar')
expect(result).toEqual('No help page for foobar')
})

it('returns documentation string an known builtin', async () => {
const result = await Builtins.documentation('exit')
const firstLine = result.split('\n')[0]
expect(firstLine).toEqual('exit: exit [n]')
})
})
77 changes: 77 additions & 0 deletions server/src/builtins.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as ShUtil from './util/sh'

// You can generate this list by running `compgen -b` in a bash session
export const LIST = [
'.',
':',
'[',
'alias',
'bg',
'bind',
'break',
'builtin',
'caller',
'cd',
'command',
'compgen',
'complete',
'continue',
'declare',
'dirs',
'disown',
'echo',
'enable',
'eval',
'exec',
'exit',
'export',
'false',
'fc',
'fg',
'getopts',
'hash',
'help',
'history',
'jobs',
'kill',
'let',
'local',
'logout',
'popd',
'printf',
'pushd',
'pwd',
'read',
'readonly',
'return',
'set',
'shift',
'shopt',
'source',
'suspend',
'test',
'times',
'trap',
'true',
'type',
'typeset',
'ulimit',
'umask',
'unalias',
'unset',
'wait',
]

export function isBuiltin(word: string): boolean {
return LIST.find(builtin => builtin === word) !== undefined
}

export async function documentation(builtin: string): Promise<string> {
const errorMessage = `No help page for ${builtin}`
try {
const doc = await ShUtil.execShellScript(`help ${builtin}`)
return doc || errorMessage
} catch (error) {
return errorMessage
}
}
66 changes: 48 additions & 18 deletions server/src/server.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as LSP from 'vscode-languageserver'

import Analyzer from './analyser'
import * as Builtins from './builtins'
import Executables from './executables'

/**
Expand Down Expand Up @@ -106,14 +107,23 @@ export default class BashServer {

const word = this.getWordAtPoint(pos)

return this.executables.isExecutableOnPATH(word)
? this.executables.documentation(word).then(doc => ({
contents: {
language: 'plaintext',
value: doc,
},
}))
: null
if (Builtins.isBuiltin(word)) {
return Builtins.documentation(word).then(doc => ({
contents: {
language: 'plaintext',
value: doc,
},
}))
} else if (this.executables.isExecutableOnPATH(word)) {
return this.executables.documentation(word).then(doc => ({
contents: {
language: 'plaintext',
value: doc,
},
}))
} else {
return null
}
}

private onDefinition(pos: LSP.TextDocumentPositionParams): LSP.Definition {
Expand Down Expand Up @@ -159,20 +169,40 @@ export default class BashServer {
}
})

return symbolCompletions.concat(programCompletions)
const builtinsCompletions = Builtins.LIST.map(builtin => ({
label: builtin,
kind: LSP.SymbolKind.Method, // ??
data: {
name: builtin,
type: 'builtin',
},
}))

return [...symbolCompletions, ...programCompletions, ...builtinsCompletions]
}

private onCompletionResolve(item: LSP.CompletionItem): Promise<LSP.CompletionItem> {
if (item.data.type === 'executable') {
return this.executables
.documentation(item.data.name)
.then(doc => ({
private async onCompletionResolve(
item: LSP.CompletionItem,
): Promise<LSP.CompletionItem> {
const { data: { name, type } } = item
try {
if (type === 'executable') {
const doc = await this.executables.documentation(name)
return {
...item,
detail: doc,
}))
.catch(() => item)
} else {
return Promise.resolve(item)
}
} else if (type === 'builtin') {
const doc = await Builtins.documentation(name)
return {
...item,
detail: doc,
}
} else {
return item
}
} catch (error) {
return item
}
}
}
2 changes: 1 addition & 1 deletion server/src/util/sh.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as ChildProcess from 'child_process'
*/
export function execShellScript(body: string): Promise<string> {
const args = ['-c', body]
const process = ChildProcess.spawn('sh', args)
const process = ChildProcess.spawn('bash', args)

return new Promise((resolve, reject) => {
let output = ''
Expand Down

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