1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 18:23:44 +03:00
semantic/prototype/doubt-difftool/TSNode.swift
2015-10-29 14:58:07 -04:00

46 lines
1.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright © 2015 GitHub. All rights reserved.
typealias TSDocument = COpaquePointer
typealias TSLanguage = COpaquePointer
extension TSNode {
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" }
return category
}
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)
}
var namedChildren: ChildrenCollection {
return ChildrenCollection(node: self, count: ts_node_named_child_count(self), child: ts_node_named_child)
}
struct ChildrenCollection: CollectionType {
let node: TSNode
let count: Int
let child: (TSNode, Int) -> TSNode
subscript (index: Int) -> TSNode {
return child(node, index)
}
let startIndex = 0
var endIndex: Int {
return count
}
}
}