From: Dmitry Gutov Date: Mon, 16 Jan 2023 03:17:05 +0000 (+0200) Subject: ruby-ts-mode: Support the option ruby-block-indent X-Git-Tag: emacs-29.0.90~712 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=352e41016bcaab8566347091b6b61908a9b57b57;p=emacs.git ruby-ts-mode: Support the option ruby-block-indent * lisp/progmodes/ruby-ts-mode.el (ruby-ts--block-indent-anchor): New function. (ruby-ts--indent-rules): Use it. * test/lisp/progmodes/ruby-ts-mode-tests.el: Run indent test for ruby-block-indent.rb. --- diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el index d68b57966ba..939c054b041 100644 --- a/lisp/progmodes/ruby-ts-mode.el +++ b/lisp/progmodes/ruby-ts-mode.el @@ -780,12 +780,20 @@ i.e. expr of def foo(args) = expr is returned." ;; but with node set to the statement and parent set to ;; body_statement for all others. ... Fine. Be that way. ;; Ditto for "block" and "block_body" - ((node-is "body_statement") parent-bol ruby-indent-level) - ((parent-is "body_statement") (ruby-ts--bol ruby-ts--grand-parent-node) ruby-indent-level) - ((match "end" "do_block") parent-bol 0) - ((n-p-gp "block_body" "block" nil) parent-bol ruby-indent-level) - ((n-p-gp nil "block_body" "block") (ruby-ts--bol ruby-ts--grand-parent-node) ruby-indent-level) - ((match "}" "block") parent-bol 0) + ((node-is "body_statement") + (ruby-ts--block-indent-anchor ruby-ts--parent-node) + ruby-indent-level) + ((parent-is "body_statement") + (ruby-ts--block-indent-anchor ruby-ts--grand-parent-node) + ruby-indent-level) + ((match "end" "do_block") (ruby-ts--block-indent-anchor ruby-ts--parent-node) 0) + ((n-p-gp "block_body" "block" nil) + (ruby-ts--block-indent-anchor ruby-ts--parent-node) + ruby-indent-level) + ((n-p-gp nil "block_body" "block") + (ruby-ts--block-indent-anchor ruby-ts--grand-parent-node) + ruby-indent-level) + ((match "}" "block") (ruby-ts--block-indent-anchor ruby-ts--parent-node) 0) ;; Chained strings ((match "string" "chained_string") first-sibling 0) @@ -794,6 +802,18 @@ i.e. expr of def foo(args) = expr is returned." (catch-all parent-bol ruby-indent-level)))) `((ruby . ,common)))) +(defun ruby-ts--block-indent-anchor (block-node-getter) + (lambda (node parent _bol &rest _rest) + (let ((block-node (funcall block-node-getter node parent))) + (save-excursion + (goto-char + (treesit-node-start + (if ruby-block-indent + (ruby-ts--statement-ancestor block-node) + block-node))) + (back-to-indentation) + (point))))) + (defun ruby-ts--class-or-module-p (node) "Predicate if NODE is a class or module." (string-match-p ruby-ts--class-or-module-regex (treesit-node-type node))) diff --git a/test/lisp/progmodes/ruby-ts-mode-tests.el b/test/lisp/progmodes/ruby-ts-mode-tests.el index b2c990f8e56..eaf6367a306 100644 --- a/test/lisp/progmodes/ruby-ts-mode-tests.el +++ b/test/lisp/progmodes/ruby-ts-mode-tests.el @@ -251,6 +251,7 @@ The whitespace before and including \"|\" on each line is removed." (kill-buffer buf))))) (ruby-ts-deftest-indent "ruby-method-params-indent.rb") +(ruby-ts-deftest-indent "ruby-block-indent.rb") (provide 'ruby-ts-mode-tests)