@@ -262,15 +262,15 @@ public func addQueuedDiagnostic(
262
262
text: UnsafePointer < UInt8 > ,
263
263
textLength: Int ,
264
264
severity: BridgedDiagnosticSeverity ,
265
- position : BridgedSourceLoc ,
265
+ cLoc : BridgedSourceLoc ,
266
266
highlightRangesPtr: UnsafePointer < BridgedSourceLoc > ? ,
267
267
numHighlightRanges: Int
268
268
) {
269
269
let queuedDiagnostics = queuedDiagnosticsPtr. assumingMemoryBound (
270
270
to: QueuedDiagnostics . self
271
271
)
272
272
273
- guard let rawPosition = position . getOpaquePointerValue ( ) else {
273
+ guard let rawPosition = cLoc . getOpaquePointerValue ( ) else {
274
274
return
275
275
}
276
276
@@ -280,18 +280,33 @@ public func addQueuedDiagnostic(
280
280
return false
281
281
}
282
282
283
- return rawPosition >= baseAddress && rawPosition < baseAddress + sf. buffer. count
283
+ return rawPosition >= baseAddress && rawPosition <= baseAddress + sf. buffer. count
284
284
}
285
285
guard let sourceFile = sourceFile else {
286
286
// FIXME: Hard to report an error here...
287
287
return
288
288
}
289
289
290
- // Find the token at that offset.
291
290
let sourceFileBaseAddress = UnsafeRawPointer ( sourceFile. buffer. baseAddress!)
292
291
let sourceFileEndAddress = sourceFileBaseAddress + sourceFile. buffer. count
293
292
let offset = rawPosition - sourceFileBaseAddress
294
- guard let token = sourceFile. syntax. token ( at: AbsolutePosition ( utf8Offset: offset) ) else {
293
+ let position = AbsolutePosition ( utf8Offset: offset)
294
+
295
+ // Find the token at that offset.
296
+ let node : Syntax
297
+ if let token = sourceFile. syntax. token ( at: position) {
298
+ node = Syntax ( token)
299
+ } else if position == sourceFile. syntax. endPosition {
300
+ // FIXME: EOF token is not included in '.token(at: position)'
301
+ // We might want to include it, but want to avoid special handling.
302
+ // Also 'sourceFile.syntax' is not guaranteed to be 'SourceFileSyntax'.
303
+ if let token = sourceFile. syntax. lastToken ( viewMode: . all) {
304
+ node = Syntax ( token)
305
+ } else {
306
+ node = sourceFile. syntax
307
+ }
308
+ } else {
309
+ // position out of range.
295
310
return
296
311
}
297
312
@@ -346,7 +361,8 @@ public func addQueuedDiagnostic(
346
361
347
362
let textBuffer = UnsafeBufferPointer ( start: text, count: textLength)
348
363
let diagnostic = Diagnostic (
349
- node: Syntax ( token) ,
364
+ node: node,
365
+ position: position,
350
366
message: SimpleDiagnostic (
351
367
message: String ( decoding: textBuffer, as: UTF8 . self) ,
352
368
severity: severity. asSeverity
0 commit comments