@@ -5,6 +5,7 @@ import * as LSP from 'vscode-languageserver'
5
5
import * as Parser from 'web-tree-sitter'
6
6
7
7
import { getGlobPattern } from './config'
8
+ import { BashCompletionItem , CompletionItemDataType } from './types'
8
9
import { uniqueBasedOnHash } from './util/array'
9
10
import { flattenArray , flattenObjectValues } from './util/flatten'
10
11
import { getFilePaths } from './util/fs'
@@ -118,17 +119,17 @@ export default class Analyzer {
118
119
}
119
120
120
121
public async getExplainshellDocumentation ( {
121
- pos ,
122
+ params ,
122
123
endpoint,
123
124
} : {
124
- pos : LSP . TextDocumentPositionParams
125
+ params : LSP . TextDocumentPositionParams
125
126
endpoint : string
126
127
} ) : Promise < any > {
127
128
const leafNode = this . uriToTreeSitterTrees [
128
- pos . textDocument . uri
129
+ params . textDocument . uri
129
130
] . rootNode . descendantForPosition ( {
130
- row : pos . position . line ,
131
- column : pos . position . character ,
131
+ row : params . position . line ,
132
+ column : params . position . character ,
132
133
} )
133
134
134
135
// explainshell needs the whole command, not just the "word" (tree-sitter
@@ -138,7 +139,7 @@ export default class Analyzer {
138
139
// encounters newlines.
139
140
const interestingNode = leafNode . type === 'word' ? leafNode . parent : leafNode
140
141
141
- const cmd = this . uriToFileContent [ pos . textDocument . uri ] . slice (
142
+ const cmd = this . uriToFileContent [ params . textDocument . uri ] . slice (
142
143
interestingNode . startIndex ,
143
144
interestingNode . endIndex ,
144
145
)
@@ -162,7 +163,7 @@ export default class Analyzer {
162
163
return { ...response , status : 'error' }
163
164
} else {
164
165
const offsetOfMousePointerInCommand =
165
- this . uriToTextDocument [ pos . textDocument . uri ] . offsetAt ( pos . position ) -
166
+ this . uriToTextDocument [ params . textDocument . uri ] . offsetAt ( params . position ) -
166
167
interestingNode . startIndex
167
168
168
169
const match = explainshellResponse . matches . find (
@@ -232,7 +233,7 @@ export default class Analyzer {
232
233
/**
233
234
* Find unique symbol completions for the given file.
234
235
*/
235
- public findSymbolCompletions ( uri : string ) : LSP . CompletionItem [ ] {
236
+ public findSymbolCompletions ( uri : string ) : BashCompletionItem [ ] {
236
237
const hashFunction = ( { name, kind } : LSP . SymbolInformation ) => `${ name } ${ kind } `
237
238
238
239
return uniqueBasedOnHash ( this . findSymbols ( uri ) , hashFunction ) . map (
@@ -241,7 +242,7 @@ export default class Analyzer {
241
242
kind : this . symbolKindToCompletionKind ( symbol . kind ) ,
242
243
data : {
243
244
name : symbol . name ,
244
- type : 'function' ,
245
+ type : CompletionItemDataType . Symbol ,
245
246
} ,
246
247
} ) ,
247
248
)
@@ -328,13 +329,25 @@ export default class Analyzer {
328
329
const document = this . uriToTreeSitterTrees [ uri ]
329
330
const contents = this . uriToFileContent [ uri ]
330
331
331
- const node = document . rootNode . namedDescendantForPosition ( { row : line , column } )
332
+ if ( ! document . rootNode ) {
333
+ // Check for lacking rootNode (due to failed parse?)
334
+ return null
335
+ }
336
+
337
+ const point = { row : line , column }
338
+
339
+ const node = TreeSitterUtil . namedLeafDescendantForPosition ( point , document . rootNode )
340
+
341
+ if ( ! node ) {
342
+ return null
343
+ }
332
344
333
345
const start = node . startIndex
334
346
const end = node . endIndex
335
347
const name = contents . slice ( start , end )
336
348
337
349
// Hack. Might be a problem with the grammar.
350
+ // TODO: Document this with a test case
338
351
if ( name . endsWith ( '=' ) ) {
339
352
return name . slice ( 0 , name . length - 1 )
340
353
}
0 commit comments