From e07e88d81d3a33365575f8d0b464b5ce21a0fc8e Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 19 Dec 2020 20:18:40 +0100 Subject: [PATCH] feat(emacs-tree-sitter-move): left and up movements, skip unnamed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We skip intermediate nodes that do not have any siblings, because they are irrelevant to navigation and just add extra keypresses without any highlight changes. This might not be the best choice, we’ll see. Change-Id: I75fbf79aa7915172e426442a076d57cfbebf5421 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2260 Reviewed-by: Profpatsch Tested-by: BuildkiteCI --- .../Profpatsch/emacs-tree-sitter-move/tmp.el | 6 ++--- .../tree-sitter-move.el | 22 ++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/users/Profpatsch/emacs-tree-sitter-move/tmp.el b/users/Profpatsch/emacs-tree-sitter-move/tmp.el index fa13da120..f7a843709 100644 --- a/users/Profpatsch/emacs-tree-sitter-move/tmp.el +++ b/users/Profpatsch/emacs-tree-sitter-move/tmp.el @@ -9,6 +9,6 @@ (define-key evil-normal-state-map (kbd "C-.") #'tree-sitter-move-reset) (define-key evil-normal-state-map (kbd "C-") #'tree-sitter-move-right) -;; (define-key evil-normal-state-map (kbd "C-") 'sp-backward-parallel-sexp) -;; (define-key evil-normal-state-map (kbd "C-") 'sp-down-sexp) -;; (define-key evil-normal-state-map (kbd "C-") 'sp-backward-up-sexp) +(define-key evil-normal-state-map (kbd "C-") #'tree-sitter-move-left) +(define-key evil-normal-state-map (kbd "C-") 'tree-sitter-move-up) +(define-key evil-normal-state-map (kbd "C-") 'tree-sitter-move-down) 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 0a5e34e4b..0fd16e675 100644 --- a/users/Profpatsch/emacs-tree-sitter-move/tree-sitter-move.el +++ b/users/Profpatsch/emacs-tree-sitter-move/tree-sitter-move.el @@ -83,14 +83,30 @@ (tree-sitter-move--set-cursor-to-node-at-point)) (defun tree-sitter-move-right () - "Moves to the next sibling. If the current node does not have siblings, go - upwards until something has siblings and then move right." (interactive) + (tree-sitter-move--move-skip-non-sibling-nodes 'tsc-get-next-named-sibling)) + +(defun tree-sitter-move-left () + (interactive) + (tree-sitter-move--move-skip-non-sibling-nodes 'tsc-get-prev-named-sibling)) + +(defun tree-sitter-move-up () + (interactive) + (tree-sitter-move--move-skip-non-sibling-nodes 'tsc-get-parent)) + +;; TODO doesn’t work yet because sibling nodes are only skipped upwards +;; (defun tree-sitter-move-down () +;; (interactive) +;; (tree-sitter-move--move-skip-non-sibling-nodes (lambda (n) (tsc-get-nth-named-child n 0)))) + +(defun tree-sitter-move--move-skip-non-sibling-nodes (move-fn) + "Moves to the sidewards next sibling. If the current node does not have siblings, go + upwards until something has siblings and then move to the side (right or left)." (tree-sitter-move--move-if-possible (lambda (cur) (when-let ((with-siblings (tsc-get-first-named-node-with-siblings-up cur))) - (tsc-get-next-named-sibling with-siblings))))) + (funcall move-fn with-siblings))))) (defun tree-sitter-move--move-if-possible (dir-fn) (let ((next (funcall dir-fn tree-sitter-move--cursor)))