|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 |
| -/// Represents a source location in a Swift file. |
14 |
| -public struct SourceLocation: Codable { |
| 13 | +/// Represent the user-facing part of SourceLocation that can be calculated |
| 14 | +/// on demand. |
| 15 | +struct ComputedLocation: Codable { |
15 | 16 | /// The line in the file where this location resides. 1-based.
|
16 |
| - public let line: Int |
| 17 | + let line: Int |
17 | 18 |
|
18 | 19 | /// The UTF-8 byte offset from the beginning of the line where this location
|
19 | 20 | /// resides. 1-based.
|
20 |
| - public let column: Int |
| 21 | + let column: Int |
| 22 | + |
| 23 | + /// The file in which this location resides. |
| 24 | + let file: String |
| 25 | + |
| 26 | + init(line: Int, column: Int, file: String) { |
| 27 | + self.line = line |
| 28 | + self.column = column |
| 29 | + self.file = file |
| 30 | + } |
| 31 | + init(offset: Int, using converter: SourceLocationConverter) { |
| 32 | + let loc = converter.location(for: AbsolutePosition(utf8Offset: offset)) |
| 33 | + assert(loc.offset == offset) |
| 34 | + self.line = loc.line! |
| 35 | + self.column = loc.column! |
| 36 | + self.file = loc.file! |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Represents a source location in a Swift file. |
| 41 | +public struct SourceLocation: Codable { |
| 42 | + |
| 43 | + /// Line and column that can be computed on demand. |
| 44 | + private var compLoc: ComputedLocation? |
21 | 45 |
|
22 | 46 | /// The UTF-8 byte offset into the file where this location resides.
|
23 | 47 | public let offset: Int
|
24 | 48 |
|
| 49 | + /// The line in the file where this location resides. 1-based. |
| 50 | + public var line: Int? { |
| 51 | + return compLoc?.line |
| 52 | + } |
| 53 | + |
| 54 | + /// The UTF-8 byte offset from the beginning of the line where this location |
| 55 | + /// resides. 1-based. |
| 56 | + public var column: Int? { |
| 57 | + return compLoc?.column |
| 58 | + } |
| 59 | + |
25 | 60 | /// The file in which this location resides.
|
26 |
| - public let file: String |
| 61 | + public var file: String? { |
| 62 | + return compLoc?.file |
| 63 | + } |
27 | 64 |
|
28 | 65 | public init(line: Int, column: Int, offset: Int, file: String) {
|
29 |
| - self.line = line |
30 |
| - self.column = column |
31 | 66 | self.offset = offset
|
32 |
| - self.file = file |
| 67 | + self.compLoc = ComputedLocation(line: line, column: column, file: file) |
| 68 | + } |
| 69 | + |
| 70 | + /// Initialize SourceLocation with a utf8 offset. Line, column and file will |
| 71 | + /// be nil after invoking this initializer. Client can call computeLineColumn(using) |
| 72 | + /// later to populate these fields. |
| 73 | + public init(offset: Int) { |
| 74 | + self.offset = offset |
| 75 | + } |
| 76 | + |
| 77 | + /// Populate line, column and file by using a SourceLocationConverter. |
| 78 | + /// It will assert if these fileds have already been populated. |
| 79 | + public mutating func computeLineColumn(using converter: SourceLocationConverter) { |
| 80 | + assert(compLoc == nil, "line and column have already been calculated") |
| 81 | + self.compLoc = ComputedLocation(offset: offset, using: converter) |
33 | 82 | }
|
34 | 83 | }
|
35 | 84 |
|
|
0 commit comments