Subject: Re: bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python

From: Wilhelm Kirschbaum <[email-masked]>
Date: Sun, 02 Apr 2023 09:49:20 +0200

Wilhelm Kirschbaum <[email-masked]> writes:

>>> From: 牟 桐 <[email-masked]>
>>> Date: Thu, 30 Mar 2023 04:25:07 +0000
>>> In elixir, the docs in src are like this:
>>> ``` elixir-ts-mode
>>> defmodule Foo do
>>>   @moduledoc """
>>>   Foo-related functions.
>>>   ## Examples
>>>       iex> Foo.sum(1, 2)
>>>       3
>>>   """
>>>   @doc """
>>>   Calculate the sum of two numbers.
>>>   """
>>>   def sum(a, b), do: a + b
>>> end
>>> ```
>>> In python-mode, input the continious triple quotes, it will 
>>> insert
>>> the
>>> left quotes when electric-pair-mode is on.
>>> python-mode did it here:
>>> https://github.com/emacs-mirror/emacs/blob/bfa3500c3c6e4df58978e84753718cd5358c06fb/lisp/progmodes/python.el#L6599-L6607
>>> https://github.com/emacs-mirror/emacs/blob/bfa3500c3c6e4df58978e84753718cd5358c06fb/lisp/progmodes/python.el#L6637-L6639
>>> This behavior is also very common in elixir, so can we add 
>>> this to
>>> elixir-ts-mode? thx

Will it make sense to also add a newline when closing a multiline
comment/heredoc? It feels smoother to me rather than having to 
press
enter and then C-o ( open-line ) to position the cursor correctly?

Instead of this:

(save-excursion
      (insert (make-string 2 last-command-event)))

perhaps this:

(save-excursion
      (newline)
      (insert (make-string 2 last-command-event)))
(newline)

Then the when you type

@moduledoc """

it will jump to

@moduledoc """
|
"""

instead of
@moduledoc """|"""      

I am adding two patches, one for the electric pair and another to 
fix
the issue where the point jumps to the end of a multi-line comment 
when
type `"` within the multi-line comment.

>From 5005439e007aa3f766b323f9a07cb9e5039820e8 Mon Sep 17 00:00:00 2001
From: Wilhelm H Kirschbaum <[email-masked]>
Date: Tue, 21 Mar 2023 16:34:48 +0200
Subject: [PATCH 2/5] Propertize heredocs in elixir-ts-mode

* lisp/progmodes/elixir-ts-mode.el
(elixir-ts--syntax-propertize-query): New variable.
(elixir-ts--syntax-propertize): New helper.
(elixir-ts-mode): Set syntax-propertize-function.
---
 lisp/progmodes/elixir-ts-mode.el | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/elixir-ts-mode.el b/lisp/progmodes/elixir-ts-mode.el
index 628d2000bd7..1985289d386 100644
--- a/lisp/progmodes/elixir-ts-mode.el
+++ b/lisp/progmodes/elixir-ts-mode.el
@@ -55,7 +55,9 @@
 (declare-function treesit-parser-list "treesit.c")
 (declare-function treesit-node-parent "treesit.c")
 (declare-function treesit-node-start "treesit.c")
+(declare-function treesit-node-end "treesit.c")
 (declare-function treesit-query-compile "treesit.c")
+(declare-function treesit-query-capture "treesit.c")
 (declare-function treesit-node-eq "treesit.c")
 (declare-function treesit-node-prev-sibling "treesit.c")
 
@@ -544,6 +546,22 @@ elixir-ts--defun-name
                 (_ nil))))
     (_ nil)))
 
+(defvar elixir-ts--syntax-propertize-query
+  (when (treesit-available-p)
+    (treesit-query-compile
+     'elixir
+     '(((["\"\"\""] @quoted-text))))))
+
+(defun elixir-ts--syntax-propertize (start end)
+  "Apply syntax text properties between START and END for `elixir-ts-mode'."
+  (let ((captures
+         (treesit-query-capture 'elixir elixir-ts--syntax-propertize-query start end)))
+    (pcase-dolist (`(,name . ,node) captures)
+      (pcase-exhaustive name
+        ('quoted-text
+         (put-text-property (1- (treesit-node-end node)) (treesit-node-end node)
+                            'syntax-table (string-to-syntax "$")))))))
+
 ;;;###autoload
 (define-derived-mode elixir-ts-mode prog-mode "Elixir"
   "Major mode for editing Elixir, powered by tree-sitter."
@@ -624,7 +642,8 @@ elixir-ts-mode
                     ( elixir-sigil elixir-string-escape
                       elixir-string-interpolation ))))
 
-    (treesit-major-mode-setup)))
+    (treesit-major-mode-setup)
+    (setq-local syntax-propertize-function #'elixir-ts--syntax-propertize)))
 
 (if (treesit-ready-p 'elixir)
     (progn
-- 
2.40.0

>From 19704012ac83530e1a06531905411d0972e88a44 Mon Sep 17 00:00:00 2001
From: Wilhelm H Kirschbaum <[email-masked]>
Date: Sun, 2 Apr 2023 09:43:20 +0200
Subject: [PATCH 5/5] Handle electric heredocs pairs in elixir-ts-mode

* lisp/progmodes/elixir-ts-mode.el
(elixir-ts--electric-pair-string-delimiter): New helper.
(elixir-ts-mode): Add post-self-insert-hook.
---
 lisp/progmodes/elixir-ts-mode.el | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/lisp/progmodes/elixir-ts-mode.el b/lisp/progmodes/elixir-ts-mode.el
index e2d9515c10f..576afd8104f 100644
--- a/lisp/progmodes/elixir-ts-mode.el
+++ b/lisp/progmodes/elixir-ts-mode.el
@@ -568,13 +568,25 @@ elixir-ts--syntax-propertize
          (put-text-property (1- (treesit-node-end node)) (treesit-node-end node)
                             'syntax-table (string-to-syntax "$")))))))
 
+(defun elixir-ts--electric-pair-string-delimiter ()
+  "Insert corresponding multi-line string for `electric-pair-mode'."
+  (when (and electric-pair-mode
+             (eq last-command-event ?\")
+             (let ((count 0))
+               (while (eq (char-before (- (point) count)) last-command-event)
+                 (cl-incf count))
+               (= count 3))
+             (eq (char-after) last-command-event))
+    (save-excursion
+      (insert (make-string 2 last-command-event)))))
+
 ;;;###autoload
 (define-derived-mode elixir-ts-mode prog-mode "Elixir"
   "Major mode for editing Elixir, powered by tree-sitter."
   :group 'elixir-ts
   :syntax-table elixir-ts--syntax-table
 
-  ;; Comments
+  ;; Comments.
   (setq-local comment-start "# ")
   (setq-local comment-start-skip
               (rx "#" (* (syntax whitespace))))
@@ -584,9 +596,13 @@ elixir-ts-mode
               (rx (* (syntax whitespace))
                   (group (or (syntax comment-end) "\n"))))
 
-  ;; Compile
+  ;; Compile.
   (setq-local compile-command "mix")
 
+  ;; Electric pair.
+  (add-hook 'post-self-insert-hook
+            #'elixir-ts--electric-pair-string-delimiter 'append t)
+
   (when (treesit-ready-p 'elixir)
     ;; The HEEx parser has to be created first for elixir to ensure elixir
     ;; is the first language when looking for treesit ranges.
@@ -617,14 +633,14 @@ elixir-ts-mode
     ;; Indent.
     (setq-local treesit-simple-indent-rules elixir-ts--indent-rules)
 
-    ;; Navigation
+    ;; Navigation.
     (setq-local forward-sexp-function #'elixir-ts--forward-sexp)
     (setq-local treesit-defun-type-regexp
                 '("call" . elixir-ts--defun-p))
 
     (setq-local treesit-defun-name-function #'elixir-ts--defun-name)
 
-    ;; Embedded Heex
+    ;; Embedded Heex.
     (when (treesit-ready-p 'heex)
       (setq-local treesit-range-settings elixir-ts--treesit-range-rules)
 
-- 
2.40.0