emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Are variations such as these desired?


From: Perry Smith
Subject: Are variations such as these desired?
Date: Mon, 12 Dec 2022 18:46:29 -0600

Adding on to the ruby-ts-mode I thought I would point out an example of the type of power available with tree sitter.

With a not so complex function:

(defun my-func ( node parent &rest _)
  "Proof of concept to do right justification of NODE and PARENT."
  (message "here %s" (treesit-node-text node))
  (let* ((children (treesit-node-children parent t))
         (open-bracket (nth 0 (treesit-node-children parent nil)))
         (first-child (nth 0 children))
         (same-line (equal (line-number-at-pos (treesit-node-start open-bracket))
                           (line-number-at-pos (treesit-node-start first-child))))
         (max-length (apply #'max (mapcar (lambda ( child )
                                            (- (treesit-node-end child) (treesit-node-start child)))
                                          children)))
         (node-length (- (treesit-node-end node) (treesit-node-start node)))
         (grand-parent-bol (save-excursion
                             (goto-char (treesit-node-start (treesit-node-parent parent)))
                             (back-to-indentation)
                             (point)))
         (align-column (if same-line
                           (- (+ (treesit-node-end open-bracket) max-length 1) ruby-ts-mode-indent-offset)
                         (+ grand-parent-bol max-length 1))))

    (- align-column node-length)))

And two indent rules:

           ((query "(array \"[\" ( (_) ( \",\" (_) )*) @indent \",\"? \"]\")") my-func ruby-ts-mode-indent-offset)
           ((n-p-gp "]" "array" "assignment") grand-parent ruby-ts-mode-indent-offset)

The programmer can now right justify elements of an array in two different fashions:


if dog
  array = [
       145,
     21110,
        11
    ]
end

if dog
  array = [   145,
            21110,
               11]
end

The two rules could be conditionally added to the rest of the rules via a customizable boolean variable such as ruby-ts-mode--right-justify-arrays (for example).

And this concept could be applied to hashes.  The possibilities are almost endless.

My question is, do others see adding this versatility at this point in time to the Emacs distribution as desirable?  Should we wait and see how users use the new system?  Perhaps some see both of the above formats as abominations.

Perry

Attachment: signature.asc
Description: Message signed with OpenPGP


reply via email to

[Prev in Thread] Current Thread [Next in Thread]