all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Wilhelm Kirschbaum <wkirschbaum@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 62536@debbugs.gnu.org, "牟 桐" <mou.tong@outlook.com>
Subject: bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python
Date: Sun, 02 Apr 2023 09:49:20 +0200	[thread overview]
Message-ID: <87lejay8n3.fsf@gmail.com> (raw)
In-Reply-To: <87edp6oim6.fsf@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1735 bytes --]


Wilhelm Kirschbaum <wkirschbaum@gmail.com> writes:

>>> From: 牟 桐 <mou.tong@outlook.com>
>>> 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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Propertize heredocs --]
[-- Type: text/x-patch, Size: 2372 bytes --]

From 5005439e007aa3f766b323f9a07cb9e5039820e8 Mon Sep 17 00:00:00 2001
From: Wilhelm H Kirschbaum <wkirschbaum@gmail.com>
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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: Handle electric heredocs --]
[-- Type: text/x-patch, Size: 2811 bytes --]

From 19704012ac83530e1a06531905411d0972e88a44 Mon Sep 17 00:00:00 2001
From: Wilhelm H Kirschbaum <wkirschbaum@gmail.com>
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


  parent reply	other threads:[~2023-04-02  7:49 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-30  4:25 bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 牟 桐
2023-03-30  5:44 ` Eli Zaretskii
2023-03-30  6:19   ` Wilhelm Kirschbaum
2023-03-30  8:25     ` bug#62536: 回复: " Mou Tong
2023-04-02  7:49     ` Wilhelm Kirschbaum [this message]
2023-04-02 16:38       ` Mou Tong
2023-04-02 17:21         ` Wilhelm Kirschbaum
2023-04-02 19:24           ` Wilhelm Kirschbaum
2023-04-03  2:41             ` Mou Tong
2023-04-03  8:26               ` Wilhelm Kirschbaum
2023-04-03  9:42                 ` bug#62536: 回复: " Mou Tong
2023-04-03 10:38                   ` Wilhelm Kirschbaum
2023-04-03 12:02                 ` João Távora
2023-04-03 12:08                   ` Wilhelm Kirschbaum
2023-04-03 14:02                 ` Eli Zaretskii
2023-04-03 14:17                   ` Wilhelm Kirschbaum
2023-04-03 14:38                     ` Eli Zaretskii
2023-04-04  5:39                       ` Wilhelm Kirschbaum
2023-04-04  9:08                         ` João Távora
2023-04-04 17:54                           ` Wilhelm Kirschbaum
2023-04-04 18:39                             ` João Távora
2023-04-04 19:03                               ` Wilhelm Kirschbaum
2023-04-04 19:46                                 ` Wilhelm Kirschbaum
2023-04-04 20:29                                 ` Wilhelm Kirschbaum
2023-04-05 13:09                                   ` João Távora
2023-04-05 14:33                                   ` João Távora
2023-04-05 17:28                                     ` Wilhelm Kirschbaum
2023-04-06  0:17                                       ` João Távora
2023-04-06  5:39                                         ` Wilhelm Kirschbaum
2023-04-06 10:07                         ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lejay8n3.fsf@gmail.com \
    --to=wkirschbaum@gmail.com \
    --cc=62536@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=mou.tong@outlook.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.