@@ -300,7 +300,11 @@ extension SwiftLanguageServer {
300
300
return
301
301
}
302
302
303
- let completionPos = adjustCompletionLocation ( req. params. position, in: snapshot)
303
+ guard let completionPos = adjustCompletionLocation ( req. params. position, in: snapshot) else {
304
+ log ( " invalid completion position \( req. params. position) " )
305
+ req. reply ( CompletionList ( isIncomplete: true , items: [ ] ) )
306
+ return
307
+ }
304
308
305
309
guard let offset = snapshot. utf8Offset ( of: completionPos) else {
306
310
log ( " invalid completion position \( req. params. position) (adjusted: \( completionPos) " )
@@ -388,13 +392,20 @@ extension SwiftLanguageServer {
388
392
}
389
393
390
394
/// Adjust completion position to the start of identifier characters.
391
- func adjustCompletionLocation( _ pos: Position , in snapshot: DocumentSnapshot ) -> Position {
395
+ func adjustCompletionLocation( _ pos: Position , in snapshot: DocumentSnapshot ) -> Position ? {
396
+ guard pos. line < snapshot. lineTable. count else {
397
+ // Line out of range.
398
+ return nil
399
+ }
392
400
let lineSlice = snapshot. lineTable [ pos. line]
393
401
let startIndex = lineSlice. startIndex
394
402
395
403
let identifierChars = CharacterSet . alphanumerics. union ( CharacterSet ( charactersIn: " _ " ) )
396
404
397
- var loc = lineSlice. utf16. index ( startIndex, offsetBy: pos. utf16index)
405
+ guard var loc = lineSlice. utf16. index ( startIndex, offsetBy: pos. utf16index, limitedBy: lineSlice. endIndex) else {
406
+ // Column out of range.
407
+ return nil
408
+ }
398
409
while loc != startIndex {
399
410
let prev = lineSlice. index ( before: loc)
400
411
if !identifierChars. contains ( lineSlice. unicodeScalars [ prev] ) {
0 commit comments