Skip to content

Commit 38dc7a2

Browse files
authored
Support for treesit-beginning-of-defun and friends and which-function-mode (KaranAhlawat#13)
* Support for `treesit-beginning-of-defun` and friends and `which-function-mode` * Use built-in treesit-parent-until and use rx instead of regexp-opt
1 parent 88f9ad9 commit 38dc7a2

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

scala-ts-mode.el

+29-1
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,20 @@
445445
(back-to-indentation)
446446
(point))))))
447447

448+
(defun scala-ts--move-out-of-indented-block (&rest _)
449+
"Correction when jumping at the beginning of a defun.
450+
If point is in \"indented_block\" move it at the beginning of the
451+
block. End search as soon as either \"indented_block\" is found
452+
or node matching `treesit-defun-type-regexp' is found."
453+
(let* ((pred (lambda (node)
454+
(let ((type (treesit-node-type node)))
455+
(or
456+
(string= "indented_block" type)
457+
(string-match-p treesit-defun-type-regexp type)))))
458+
(node (treesit-parent-until (treesit-node-at (point)) pred t)))
459+
(when (string= "indented_block" (treesit-node-type node))
460+
(goto-char (treesit-node-start node)))))
461+
448462
(defvar scala-ts--indent-rules
449463
(let ((offset scala-ts-indent-offset))
450464
`((scala
@@ -549,6 +563,10 @@ Return nil if there is no name or if NODE is not a defun node."
549563
(treesit-node-text
550564
(treesit-node-child-by-field-name node "name")
551565
t))
566+
("val_definition"
567+
(treesit-node-text
568+
(treesit-node-child-by-field-name node "pattern")
569+
t))
552570
("function_declaration"
553571
(treesit-node-text
554572
(treesit-node-child node 0)
@@ -579,6 +597,14 @@ Return nil if there is no name or if NODE is not a defun node."
579597
(setq-local
580598
treesit-simple-indent-rules scala-ts--indent-rules)
581599

600+
;; Navigation.
601+
(setq-local treesit-defun-type-regexp
602+
(rx (or "class_definition"
603+
"object_definition"
604+
"trait_definition"
605+
"function_definition"
606+
"val_definition")))
607+
582608
(setq-local treesit-defun-name-function #'scala-ts--defun-name)
583609
;; TODO (could possibly be more complex?)
584610
(setq-local treesit-simple-imenu-settings
@@ -588,7 +614,9 @@ Return nil if there is no name or if NODE is not a defun node."
588614
("Object" "\\`object_definition\\'" nil nil)
589615
("Function" "\\`function_definition\\'" nil nil)
590616
("Definition" "\\`function_declaration'" nil nil)))
591-
617+
618+
(advice-add 'treesit-beginning-of-defun :before #'scala-ts--move-out-of-indented-block)
619+
592620
(treesit-major-mode-setup)))
593621
594622
;;;###autoload

0 commit comments

Comments
 (0)