fix(emacs-tree-sitter-move): get named parents & check for nils
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
This commit is contained in:
parent
fdb47be7d7
commit
5a6f781c3e
1 changed files with 16 additions and 8 deletions
|
@ -50,19 +50,27 @@
|
||||||
(tsc-get-named-descendant-for-position-range
|
(tsc-get-named-descendant-for-position-range
|
||||||
(tsc-root-node tree-sitter-tree) p p)))
|
(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)
|
(defun tsc-get-first-named-node-with-siblings-up (node)
|
||||||
"Returns the first 'upwards' node that has siblings. That includes the current
|
"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
|
node, so if the given node has siblings, it is returned. Returns nil if there
|
||||||
is no such node until the root"
|
is no such node until the root"
|
||||||
(when-let ((has-siblings-p
|
(when-let ((has-siblings-p
|
||||||
(lambda (parent-node)
|
(lambda (parent-node)
|
||||||
(> (tsc-count-named-children parent-node)
|
(> (tsc-count-named-children parent-node)
|
||||||
1)))
|
1)))
|
||||||
(cur node)
|
(cur node)
|
||||||
(parent (tsc-get-parent node)))
|
(parent (tsc-get-named-parent node)))
|
||||||
(while (not (funcall has-siblings-p parent))
|
(while (and parent (not (funcall has-siblings-p parent)))
|
||||||
(setq cur parent)
|
(setq cur parent)
|
||||||
(setq parent (tsc-get-parent cur)))
|
(setq parent (tsc-get-named-parent cur)))
|
||||||
cur))
|
cur))
|
||||||
|
|
||||||
(defun tree-sitter-move--set-cursor-to-node (node)
|
(defun tree-sitter-move--set-cursor-to-node (node)
|
||||||
|
|
Loading…
Reference in a new issue