@@ -512,29 +512,14 @@ extension SyntaxProtocol {
512
512
fileprivate func computeLines(
513
513
tree: Syntax
514
514
) -> ( [ AbsolutePosition ] , AbsolutePosition ) {
515
- class ComputeLineVisitor : SyntaxVisitor {
516
- var lines : [ AbsolutePosition ] = [ . startOfFile]
517
- var position : AbsolutePosition = . startOfFile
518
- var curPrefix : SourceLength = . zero
519
-
520
- init ( ) {
521
- super. init ( viewMode: . sourceAccurate)
522
- }
523
-
524
- private func addLine( _ lineLength: SourceLength ) {
525
- position += lineLength
526
- lines. append ( position)
527
- }
515
+ var lines : [ AbsolutePosition ] = [ . startOfFile]
516
+ var position : AbsolutePosition = . startOfFile
528
517
529
- override func visit( _ token: TokenSyntax ) -> SyntaxVisitorContinueKind {
530
- curPrefix = token. forEachLineLength ( prefix: curPrefix, body: addLine)
531
- return . skipChildren
532
- }
518
+ let lastLineLength = tree. raw. forEachLineLength { lineLength in
519
+ position += lineLength
520
+ lines. append ( position)
533
521
}
534
-
535
- let visitor = ComputeLineVisitor ( )
536
- visitor. walk ( tree)
537
- return ( visitor. lines, visitor. position + visitor. curPrefix)
522
+ return ( lines, position + lastLineLength)
538
523
}
539
524
540
525
fileprivate func computeLines( _ source: SyntaxText ) -> ( [ AbsolutePosition ] , AbsolutePosition ) {
@@ -661,7 +646,7 @@ fileprivate extension RawTriviaPieceBuffer {
661
646
}
662
647
}
663
648
664
- fileprivate extension TokenSyntax {
649
+ fileprivate extension RawSyntax {
665
650
/// Walks and passes to `body` the ``SourceLength`` for every detected line,
666
651
/// with the newline character included.
667
652
/// - Returns: The leftover ``SourceLength`` at the end of the walk.
@@ -670,15 +655,17 @@ fileprivate extension TokenSyntax {
670
655
body: ( SourceLength ) -> ( )
671
656
) -> SourceLength {
672
657
var curPrefix = prefix
673
- switch self . raw . rawData. payload {
658
+ switch self . rawData. payload {
674
659
case . parsedToken( let dat) :
675
660
curPrefix = dat. wholeText. forEachLineLength ( prefix: curPrefix, body: body)
676
661
case . materializedToken( let dat) :
677
662
curPrefix = dat. leadingTrivia. forEachLineLength ( prefix: curPrefix, body: body)
678
663
curPrefix = dat. tokenText. forEachLineLength ( prefix: curPrefix, body: body)
679
664
curPrefix = dat. trailingTrivia. forEachLineLength ( prefix: curPrefix, body: body)
680
- case . layout( _) :
681
- preconditionFailure ( " forEachLineLength is called non-token raw syntax " )
665
+ case . layout( let dat) :
666
+ for case let node? in dat. layout where SyntaxTreeViewMode . sourceAccurate. shouldTraverse ( node: node) {
667
+ curPrefix = node. forEachLineLength ( prefix: curPrefix, body: body)
668
+ }
682
669
}
683
670
return curPrefix
684
671
}
0 commit comments