Closed mathrick closed 3 months ago
This is apparently an issue for all traversal utils, for example:
;; This will also always return nil
(defun node-field (node)
(block nil
(tsc-traverse-do ([field] node)
(return field))))
I think it's the same issue as https://github.com/tree-sitter/tree-sitter/issues/567. Basically the cursor can't walk out of the starting node, so it can't know which field it's on within the parent node. We definitely should document this better.
Hmm, is it a shortcoming in the underlying treesitter API, or is there some functionality that's present in the base API but not exposed by tree-sitter.el
? Because as things are right now, there's no straightforward way to get a node's field without gymnastics involving cursors and weird edge cases (see also #274). Something like tsc-node-field
sounds like it should exist.
Also, I don't know whether that is a thing that even makes sense to want, but this limitation means we can never have a field on the root node of a tree.
I did some more reading, and it turns out I was confused about the meaning of fields. I was under the impression that fields were the properties of nodes, but in reality, they're properties of the relationship between a parent and its children. So the API really doesn't have a concept of "a node's field", only "given child's field".
Yes, I found this confusing as well, but IIUC, this is close to my impression of how things work (my impression is vague (^^; ):
in reality, they're properties of the relationship between a parent and its children. So the API really doesn't have a concept of "a node's field", only "given child's field".
Yeah, field
is basically the nickname by which the parent calls the child at home.
Semi-formally, it's the (name of the) edge between the parent node and the child node.
It seems that
tsc-current-field
always returnsnil
when run against the cursor's starting node, ie.This is not the case when navigating to other nodes via the cursor. Here's a repro that demonstrates it:
This results in the following output in
*Messages*
:Notice that the output is identical, except for the fields.
Version information: