summaryrefslogtreecommitdiff
path: root/lisp/progmodes/ruby-ts-mode.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/progmodes/ruby-ts-mode.el')
-rw-r--r--lisp/progmodes/ruby-ts-mode.el89
1 files changed, 76 insertions, 13 deletions
diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el
index b182fa867da..7133cb0b5b0 100644
--- a/lisp/progmodes/ruby-ts-mode.el
+++ b/lisp/progmodes/ruby-ts-mode.el
@@ -25,7 +25,7 @@
;;; Commentary:
;; This file defines ruby-ts-mode which is a major mode for editing
-;; Ruby files that uses Tree Sitter to parse the language. More
+;; Ruby files that uses Tree Sitter to parse the language. More
;; information about Tree Sitter can be found in the ELisp Info pages
;; as well as this website: https://tree-sitter.github.io/tree-sitter/
@@ -1088,6 +1088,15 @@ leading double colon is not added."
(put-text-property pos (1+ pos) 'syntax-table
(string-to-syntax "!"))))))))
+(defun ruby-ts--sexp-p (node)
+ ;; Skip parenless calls (implicit parens are both non-obvious to the
+ ;; user, and might take over when we want to just over some physical
+ ;; parens/braces).
+ (or (not (equal (treesit-node-type node)
+ "argument_list"))
+ (equal (treesit-node-type (treesit-node-child node 0))
+ "(")))
+
(defvar-keymap ruby-ts-mode-map
:doc "Keymap used in Ruby mode"
:parent prog-mode-map
@@ -1115,12 +1124,74 @@ leading double colon is not added."
;; Navigation.
(setq-local treesit-defun-type-regexp ruby-ts--method-regex)
+ (setq-local treesit-thing-settings
+ `((ruby
+ (sexp ,(cons (rx
+ bol
+ (or
+ "class"
+ "singleton_class"
+ "module"
+ "method"
+ "singleton_method"
+ "array"
+ "hash"
+ "parenthesized_statements"
+ "method_parameters"
+ "array_pattern"
+ "hash_pattern"
+ "if"
+ "else"
+ "then"
+ "unless"
+ "case"
+ "case_match"
+ "when"
+ "while"
+ "until"
+ "for"
+ "block"
+ "do_block"
+ "begin"
+ "integer"
+ "identifier"
+ "self"
+ "super"
+ "constant"
+ "simple_symbol"
+ "hash_key_symbol"
+ "symbol_array"
+ "string"
+ "string_array"
+ "heredoc_body"
+ "regex"
+ "argument_list"
+ "interpolation"
+ "instance_variable"
+ "global_variable"
+ )
+ eol)
+ #'ruby-ts--sexp-p)))))
+
;; AFAIK, Ruby can not nest methods
(setq-local treesit-defun-prefer-top-level nil)
;; Imenu.
(setq-local imenu-create-index-function #'ruby-ts--imenu)
+ ;; Outline minor mode.
+ (setq-local treesit-outline-predicate
+ (rx bos (or "singleton_method"
+ "method"
+ "alias"
+ "class"
+ "module")
+ eos))
+ ;; Restore default values of outline variables
+ ;; to use `treesit-outline-predicate'.
+ (kill-local-variable 'outline-regexp)
+ (kill-local-variable 'outline-level)
+
(setq-local treesit-simple-indent-rules (ruby-ts--indent-rules))
;; Font-lock.
@@ -1139,19 +1210,11 @@ leading double colon is not added."
(setq-local syntax-propertize-function #'ruby-ts--syntax-propertize))
+(derived-mode-add-parents 'ruby-ts-mode '(ruby-mode))
+
(if (treesit-ready-p 'ruby)
- ;; Copied from ruby-mode.el.
- (add-to-list 'auto-mode-alist
- (cons (concat "\\(?:\\.\\(?:"
- "rbw?\\|ru\\|rake\\|thor"
- "\\|jbuilder\\|rabl\\|gemspec\\|podspec"
- "\\)"
- "\\|/"
- "\\(?:Gem\\|Rake\\|Cap\\|Thor"
- "\\|Puppet\\|Berks\\|Brew"
- "\\|Vagrant\\|Guard\\|Pod\\)file"
- "\\)\\'")
- 'ruby-ts-mode)))
+ (add-to-list 'major-mode-remap-defaults
+ '(ruby-mode . ruby-ts-mode)))
(provide 'ruby-ts-mode)