2015-10-28 22:22:21 +03:00
|
|
|
|
// Copyright © 2015 GitHub. All rights reserved.
|
|
|
|
|
|
2015-10-28 23:55:28 +03:00
|
|
|
|
typealias TSDocument = COpaquePointer
|
|
|
|
|
|
2015-10-28 22:22:21 +03:00
|
|
|
|
extension TSNode {
|
2015-10-29 00:48:51 +03:00
|
|
|
|
func category(document: TSDocument) throws -> String {
|
2015-10-29 01:17:16 +03:00
|
|
|
|
guard let category = String.fromCString(ts_node_name(self, document)) else { throw "couldn’t make a String from the node name" }
|
2015-10-29 00:23:06 +03:00
|
|
|
|
return category
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 00:36:23 +03:00
|
|
|
|
var range: Range<Int> {
|
|
|
|
|
let start = ts_node_pos(self).chars
|
|
|
|
|
return start..<(start + ts_node_size(self).chars)
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 17:03:11 +03:00
|
|
|
|
func substring(string: String) throws -> String {
|
|
|
|
|
guard let result = String(string.utf16[range]) else { throw "could not make a string from utf16 range '\(range)'" }
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 01:23:58 +03:00
|
|
|
|
var children: ChildrenCollection {
|
|
|
|
|
return ChildrenCollection(node: self, count: ts_node_child_count(self), child: ts_node_child)
|
2015-10-28 23:28:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 01:23:58 +03:00
|
|
|
|
var namedChildren: ChildrenCollection {
|
|
|
|
|
return ChildrenCollection(node: self, count: ts_node_named_child_count(self), child: ts_node_named_child)
|
2015-10-28 23:28:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-10-29 01:23:58 +03:00
|
|
|
|
struct ChildrenCollection: CollectionType {
|
2015-10-28 22:22:21 +03:00
|
|
|
|
let node: TSNode
|
2015-10-29 01:22:53 +03:00
|
|
|
|
let count: Int
|
2015-10-28 22:22:21 +03:00
|
|
|
|
let child: (TSNode, Int) -> TSNode
|
|
|
|
|
|
|
|
|
|
subscript (index: Int) -> TSNode {
|
|
|
|
|
return child(node, index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let startIndex = 0
|
|
|
|
|
var endIndex: Int {
|
2015-10-29 01:22:53 +03:00
|
|
|
|
return count
|
2015-10-28 22:22:21 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|