diff options
| author | Yuan Fu | 2023-01-12 17:07:21 -0800 |
|---|---|---|
| committer | Yuan Fu | 2023-01-12 17:11:38 -0800 |
| commit | 956889d8ff1c79db45ca9b1711f406961e71c272 (patch) | |
| tree | 4d44cc819db4ff7433ac2a02bf61f693182de00f /src | |
| parent | 8f446c2d39736d752829e37100eede3f484b827e (diff) | |
| download | emacs-956889d8ff1c79db45ca9b1711f406961e71c272.tar.gz emacs-956889d8ff1c79db45ca9b1711f406961e71c272.zip | |
Equal now recognizes tree-sitter nodes (bug#60659)
Now equal uses ts_node_eq to check equality between nodes.
* doc/lispref/parsing.texi:
(Accessing Node Information): Update manual.
* src/fns.c (internal_equal): Handle tree-sitter nodes.
* src/treesit.c (treesit_node_eq): New function.
(Ftreesit_node_eq): Factor out. Update docstring.
* src/treesit.h (treesit_node_eq): Declare new function.
Diffstat (limited to 'src')
| -rw-r--r-- | src/fns.c | 8 | ||||
| -rw-r--r-- | src/treesit.c | 20 | ||||
| -rw-r--r-- | src/treesit.h | 1 |
3 files changed, 22 insertions, 7 deletions
| @@ -38,6 +38,10 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */ | |||
| 38 | #include "puresize.h" | 38 | #include "puresize.h" |
| 39 | #include "gnutls.h" | 39 | #include "gnutls.h" |
| 40 | 40 | ||
| 41 | #ifdef HAVE_TREE_SITTER | ||
| 42 | #include "treesit.h" | ||
| 43 | #endif | ||
| 44 | |||
| 41 | enum equal_kind { EQUAL_NO_QUIT, EQUAL_PLAIN, EQUAL_INCLUDING_PROPERTIES }; | 45 | enum equal_kind { EQUAL_NO_QUIT, EQUAL_PLAIN, EQUAL_INCLUDING_PROPERTIES }; |
| 42 | static bool internal_equal (Lisp_Object, Lisp_Object, | 46 | static bool internal_equal (Lisp_Object, Lisp_Object, |
| 43 | enum equal_kind, int, Lisp_Object); | 47 | enum equal_kind, int, Lisp_Object); |
| @@ -2822,6 +2826,10 @@ internal_equal (Lisp_Object o1, Lisp_Object o2, enum equal_kind equal_kind, | |||
| 2822 | && !memcmp (bool_vector_data (o1), bool_vector_data (o2), | 2826 | && !memcmp (bool_vector_data (o1), bool_vector_data (o2), |
| 2823 | bool_vector_bytes (size))); | 2827 | bool_vector_bytes (size))); |
| 2824 | } | 2828 | } |
| 2829 | if (TS_NODEP (o1)) | ||
| 2830 | { | ||
| 2831 | return treesit_node_eq (o1, o2); | ||
| 2832 | } | ||
| 2825 | 2833 | ||
| 2826 | /* Aside from them, only true vectors, char-tables, compiled | 2834 | /* Aside from them, only true vectors, char-tables, compiled |
| 2827 | functions, and fonts (font-spec, font-entity, font-object) | 2835 | functions, and fonts (font-spec, font-entity, font-object) |
diff --git a/src/treesit.c b/src/treesit.c index 55463122d14..d2db91604ab 100644 --- a/src/treesit.c +++ b/src/treesit.c | |||
| @@ -2154,11 +2154,22 @@ If NODE is nil, return nil. */) | |||
| 2154 | return make_treesit_node (XTS_NODE (node)->parser, child); | 2154 | return make_treesit_node (XTS_NODE (node)->parser, child); |
| 2155 | } | 2155 | } |
| 2156 | 2156 | ||
| 2157 | /* Return true if NODE1 and NODE2 are the same node. Assumes they are | ||
| 2158 | TS_NODE type. */ | ||
| 2159 | bool treesit_node_eq (Lisp_Object node1, Lisp_Object node2) | ||
| 2160 | { | ||
| 2161 | treesit_initialize (); | ||
| 2162 | TSNode treesit_node_1 = XTS_NODE (node1)->node; | ||
| 2163 | TSNode treesit_node_2 = XTS_NODE (node2)->node; | ||
| 2164 | return ts_node_eq (treesit_node_1, treesit_node_2); | ||
| 2165 | } | ||
| 2166 | |||
| 2157 | DEFUN ("treesit-node-eq", | 2167 | DEFUN ("treesit-node-eq", |
| 2158 | Ftreesit_node_eq, | 2168 | Ftreesit_node_eq, |
| 2159 | Streesit_node_eq, 2, 2, 0, | 2169 | Streesit_node_eq, 2, 2, 0, |
| 2160 | doc: /* Return non-nil if NODE1 and NODE2 are the same node. | 2170 | doc: /* Return non-nil if NODE1 and NODE2 are the same node. |
| 2161 | If any one of NODE1 and NODE2 is nil, return nil. */) | 2171 | If any one of NODE1 and NODE2 is nil, return nil. |
| 2172 | This function uses the same equivalence metric as `equal'. */) | ||
| 2162 | (Lisp_Object node1, Lisp_Object node2) | 2173 | (Lisp_Object node1, Lisp_Object node2) |
| 2163 | { | 2174 | { |
| 2164 | if (NILP (node1) || NILP (node2)) | 2175 | if (NILP (node1) || NILP (node2)) |
| @@ -2166,12 +2177,7 @@ If any one of NODE1 and NODE2 is nil, return nil. */) | |||
| 2166 | CHECK_TS_NODE (node1); | 2177 | CHECK_TS_NODE (node1); |
| 2167 | CHECK_TS_NODE (node2); | 2178 | CHECK_TS_NODE (node2); |
| 2168 | 2179 | ||
| 2169 | treesit_initialize (); | 2180 | bool same_node = treesit_node_eq (node1, node2); |
| 2170 | |||
| 2171 | TSNode treesit_node_1 = XTS_NODE (node1)->node; | ||
| 2172 | TSNode treesit_node_2 = XTS_NODE (node2)->node; | ||
| 2173 | |||
| 2174 | bool same_node = ts_node_eq (treesit_node_1, treesit_node_2); | ||
| 2175 | return same_node ? Qt : Qnil; | 2181 | return same_node ? Qt : Qnil; |
| 2176 | } | 2182 | } |
| 2177 | 2183 | ||
diff --git a/src/treesit.h b/src/treesit.h index 909609737d3..5382bc58817 100644 --- a/src/treesit.h +++ b/src/treesit.h | |||
| @@ -191,6 +191,7 @@ extern bool treesit_node_uptodate_p (Lisp_Object); | |||
| 191 | extern void treesit_delete_parser (struct Lisp_TS_Parser *); | 191 | extern void treesit_delete_parser (struct Lisp_TS_Parser *); |
| 192 | extern void treesit_delete_query (struct Lisp_TS_Query *); | 192 | extern void treesit_delete_query (struct Lisp_TS_Query *); |
| 193 | extern bool treesit_named_node_p (TSNode); | 193 | extern bool treesit_named_node_p (TSNode); |
| 194 | extern bool treesit_node_eq (Lisp_Object, Lisp_Object); | ||
| 194 | 195 | ||
| 195 | #endif /* HAVE_TREE_SITTER */ | 196 | #endif /* HAVE_TREE_SITTER */ |
| 196 | 197 | ||