{"id":18008040,"url":"https://github.com/luizzak/swiftsyntaxsearch","last_synced_at":"2025-03-26T12:32:01.274Z","repository":{"id":60553156,"uuid":"543791104","full_name":"LuizZak/SwiftSyntaxSearch","owner":"LuizZak","description":"A small experimental library containing generic types for performing search and replacement on Swift Syntax trees.","archived":false,"fork":false,"pushed_at":"2023-03-21T21:08:54.000Z","size":37,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-21T19:04:15.605Z","etag":null,"topics":["swift","swift-syntax","swiftsyntax"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LuizZak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-30T21:09:19.000Z","updated_at":"2025-01-08T04:55:51.000Z","dependencies_parsed_at":"2024-10-30T01:43:16.255Z","dependency_job_id":null,"html_url":"https://github.com/LuizZak/SwiftSyntaxSearch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LuizZak%2FSwiftSyntaxSearch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LuizZak%2FSwiftSyntaxSearch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LuizZak%2FSwiftSyntaxSearch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LuizZak%2FSwiftSyntaxSearch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LuizZak","download_url":"https://codeload.github.com/LuizZak/SwiftSyntaxSearch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245654288,"owners_count":20650838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["swift","swift-syntax","swiftsyntax"],"created_at":"2024-10-30T01:17:09.697Z","updated_at":"2025-03-26T12:32:00.965Z","avatar_url":"https://github.com/LuizZak.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftSyntaxSearch\n\nA small experimental library containing generic types for performing search and replacement on Swift Syntax trees.\n\n## Requirements\n\nSwift 5.7\n\n## Searching\n\nFrom a given syntax tree:\n\n```swift\nclass AClass {\n    init() {\n        var decl: Int = 0\n    }\n    func member1() {\n        var decl2: Int = 0\n        var decl: Int = 1\n    }\n    func member2() {\n        var decl: Int = 0, decl2: Int = 0\n    }\n}\n```\n\nWe can query for all variable declarations whose first pattern binding index binds an identifier `decl` to an initial value of `0` with the following search term:\n\n```swift\nlet declOf0Search = SyntaxSearchTerm\u003cVariableDeclSyntax\u003e\n    .child(\n        \\VariableDeclSyntax.bindings[index: 0],\n        matches:\n            (\\PatternBindingSyntax.pattern).matches(\n                as: IdentifierPatternSyntax.self,\n                SyntaxReplacer\u003cIdentifierPatternSyntax\u003e\n                    .token(\\.identifier, matches: \"decl\")\n            ) \u0026\u0026\n            (\\PatternBindingSyntax.initializer?.value).matches(\n                as: IntegerLiteralExprSyntax.self,\n                SyntaxReplacer\u003cIntegerLiteralExprSyntax\u003e\n                    .token(\\.digits, matches: \"0\")\n            )\n    )\n```\n\nAnd search for the syntax tree like so:\n\n```swift\nsyntax.findAllDepthFirst(declOf0Search)\n// Returns syntax nodes:\n// var decl: Int = 0\n// var decl: Int = 0, decl2: Int = 0\n```\n\n## Find and Replace\n\nFrom a given syntax tree:\n\n```swift\nclass AClass {\n    init() {\n        var decl: Int = 0\n    }\n    func member(_ param: Int = 1) {\n        var decl: Int = 2\n    }\n}\n\nlet global = 3\n```\n\nWe can find and replace all variable declarations that bind an integer of value `0` or `1`, and invoke a closure to construct a replacement to the syntax node:\n\n```swift\nlet declOf0Or2Replacer =\n    SyntaxReplacer\u003cIntegerLiteralExprSyntax\u003e(searchTerm:\n        .or([\n            SyntaxReplacer\u003cIntegerLiteralExprSyntax\u003e\n                .token(\\.digits, matches: \"0\"),\n            SyntaxReplacer\u003cIntegerLiteralExprSyntax\u003e\n                .token(\\.digits, matches: \"2\"),\n        ])\n    ) { node in\n        node.withDigits(\n            SyntaxFactory.makeIntegerLiteral(\"50_\" + node.digits.text)\n        )\n    }\n```\n\nAnd create a new the syntax tree with the replacements applied like so:\n\n```swift\nsyntax.replacingAll(declOf0Or2Replacer)\n// Prints the new syntax tree:\n// class AClass {\n//     init() {\n//         var decl: Int = 50_0\n//     }\n//     func member(_ param: Int = 1) {\n//         var decl: Int = 50_2\n//     }\n// }\n// \n// let global = 3\n```\n\n### Creating search terms\n\nThe following syntaxes are available and produce the same result:\n\n```swift\n// Keypath-based binding\n(\\PatternBindingSyntax.pattern).matches(\n    as: IdentifierPatternSyntax.self,\n    SyntaxSearchTerm\u003cIdentifierPatternSyntax\u003e\n        .token(\\.identifier, matches: \"decl\")\n)\n\n// Struct creation\nSyntaxSearchTerm\u003cPatternBindingSyntax\u003e\n    .child(\n        // KeyPath\u003cPatternBindingSyntax, T\u003e\n        \\.pattern,\n\n        // Cast `T` to IdentifierPatternSyntax, and if successful, invokes the matcher, otherwise matching fails.\n        castTo: IdentifierPatternSyntax.self,\n        \n        // Match IdentifierPatternSyntax.identifier (a TokenSyntax) with a given StringMatcher (string literals match with `==`)\n        matches:\n            SyntaxSearchTerm\u003cIdentifierPatternSyntax\u003e\n                .token(\\.identifier, matches: \"decl\")\n    )\n\n// Appending to existing search term\nlet emptySearch = SyntaxSearchTerm\u003cPatternBindingSyntax\u003e()\nlet declIdentSearch = emptySearch\n    .child(\n        // KeyPath\u003cPatternBindingSyntax, T\u003e\n        \\.pattern,\n\n        // Cast `T` to IdentifierPatternSyntax, and if successful, invokes the matcher, otherwise matching fails.\n        castTo: IdentifierPatternSyntax.self,\n\n        // Match IdentifierPatternSyntax.identifier (a TokenSyntax) with a given StringMatcher (string literals match with `==`)\n        matches:\n            SyntaxSearchTerm\u003cIdentifierPatternSyntax\u003e\n                .token(\\.identifier, matches: \"decl\")\n    )\n```\n\nSearch terms that inspect tokens can use the shortcut `KeyPath\u003c_, TokenSyntax\u003e.==` to generate token string matches like with `SyntaxSearchTerm.token(\\.identifier, matches: \"decl\")`:\n\n```swift\nlet declIdentSearch: SyntaxSearchTerm\u003cIdentifierPatternSyntax\u003e\ndeclIdentSearch = \\.identifier == \"decl\" // equivalent to declIdentSearch = .token(\\.identifier, matches: \"decl\")\n```\n\n#### StringMatcher\n\nA simple enum-based string matcher that performs matches based on equality, prefix, suffix or string containment. Used by `SyntaxSearchTerm` to perform token-based string equality:\n\n```swift\n// StringMatcher.exact\nlet exact = StringMatcher.exact(\"a text\")\n\nprint(exact.matches(\"a text\")) // true\nprint(exact.matches(\"\")) // false\nprint(exact.matches(\"A Text\")) // false\nprint(exact.matches(\"a string containing a text with prefix and suffix\")) // false\nprint(exact.matches(\"a text with suffix\")) // false\nprint(exact.matches(\"prefix and then a text\")) // false\n\n// StringMatcher.contains\nlet contains = StringMatcher.contains(\"a text\")\n\nprint(contains.matches(\"a text\")) // true\nprint(contains.matches(\"\")) // false\nprint(contains.matches(\"A Text\")) // false\nprint(contains.matches(\"a string containing a text with prefix and suffix\")) // true\nprint(contains.matches(\"a text with suffix\")) // true\nprint(contains.matches(\"prefix and then a text\")) // true\n\n// StringMatcher.prefix\nlet prefix = StringMatcher.prefix(\"a text\")\n\nprint(prefix.matches(\"a text\")) // true\nprint(prefix.matches(\"\")) // false\nprint(prefix.matches(\"A Text\")) // false\nprint(prefix.matches(\"a string containing a text with prefix and suffix\")) // false\nprint(prefix.matches(\"a text with suffix\")) // true\nprint(prefix.matches(\"prefix and then a text\")) // false\n\n// StringMatcher.suffix\nlet suffix = StringMatcher.suffix(\"a text\")\n\nprint(suffix.matches(\"a text\")) // true\nprint(suffix.matches(\"\")) // false\nprint(suffix.matches(\"A Text\")) // false\nprint(suffix.matches(\"a string containing a text with prefix and suffix\")) // false\nprint(suffix.matches(\"a text with suffix\")) // false\nprint(suffix.matches(\"prefix and then a text\")) // true\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluizzak%2Fswiftsyntaxsearch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluizzak%2Fswiftsyntaxsearch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluizzak%2Fswiftsyntaxsearch/lists"}