1
1
mirror of https://github.com/github/semantic.git synced 2024-12-29 18:06:14 +03:00
semantic/prototype/doubt-difftool/TSNode.swift

46 lines
1.2 KiB
Swift
Raw Normal View History

// Copyright © 2015 GitHub. All rights reserved.
2015-10-28 23:55:28 +03:00
typealias TSDocument = COpaquePointer
2015-10-29 21:58:07 +03:00
typealias TSLanguage = COpaquePointer
2015-10-28 23:55:28 +03:00
extension TSNode {
2015-10-29 00:48:51 +03:00
func category(document: TSDocument) throws -> String {
guard let category = String.fromCString(ts_node_name(self, document)) else { throw "couldnt 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)
}
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
}
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
}
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
}
struct ChildrenCollection: CollectionType {
let node: TSNode
2015-10-29 01:22:53 +03:00
let count: Int
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
}
}
}