Skip to content

Commit fa33e7a

Browse files
committed
[SourceLocationConverter] Visit RawSyntax directly
1 parent be12338 commit fa33e7a

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

Sources/SwiftSyntax/SourceLocation.swift

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -512,29 +512,14 @@ extension SyntaxProtocol {
512512
fileprivate func computeLines(
513513
tree: Syntax
514514
) -> ([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
528517

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)
533521
}
534-
535-
let visitor = ComputeLineVisitor()
536-
visitor.walk(tree)
537-
return (visitor.lines, visitor.position + visitor.curPrefix)
522+
return (lines, position + lastLineLength)
538523
}
539524

540525
fileprivate func computeLines(_ source: SyntaxText) -> ([AbsolutePosition], AbsolutePosition) {
@@ -661,7 +646,7 @@ fileprivate extension RawTriviaPieceBuffer {
661646
}
662647
}
663648

664-
fileprivate extension TokenSyntax {
649+
fileprivate extension RawSyntax {
665650
/// Walks and passes to `body` the ``SourceLength`` for every detected line,
666651
/// with the newline character included.
667652
/// - Returns: The leftover ``SourceLength`` at the end of the walk.
@@ -670,15 +655,17 @@ fileprivate extension TokenSyntax {
670655
body: (SourceLength) -> ()
671656
) -> SourceLength {
672657
var curPrefix = prefix
673-
switch self.raw.rawData.payload {
658+
switch self.rawData.payload {
674659
case .parsedToken(let dat):
675660
curPrefix = dat.wholeText.forEachLineLength(prefix: curPrefix, body: body)
676661
case .materializedToken(let dat):
677662
curPrefix = dat.leadingTrivia.forEachLineLength(prefix: curPrefix, body: body)
678663
curPrefix = dat.tokenText.forEachLineLength(prefix: curPrefix, body: body)
679664
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+
}
682669
}
683670
return curPrefix
684671
}

0 commit comments

Comments
 (0)