Getting SwiftSyntax subtree using source range

It take the range the give the syntax node at given range

I have tested on this swift-file

public func format<Output: TextOutputStream>(syntax: Syntax, assumingFileURL url: URL, to outputStream: inout Output
    ) throws

//let transformedSyntax = pipeline.visit(syntax) it does not seem to work with transformedSyntax

let types = [ImportDeclSyntax.self, StructDeclSyntax.self, ClassDeclSyntax.self, UnknownDeclSyntax.self, FunctionDeclSyntax.self, VariableDeclSyntax.self, IfStmtSyntax.self, SwitchStmtSyntax.self, ForInStmtSyntax.self, WhileStmtSyntax.self, RepeatWhileStmtSyntax.self, DoStmtSyntax.self, CatchClauseSyntax.self, FunctionCallExprSyntax.self] as [Any.Type]`
let range = NSRange(location: 2222, length: 34)

visit(node: syntax) { (syntax) in
    
    if types.contains(where: { $0 == type(of: syntax)}) {
        
        if let syntax = getSyntax(syntax, by: range) {
            prettyPrint(context: context, node: syntax, debugOptions: debugOptions)
        }
    } else if type(of: syntax) != CodeBlockItemSyntax.self {
        
        if let syntax = getSyntax(syntax, by: range) {
            prettyPrint(context: context, node: syntax, debugOptions: debugOptions)
        }
    }

func visit(node: Syntax, printNode: (Syntax) -> Void) {
    
    printNode(node)
    
    for child in node.children {
        visit(node: child, printNode: printNode)
    }
}

func getSyntax(_ syntax: Syntax, by range: NSRange) -> Syntax? {
    
    if syntax.contentLength.utf8Length == range.length && syntax.endPosition.utf8Offset == range.length + range.location {
        return syntax
    }
    
    return nil
}

if the range is {2222, 34} it will give the let url = URL(string: "some url")!
range is {934, 23} it will give the case apiError(ApiError)
range is {726, 54} it will give the
struct ApiError: Decodable { let message: String }

but it does not work with transformedSyntax

let transformedSyntax = pipeline.visit(syntax)

Another problem that i am confused with

Suppose the range is {782, 22} it should return this node
class NetworkService {
but it is not returning this node because the syntax tree does not have it

if we have this struct

struct Sample { let message: String }

The nodes will be

  1. struct
  2. Sample
  3. { let message: String }
  4. {
  5. let message: String
  6. let
  7. message: String
  8. message
  9. : String
  10. :
  11. String
  12. }

How can i solve this problem ?

I've moved the post to a separate topic. @harlanhaskins could you provide some guidance ?