11import * as fs from 'fs'
22import * as FuzzySearch from 'fuzzy-search'
3- import * as request from 'request-promise-native '
3+ import fetch from 'node-fetch '
44import * as URI from 'urijs'
55import * as url from 'url'
66import { 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,
0 commit comments