From 5a6f781c3efa21460657d14ee9c84fc4bb7e4832 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 19 Dec 2020 22:29:31 +0100 Subject: fix(emacs-tree-sitter-move): get named parents & check for nils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Tested-by: BuildkiteCI --- .../emacs-tree-sitter-move/tree-sitter-move.el | 24 ++++++++++++++-------- 1 file 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) -- cgit 1.4.1