Skip to content

Instantly share code, notes, and snippets.

@iby
Created June 17, 2019 14:27
Show Gist options
  • Save iby/913fce8729d4f792158de016b66e02f9 to your computer and use it in GitHub Desktop.
Save iby/913fce8729d4f792158de016b66e02f9 to your computer and use it in GitHub Desktop.
Slang Example
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