8000 Merge pull request #6 from allevato/swift-4.2-cherrypicks · swiftlang/swift-syntax@aa3f4bd · GitHub
[go: up one dir, main page]

Skip to content

Commit aa3f4bd

Browse files
authored
Merge pull request #6 from allevato/swift-4.2-cherrypicks
Cherry-pick some post-4.2 changes into swift-4.2-branch
2 parents b4295c5 + bd3484b commit aa3f4bd

21 files changed

+710
-436
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===--------------- AbsolutePosition.swift - Source Positions ------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// An absolute position in a source file as text - the absolute utf8Offset from
14+
/// the start, line, and column.
15+
public struct AbsolutePosition {
16+
public let utf8Offset: Int
17+
public let line: Int
18+
public let column: Int
19+
20+ static let startOfFile = AbsolutePosition(line: 1, column: 1, utf8Offset: 0)
21+
22+
public init(line: Int, column: Int, utf8Offset: Int) {
23+
self.line = line
24+
self.column = column
25+
self.utf8Offset = utf8Offset
26+
}
27+
}

Sources/SwiftSyntax/Diagnostic.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ public struct SourceLocation: Codable {
3030
public let file: String
3131

3232
public init(file: String, position: AbsolutePosition) {
33-
assert(position is UTF8Position, "must be utf8 position")
3433
self.init(line: position.line, column: position.column,
35-
offset: position.byteOffset, file: file)
34+
offset: position.utf8Offset, file: file)
3635
}
3736

3837
public init(line: Int, column: Int, offset: Int, file: String) {
@@ -88,7 +87,7 @@ public enum FixIt: Codable {
8887
let string = try container.decode(String.self, forKey: .string)
8988
let loc = try container.decode(SourceLocation.self, forKey: .location)
9089
self = .insert(loc, string)
91-
case "replace":
90+
case "replace":
9291
let string = try container.decode(String.self, forKey: .string)
9392
let range = try container.decode(SourceRange.self, forKey: .range)
9493
self = .replace(range, string)
@@ -202,7 +201,11 @@ public struct Diagnostic: Codable {
202201
/// An array of possible FixIts to apply to this diagnostic.
203202
public let fixIts: [FixIt]
204203

205-
/// A diagnostic builder that
204+
/// A diagnostic builder that exposes mutating operations for notes,
205+
/// highlights, and FixIts. When a Diagnostic is created, a builder
206+
/// will be provided in a closure where the user can conditionally
207+
/// add notes, highlights, and FixIts, that will then be wrapped
208+
/// into the immutable Diagnostic object.
206209
public struct Builder {
207210
/// An in-flight array of notes.
208211
internal var notes = [Note]()
@@ -225,7 +228,7 @@ public struct Diagnostic: Codable {
225228
/// - fixIts: Any FixIts that should be attached to this note.
226229
public mutating func note(_ message: Message,
227230
location: SourceLocation? = nil,
228-
highlights: [SourceRange] = [],
231+
highlights: [SourceRange] = [],
229232
fixIts: [FixIt] = []) {
230233
self.notes.append(Note(message: message, location: location,
231234
highlights: highlights, fixIts: fixIts))
@@ -252,7 +255,7 @@ public struct Diagnostic: Codable {
252255

253256
/// Adds a FixIt to replace the contents of the source file corresponding
254257
/// to the provided SourceRange with the provided text.
255-
public mutating
258+
public mutating
256259
func fixItReplace(_ sourceRange: SourceRange, with text: String) {
257260
fixIts.append(.replace(sourceRange, text))
258261
}

0 commit comments

Comments
 (0)
0