Skip to content

Commit 0edd761

Browse files
committed
Rename contentLength -> trimmedLength
1 parent 5bfa847 commit 0edd761

File tree

9 files changed

+30
-19
lines changed

9 files changed

+30
-19
lines changed

Sources/SwiftParserDiagnostics/MissingTokenError.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ extension ParseDiagnosticsGenerator {
136136
missingToken: TokenSyntax,
137137
invalidTokenContainer: UnexpectedNodesSyntax
138138
) -> Bool {
139-
let isTooMany = invalidToken.contentLength > missingToken.contentLength
139+
let isTooMany = invalidToken.trimmedLength > missingToken.trimmedLength
140140
let message: DiagnosticMessage
141141
if missingToken.parent?.is(ExpressionSegmentSyntax.self) == true {
142142
message = .tooManyRawStringDelimitersToStartInterpolation
@@ -157,7 +157,7 @@ extension ParseDiagnosticsGenerator {
157157
)
158158
addDiagnostic(
159159
invalidToken,
160-
position: invalidToken.positionAfterSkippingLeadingTrivia.advanced(by: missingToken.contentLength.utf8Length),
160+
position: invalidToken.positionAfterSkippingLeadingTrivia.advanced(by: missingToken.trimmedLength.utf8Length),
161161
message,
162162
fixIts: [fixIt],
163163
handledNodes: [invalidTokenContainer.id]

Sources/SwiftSyntax/Raw/RawSyntax.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ extension RawSyntax {
508508
/// The length of this node’s content, without the first leading and the last
509509
/// trailing trivia. Intermediate trivia inside a layout node is included in
510510
/// this.
511-
var contentByteLength: Int {
511+
var trimmedByteLength: Int {
512512
let result = byteLength - leadingTriviaByteLength - trailingTriviaByteLength
513513
precondition(result >= 0)
514514
return result
@@ -523,8 +523,8 @@ extension RawSyntax {
523523
}
524524

525525
/// The length of this node excluding its leading and trailing trivia.
526-
var contentLength: SourceLength {
527-
SourceLength(utf8Length: contentByteLength)
526+
var trimmedLength: SourceLength {
527+
SourceLength(utf8Length: trimmedByteLength)
528528
}
529529
}
530530

Sources/SwiftSyntax/Raw/RawSyntaxTokenView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public struct RawSyntaxTokenView {
229229
}
230230

231231
@_spi(RawSyntax)
232-
public var contentLength: SourceLength {
232+
public var trimmedLength: SourceLength {
233233
SourceLength(utf8Length: textByteLength)
234234
}
235235

Sources/SwiftSyntax/SourceLocation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public extension Syntax {
400400
) -> SourceLocation {
401401
var pos = data.position
402402
pos += raw.leadingTriviaLength
403-
pos += raw.contentLength
403+
pos += raw.trimmedLength
404404
if afterTrailingTrivia {
405405
pos += raw.trailingTriviaLength
406406
}

Sources/SwiftSyntax/Syntax.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,11 @@ public extension SyntaxProtocol {
484484

485485
/// The length this node takes up spelled out in the source, excluding its
486486
/// leading or trailing trivia.
487-
var contentLength: SourceLength {
488-
return raw.contentLength
487+
///
488+
/// - Note: If this node consists of multiple tokens, only the first token’s
489+
/// leading and the last token’s trailing trivia will be trimmed.
490+
var trimmedLength: SourceLength {
491+
return raw.trimmedLength
489492
}
490493

491494
/// The byte source range of this node including leading and trailing trivia.
@@ -494,13 +497,21 @@ public extension SyntaxProtocol {
494497
}
495498

496499
/// The byte source range of this node excluding leading and trailing trivia.
497-
var contentByteRange: ByteSourceRange {
500+
///
501+
/// - Note: If this node consists of multiple tokens, only the first token’s
502+
/// leading and the last token’s trailing trivia will be trimmed.
503+
var trimmedByteRange: ByteSourceRange {
498504
return ByteSourceRange(
499505
offset: positionAfterSkippingLeadingTrivia.utf8Offset,
500-
length: contentLength.utf8Length
506+
length: trimmedLength.utf8Length
501507
)
502508
}
503509

510+
@available(*, deprecated, renamed: "trimmedLength")
511+
var contentLength: SourceLength {
512+
return trimmedLength
513+
}
514+
504515
@available(*, deprecated, renamed: "totalByteRange")
505516
var byteRange: ByteSourceRange {
506517
return ByteSourceRange(offset: position.utf8Offset, length: totalLength.utf8Length)
@@ -513,9 +524,9 @@ public extension SyntaxProtocol {
513524
}
514525

515526
/// The textual byte length of this node exluding leading and trailing trivia.
516-
@available(*, deprecated, message: "Use contentLength.utf8Length")
527+
@available(*, deprecated, message: "Use trimmedLength.utf8Length")
517528
var byteSizeAfterTrimmingTrivia: Int {
518-
return contentLength.utf8Length
529+
return trimmedLength.utf8Length
519530
}
520531
}
521532

Sources/SwiftSyntax/TokenSyntax.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
121121

122122
/// The length this node takes up spelled out in the source, excluding its
123123
/// leading or trailing trivia.
124-
public var contentLength: SourceLength {
125-
return tokenView.contentLength
124+
public var trimmedLength: SourceLength {
125+
return tokenView.trimmedLength
126126
}
127127

128128
/// The length this node's leading trivia takes up spelled out in source.

Sources/_SwiftSyntaxTestSupport/IncrementalParseTestUtils.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ public func assertIncrementalParse(
8484
continue
8585
}
8686

87-
guard let reusedNode = reusedNodes.first(where: { $0.contentByteRange == range }) else {
87+
guard let reusedNode = reusedNodes.first(where: { $0.trimmedByteRange == range }) else {
8888
XCTFail(
8989
"""
9090
Fail to match the range of \(expectedReusedNode.source) in:
91-
\(reusedNodes.map({"\($0.contentByteRange): \($0.description)"}).joined(separator: "\n"))
91+
\(reusedNodes.map({"\($0.trimmedByteRange): \($0.description)"}).joined(separator: "\n"))
9292
""",
9393
file: expectedReusedNode.file,
9494
line: expectedReusedNode.line

Tests/SwiftSyntaxTest/AbsolutePositionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class AbsolutePositionTests: XCTestCase {
8383
state.totalLength.utf8Length,
8484
state.leadingTrivia.sourceLength.utf8Length
8585
+ state.trailingTrivia.sourceLength.utf8Length
86-
+ state.contentLength.utf8Length
86+
+ state.trimmedLength.utf8Length
8787
)
8888

8989
// Test Node trivia setters and getters

Tests/SwiftSyntaxTest/SyntaxTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,6 @@ public class SyntaxTests: XCTestCase {
123123
XCTAssertEqual(funcKW.positionAfterSkippingLeadingTrivia, AbsolutePosition(utf8Offset: 2))
124124
XCTAssertEqual(funcKW.endPositionBeforeTrailingTrivia, AbsolutePosition(utf8Offset: 6))
125125
XCTAssertEqual(funcKW.endPosition, AbsolutePosition(utf8Offset: 7))
126-
XCTAssertEqual(funcKW.contentLength, SourceLength(utf8Length: 4))
126+
XCTAssertEqual(funcKW.trimmedLength, SourceLength(utf8Length: 4))
127127
}
128128
}

0 commit comments

Comments
 (0)