Skip to content

Get rid of deprecated HTTP library for explainshell #564

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 4 commits into from
Nov 26, 2022
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

## 3.2.2

- Get rid of deprecated dependencies for the explainshell integration https://github.com/bash-lsp/bash-language-server/pull/564

## 3.2.1

- Fix shebang parser that ignores some bash files https://github.com/bash-lsp/bash-language-server/pull/560
Expand Down
7 changes: 3 additions & 4 deletions 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": "3.2.1",
"version": "3.2.2",
"publisher": "mads-hartmann",
"main": "./out/server.js",
"typings": "./out/server.d.ts",
Expand All @@ -18,10 +18,10 @@
"node": ">=12.0.0"
},
"dependencies": {
"@types/node-fetch": "2.6.2",
"fuzzy-search": "^3.2.1",
"glob": "^8.0.0",
"request": "^2.83.0",
"request-promise-native": "^1.0.5",
"node-fetch": "^2.6.7",
"turndown": "^7.0.0",
"urijs": "^1.19.11",
"vscode-languageserver": "^6.1.1",
Expand All @@ -34,7 +34,6 @@
"devDependencies": {
"@types/fuzzy-search": "2.1.2",
"@types/glob": "8.0.0",
"@types/request-promise-native": "1.0.18",
"@types/turndown": "5.0.1",
"@types/urijs": "1.19.19"
}
Expand Down
46 changes: 19 additions & 27 deletions server/src/analyser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs'
import * as FuzzySearch from 'fuzzy-search'
import * as request from 'request-promise-native'
import fetch from 'node-fetch'
import * as URI from 'urijs'
import * as url from 'url'
import { promisify } from 'util'
Expand Down Expand Up @@ -33,7 +33,8 @@ export default class Analyzer {
* root path.
*
* If the rootPath is provided it will initialize all shell files it can find
* anywhere on that path. This non-exhaustive glob is used to preload the parser.
* anywhere on that path. This non-exhaustive glob is used to preload the parser
* to support features across files.
*/
public static async fromRoot({
connection,
Expand Down Expand Up @@ -147,7 +148,7 @@ export default class Analyzer {
}: {
params: LSP.TextDocumentPositionParams
endpoint: string
}): Promise<any> {
}): Promise<{ helpHTML?: string }> {
const leafNode = this.uriToTreeSitterTrees[
params.textDocument.uri
].rootNode.descendantForPosition({
Expand All @@ -163,49 +164,38 @@ export default class Analyzer {
const interestingNode = leafNode.type === 'word' ? leafNode.parent : leafNode

if (!interestingNode) {
return {
status: 'error',
message: 'no interestingNode found',
}
return {}
}

const cmd = this.uriToFileContent[params.textDocument.uri].slice(
interestingNode.startIndex,
interestingNode.endIndex,
)
type ExplainshellResponse = {
matches?: Array<{ helpHTML: string; start: number; end: number }>
}

// FIXME: type the response and unit test it
const explainshellResponse = await request({
uri: URI(endpoint).path('/api/explain').addQuery('cmd', cmd).toString(),
json: true,
})

// Attaches debugging information to the return value (useful for logging to
// VS Code output).
const response = { ...explainshellResponse, cmd, cmdType: interestingNode.type }
const url = URI(endpoint).path('/api/explain').addQuery('cmd', cmd).toString()
const explainshellRawResponse = await fetch(url)
const explainshellResponse =
(await explainshellRawResponse.json()) as ExplainshellResponse

if (explainshellResponse.status === 'error') {
return response
if (!explainshellRawResponse.ok) {
throw new Error(`HTTP request failed: ${url}`)
} else if (!explainshellResponse.matches) {
return { ...response, status: 'error' }
return {}
} else {
const offsetOfMousePointerInCommand =
this.uriToTextDocument[params.textDocument.uri].offsetAt(params.position) -
interestingNode.startIndex

const match = explainshellResponse.matches.find(
(helpItem: any) =>
(helpItem) =>
helpItem.start <= offsetOfMousePointerInCommand &&
offsetOfMousePointerInCommand < helpItem.end,
)

const helpHTML = match && match.helpHTML

if (!helpHTML) {
return { ...response, status: 'error' }
}

return { ...response, helpHTML }
return { helpHTML: match && match.helpHTML }
}
}

Expand Down Expand Up @@ -261,6 +251,8 @@ export default class Analyzer {

/**
* Find symbol completions for the given word.
*
* TODO: if a file is not included we probably shouldn't include it declarations from it.
*/
public findSymbolsMatchingWord({
exactMatch,
Expand Down
11 changes: 3 additions & 8 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,17 @@ export default class BashServer {

const explainshellEndpoint = config.getExplainshellEndpoint()
if (explainshellEndpoint) {
this.connection.console.log(`Query ${explainshellEndpoint}`)
try {
const response = await this.analyzer.getExplainshellDocumentation({
const { helpHTML } = await this.analyzer.getExplainshellDocumentation({
params,
endpoint: explainshellEndpoint,
})

if (response.status === 'error') {
this.connection.console.log(
`getExplainshellDocumentation returned: ${JSON.stringify(response, null, 4)}`,
)
} else {
if (helpHTML) {
return {
contents: {
kind: 'markdown',
value: new TurndownService().turndown(response.helpHTML),
value: new TurndownService().turndown(helpHTML),
},
}
}
Expand Down
Loading