Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for treesit-beginning-of-defun and friends and which-function-mode #13

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion scala-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,20 @@
(back-to-indentation)
(point))))))

(defun scala-ts--move-out-of-indented-block (&rest _)
"Correction when jumping at the beginning of a defun.
If point is in \"indented_block\" move it at the beginning of the
block. End search as soon as either \"indented_block\" is found
or node matching `treesit-defun-type-regexp' is found."
(let* ((pred (lambda (node)
(let ((type (treesit-node-type node)))
(or
(string= "indented_block" type)
(string-match-p treesit-defun-type-regexp type)))))
(node (treesit-parent-until (treesit-node-at (point)) pred t)))
(when (string= "indented_block" (treesit-node-type node))
(goto-char (treesit-node-start node)))))

(defvar scala-ts--indent-rules
(let ((offset scala-ts-indent-offset))
`((scala
Expand Down Expand Up @@ -532,7 +546,7 @@
((parent-is "^indented_block$") parent 0)
((node-is "^indented_block$") parent-bol ,offset)
((node-is "^block$") parent ,offset)
)))

Check warning on line 549 in scala-ts-mode.el

View workflow job for this annotation

GitHub Actions / check (release-snapshot, true)

Closing parens should not be wrapped onto new lines.
"Tree-sitter indent rules for `scala-ts-mode'.")

(defun scala-ts--defun-name (node)
Expand All @@ -549,6 +563,10 @@
(treesit-node-text
(treesit-node-child-by-field-name node "name")
t))
("val_definition"
(treesit-node-text
(treesit-node-child-by-field-name node "pattern")
t))
("function_declaration"
(treesit-node-text
(treesit-node-child node 0)
Expand Down Expand Up @@ -579,6 +597,14 @@
(setq-local
treesit-simple-indent-rules scala-ts--indent-rules)

;; Navigation.
(setq-local treesit-defun-type-regexp
(rx (or "class_definition"
"object_definition"
"trait_definition"
"function_definition"
"val_definition")))

(setq-local treesit-defun-name-function #'scala-ts--defun-name)
;; TODO (could possibly be more complex?)
(setq-local treesit-simple-imenu-settings
Expand All @@ -588,7 +614,9 @@
("Object" "\\`object_definition\\'" nil nil)
("Function" "\\`function_definition\\'" nil nil)
("Definition" "\\`function_declaration'" nil nil)))


(advice-add 'treesit-beginning-of-defun :before #'scala-ts--move-out-of-indented-block)

(treesit-major-mode-setup)))

;;;###autoload
Expand Down
Loading