@@ -201,10 +201,8 @@ public class WhitespaceLinter {
201
201
return
202
202
}
203
203
204
- let pos = calculatePosition ( offset: adjustedUserOffset, data: self . userText)
205
-
206
204
isLineTooLong = true
207
- diagnose ( . lineLengthError, line : pos . line , column : pos . column , utf8Offset: 0 )
205
+ diagnose ( . lineLengthError, utf8Offset: adjustedUserOffset )
208
206
}
209
207
210
208
/// Compare user and formatted whitespace buffers, and check for indentation errors.
@@ -231,11 +229,7 @@ public class WhitespaceLinter {
231
229
if form [ 0 ] != user [ 0 ] {
232
230
let actual = indentation ( of: user [ 0 ] )
233
231
let expected = indentation ( of: form [ 0 ] )
234
- diagnose (
235
- . indentationError( expected: expected, actual: actual) ,
236
- line: 1 ,
237
- column: 1 ,
238
- utf8Offset: 0 )
232
+ diagnose ( . indentationError( expected: expected, actual: actual) , utf8Offset: 0 )
239
233
}
240
234
}
241
235
return
@@ -245,14 +239,10 @@ public class WhitespaceLinter {
245
239
offset += user [ i] . count + 1
246
240
}
247
241
if form. last != user. last {
248
- let pos = calculatePosition ( offset: userOffset + offset, data: self . userText)
249
242
let actual = indentation ( of: user. last ?? [ ] )
250
243
let expected = indentation ( of: form. last ?? [ ] )
251
244
diagnose (
252
- . indentationError( expected: expected, actual: actual) ,
253
- line: pos. line,
254
- column: pos. column,
255
- utf8Offset: 0 )
245
+ . indentationError( expected: expected, actual: actual) , utf8Offset: userOffset + offset)
256
246
}
257
247
}
258
248
@@ -269,8 +259,7 @@ public class WhitespaceLinter {
269
259
var offset = 0
270
260
for i in 0 ..< ( user. count - 1 ) {
271
261
if user [ i] . count > 0 {
272
- let pos = calculatePosition ( offset: userOffset + offset, data: self . userText)
273
- diagnose ( . trailingWhitespaceError, line: pos. line, column: pos. column, utf8Offset: 0 )
262
+ diagnose ( . trailingWhitespaceError, utf8Offset: userOffset + offset)
274
263
}
275
264
offset += user [ i] . count + 1
276
265
}
@@ -295,13 +284,12 @@ public class WhitespaceLinter {
295
284
guard form. count == 1 && user. count == 1 && !isFirstCharacter else { return }
296
285
guard form [ 0 ] != user [ 0 ] else { return }
297
286
298
- let pos = calculatePosition ( offset: userOffset, data: self . userText)
299
287
let illegalSpacingCharacters : [ UTF8 . CodeUnit ] = [ utf8Tab]
300
288
if illegalSpacingCharacters. contains ( where: { user [ 0 ] . contains ( $0) } ) {
301
- diagnose ( . spacingCharError, line : pos . line , column : pos . column , utf8Offset: 0 )
289
+ diagnose ( . spacingCharError, utf8Offset: userOffset )
302
290
} else if form [ 0 ] . count != user [ 0 ] . count {
303
291
let delta = form [ 0 ] . count - user[ 0 ] . count
304
- diagnose ( . spacingError( delta) , line : pos . line , column : pos . column , utf8Offset: 0 )
292
+ diagnose ( . spacingError( delta) , utf8Offset: userOffset )
305
293
}
306
294
}
307
295
@@ -328,8 +316,7 @@ public class WhitespaceLinter {
328
316
guard form. count < user. count else { return }
329
317
var offset = 0
330
318
for i in 0 ..< ( user. count - form. count) {
331
- let pos = calculatePosition ( offset: userOffset + offset, data: self . userText)
332
- diagnose ( . removeLineError, line: pos. line, column: pos. column, utf8Offset: 0 )
319
+ diagnose ( . removeLineError, utf8Offset: userOffset + offset)
333
320
offset += user [ i] . count + 1
334
321
}
335
322
}
@@ -356,10 +343,7 @@ public class WhitespaceLinter {
356
343
userOffset: Int , user: [ ArraySlice < UTF8 . CodeUnit > ] , form: [ ArraySlice < UTF8 . CodeUnit > ]
357
344
) {
358
345
guard form. count > user. count && !isLineTooLong else { return }
359
- let pos = calculatePosition ( offset: userOffset, data: self . userText)
360
- diagnose (
361
- . addLinesError( form. count - user. count) , line: pos. line, column: pos. column, utf8Offset: 0
362
- )
346
+ diagnose ( . addLinesError( form. count - user. count) , utf8Offset: userOffset)
363
347
}
364
348
365
349
/// Find the next non-whitespace character in a given string, and any leading whitespace before
@@ -392,28 +376,6 @@ public class WhitespaceLinter {
392
376
return ( offset: data. count - 1 , char: nil , whitespace: whitespaceBuffer)
393
377
}
394
378
395
- /// Given a string and a printable charater offset, calculate the line and column number.
396
- ///
397
- /// - Parameters:
398
- /// - offset: The printable character offset.
399
- /// - data: The input string for which we want the line and column numbers.
400
- /// - Returns a tuple with the line and column numbers within `data`.
401
- private func calculatePosition( offset: Int , data: [ UTF8 . CodeUnit ] ) -> ( line: Int , column: Int ) {
402
- var line = 1
403
- var column = 0
404
-
405
- for (index, char) in data. enumerated ( ) {
406
- if char == utf8Newline {
407
- line += 1
408
- column = 0
409
- } else {
410
- column += 1
411
- }
412
- if index == offset { break }
413
- }
414
- return ( line: line, column: column)
415
- }
416
-
417
379
/// Emits the provided diagnostic message to the DiagnosticEngine. The message will correspond to
418
380
/// a specific location (line and column number) in the input Swift source file (`userText`).
419
381
///
@@ -425,18 +387,14 @@ public class WhitespaceLinter {
425
387
/// - actions: Used for attaching notes, highlights, etc.
426
388
private func diagnose(
427
389
_ message: Diagnostic . Message ,
428
- line: Int ,
429
- column: Int ,
430
390
utf8Offset: Int ,
431
391
actions: ( ( inout Diagnostic . Builder ) -> Void ) ? = nil
432
392
) {
433
- let loc = SourceLocation (
434
- line: line, column: column, offset: utf8Offset, file: context. fileURL. path)
435
- context. diagnosticEngine? . diagnose (
436
- message,
437
- location: loc,
438
- actions: actions
439
- )
393
+ guard let diagnosticEngine = context. diagnosticEngine else { return }
394
+
395
+ let absolutePosition = AbsolutePosition ( utf8Offset: utf8Offset)
396
+ let sourceLocation = context. sourceLocationConverter. location ( for: absolutePosition)
397
+ diagnosticEngine. diagnose ( message, location: sourceLocation, actions: actions)
440
398
}
441
399
442
400
/// Returns the indentation that represents the indentation of the given whitespace, which is the
0 commit comments