[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
emacs-29 300ca6ac37 4/6: ruby-ts-mode: Fix indent after operator or cond
From: |
Dmitry Gutov |
Subject: |
emacs-29 300ca6ac37 4/6: ruby-ts-mode: Fix indent after operator or conditional |
Date: |
Tue, 17 Jan 2023 20:28:44 -0500 (EST) |
branch: emacs-29
commit 300ca6ac37250711b7d6484e0a870bf37e9e00cb
Author: Dmitry Gutov <dgutov@yandex.ru>
Commit: Dmitry Gutov <dgutov@yandex.ru>
ruby-ts-mode: Fix indent after operator or conditional
Make it match ruby-mode's indentation behavior.
* lisp/progmodes/ruby-ts-mode.el (ruby-ts--binary-indent-anchor):
New function.
(ruby-ts--indent-rules): Use it instead of a composite matcher.
Add a rule for 'conditional'.
(ruby-ts--assignment-ancestor, ruby-ts--is-in-condition)
(ruby-ts--endless-method): Remove.
* test/lisp/progmodes/ruby-mode-resources/ruby-ts.rb:
Add examples.
---
lisp/progmodes/ruby-ts-mode.el | 41 +++++++---------------
test/lisp/progmodes/ruby-mode-resources/ruby-ts.rb | 13 +++++++
2 files changed, 25 insertions(+), 29 deletions(-)
diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el
index cbf86544be..e629ff1967 100644
--- a/lisp/progmodes/ruby-ts-mode.el
+++ b/lisp/progmodes/ruby-ts-mode.el
@@ -512,10 +512,6 @@ array or hash."
(first-child (ruby-ts--first-non-comment-child parent)))
(= (ruby-ts--lineno open-brace) (ruby-ts--lineno first-child))))
-(defun ruby-ts--assignment-ancestor (node &rest _)
- "Return the assignment ancestor of NODE if any."
- (treesit-parent-until node (ruby-ts--type-pred "\\`assignment\\'")))
-
(defun ruby-ts--statement-ancestor (node &rest _)
"Return the statement ancestor of NODE if any.
A statement is defined as a child of a statement container where
@@ -531,26 +527,6 @@ a statement container is a node that matches
parent (treesit-node-parent parent)))
statement))
-(defun ruby-ts--is-in-condition (node &rest _)
- "Return the condition node if NODE is within a condition."
- (while (and node
- (not (equal "condition" (treesit-node-field-name node)))
- (not (string-match-p ruby-ts--statement-container-regexp
- (treesit-node-type node))))
- (setq node (treesit-node-parent node)))
- (and (equal "condition" (treesit-node-field-name node)) node))
-
-(defun ruby-ts--endless-method (node &rest _)
- "Return the expression node if NODE is in an endless method.
-i.e. expr of def foo(args) = expr is returned."
- (let* ((method node))
- (while (and method
- (not (string-match-p ruby-ts--method-regex (treesit-node-type
method))))
- (setq method (treesit-node-parent method)))
- (when method
- (if (equal "=" (treesit-node-type (treesit-node-child method 3 nil)))
- (treesit-node-child method 4 nil)))))
-
;;
;; end of functions that can be used for queries
;;
@@ -709,11 +685,10 @@ i.e. expr of def foo(args) = expr is returned."
;; Old... probably too simple
((parent-is "block_parameters") first-sibling 1)
- ((and (parent-is "binary")
- (or ruby-ts--assignment-ancestor
- ruby-ts--is-in-condition
- ruby-ts--endless-method))
- first-sibling 0)
+ ((parent-is "binary")
+ ruby-ts--binary-indent-anchor 0)
+
+ ((parent-is "conditional") parent ruby-indent-level)
;; ruby-mode does not touch these...
((match "bare_string" "string_array") no-indent 0)
@@ -807,6 +782,14 @@ i.e. expr of def foo(args) = expr is returned."
(back-to-indentation)
(point)))))
+(defun ruby-ts--binary-indent-anchor (_node parent _bol &rest _)
+ (save-excursion
+ (goto-char (treesit-node-start parent))
+ (when (string-match-p ruby-ts--statement-container-regexp
+ (treesit-node-type (treesit-node-parent parent)))
+ (forward-char ruby-indent-level))
+ (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-mode-resources/ruby-ts.rb
b/test/lisp/progmodes/ruby-mode-resources/ruby-ts.rb
index 1a07ababc4..92d62f92e5 100644
--- a/test/lisp/progmodes/ruby-mode-resources/ruby-ts.rb
+++ b/test/lisp/progmodes/ruby-mode-resources/ruby-ts.rb
@@ -41,6 +41,19 @@ foo2 =
2
)
+foo > bar &&
+ tee < qux
+
+1 .. 2 &&
+ 3
+
+a = foo(j, k) -
+ bar_tee
+
+qux = foo.fee ?
+ bar :
+ tee
+
# Local Variables:
# mode: ruby-ts
# End:
- emacs-29 updated (c4f0b6ccea -> 343b9b3dfe), Dmitry Gutov, 2023/01/17
- emacs-29 343b9b3dfe 6/6: ruby-ts-mode: Obey the option ruby-method-call-indent, Dmitry Gutov, 2023/01/17
- emacs-29 045404d1aa 5/6: ruby-ts-mode: Obey the option ruby-after-operator-indent, Dmitry Gutov, 2023/01/17
- emacs-29 9ed9ff4690 1/6: ruby-ts-mode: Fix the rules for hanging arrays and hashes, Dmitry Gutov, 2023/01/17
- emacs-29 5e2e68a0c2 2/6: ruby-ts-mode: Fix indent inside parenthesized_expr and else/end after unless, Dmitry Gutov, 2023/01/17
- emacs-29 ac5516bd7d 3/6: ruby-ts-mode: Fix/change indentation of a continuation method call, Dmitry Gutov, 2023/01/17
- emacs-29 300ca6ac37 4/6: ruby-ts-mode: Fix indent after operator or conditional,
Dmitry Gutov <=