Skip to content

Commit 7b3b567

Browse files
committed
Add isSelfCompleted to RawSyntax
This should prevent completed node from not being reused due to the lookahead range
1 parent 45dcd15 commit 7b3b567

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Sources/SwiftParser/IncrementalParseTransition.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,20 @@ public struct IncrementalParseLookup {
190190
nextLeafNodeLength = nextToken.leadingTriviaLength + nextToken.contentLength
191191
}
192192
}
193+
194+
var underlyingRaw = node.raw
195+
if let codeBlockItem = underlyingRaw.as(RawCodeBlockItemSyntax.self) {
196+
underlyingRaw = codeBlockItem.item
197+
}
198+
199+
let nodeAffectLength =
200+
underlyingRaw.isSelfCompleted
201+
? (node.totalLength + nextLeafNodeLength).utf8Length
202+
: max(mergeLookaheadRange(at: node.position.utf8Offset, length: node.totalLength.utf8Length), (node.totalLength + nextLeafNodeLength).utf8Length)
203+
193204
let nodeAffectRange = ByteSourceRange(
194205
offset: node.position.utf8Offset,
195-
length: max(mergeLookaheadRange(at: node.position.utf8Offset, length: node.totalLength.utf8Length), (node.totalLength + nextLeafNodeLength).utf8Length)
206+
length: nodeAffectLength
196207
)
197208

198209
for edit in edits.edits {

Sources/SwiftSyntax/Raw/RawSyntax.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,24 @@ extension RawSyntax {
917917
}
918918
}
919919

920+
extension RawSyntax {
921+
public var isSelfCompleted: Bool {
922+
if let children = self.layoutView?.children,
923+
let lastNonNilChildIdx = children.lastIndex(where: {
924+
$0 != nil
925+
}),
926+
lastNonNilChildIdx != children.endIndex
927+
{
928+
929+
let nextPotentialChildIndex = children.index(after: lastNonNilChildIdx)
930+
return nextPotentialChildIndex == children.index(before: children.endIndex)
931+
|| children.index(after: nextPotentialChildIndex) == children.index(before: children.endIndex)
932+
}
933+
934+
return true
935+
}
936+
}
937+
920938
#if DEBUG
921939
/// See `SyntaxMemoryLayout`.
922940
var RawSyntaxDataMemoryLayouts: [String: SyntaxMemoryLayout.Value] = [

Tests/SwiftParserTest/IncrementalParsingTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,18 @@ public class IncrementalParsingTests: XCTestCase {
6262
]
6363
)
6464
}
65+
66+
public func testDeclFollowedByLabeledStmt() {
67+
assertIncrementalParse(
68+
"""
69+
class foo {}
70+
trailingClosure: ⏩️switch x {
71+
default: break
72+
}⏸️{}⏪️
73+
""",
74+
reusedNodes: [
75+
ReusedNodeSpec("class foo {}", kind: .codeBlockItem)
76+
]
77+
)
78+
}
6579
}

0 commit comments

Comments
 (0)