Skip to content

Commit ec8c828

Browse files
authored
Merge pull request #564 from bash-lsp/explain-shell-update
Get rid of deprecated HTTP library for explainshell
2 parents fa9120c + 04f2d23 commit ec8c828

File tree

5 files changed

+77
-406
lines changed

5 files changed

+77
-406
lines changed

server/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Bash Language Server
22

3+
## 3.2.2
4+
5+
- Get rid of deprecated dependencies for the explainshell integration https://github.com/bash-lsp/bash-language-server/pull/564
6+
37
## 3.2.1
48

59
- Fix shebang parser that ignores some bash files https://github.com/bash-lsp/bash-language-server/pull/560

server/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "A language server for Bash",
44
"author": "Mads Hartmann",
55
"license": "MIT",
6-
"version": "3.2.1",
6+
"version": "3.2.2",
77
"publisher": "mads-hartmann",
88
"main": "./out/server.js",
99
"typings": "./out/server.d.ts",
@@ -18,10 +18,10 @@
1818
"node": ">=12.0.0"
1919
},
2020
"dependencies": {
21+
"@types/node-fetch": "2.6.2",
2122
"fuzzy-search": "^3.2.1",
2223
"glob": "^8.0.0",
23-
"request": "^2.83.0",
24-
"request-promise-native": "^1.0.5",
24+
"node-fetch": "^2.6.7",
2525
"turndown": "^7.0.0",
2626
"urijs": "^1.19.11",
2727
"vscode-languageserver": "^6.1.1",
@@ -34,7 +34,6 @@
3434
"devDependencies": {
3535
"@types/fuzzy-search": "2.1.2",
3636
"@types/glob": "8.0.0",
37-
"@types/request-promise-native": "1.0.18",
3837
"@types/turndown": "5.0.1",
3938
"@types/urijs": "1.19.19"
4039
}

server/src/analyser.ts

+19-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs'
22
import * as FuzzySearch from 'fuzzy-search'
3-
import * as request from 'request-promise-native'
3+
import fetch from 'node-fetch'
44
import * as URI from 'urijs'
55
import * as url from 'url'
66
import { promisify } from 'util'
@@ -33,7 +33,8 @@ export default class Analyzer {
3333
* root path.
3434
*
3535
* If the rootPath is provided it will initialize all shell files it can find
36-
* anywhere on that path. This non-exhaustive glob is used to preload the parser.
36+
* anywhere on that path. This non-exhaustive glob is used to preload the parser
37+
* to support features across files.
3738
*/
3839
public static async fromRoot({
3940
connection,
@@ -147,7 +148,7 @@ export default class Analyzer {
147148
}: {
148149
params: LSP.TextDocumentPositionParams
149150
endpoint: string
150-
}): Promise<any> {
151+
}): Promise<{ helpHTML?: string }> {
151152
const leafNode = this.uriToTreeSitterTrees[
152153
params.textDocument.uri
153154
].rootNode.descendantForPosition({
@@ -163,49 +164,38 @@ export default class Analyzer {
163164
const interestingNode = leafNode.type === 'word' ? leafNode.parent : leafNode
164165

165166
if (!interestingNode) {
166-
return {
167-
status: 'error',
168-
message: 'no interestingNode found',
169-
}
167+
return {}
170168
}
171169

172170
const cmd = this.uriToFileContent[params.textDocument.uri].slice(
173171
interestingNode.startIndex,
174172
interestingNode.endIndex,
175173
)
174+
type ExplainshellResponse = {
175+
matches?: Array<{ helpHTML: string; start: number; end: number }>
176+
}
176177

177-
// FIXME: type the response and unit test it
178-
const explainshellResponse = await request({
179-
uri: URI(endpoint).path('/api/explain').addQuery('cmd', cmd).toString(),
180-
json: true,
181-
})
182-
183-
// Attaches debugging information to the return value (useful for logging to
184-
// VS Code output).
185-
const response = { ...explainshellResponse, cmd, cmdType: interestingNode.type }
178+
const url = URI(endpoint).path('/api/explain').addQuery('cmd', cmd).toString()
179+
const explainshellRawResponse = await fetch(url)
180+
const explainshellResponse =
181+
(await explainshellRawResponse.json()) as ExplainshellResponse
186182

187-
if (explainshellResponse.status === 'error') {
188-
return response
183+
if (!explainshellRawResponse.ok) {
184+
throw new Error(`HTTP request failed: ${url}`)
189185
} else if (!explainshellResponse.matches) {
190-
return { ...response, status: 'error' }
186+
return {}
191187
} else {
192188
const offsetOfMousePointerInCommand =
193189
this.uriToTextDocument[params.textDocument.uri].offsetAt(params.position) -
194190
interestingNode.startIndex
195191

196192
const match = explainshellResponse.matches.find(
197-
(helpItem: any) =>
193+
(helpItem) =>
198194
helpItem.start <= offsetOfMousePointerInCommand &&
199195
offsetOfMousePointerInCommand < helpItem.end,
200196
)
201197

202-
const helpHTML = match && match.helpHTML
203-
204-
if (!helpHTML) {
205-
return { ...response, status: 'error' }
206-
}
207-
208-
return { ...response, helpHTML }
198+
return { helpHTML: match && match.helpHTML }
209199
}
210200
}
211201

@@ -261,6 +251,8 @@ export default class Analyzer {
261251

262252
/**
263253
* Find symbol completions for the given word.
254+
*
255+
* TODO: if a file is not included we probably shouldn't include it declarations from it.
264256
*/
265257
public findSymbolsMatchingWord({
266258
exactMatch,

server/src/server.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,17 @@ export default class BashServer {
232232

233233
const explainshellEndpoint = config.getExplainshellEndpoint()
234234
if (explainshellEndpoint) {
235-
this.connection.console.log(`Query ${explainshellEndpoint}`)
236235
try {
237-
const response = await this.analyzer.getExplainshellDocumentation({
236+
const { helpHTML } = await this.analyzer.getExplainshellDocumentation({
238237
params,
239238
endpoint: explainshellEndpoint,
240239
})
241240

242-
if (response.status === 'error') {
243-
this.connection.console.log(
244-
`getExplainshellDocumentation returned: ${JSON.stringify(response, null, 4)}`,
245-
)
246-
} else {
241+
if (helpHTML) {
247242
return {
248243
contents: {
249244
kind: 'markdown',
250-
value: new TurndownService().turndown(response.helpHTML),
245+
value: new TurndownService().turndown(helpHTML),
251246
},
252247
}
253248
}

0 commit comments

Comments
 (0)