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-28 23:28:37 +03:00
|
|
|
|
var children: AnyRandomAccessCollection<TSNode> {
|
|
|
|
|
return AnyRandomAccessCollection(ChildrenCollection(node: self, count: ts_node_child_count, child: ts_node_child))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var namedChildren: AnyRandomAccessCollection<TSNode> {
|
|
|
|
|
return AnyRandomAccessCollection(ChildrenCollection(node: self, count: ts_node_named_child_count, child: ts_node_named_child))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-10-28 22:22:21 +03:00
|
|
|
|
private struct ChildrenCollection: CollectionType {
|
|
|
|
|
let node: TSNode
|
|
|
|
|
let count: TSNode -> Int
|
|
|
|
|
let child: (TSNode, Int) -> TSNode
|
|
|
|
|
|
|
|
|
|
subscript (index: Int) -> TSNode {
|
|
|
|
|
return child(node, index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let startIndex = 0
|
|
|
|
|
var endIndex: Int {
|
|
|
|
|
return count(node)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|