]> git.eshelyaron.com Git - emacs.git/commitdiff
ruby-ts-mode: Support the option ruby-block-indent
authorDmitry Gutov <dgutov@yandex.ru>
Mon, 16 Jan 2023 03:17:05 +0000 (05:17 +0200)
committerDmitry Gutov <dgutov@yandex.ru>
Mon, 16 Jan 2023 03:50:28 +0000 (05:50 +0200)
* 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.

lisp/progmodes/ruby-ts-mode.el
test/lisp/progmodes/ruby-ts-mode-tests.el

index d68b57966bae2a23b1c747e8fb2dbc5684b98f08..939c054b041b455118c17eab3f4b4d2a733ea69a 100644 (file)
@@ -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)))
index b2c990f8e56261ef633f8cd2ea02acfec9792b1a..eaf6367a306f1ad139d1d3530cb26765a4019f51 100644 (file)
@@ -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)