1
1
import * as fs from 'fs'
2
2
import * as FuzzySearch from 'fuzzy-search'
3
- import * as request from 'request-promise-native '
3
+ import fetch from 'node-fetch '
4
4
import * as URI from 'urijs'
5
5
import * as url from 'url'
6
6
import { promisify } from 'util'
@@ -33,7 +33,8 @@ export default class Analyzer {
33
33
* root path.
34
34
*
35
35
* 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.
37
38
*/
38
39
public static async fromRoot ( {
39
40
connection,
@@ -147,7 +148,7 @@ export default class Analyzer {
147
148
} : {
148
149
params : LSP . TextDocumentPositionParams
149
150
endpoint : string
150
- } ) : Promise < any > {
151
+ } ) : Promise < { helpHTML ?: string } > {
151
152
const leafNode = this . uriToTreeSitterTrees [
152
153
params . textDocument . uri
153
154
] . rootNode . descendantForPosition ( {
@@ -163,49 +164,38 @@ export default class Analyzer {
163
164
const interestingNode = leafNode . type === 'word' ? leafNode . parent : leafNode
164
165
165
166
if ( ! interestingNode ) {
166
- return {
167
- status : 'error' ,
168
- message : 'no interestingNode found' ,
169
- }
167
+ return { }
170
168
}
171
169
172
170
const cmd = this . uriToFileContent [ params . textDocument . uri ] . slice (
173
171
interestingNode . startIndex ,
174
172
interestingNode . endIndex ,
175
173
)
174
+ type ExplainshellResponse = {
175
+ matches ?: Array < { helpHTML : string ; start : number ; end : number } >
176
+ }
176
177
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
186
182
187
- if ( explainshellResponse . status === 'error' ) {
188
- return response
183
+ if ( ! explainshellRawResponse . ok ) {
184
+ throw new Error ( `HTTP request failed: ${ url } ` )
189
185
} else if ( ! explainshellResponse . matches ) {
190
- return { ... response , status : 'error' }
186
+ return { }
191
187
} else {
192
188
const offsetOfMousePointerInCommand =
193
189
this . uriToTextDocument [ params . textDocument . uri ] . offsetAt ( params . position ) -
194
190
interestingNode . startIndex
195
191
196
192
const match = explainshellResponse . matches . find (
197
- ( helpItem : any ) =>
193
+ ( helpItem ) =>
198
194
helpItem . start <= offsetOfMousePointerInCommand &&
199
195
offsetOfMousePointerInCommand < helpItem . end ,
200
196
)
201
197
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 }
209
199
}
210
200
}
211
201
@@ -261,6 +251,8 @@ export default class Analyzer {
261
251
262
252
/**
263
253
* 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.
264
256
*/
265
257
public findSymbolsMatchingWord ( {
266
258
exactMatch,
0 commit comments