Skip to content

Commit da4ff2d

Browse files
committed
Add isSelfCompleted to RawSyntax
This should prevent completed node from not being reused due to the lookahead range
1 parent 0055ebe commit da4ff2d

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Sources/SwiftSyntax/IncrementalParseTransition.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,20 @@ public struct IncrementalParseLookup {
323323
nextLeafNodeLength = nextToken.raw.totalLength - nextToken.trailingTriviaLength
324324
}
325325
}
326+
327+
var underlyingRaw = node.raw
328+
if let codeBlockItem = underlyingRaw.as(RawCodeBlockItemSyntax.self) {
329+
underlyingRaw = codeBlockItem.item
330+
}
331+
332+
let nodeAffectLength =
333+
underlyingRaw.isSelfCompleted
334+
? (node.raw.totalLength + nextLeafNodeLength).utf8Length
335+
: max(mergeLookaheadRange(at: node.position.utf8Offset, length: node.raw.totalLength.utf8Length), (node.raw.totalLength + nextLeafNodeLength).utf8Length)
336+
326337
let nodeAffectRange = ByteSourceRange(
327338
offset: node.position.utf8Offset,
328-
length: max(mergeLookaheadRange(at: node.position.utf8Offset, length: node.raw.totalLength.utf8Length), (node.raw.totalLength + nextLeafNodeLength).utf8Length)
339+
length: nodeAffectLength
329340
)
330341

331342
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)