Skip to content

Commit

Permalink
Call evil-range only if we are able to find a textobj
Browse files Browse the repository at this point in the history
  • Loading branch information
meain committed Aug 22, 2021
1 parent f5306d4 commit 4859ea3
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions evil-textobj-treesitter.el
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ set."
(and (= (car (tsc-node-byte-range x)) (car (tsc-node-byte-range y)))
(= (cdr (tsc-node-byte-range x)) (cdr (tsc-node-byte-range y)))))))
(nodes-within (evil-textobj-treesitter--nodes-within nodes-nodupes))
(nodes-after (evil-textobj-treesitter--nodes-after nodes-nodupes)))
(cl-subseq (append nodes-within nodes-after)
0
count)))
(nodes-after (evil-textobj-treesitter--nodes-after nodes-nodupes))
(all-nodes (append nodes-within nodes-after)))
(progn
(if (> (length all-nodes) 0)
(cl-subseq all-nodes 0 count)))))

(defun evil-textobj-treesitter--range (count ts-group &optional query)
"Get the range of the closeset item of type `TS-GROUP'.
Expand All @@ -128,19 +129,20 @@ are doing. If a `QUERY' alist is provided, we make use of that
instead of the builtin query set."
(if (equal tree-sitter-mode nil)
(message "tree-sitter-mode not enabled for buffer")
(let* ((nodes (evil-textobj-treesitter--get-nodes ts-group
count query))
(range-min (apply #'min
(seq-map (lambda (x)
(car (tsc-node-byte-range x)))
nodes)))
(range-max (apply #'max
(seq-map (lambda (x)
(cdr (tsc-node-byte-range x)))
nodes))))
;; Have to compute min and max like this as we might have nested functions
;; We have to use `cl-callf byte-to-position` ot the positioning might be off for unicode chars
(cons (cl-callf byte-to-position range-min) (cl-callf byte-to-position range-max)))))
(let ((nodes (evil-textobj-treesitter--get-nodes ts-group
count query)))
(if (not (eq nodes nil))
(let* ((range-min (apply #'min
(seq-map (lambda (x)
(car (tsc-node-byte-range x)))
nodes)))
(range-max (apply #'max
(seq-map (lambda (x)
(cdr (tsc-node-byte-range x)))
nodes))))
;; Have to compute min and max like this as we might have nested functions
;; We have to use `cl-callf byte-to-position` ot the positioning might be off for unicode chars
(cons (cl-callf byte-to-position range-min) (cl-callf byte-to-position range-max)))))))

;;;###autoload
(defmacro evil-textobj-treesitter-get-textobj (group &optional query)
Expand All @@ -166,8 +168,10 @@ https://github.com/nvim-treesitter/nvim-treesitter-textobjects#built-in-textobje
(count &rest _)
(let ((range (evil-textobj-treesitter--range count ',interned-groups
,query)))
(evil-range (car range)
(cdr range))))))
(if (not (eq range nil))
(evil-range (car range)
(cdr range))
(message (concat "No '" ,group "' text object found")))))))

(provide 'evil-textobj-treesitter)
;;; evil-textobj-treesitter.el ends here

0 comments on commit 4859ea3

Please sign in to comment.