Add treesit-defun-at-point and fix c-ts-mode-indent-defun
* lisp/treesit.el (treesit-defun-at-point): New function. * lisp/progmodes/c-ts-mode.el (c-ts-mode-indent-defun): Implement with treesit-defun-at-point.
This commit is contained in:
parent
69123d4aa4
commit
7dea58b88d
2 changed files with 27 additions and 7 deletions
|
@ -556,13 +556,10 @@ the semicolon. This function skips the semicolon."
|
||||||
|
|
||||||
`treesit-defun-type-regexp' defines what constructs to indent."
|
`treesit-defun-type-regexp' defines what constructs to indent."
|
||||||
(interactive "*")
|
(interactive "*")
|
||||||
(let ((orig-point (point-marker)))
|
(when-let ((orig-point (point-marker))
|
||||||
;; If `treesit-beginning-of-defun' returns nil, we are not in a
|
(node (treesit-defun-at-point)))
|
||||||
;; defun, so don't indent anything.
|
(indent-region (treesit-node-start node)
|
||||||
(when (treesit-beginning-of-defun)
|
(treesit-node-end node))
|
||||||
(let ((start (point)))
|
|
||||||
(treesit-end-of-defun)
|
|
||||||
(indent-region start (point))))
|
|
||||||
(goto-char orig-point)))
|
(goto-char orig-point)))
|
||||||
|
|
||||||
(defvar-keymap c-ts-mode-map
|
(defvar-keymap c-ts-mode-map
|
||||||
|
|
|
@ -1834,6 +1834,29 @@ function is called recursively."
|
||||||
;; Counter equal to 0 means we successfully stepped ARG steps.
|
;; Counter equal to 0 means we successfully stepped ARG steps.
|
||||||
(if (eq counter 0) pos nil)))
|
(if (eq counter 0) pos nil)))
|
||||||
|
|
||||||
|
;; TODO: In corporate into thing-at-point.
|
||||||
|
(defun treesit-defun-at-point ()
|
||||||
|
"Return the defun at point or nil if none is found.
|
||||||
|
|
||||||
|
Respects `treesit-defun-tactic': return the top-level defun if it
|
||||||
|
is `top-level', return the immediate parent defun if it is
|
||||||
|
`nested'."
|
||||||
|
(pcase-let* ((`(,regexp . ,pred)
|
||||||
|
(if (consp treesit-defun-type-regexp)
|
||||||
|
treesit-defun-type-regexp
|
||||||
|
(cons treesit-defun-type-regexp nil)))
|
||||||
|
(`(,_ ,next ,parent)
|
||||||
|
(treesit--defuns-around (point) regexp pred))
|
||||||
|
;; If point is at the beginning of a defun, we
|
||||||
|
;; prioritize that defun over the parent in nested
|
||||||
|
;; mode.
|
||||||
|
(node (or (and (eq (treesit-node-start next) (point))
|
||||||
|
next)
|
||||||
|
parent)))
|
||||||
|
(if (eq treesit-defun-tactic 'top-level)
|
||||||
|
(treesit--top-level-defun node regexp pred)
|
||||||
|
node)))
|
||||||
|
|
||||||
;;; Activating tree-sitter
|
;;; Activating tree-sitter
|
||||||
|
|
||||||
(defun treesit-ready-p (language &optional quiet)
|
(defun treesit-ready-p (language &optional quiet)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue