From 300ca6ac37250711b7d6484e0a870bf37e9e00cb Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Wed, 18 Jan 2023 02:40:00 +0200 Subject: [PATCH] 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 ++++++------------- .../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 cbf86544bed..e629ff19672 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 1a07ababc46..92d62f92e52 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: -- 2.39.2