Created
June 17, 2019 14:27
-
-
Save iby/913fce8729d4f792158de016b66e02f9 to your computer and use it in GitHub Desktop.
Slang Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Slang | |
let source: String = "import Foundation; class Foo { let bar = 1 }" | |
let file: File = File(source) | |
let disassembly: Disassembly = try! Disassembly(file) | |
var edits: [Edit] = [] | |
// Change "Foundation" identifier to "AppKit". | |
edits.append(Edit(disassembly.query.syntax.first(of: .identifier).select(where: { $0.contents == "Foundation" }).one!, "AppKit")) | |
// Change "class" keyword to "struct". | |
edits.append(Edit(disassembly.query.structure.children(of: .decl(.class)).syntax.first(of: .keyword).one!, "struct")) | |
// Change "bar" property value from "1" to "BAR". | |
edits.append(Edit(disassembly.query.structure.children(of: .decl(.class)).syntax.last(of: .number).one!, "\"BAR\"")) | |
print(file.contents.applying(edits)) | |
// import AppKit; struct Foo { let bar = "BAR" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment