Skip to content

Commit 5bfa847

Browse files
committed
Clean up position/range API on SyntaxProtocol
The position and range API on `SyntaxProtocol` was a little inconsistent and misleading. Clean it up so that we have `totalLength` (including trivia), `contentLength` (excluding trivia) and a matching pair for `byteRange`.
1 parent 41bf817 commit 5bfa847

File tree

6 files changed

+34
-26
lines changed

6 files changed

+34
-26
lines changed

Sources/SwiftIDEUtils/Syntax+Classifications.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public extension SyntaxProtocol {
2121
/// consecutive tokens would have the same classification then a single classified
2222
/// range is provided for all of them.
2323
var classifications: SyntaxClassifications {
24-
let fullRange = ByteSourceRange(offset: 0, length: byteSize)
24+
let fullRange = ByteSourceRange(offset: 0, length: totalLength.utf8Length)
2525
return SyntaxClassifications(_syntaxNode, in: fullRange)
2626
}
2727

Sources/SwiftParser/IncrementalParseTransition.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension Parser {
2020

2121
let currentOffset = self.lexemes.offsetToStart(self.currentToken)
2222
if let node = parseLookup!.lookUp(currentOffset, kind: kind) {
23-
self.lexemes.advance(by: node.byteSize, currentToken: &self.currentToken)
23+
self.lexemes.advance(by: node.totalLength.utf8Length, currentToken: &self.currentToken)
2424
return node
2525
}
2626

Sources/SwiftSyntax/SourceLocation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public final class SourceLocationConverter {
192192
self.file = file
193193
self.source = tree.syntaxTextBytes
194194
(self.lines, endOfFile) = computeLines(tree: Syntax(tree))
195-
precondition(tree.byteSize == endOfFile.utf8Offset)
195+
precondition(tree.totalLength.utf8Length == endOfFile.utf8Offset)
196196

197197
for directive in SourceLocationCollector.collectSourceLocations(in: tree) {
198198
let location = self.physicalLocation(for: directive.positionAfterSkippingLeadingTrivia)

Sources/SwiftSyntax/Syntax.swift

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,9 @@ public extension SyntaxProtocol {
477477
return data.endPosition
478478
}
479479

480-
/// The textual byte length of this node including leading and trailing trivia.
481-
var byteSize: Int {
482-
return totalLength.utf8Length
483-
}
484-
485-
/// The byte source range of this node including leading and trailing trivia.
486-
var byteRange: ByteSourceRange {
487-
return ByteSourceRange(offset: position.utf8Offset, length: byteSize)
480+
/// The length of this node including all of its trivia.
481+
var totalLength: SourceLength {
482+
return raw.totalLength
488483
}
489484

490485
/// The length this node takes up spelled out in the source, excluding its
@@ -493,12 +488,32 @@ public extension SyntaxProtocol {
493488
return raw.contentLength
494489
}
495490

496-
/// The length of this node including all of its trivia.
497-
var totalLength: SourceLength {
498-
return raw.totalLength
491+
/// The byte source range of this node including leading and trailing trivia.
492+
var totalByteRange: ByteSourceRange {
493+
return ByteSourceRange(offset: position.utf8Offset, length: totalLength.utf8Length)
494+
}
495+
496+
/// The byte source range of this node excluding leading and trailing trivia.
497+
var contentByteRange: ByteSourceRange {
498+
return ByteSourceRange(
499+
offset: positionAfterSkippingLeadingTrivia.utf8Offset,
500+
length: contentLength.utf8Length
501+
)
502+
}
503+
504+
@available(*, deprecated, renamed: "totalByteRange")
505+
var byteRange: ByteSourceRange {
506+
return ByteSourceRange(offset: position.utf8Offset, length: totalLength.utf8Length)
507+
}
508+
509+
/// The textual byte length of this node including leading and trailing trivia.
510+
@available(*, deprecated, message: "Use totalLength.utf8Length")
511+
var byteSize: Int {
512+
return totalLength.utf8Length
499513
}
500514

501515
/// The textual byte length of this node exluding leading and trailing trivia.
516+
@available(*, deprecated, message: "Use contentLength.utf8Length")
502517
var byteSizeAfterTrimmingTrivia: Int {
503518
return contentLength.utf8Length
504519
}

Sources/_SwiftSyntaxTestSupport/IncrementalParseTestUtils.swift

Lines changed: 2 additions & 9 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.byteRangeAfterTrimmingTrivia == range }) else {
87+
guard let reusedNode = reusedNodes.first(where: { $0.contentByteRange == range }) else {
8888
XCTFail(
8989
"""
9090
Fail to match the range of \(expectedReusedNode.source) in:
91-
\(reusedNodes.map({"\($0.byteRangeAfterTrimmingTrivia): \($0.description)"}).joined(separator: "\n"))
91+
\(reusedNodes.map({"\($0.contentByteRange): \($0.description)"}).joined(separator: "\n"))
9292
""",
9393
file: expectedReusedNode.file,
9494
line: expectedReusedNode.line
@@ -220,10 +220,3 @@ public func applyEdits(
220220
}
221221
return String(bytes: bytes, encoding: .utf8)!
222222
}
223-
224-
fileprivate extension Syntax {
225-
/// The byte source range of this node exluding leading and trailing trivia.
226-
var byteRangeAfterTrimmingTrivia: ByteSourceRange {
227-
return ByteSourceRange(offset: positionAfterSkippingLeadingTrivia.utf8Offset, length: byteSizeAfterTrimmingTrivia)
228-
}
229-
}

Tests/SwiftSyntaxTest/AbsolutePositionTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class AbsolutePositionTests: XCTestCase {
3434
endOfFileToken: .endOfFileToken()
3535
)
3636
_ = root.statements[idx].position
37-
_ = root.statements[idx].byteSize
37+
_ = root.statements[idx].totalLength.utf8Length
3838
_ = root.statements[idx].positionAfterSkippingLeadingTrivia
3939
}
4040

@@ -80,10 +80,10 @@ public class AbsolutePositionTests: XCTestCase {
8080
XCTAssertEqual(3, state.leadingTrivia.count)
8181
XCTAssertEqual(2, state.trailingTrivia.count)
8282
XCTAssertEqual(
83-
state.byteSize,
83+
state.totalLength.utf8Length,
8484
state.leadingTrivia.sourceLength.utf8Length
8585
+ state.trailingTrivia.sourceLength.utf8Length
86-
+ state.byteSizeAfterTrimmingTrivia
86+
+ state.contentLength.utf8Length
8787
)
8888

8989
// Test Node trivia setters and getters

0 commit comments

Comments
 (0)