import Cocoa import Doubt import Prelude func readFile(path: String) -> String? { guard let data = try? NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) else { return nil } return data as String? } typealias Term = Cofree> struct Info: Categorizable, CustomJSONConvertible, Equatable { let range: Range // MARK: Categorizable let categories: Set // MARK: CustomJSONConvertible var JSON: Doubt.JSON { return [ "range": [ range.startIndex.JSON, (range.endIndex - range.startIndex).JSON, ], "categories": Array(categories).JSON ] } } func == (left: Info, right: Info) -> Bool { return left.range == right.range && left.categories == right.categories } func termWithInput(string: String) -> Term? { let document = ts_document_make() defer { ts_document_free(document) } return string.withCString { ts_document_set_language(document, ts_language_javascript()) ts_document_set_input_string(document, $0) ts_document_parse(document) let root = ts_document_root_node(document) struct E: ErrorType {} return try? Cofree .ana { node, category in let count = node.namedChildren.count guard count > 0 else { return Syntax.Leaf(category) } switch category { case "pair": return try .Fixed(node.namedChildren.map { ($0, try $0.category(document)) }) case "object": return try .Keyed(Dictionary(elements: node.namedChildren.map { guard let range = $0.namedChildren.first?.range else { throw E() } guard let name = String(string.utf16[String.UTF16View.Index(_offset: range.startIndex)..(equal: Term.equals(annotation: const(true), leaf: ==), comparable: const(true), cost: Free.sum(Patch.difference)).run(a, b) let range: Range -> Doubt.JSON = { [ $0.startIndex.JSON, ($0.endIndex - $0.startIndex).JSON, ] } let JSON: Doubt.JSON = [ "before": .String(aString), "after": .String(bString), "diff": diff.JSON(pure: { $0.JSON { $0.JSON(annotation: range, leaf: Doubt.JSON.String) } }, leaf: Doubt.JSON.String, annotation: { [ "before": range($0), "after": range($1), ] }), ] let data = JSON.serialize() try data.writeToFile(c, options: .DataWritingAtomic) let components = NSURLComponents() components.scheme = "file" components.path = ui components.query = c if let URL = components.URL { NSWorkspace.sharedWorkspace().openURL(URL) } } }