Skip to content

Commit 4107a32

Browse files
committed
Centralize shell documentation
1 parent 991ad3d commit 4107a32

File tree

10 files changed

+567
-82
lines changed

10 files changed

+567
-82
lines changed

server/src/__tests__/builtins.test.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

server/src/__tests__/executables.test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ describe('list', () => {
2727
})
2828
})
2929

30-
describe('documentation', () => {
31-
it('uses `man` so it disregards the PATH it has been initialized with', async () => {
32-
const result = await executables.documentation('ls')
33-
expect(result).toBeTruthy()
34-
})
35-
})
36-
3730
describe('isExecutableOnPATH', () => {
3831
it('looks at the PATH it has been initialized with', async () => {
3932
const result = executables.isExecutableOnPATH('ls')

server/src/__tests__/server.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('server', () => {
7171
expect(result).toEqual({
7272
contents: {
7373
kind: 'markdown',
74-
value: expect.stringContaining('RM(1)'),
74+
value: expect.stringContaining('remove directory'),
7575
},
7676
})
7777
})

server/src/builtins.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as ShUtil from './util/sh'
2-
31
// You can generate this list by running `compgen -b` in a bash session
42
export const LIST = [
53
'.',
@@ -68,13 +66,3 @@ const SET = new Set(LIST)
6866
export function isBuiltin(word: string): boolean {
6967
return SET.has(word)
7068
}
71-
72-
export async function documentation(builtin: string): Promise<string> {
73-
const errorMessage = `No help page for ${builtin}`
74-
try {
75-
const doc = await ShUtil.execShellScript(`help ${builtin}`)
76-
return doc || errorMessage
77-
} catch (error) {
78-
return errorMessage
79-
}
80-
}

server/src/executables.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { promisify } from 'util'
44

55
import * as ArrayUtil from './util/array'
66
import * as FsUtil from './util/fs'
7-
import * as ShUtil from './util/sh'
87

98
const lstatAsync = promisify(fs.lstat)
109
const readdirAsync = promisify(fs.readdir)
@@ -44,19 +43,6 @@ export default class Executables {
4443
public isExecutableOnPATH(executable: string): boolean {
4544
return this.executables.has(executable)
4645
}
47-
48-
/**
49-
* Look up documentation for the given executable.
50-
*
51-
* For now it simply tries to look up the MAN documentation.
52-
*/
53-
public documentation(executable: string): Promise<string> {
54-
return ShUtil.execShellScript(`man ${executable} | col -b`).then(doc => {
55-
return !doc
56-
? Promise.resolve(`No MAN page for ${executable}`)
57-
: Promise.resolve(doc)
58-
})
59-
}
6046
}
6147

6248
/**

server/src/reservedWords.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as ShUtil from './util/sh'
2-
31
// https://www.gnu.org/software/bash/manual/html_node/Reserved-Word-Index.html
42

53
export const LIST = [
@@ -31,12 +29,3 @@ const SET = new Set(LIST)
3129
export function isReservedWord(word: string): boolean {
3230
return SET.has(word)
3331
}
34-
35-
export async function documentation(reservedWord: string): Promise<string> {
36-
try {
37-
const doc = await ShUtil.execShellScript(`help ${reservedWord}`)
38-
return doc || ''
39-
} catch (error) {
40-
return ''
41-
}
42-
}

server/src/server.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { initializeParser } from './parser'
1111
import * as ReservedWords from './reservedWords'
1212
import { BashCompletionItem, CompletionItemDataType } from './types'
1313
import { uniqueBasedOnHash } from './util/array'
14+
import { getShellDocumentation } from './util/sh'
1415

1516
/**
1617
* The BashServer glues together the separate components to implement
@@ -138,6 +139,7 @@ export default class BashServer {
138139
params: LSP.TextDocumentPositionParams,
139140
): Promise<LSP.Hover | null> {
140141
const word = this.getWordAtPoint(params)
142+
const currentUri = params.textDocument.uri
141143

142144
this.logRequest({ request: 'onHover', params, word })
143145

@@ -167,29 +169,16 @@ export default class BashServer {
167169
}
168170
}
169171

170-
const getMarkdownHoverItem = (doc: string) => ({
171-
// LSP.MarkupContent
172-
value: ['``` man', doc, '```'].join('\n'),
173-
// Passed as markdown for syntax highlighting
174-
kind: 'markdown' as const,
175-
})
176-
177-
if (Builtins.isBuiltin(word)) {
178-
return Builtins.documentation(word).then(doc => ({
179-
contents: getMarkdownHoverItem(doc),
180-
}))
181-
}
182-
183-
if (ReservedWords.isReservedWord(word)) {
184-
return ReservedWords.documentation(word).then(doc => ({
185-
contents: getMarkdownHoverItem(doc),
186-
}))
187-
}
188-
189-
if (this.executables.isExecutableOnPATH(word)) {
190-
return this.executables.documentation(word).then(doc => ({
191-
contents: getMarkdownHoverItem(doc),
192-
}))
172+
if (
173+
ReservedWords.isReservedWord(word) ||
174+
Builtins.isBuiltin(word) ||
175+
this.executables.isExecutableOnPATH(word)
176+
) {
177+
const shellDocumentation = await getShellDocumentation({ word })
178+
if (shellDocumentation) {
179+
// eslint-disable-next-line no-console
180+
return { contents: getMarkdownContent(shellDocumentation) }
181+
}
193182
}
194183

195184
// FIXME: could also be a symbol
Binary file not shown.

0 commit comments

Comments
 (0)