Skip to content

Commit c5c1863

Browse files
authored
Merge pull request swiftlang#65 from akyrtzi/fix-length-calc
[RawSyntax] For the `RawSyntax.contentLength` calculation make sure to ignore `missing` tokens
2 parents 4d56be3 + fcdeabe commit c5c1863

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Diff for: Sources/SwiftSyntax/RawSyntax.swift

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ final class RawSyntax {
4141
/// The length of this node excluding its leading and trailing trivia
4242
var contentLength: SourceLength {
4343
return _contentLength.value({
44+
if isMissing {
45+
return Box(SourceLength.zero)
46+
}
4447
switch data {
4548
case .node(kind: _, layout: let layout):
4649
let firstElementIndex = layout.firstIndex(where: { $0 != nil })

Diff for: Tests/LinuxMain.swift

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ XCTMain({ () -> [XCTestCaseEntry] in
55
var testCases: [XCTestCaseEntry] = [
66
testCase(AbsolutePositionTestCase.allTests),
77
testCase(DiagnosticTestCase.allTests),
8+
testCase(IncrementalParsingTestCase.allTests),
89
// We need to make syntax node thread safe to enable these tests
910
// testCase(LazyCachingTestCase.allTests),
1011
testCase(ParseFileTestCase.allTests),

Diff for: Tests/SwiftSyntaxTest/IncrementalParsingTests.swift

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import XCTest
2+
import SwiftSyntax
3+
4+
public class IncrementalParsingTestCase: XCTestCase {
5+
6+
public static let allTests = [
7+
("testIncrementalInvalid", testIncrementalInvalid),
8+
]
9+
10+
public func testIncrementalInvalid() {
11+
let original = "struct A { func f() {"
12+
let step: (String, (Int, Int, String)) =
13+
("struct AA { func f() {", (8, 0, "A"))
14+
15+
var tree = try! SyntaxParser.parse(source: original)
16+
let sourceEdit = SourceEdit(range: ByteSourceRange(offset: step.1.0, length: step.1.1), replacementLength: step.1.2.utf8.count)
17+
let lookup = IncrementalEditTransition(previousTree: tree, edits: [sourceEdit])
18+
tree = try! SyntaxParser.parse(source: step.0, parseLookup: lookup)
19+
XCTAssertEqual("\(tree)", step.0)
20+
}
21+
}

0 commit comments

Comments
 (0)