Skip to content

Commit 2ae353f

Browse files
committed
Add documentation to (nearly) all public functions in the SwiftSyntax module
1 parent 5ebcc29 commit 2ae353f

18 files changed

+307
-42
lines changed

Sources/SwiftParserDiagnostics/MultiLineStringLiteralDiagnosticsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ final class MultiLineStringLiteralIndentatinDiagnosticsGenerator: SyntaxVisitor
6161
let indentationStartIndex = tokenLeadingTrivia.pieces.lastIndex(where: { $0.isNewline })?.advanced(by: 1) ?? tokenLeadingTrivia.startIndex
6262
let preIndentationTrivia = Trivia(pieces: tokenLeadingTrivia[0..<indentationStartIndex])
6363
let indentationTrivia = Trivia(pieces: tokenLeadingTrivia[indentationStartIndex...])
64-
var positionOffset = preIndentationTrivia.byteSize
64+
var positionOffset = preIndentationTrivia.sourceLength.utf8Length
6565

6666
for (invalidTriviaPiece, missingTriviaPiece) in zip(indentationTrivia.decomposed, closeQuote.leadingTrivia.decomposed) {
6767
if invalidTriviaPiece == missingTriviaPiece {

Sources/SwiftParserDiagnostics/Utils.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
extension String {
14+
/// Returns this string with the first letter uppercased.
15+
///
16+
/// If thes string does not start with a letter, no change is made to it.
1417
func withFirstLetterUppercased() -> String {
1518
if let firstLetter = self.first {
1619
return firstLetter.uppercased() + self.dropFirst()
@@ -19,13 +22,19 @@ extension String {
1922
}
2023
}
2124

25+
/// Replace the first occurance of `character` with `replacement`.
26+
///
27+
/// If `character` does not occur in this string, no change is made.
2228
func replacingFirstOccurence(of character: Character, with replacement: Character) -> String {
2329
guard let match = self.firstIndex(of: character) else {
2430
return self
2531
}
2632
return self[startIndex..<match] + String(replacement) + self[index(after: match)...]
2733
}
2834

35+
/// Replace the last occurance of `character` with `replacement`.
36+
///
37+
/// If `character` does not occur in this string, no change is made.
2938
func replacingLastOccurence(of character: Character, with replacement: Character) -> String {
3039
guard let match = self.lastIndex(of: character) else {
3140
return self

Sources/SwiftSyntax/BumpPtrAllocator.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// Bump-pointer allocation.
13+
/// A `BumpPtrAllocator` that allocates `slabSize` at a time.
14+
/// Once all memory in a slab has been used, it allocates a new slab and no
15+
/// memory allocations are necessary until that slab is completely filled up.
1416
@_spi(RawSyntax) @_spi(Testing)
1517
public class BumpPtrAllocator {
1618
typealias Slab = UnsafeMutableRawBufferPointer
@@ -35,6 +37,9 @@ public class BumpPtrAllocator {
3537
private var customSizeSlabs: [Slab]
3638
private var _totalBytesAllocated: Int
3739

40+
/// Construct a new ``BumpPtrAllocator`` that allocates `slabSize` at a time.
41+
/// Once all memory in a slab has been used, it allocates a new slab and no
42+
/// memory allocations are necessary until that slab is completely filled up.
3843
public init(slabSize: Int) {
3944
self.slabSize = slabSize
4045
slabs = []

Sources/SwiftSyntax/IncrementalParseTransition.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ public struct IncrementalParseLookup {
206206
fileprivate let transition: IncrementalParseTransition
207207
fileprivate var cursor: SyntaxCursor
208208

209+
/// Create a new `IncrementalParseLookup` that can look nodes up based on the
210+
/// given ``IncrementalParseTransition``.
209211
public init(transition: IncrementalParseTransition) {
210212
self.transition = transition
211213
self.cursor = .init(root: transition.previousTree.data)

Sources/SwiftSyntax/Raw/RawSyntax.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ extension RawSyntax {
342342
}
343343

344344
extension RawSyntax {
345+
@_spi(RawSyntax)
345346
public func toOpaque() -> UnsafeRawPointer {
346347
UnsafeRawPointer(pointer)
347348
}
348349

350+
@_spi(RawSyntax)
349351
public static func fromOpaque(_ pointer: UnsafeRawPointer) -> RawSyntax {
350352
Self(pointer: pointer.assumingMemoryBound(to: RawSyntaxData.self))
351353
}
@@ -866,6 +868,7 @@ extension RawSyntax: CustomDebugStringConvertible {
866868
target.write(")")
867869
}
868870

871+
@_spi(RawSyntax)
869872
public var debugDescription: String {
870873
var string = ""
871874
debugWrite(to: &string, indent: 0, withChildren: false)
@@ -874,6 +877,7 @@ extension RawSyntax: CustomDebugStringConvertible {
874877
}
875878

876879
extension RawSyntax: CustomReflectable {
880+
@_spi(RawSyntax)
877881
public var customMirror: Mirror {
878882

879883
let mirrorChildren: [Any]

Sources/SwiftSyntax/Raw/RawSyntaxNodeProtocol.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@ public extension RawSyntaxNodeProtocol {
5858

5959
/// `RawSyntax` itself conforms to `RawSyntaxNodeProtocol`.
6060
extension RawSyntax: RawSyntaxNodeProtocol {
61+
@_spi(RawSyntax)
6162
public static func isKindOf(_ raw: RawSyntax) -> Bool {
6263
return true
6364
}
65+
66+
@_spi(RawSyntax)
6467
public var raw: RawSyntax { self }
6568

6669
init(raw: RawSyntax) {
6770
self = raw
6871
}
6972

73+
@_spi(RawSyntax)
7074
public init(_ other: some RawSyntaxNodeProtocol) {
7175
self.init(raw: other.raw)
7276
}
@@ -75,6 +79,7 @@ extension RawSyntax: RawSyntaxNodeProtocol {
7579
#if swift(<5.8)
7680
// Cherry-pick this function from SE-0370
7781
extension Slice {
82+
@_spi(RawSyntax)
7883
@inlinable
7984
public func initialize<S>(
8085
from source: S
@@ -90,17 +95,20 @@ extension Slice {
9095

9196
@_spi(RawSyntax)
9297
public struct RawTokenSyntax: RawSyntaxNodeProtocol {
98+
@_spi(RawSyntax)
9399
public typealias SyntaxType = TokenSyntax
94100

95101
@_spi(RawSyntax)
96102
public var tokenView: RawSyntaxTokenView {
97103
return raw.tokenView!
98104
}
99105

106+
@_spi(RawSyntax)
100107
public static func isKindOf(_ raw: RawSyntax) -> Bool {
101108
return raw.kind == .token
102109
}
103110

111+
@_spi(RawSyntax)
104112
public var raw: RawSyntax
105113

106114
init(raw: RawSyntax) {
@@ -112,43 +120,53 @@ public struct RawTokenSyntax: RawSyntaxNodeProtocol {
112120
self.raw = raw
113121
}
114122

123+
@_spi(RawSyntax)
115124
public init?(_ other: some RawSyntaxNodeProtocol) {
116125
guard Self.isKindOf(other.raw) else { return nil }
117126
self.init(unchecked: other.raw)
118127
}
119128

129+
@_spi(RawSyntax)
120130
public var tokenKind: RawTokenKind {
121131
return tokenView.rawKind
122132
}
123133

134+
@_spi(RawSyntax)
124135
public var tokenText: SyntaxText {
125136
return tokenView.rawText
126137
}
127138

139+
@_spi(RawSyntax)
128140
public var byteLength: Int {
129141
return raw.byteLength
130142
}
131143

144+
@_spi(RawSyntax)
132145
public var presence: SourcePresence {
133146
tokenView.presence
134147
}
135148

149+
@_spi(RawSyntax)
136150
public var isMissing: Bool {
137151
presence == .missing
138152
}
139153

154+
@_spi(RawSyntax)
140155
public var leadingTriviaByteLength: Int {
141156
return tokenView.leadingTriviaByteLength
142157
}
143158

159+
@_spi(RawSyntax)
144160
public var leadingTriviaPieces: [RawTriviaPiece] {
145161
tokenView.leadingRawTriviaPieces
146162
}
147163

164+
@_spi(RawSyntax)
148165
public var trailingTriviaByteLength: Int {
149166
return tokenView.trailingTriviaByteLength
150167
}
151168

169+
@_spi(RawSyntax)
152170
public var trailingTriviaPieces: [RawTriviaPiece] {
153171
tokenView.trailingRawTriviaPieces
154172
}

Sources/SwiftSyntax/SourceLength.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
/// The length a syntax node spans in the source code. From any AbsolutePosition
1414
/// you reach a node's end location by adding its UTF-8 length.
1515
public struct SourceLength: Comparable {
16+
/// The length in bytes when the text is represented as UTF-8.
1617
public let utf8Length: Int
1718

1819
/// Construct the source length of a given text
1920
public init(of text: String) {
2021
self.utf8Length = text.utf8.count
2122
}
2223

24+
/// Construct a source length that takes up `utf8Length` bytes when represented as UTF-8.
2325
public init(utf8Length: Int) {
2426
self.utf8Length = utf8Length
2527
}
@@ -28,6 +30,7 @@ public struct SourceLength: Comparable {
2830
public static let zero: SourceLength =
2931
SourceLength(utf8Length: 0)
3032

33+
/// Returns `true` if `lhs` is shorter than `rhs`.
3134
public static func < (lhs: SourceLength, rhs: SourceLength) -> Bool {
3235
return lhs.utf8Length < rhs.utf8Length
3336
}
@@ -38,15 +41,18 @@ public struct SourceLength: Comparable {
3841
return SourceLength(utf8Length: utf8Length)
3942
}
4043

44+
/// Extend `lhs` by `rhs` bytes.
4145
public static func += (lhs: inout SourceLength, rhs: SourceLength) {
4246
lhs = lhs + rhs
4347
}
4448

49+
/// Return a source length that's `rhs` bytes shorter than `lhs`.
4550
public static func - (lhs: SourceLength, rhs: SourceLength) -> SourceLength {
4651
let utf8Length = lhs.utf8Length - rhs.utf8Length
4752
return SourceLength(utf8Length: utf8Length)
4853
}
4954

55+
/// Change `lhs` to be `rhs` bytes shorter than `lhs`.
5056
public static func -= (lhs: inout SourceLength, rhs: SourceLength) {
5157
lhs = lhs - rhs
5258
}
@@ -62,10 +68,12 @@ extension AbsolutePosition {
6268
return AbsolutePosition(utf8Offset: utf8Offset)
6369
}
6470

71+
/// Advance `lhs` by `rhs`, i.e. changing it to the position `rhs` bytes after `lhs`.
6572
public static func += (lhs: inout AbsolutePosition, rhs: SourceLength) {
6673
lhs = lhs + rhs
6774
}
6875

76+
/// Return the position `rhs` bytes before `lhs`.
6977
public static func - (
7078
lhs: AbsolutePosition,
7179
rhs: SourceLength
@@ -74,6 +82,7 @@ extension AbsolutePosition {
7482
return AbsolutePosition(utf8Offset: utf8Offset)
7583
}
7684

85+
/// Reverse `lhs` by `rhs`, i.e. changing it `lhs` to the position `rhs` bytes before `lhs`.
7786
public static func -= (lhs: inout AbsolutePosition, rhs: SourceLength) {
7887
lhs = lhs - rhs
7988
}

Sources/SwiftSyntax/SourceLocation.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,27 @@ public struct SourceLocation: Hashable, Codable, CustomDebugStringConvertible {
2626
/// The file in which this location resides.
2727
public let file: String
2828

29+
/// Returns the location as `<line>:<column>` for debugging purposes.
30+
/// Do not rely on this output being stable.
2931
public var debugDescription: String {
3032
// Print file name?
3133
return "\(line):\(column)"
3234
}
3335

36+
/// Create a new source location at the specified `line` and `column` in `file`.
37+
///
38+
/// - Parameters:
39+
/// - line: 1-based, i.e. the first line in the file has line number 1
40+
/// - column: The UTF-8 byte offset of the location with its line, i.e. the
41+
/// number of bytes all characters in the line before the location
42+
/// occupy when encoded as UTF-8. 1-based, i.e. the leftmost
43+
/// column in the file has column 1.
44+
/// - offset: The UTF-8 offset of the location within the entire file, i.e.
45+
/// the number of bytes all source code before the location
46+
/// occupies when encoded as UTF-8. 0-based, i.e. the first
47+
/// location in the source file has `offset` 0.
48+
/// - file: A string describing the name of the file in which this location
49+
/// is contained.
3450
public init(line: Int, column: Int, offset: Int, file: String) {
3551
self.line = line
3652
self.offset = offset
@@ -43,15 +59,22 @@ public struct SourceLocation: Hashable, Codable, CustomDebugStringConvertible {
4359
public struct SourceRange: Hashable, Codable, CustomDebugStringConvertible {
4460

4561
/// The beginning location in the source range.
62+
///
63+
/// This location is included in the range
4664
public let start: SourceLocation
4765

4866
/// The beginning location in the source range.
67+
///
68+
/// This location is no longer part of the range
4969
public let end: SourceLocation
5070

71+
/// A description describing this range for debugging purposes, don't rely on it.
5172
public var debugDescription: String {
5273
return "(\(start.debugDescription),\(end.debugDescription))"
5374
}
5475

76+
/// Construct a new source range, starting at `start` (inclusive) and ending
77+
/// at `end` (exclusive).
5578
public init(start: SourceLocation, end: SourceLocation) {
5679
self.start = start
5780
self.end = end

0 commit comments

Comments
 (0)