Function: ruby-ts-mode

ruby-ts-mode is an autoloaded, interactive and byte-compiled function defined in ruby-ts-mode.el.gz.

Signature

(ruby-ts-mode)

Documentation

Major mode for editing Ruby, powered by tree-sitter.

In addition to any hooks its parent mode ruby-base-mode might have run, this mode runs the hook ruby-ts-mode-hook, as the final or penultimate step during initialization.

C-M-q prog-indent-sexp
C-c ' ruby-toggle-string-quotes
C-c C-f ruby-find-library-file
C-c { ruby-toggle-block
M-q prog-fill-reindent-defun

Probably introduced at or before Emacs version 29.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/ruby-ts-mode.el.gz
;;;###autoload
(define-derived-mode ruby-ts-mode ruby-base-mode "Ruby"
  "Major mode for editing Ruby, powered by tree-sitter."
  :group 'ruby
  :syntax-table ruby-mode-syntax-table

  (unless (treesit-ensure-installed 'ruby)
    (error "Tree-sitter for Ruby isn't available"))

  (setq treesit-primary-parser (treesit-parser-create 'ruby))

  (setq-local add-log-current-defun-function #'ruby-ts-add-log-current-function)

  ;; Navigation.
  (setq-local treesit-defun-type-regexp ruby-ts--method-regex)

  (setq-local treesit-thing-settings
              `((ruby
                 (sexp (not (or (and named
                                     ,(rx bos (or "program"
                                                  "body_statement"
                                                  "comment"
                                                  "then")
                                          eos))
                                (and anonymous
                                     ,(rx bos (or "do" "begin"
                                                  "if" "unless"
                                                  "def" "end"
                                                  "(" ")" "[" "]"
                                                  "{" "}" "|" "," ";")
                                          eos)))))
                 (list ,(cons (rx
                               bos
                               (or
                                "begin_block"
                                "end_block"
                                "method"
                                "singleton_method"
                                "method_parameters"
                                "parameters"
                                "block_parameters"
                                "class"
                                "singleton_class"
                                "module"
                                "do"
                                "case"
                                "case_match"
                                "array_pattern"
                                "find_pattern"
                                "hash_pattern"
                                "parenthesized_pattern"
                                "expression_reference_pattern"
                                "if"
                                "unless"
                                "begin"
                                "parenthesized_statements"
                                "argument_list"
                                "do_block"
                                "block"
                                "destructured_left_assignment"
                                "interpolation"
                                "string"
                                "string_array"
                                "symbol_array"
                                "delimited_symbol"
                                "regex"
                                "heredoc_body"
                                "array"
                                "hash")
                               eos)
                              #'ruby-ts--list-p))
                 (sexp-default
                  ;; For `C-M-f' in "#|{a}"
                  ("#{" . ,(lambda (node)
                             (and (eq (char-after (point)) ?{)
                                  (equal (treesit-node-type (treesit-node-parent node))
                                         "interpolation")))))
                 (sentence ,(rx bos (or "return"
                                        "body_statement"
                                        "call"
                                        "assignment")
                                eos))
                 (text ,(lambda (node)
                          (or (member (treesit-node-type node)
                                      '("comment" "string_content"
                                        "heredoc_content"))))))))

  ;; Imenu.
  (setq-local imenu-create-index-function #'ruby-ts--imenu)

  ;; Outline minor mode.
  (setq-local treesit-outline-predicate
              `(and ,(rx bos (or "singleton_method"
                                 "method"
                                 "alias"
                                 "singleton_class"
                                 "class"
                                 "module")
                         eos)
                    named))
  ;; 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.
  (setq-local treesit-font-lock-settings (ruby-ts--font-lock-settings 'ruby))
  ;; Level 3 is the default.
  (setq-local treesit-font-lock-feature-list
              '(( comment method-definition parameter-definition)
                ( keyword regexp string type)
                ( builtin-variable builtin-constant builtin-function
                  delimiter escape-sequence
                  constant global instance
                  interpolation literal symbol assignment)
                ( bracket error function operator punctuation)))

  (treesit-major-mode-setup)

  (setq-local syntax-propertize-function #'ruby-ts--syntax-propertize))