Skip to content

Commit

Permalink
export tree sitter's id field (#28)
Browse files Browse the repository at this point in the history
* draft to make node hashable

* export id instead of making Node hashable

Making Node hashable is tricky. As the node equality depends on
not only node id, but also node's tree. After parsing, the id can
stay the same but the tree can change, so even if the node is hashable
based on its id, fetching a value from a directory of type [Node:Any]
can lead to surprising missing values.

So the best way so far to id a Node is to export the id field, and let
the user decide how they want to id a node.
  • Loading branch information
lijunsong authored Jun 5, 2024
1 parent bf1030d commit e06be7d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/SwiftTreeSitter/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ extension Node {
return String(cString: str)
}

public var id: UInt {
return UInt(bitPattern: internalNode.id)
}

public var symbol: Int {
return Int(ts_node_symbol(internalNode))
}
Expand Down

0 comments on commit e06be7d

Please sign in to comment.