about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2020-12-19T21·29+0100
committerProfpatsch <mail@profpatsch.de>2021-01-01T22·40+0000
commit5a6f781c3efa21460657d14ee9c84fc4bb7e4832 (patch)
tree8861c5db9b375440dc0a080b05371d72a27a4891 /users
parentfdb47be7d73652142eb15ae978d5cb63313d346a (diff)
fix(emacs-tree-sitter-move): get named parents & check for nils r/2047
If there was no parent, the while loop would try to get the parent of
a `nil`, which crashes and burns.

We now also ignore any non-named parents; this might be unnecessary,
if tree-sitter parent nodes are always named, but I don’t know that at
the moment and it’s not documented very well, so better safe than
sorry.

Change-Id: Ia72ee9241b885ab312f8ecf7a8fbfece7eea8f1b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2263
Reviewed-by: Profpatsch <mail@profpatsch.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'users')
-rw-r--r--users/Profpatsch/emacs-tree-sitter-move/tree-sitter-move.el24
1 files changed, 16 insertions, 8 deletions
diff --git a/users/Profpatsch/emacs-tree-sitter-move/tree-sitter-move.el b/users/Profpatsch/emacs-tree-sitter-move/tree-sitter-move.el
index ddccb58c8948..907e1e4081bc 100644
--- a/users/Profpatsch/emacs-tree-sitter-move/tree-sitter-move.el
+++ b/users/Profpatsch/emacs-tree-sitter-move/tree-sitter-move.el
@@ -50,19 +50,27 @@
     (tsc-get-named-descendant-for-position-range
      (tsc-root-node tree-sitter-tree) p p)))
 
+;; TODO: is this function necessary?
+;; Maybe tree-sitter always guarantees that parents are named?
+(defun tsc-get-named-parent (node)
+  (when-let ((parent (tsc-get-parent node)))
+    (while (and parent (not (tsc-node-named-p parent)))
+      (setq parent (tsc-get-parent parent)))
+    parent))
+
 (defun tsc-get-first-named-node-with-siblings-up (node)
   "Returns the first 'upwards' node that has siblings. That includes the current
   node, so if the given node has siblings, it is returned. Returns nil if there
   is no such node until the root"
   (when-let ((has-siblings-p
-            (lambda (parent-node)
-              (> (tsc-count-named-children parent-node)
-                 1)))
-           (cur node)
-           (parent (tsc-get-parent node)))
-      (while (not (funcall has-siblings-p parent))
-        (setq cur parent)
-        (setq parent (tsc-get-parent cur)))
+              (lambda (parent-node)
+                (> (tsc-count-named-children parent-node)
+                   1)))
+             (cur node)
+             (parent (tsc-get-named-parent node)))
+    (while (and parent (not (funcall has-siblings-p parent)))
+      (setq cur parent)
+      (setq parent (tsc-get-named-parent cur)))
     cur))
 
 (defun tree-sitter-move--set-cursor-to-node (node)