]> git.eshelyaron.com Git - emacs.git/commitdiff
ruby-ts-mode: Fix indentation inside empty if/unless/case/def
authorDmitry Gutov <dgutov@yandex.ru>
Thu, 9 Feb 2023 02:15:41 +0000 (04:15 +0200)
committerDmitry Gutov <dgutov@yandex.ru>
Thu, 9 Feb 2023 02:16:15 +0000 (04:16 +0200)
* lisp/progmodes/ruby-ts-mode.el (ruby-ts--indent-rules):
Add new rule.

* test/lisp/progmodes/ruby-ts-mode-tests.el
(ruby-ts-indent-empty-if-else): New test.

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

index e83bc2f9e113c56b2e36131372bbbeaf3a131e87..20ffb38fb8875d415489da4db4c0cc065f14ecef 100644 (file)
@@ -661,6 +661,13 @@ a statement container is a node that matches
            ((n-p-gp nil "body_statement" ,ruby-ts--method-regex) ;other statements
             (ruby-ts--align-keywords ruby-ts--grand-parent-node) ruby-indent-level)
 
+           ;; Quirk of the ruby parser: these "alignable" nodes don't
+           ;; have the "container" child node when there are no
+           ;; statements inside. Thus we have to have a separate rule
+           ;; for the "empty if/unless/case/def" situation.
+           ((match "\\`\\'" "\\`\\(?:if\\|unless\\|case\\|method\\)\\'")
+            (ruby-ts--align-keywords ruby-ts--parent-node) ruby-indent-level)
+
            ;; Chained calls:
            ;; if `ruby-align-chained-calls' is true, the first query
            ;; matches and the node is aligned under the first dot (.);
index 18e3e60a04a51bdfc13b94f8093d15adef83469f..c99e1a43063fab25cfc2df907760758c2178f8c7 100644 (file)
@@ -122,6 +122,22 @@ The whitespace before and including \"|\" on each line is removed."
     (funcall indent-line-function)
     (should (= (current-indentation) ruby-indent-level))))
 
+(ert-deftest ruby-ts-indent-empty-if-else ()
+  (skip-unless (treesit-ready-p 'ruby t))
+  (let* ((str "c = if foo
+      zz
+    else
+      zz
+    end
+"))
+    (ruby-ts-with-temp-buffer str
+      (goto-char (point-min))
+      (dotimes (_ 2)
+        (re-search-forward "^ *zz")
+        (replace-match "")
+        (funcall indent-line-function)
+        (should (= (current-indentation) 6))))))
+
 (ert-deftest ruby-ts-add-log-current-method-examples ()
   (skip-unless (treesit-ready-p 'ruby t))
   (let ((pairs '(("foo" . "#foo")