Subject: Re: bug#67036: 30.0.50; treesit-forward-sexp not working properly in ruby-ts-mode

From: Juri Linkov <[email-masked]>
Date: Sun, 14 Apr 2024 19:25:11 +0300

>>>>>>>> +# C-M-f on '[' doesn't jump to after ']'
>>>>>>>> +hash['key']
>>>>>>>> +
>>>>>>
>>>>>> As discussed previously, there is no specific node which spans from [ to
>>>>>> ]. Some custom code could probably be written (there *are* leaf nodes for [
>>>>>> and ]), but the current capabilities of treesit-thing-settings don't offer
>>>>>> a good way to plug that in.
>>>>> Like for point inside strings, this might require more general changes
>>>>> that take into account syntax tables.
>>>>
>>>> Possibly, but I expect a solution that doesn't use the syntax table would
>>>> be tried first.
>>> Since there is no available information from treesit, handling
>>> treesit-forward-sexp inside strings/comments could forward to the syntax table
>>> like `prog-fill-reindent-defun' forwards to `fill-paragraph'.
>
> Shouldn't treesit-forward-sexp provide a way for ts-modes to override
> the default handling?  By default for all ts-modes such a hook could
> check if point is inside strings/comments then to use syntax navigation.

This is now implemented in bug#68993.  So here is the patch that 
handles such cases as C-M-f jumping from '[' to ']' in

  hash[:key]
  hash['key']

and from '{' to '}' in

  "abc #{ghi} def"

diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el
index 7133cb0b5b0..098cca2cb56 100644
--- a/lisp/progmodes/ruby-ts-mode.el
+++ b/lisp/progmodes/ruby-ts-mode.el
@@ -1171,7 +1171,20 @@ ruby-ts-mode
                                 "global_variable"
                                 )
                                eol)
-                              #'ruby-ts--sexp-p)))))
+                              #'ruby-ts--sexp-p))
+                 (text ,(lambda (node)
+                          (or (member (treesit-node-type node)
+                                      '("comment" "string_content"))
+                              (and (member (treesit-node-text node)
+                                           '("[" "]"))
+                                   (equal (treesit-node-type
+                                           (treesit-node-parent node))
+                                          "element_reference"))
+                              (and (member (treesit-node-text node)
+                                           '("#{" "}"))
+                                   (equal (treesit-node-type
+                                           (treesit-node-parent node))
+                                          "interpolation"))))))))
 
   ;; AFAIK, Ruby can not nest methods
   (setq-local treesit-defun-prefer-top-level nil)