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

From: Wilhelm Kirschbaum <[email-masked]>
Date: Mon, 03 Apr 2023 10:26:59 +0200

Mou Tong <[email-masked]> writes:

>> There should never be a reason for the heredoc to be on a 
>> single line 
>> and also makes the tree-sitter grammar invalid, which causes 
>> the
>> blinking issue.
>
> You are right, I didn't consider that.
>
>> ```
>> @doc """|
>> """
>> ```
>>
>> This will be the change ( looking at the electric.el code ).
>
> Cool, hoping to see the new feature soon (so sorry I don't know 
> much
> about elisp and may not be able to help you much with it.)

These two patches should then be sufficient to better handle 
heredocs
for electric-pair-mode:

>From 53a1caf7db5627b9d470b6f652d21812c1fac4db Mon Sep 17 00:00:00 2001
From: Wilhelm H Kirschbaum <[email-masked]>
Date: Tue, 21 Mar 2023 16:34:48 +0200
Subject: [PATCH 1/4] 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 286f3e39f43..0cd938c2fc9 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")
 
@@ -547,6 +549,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."
@@ -627,7 +645,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 467876e261ce6f0abfe3aa29b762fae795df65d0 Mon Sep 17 00:00:00 2001
From: Wilhelm H Kirschbaum <[email-masked]>
Date: Sun, 2 Apr 2023 09:43:20 +0200
Subject: [PATCH 2/4] 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 | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/lisp/progmodes/elixir-ts-mode.el b/lisp/progmodes/elixir-ts-mode.el
index 0cd938c2fc9..9d784aadb26 100644
--- a/lisp/progmodes/elixir-ts-mode.el
+++ b/lisp/progmodes/elixir-ts-mode.el
@@ -565,13 +565,27 @@ 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)))
+    (save-excursion
+      (newline 1 t))))
+
 ;;;###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))))
@@ -581,9 +595,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.
@@ -614,14 +632,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