* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python @ 2023-03-30 4:25 牟 桐 2023-03-30 5:44 ` Eli Zaretskii 0 siblings, 1 reply; 30+ messages in thread From: 牟 桐 @ 2023-03-30 4:25 UTC (permalink / raw) To: 62536 [-- Attachment #1: Type: text/plain, Size: 776 bytes --] Hi! 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 [-- Attachment #2: Type: text/html, Size: 2707 bytes --] ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 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 0 siblings, 1 reply; 30+ messages in thread From: Eli Zaretskii @ 2023-03-30 5:44 UTC (permalink / raw) To: 牟 桐, Wilhelm H Kirschbaum; +Cc: 62536 > 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 Wilhelm, WDYT? ^ permalink raw reply [flat|nested] 30+ messages in thread
* 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 0 siblings, 2 replies; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-03-30 6:19 UTC (permalink / raw) To: Eli Zaretskii; +Cc: 62536, 牟 桐 >> 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 > > Wilhelm, WDYT? Yes, I think we should add some version of this. I am currently testing a syntax-propertize-function addition to solve an issue related to electric-pair-mode and will have a look at this straight after. There was another request to automatically insert `end` when `do` is entered, which might be a good addition as well. ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 回复: bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-03-30 6:19 ` Wilhelm Kirschbaum @ 2023-03-30 8:25 ` Mou Tong 2023-04-02 7:49 ` Wilhelm Kirschbaum 1 sibling, 0 replies; 30+ messages in thread From: Mou Tong @ 2023-03-30 8:25 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: 62536@debbugs.gnu.org [-- Attachment #1: Type: text/plain, Size: 2138 bytes --] > automatically insert `end` when `do` is entered, which might be a good > addition as well. Eglot can handle this and it works pretty good for now: Open a ex file, enable `eglot`, insert `do` and press `tab`, eglot will help you insert `end` automatically. I think it's better to let user trigger `do`'s completion. Because when someone want to write a one line function, the `end` is not needed: ```elixir-ts def foo(a), do: a ``` Anyway, hope to see the new feature ;) ________________________________ 发件人: Wilhelm Kirschbaum <wkirschbaum@gmail.com> 发送时间: 2023年3月30日 14:19 收件人: Eli Zaretskii <eliz@gnu.org> 抄送: 牟 桐 <mou.tong@outlook.com>; 62536@debbugs.gnu.org <62536@debbugs.gnu.org> 主题: Re: bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python >> 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 > > Wilhelm, WDYT? Yes, I think we should add some version of this. I am currently testing a syntax-propertize-function addition to solve an issue related to electric-pair-mode and will have a look at this straight after. There was another request to automatically insert `end` when `do` is entered, which might be a good addition as well. [-- Attachment #2: Type: text/html, Size: 4635 bytes --] ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-03-30 6:19 ` Wilhelm Kirschbaum 2023-03-30 8:25 ` bug#62536: 回复: " Mou Tong @ 2023-04-02 7:49 ` Wilhelm Kirschbaum 2023-04-02 16:38 ` Mou Tong 1 sibling, 1 reply; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-02 7:49 UTC (permalink / raw) To: Eli Zaretskii; +Cc: 62536, 牟 桐 [-- 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 ^ permalink raw reply related [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-02 7:49 ` Wilhelm Kirschbaum @ 2023-04-02 16:38 ` Mou Tong 2023-04-02 17:21 ` Wilhelm Kirschbaum 0 siblings, 1 reply; 30+ messages in thread From: Mou Tong @ 2023-04-02 16:38 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: 62536@debbugs.gnu.org > 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? I agree with you, but I think it might be better to imitate what `c-mode` did: ```c int main () {|} ``` When `electric-pair-mode` is on, input the left `{`, Emacs helps you complete the right `}`. After press enter, your cursor will move to the next line and auto-indented according to your offset config: ```c int main () { | } ``` So how about making it work like this? ```elixir-ts @doc """|""" # after press enter @doc """ | """ ``` ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-02 16:38 ` Mou Tong @ 2023-04-02 17:21 ` Wilhelm Kirschbaum 2023-04-02 19:24 ` Wilhelm Kirschbaum 0 siblings, 1 reply; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-02 17:21 UTC (permalink / raw) To: Mou Tong; +Cc: 62536@debbugs.gnu.org Mou Tong <mou.tong@outlook.com> writes: >> 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? > > I agree with you, but I think it might be better to imitate what > `c-mode` did: > > ```c > int main () {|} > ``` > > When `electric-pair-mode` is on, input the left `{`, Emacs helps > you > complete the right `}`. After press enter, your cursor will move > to the > next line and auto-indented according to your offset config: > > ```c > int main () { > | > } > ``` > > So how about making it work like this? > > ```elixir-ts > @doc """|""" > > # after press enter > @doc """ > | > """ > ``` Yes, I think that is better, let me have a go at it. I was concerned of doing unsolicited movement with my suggestion of inserting newlines. ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-02 17:21 ` Wilhelm Kirschbaum @ 2023-04-02 19:24 ` Wilhelm Kirschbaum 2023-04-03 2:41 ` Mou Tong 0 siblings, 1 reply; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-02 19:24 UTC (permalink / raw) To: Mou Tong; +Cc: 62536@debbugs.gnu.org > Mou Tong <mou.tong@outlook.com> writes: > >>> 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? >> >> I agree with you, but I think it might be better to imitate >> what >> `c-mode` did: >> >> ```c >> int main () {|} >> ``` >> >> When `electric-pair-mode` is on, input the left `{`, Emacs >> helps you >> complete the right `}`. After press enter, your cursor will >> move to >> the >> next line and auto-indented according to your offset config: >> >> ```c >> int main () { >> | >> } >> ``` >> >> So how about making it work like this? >> >> ```elixir-ts >> @doc """|""" >> >> # after press enter >> @doc """ >> | >> """ >> ``` > > Yes, I think that is better, let me have a go at it. I was > concerned > of doing unsolicited movement with my suggestion of inserting > newlines. Playing around with it I think we should add a newline, I changed my mind. Unlike the c example above, the following syntax is not valid elixir: `""""""` and it requires a newline to make it valid. Part of the appeal of making the heredoc electric is to keep the syntax valid. 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. This will mean: ``` @doc ""| ``` when typing `"` it will insert `\n"""` and thus have the minimal valid elixir syntax without doing extra movement. ``` @doc """| """ ``` This will be the change ( looking at the electric.el code ). ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-02 19:24 ` Wilhelm Kirschbaum @ 2023-04-03 2:41 ` Mou Tong 2023-04-03 8:26 ` Wilhelm Kirschbaum 0 siblings, 1 reply; 30+ messages in thread From: Mou Tong @ 2023-04-03 2:41 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: 62536@debbugs.gnu.org [-- Attachment #1: Type: text/plain, Size: 437 bytes --] > 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.) [-- Attachment #2: Type: text/html, Size: 936 bytes --] ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-03 2:41 ` Mou Tong @ 2023-04-03 8:26 ` Wilhelm Kirschbaum 2023-04-03 9:42 ` bug#62536: 回复: " Mou Tong ` (2 more replies) 0 siblings, 3 replies; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-03 8:26 UTC (permalink / raw) To: Mou Tong; +Cc: 62536@debbugs.gnu.org [-- Attachment #1: Type: text/plain, Size: 591 bytes --] Mou Tong <mou.tong@outlook.com> 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: [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #2: Propertize heredocs --] [-- Type: text/x-patch, Size: 2372 bytes --] From 53a1caf7db5627b9d470b6f652d21812c1fac4db 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 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 [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #3: Handle electric pairs for heredocs in elixir-ts-mode --] [-- Type: text/x-patch, Size: 2856 bytes --] From 467876e261ce6f0abfe3aa29b762fae795df65d0 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 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 ^ permalink raw reply related [flat|nested] 30+ messages in thread
* bug#62536: 回复: bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-03 8:26 ` Wilhelm Kirschbaum @ 2023-04-03 9:42 ` Mou Tong 2023-04-03 10:38 ` Wilhelm Kirschbaum 2023-04-03 12:02 ` João Távora 2023-04-03 14:02 ` Eli Zaretskii 2 siblings, 1 reply; 30+ messages in thread From: Mou Tong @ 2023-04-03 9:42 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: 62536@debbugs.gnu.org [-- Attachment #1: Type: text/plain, Size: 494 bytes --] I tested your new patches and it worked perfectly for me. When can we merge it so I can remove the hack I added in my configuration? 😄 ps: We can add `elixir-ts-mode` to `auto-mode-alist` by: ```elisp (add-to-list 'auto-mode-alist '("\\(\\.elixir\\|\\.exs?\\|mix\\.lock\\)\\'" . elixir-ts-mode)) ``` It might be redundant to write `add-to-list` 4 times, and other major-modes are added to `auto-mode-alist` by writing `add-to-list` just once. [-- Attachment #2: Type: text/html, Size: 1175 bytes --] ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 回复: bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-03 9:42 ` bug#62536: 回复: " Mou Tong @ 2023-04-03 10:38 ` Wilhelm Kirschbaum 0 siblings, 0 replies; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-03 10:38 UTC (permalink / raw) To: Mou Tong; +Cc: 62536, eliz > I tested your new patches and it worked perfectly for me. When > can we > merge it so I can remove the hack I added in my configuration? 😄 > Great, happy that worked. Now we wait for Eli or someone to apply the path on master if all is in order :). > ps: > > We can add `elixir-ts-mode` to `auto-mode-alist` by: > > ```elisp > (add-to-list 'auto-mode-alist > '("\\(\\.elixir\\|\\.exs?\\|mix\\.lock\\)\\'" > . elixir-ts-mode)) > ``` > > It might be redundant to write `add-to-list` 4 times, and other > major-modes are added to `auto-mode-alist` by writing > `add-to-list` > just once. It makes sense, but don't want to just change this for the sake of changing if it is working as is. ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-03 8:26 ` Wilhelm Kirschbaum 2023-04-03 9:42 ` bug#62536: 回复: " Mou Tong @ 2023-04-03 12:02 ` João Távora 2023-04-03 12:08 ` Wilhelm Kirschbaum 2023-04-03 14:02 ` Eli Zaretskii 2 siblings, 1 reply; 30+ messages in thread From: João Távora @ 2023-04-03 12:02 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: 62536@debbugs.gnu.org, Stefan Monnier, Mou Tong As the author of most of elec-pair.el, I wanted to chime in to say that it can be worth checking if this newline-inserting, indent-inserting functionality isn't already covered by one of electric-pair-open-newline-between-pairs electric-indent-mode electric-layout-mode and its electric-layout-rules which would be generally preferable to adding custom psif's to post-self-insert-hook. CCint the expert Stefan here. João ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-03 12:02 ` João Távora @ 2023-04-03 12:08 ` Wilhelm Kirschbaum 0 siblings, 0 replies; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-03 12:08 UTC (permalink / raw) To: João Távora; +Cc: 62536@debbugs.gnu.org, Stefan Monnier, Mou Tong João Távora <joaotavora@gmail.com> writes: > As the author of most of elec-pair.el, I wanted to > chime in to say that it can be worth checking if this > newline-inserting, indent-inserting functionality isn't > already covered by one of > > electric-pair-open-newline-between-pairs > electric-indent-mode > electric-layout-mode and its electric-layout-rules > > which would be generally preferable to adding custom psif's > to post-self-insert-hook. Hi, thanks for chiming in. I will have a closer look to see if there is a more elegant solution and hopefully we can avoid adding a custom psif. ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-03 8:26 ` Wilhelm Kirschbaum 2023-04-03 9:42 ` bug#62536: 回复: " Mou Tong 2023-04-03 12:02 ` João Távora @ 2023-04-03 14:02 ` Eli Zaretskii 2023-04-03 14:17 ` Wilhelm Kirschbaum 2 siblings, 1 reply; 30+ messages in thread From: Eli Zaretskii @ 2023-04-03 14:02 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: 62536, mou.tong > Cc: "62536@debbugs.gnu.org" <62536@debbugs.gnu.org> > From: Wilhelm Kirschbaum <wkirschbaum@gmail.com> > Date: Mon, 03 Apr 2023 10:26:59 +0200 > > These two patches should then be sufficient to better handle > heredocs for electric-pair-mode: Do these replace the two previous ones you posted? Or are they in addition to them? ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-03 14:02 ` Eli Zaretskii @ 2023-04-03 14:17 ` Wilhelm Kirschbaum 2023-04-03 14:38 ` Eli Zaretskii 0 siblings, 1 reply; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-03 14:17 UTC (permalink / raw) To: Eli Zaretskii; +Cc: 62536, 牟 桐 [-- Attachment #1: Type: text/plain, Size: 456 bytes --] On Mon, 3 Apr 2023, 16:02 Eli Zaretskii, <eliz@gnu.org> wrote: > > Cc: "62536@debbugs.gnu.org" <62536@debbugs.gnu.org> > > From: Wilhelm Kirschbaum <wkirschbaum@gmail.com> > > Date: Mon, 03 Apr 2023 10:26:59 +0200 > > > > These two patches should then be sufficient to better handle > > heredocs for electric-pair-mode: > > Do these replace the two previous ones you posted? Or are they in > addition to them? > Sorry, they replace the previous two. > [-- Attachment #2: Type: text/html, Size: 1234 bytes --] ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-03 14:17 ` Wilhelm Kirschbaum @ 2023-04-03 14:38 ` Eli Zaretskii 2023-04-04 5:39 ` Wilhelm Kirschbaum 0 siblings, 1 reply; 30+ messages in thread From: Eli Zaretskii @ 2023-04-03 14:38 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: 62536, mou.tong > From: Wilhelm Kirschbaum <wkirschbaum@gmail.com> > Date: Mon, 3 Apr 2023 16:17:51 +0200 > Cc: 牟 桐 <mou.tong@outlook.com>, 62536@debbugs.gnu.org > > On Mon, 3 Apr 2023, 16:02 Eli Zaretskii, <eliz@gnu.org> wrote: > > > Cc: "62536@debbugs.gnu.org" <62536@debbugs.gnu.org> > > From: Wilhelm Kirschbaum <wkirschbaum@gmail.com> > > Date: Mon, 03 Apr 2023 10:26:59 +0200 > > > > These two patches should then be sufficient to better handle > > heredocs for electric-pair-mode: > > Do these replace the two previous ones you posted? Or are they in > addition to them? > > Sorry, they replace the previous two. OK, but I understand you are still considering a better way to solve this? So I should not yet install the last patches? ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 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-06 10:07 ` Eli Zaretskii 0 siblings, 2 replies; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-04 5:39 UTC (permalink / raw) To: Eli Zaretskii; +Cc: 62536, mou.tong >> From: Wilhelm Kirschbaum <wkirschbaum@gmail.com> >> Date: Mon, 3 Apr 2023 16:17:51 +0200 >> Cc: 牟 桐 <mou.tong@outlook.com>, 62536@debbugs.gnu.org >> >> On Mon, 3 Apr 2023, 16:02 Eli Zaretskii, <eliz@gnu.org> wrote: >> >> > Cc: "62536@debbugs.gnu.org" <62536@debbugs.gnu.org> >> > From: Wilhelm Kirschbaum <wkirschbaum@gmail.com> >> > Date: Mon, 03 Apr 2023 10:26:59 +0200 >> > >> > These two patches should then be sufficient to better handle >> > heredocs for electric-pair-mode: >> >> Do these replace the two previous ones you posted? Or are >> they in >> addition to them? >> >> Sorry, they replace the previous two. > > OK, but I understand you are still considering a better way to > solve > this? So I should not yet install the last patches? I had a look and don't see how without changing elec-pair.el that there is a more elegant way to do this. I am happy with the current patches to be installed and then perhaps if there is a better way we can apply it to both python-mode and elixir-ts-mode later. ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 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-06 10:07 ` Eli Zaretskii 1 sibling, 1 reply; 30+ messages in thread From: João Távora @ 2023-04-04 9:08 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: Eli Zaretskii, 62536, mou.tong On Tue, Apr 4, 2023 at 6:43 AM Wilhelm Kirschbaum <wkirschbaum@gmail.com> wrote: > > >> From: Wilhelm Kirschbaum <wkirschbaum@gmail.com> > >> Date: Mon, 3 Apr 2023 16:17:51 +0200 > >> Cc: 牟 桐 <mou.tong@outlook.com>, 62536@debbugs.gnu.org > >> > >> On Mon, 3 Apr 2023, 16:02 Eli Zaretskii, <eliz@gnu.org> wrote: > >> > >> > Cc: "62536@debbugs.gnu.org" <62536@debbugs.gnu.org> > >> > From: Wilhelm Kirschbaum <wkirschbaum@gmail.com> > >> > Date: Mon, 03 Apr 2023 10:26:59 +0200 > >> > > >> > These two patches should then be sufficient to better handle > >> > heredocs for electric-pair-mode: > >> > >> Do these replace the two previous ones you posted? Or are > >> they in > >> addition to them? > >> > >> Sorry, they replace the previous two. > > > > OK, but I understand you are still considering a better way to > > solve > > this? So I should not yet install the last patches? > > I had a look and don't see how without changing elec-pair.el that > there > is a more elegant way to do this. I am happy with the current > patches > to be installed and then perhaps if there is a better way we can > apply > it to both python-mode and elixir-ts-mode later. It seems what you are trying to do is actually a functionality of electric-layout-mode, right? It's creating newlines between two sets of '"""' so that if the user types one newline between them, then one extra newline is inserted after and the line is indented one level deeper than the '"""'. If so, it would seem like 'electric-layout-rules' would be a good match. The behaviour would happen _only_ if the user has electric-layout-mode, which is the standard Emacs mode for controlling this kind of electricity. The last paragraph of the docstring of 'electric-layout-rules' seems to describe a powerful enough mechanism to do achieve what the custom psif does. João ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 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 0 siblings, 1 reply; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-04 17:54 UTC (permalink / raw) To: João Távora; +Cc: Eli Zaretskii, 62536, mou.tong Hi João, >> >> I had a look and don't see how without changing elec-pair.el >> that >> there >> is a more elegant way to do this. I am happy with the current >> patches >> to be installed and then perhaps if there is a better way we >> can >> apply >> it to both python-mode and elixir-ts-mode later. > > It seems what you are trying to do is actually a functionality > of electric-layout-mode, right? It's creating newlines between > two sets of '"""' so that if the user types one newline between > them, then one extra newline is inserted after and the line is > indented one level deeper than the '"""'. > > If so, it would seem like 'electric-layout-rules' would be a > good > match. The behaviour would happen _only_ if the user has > electric-layout-mode, which is the standard Emacs mode for > controlling this kind of electricity. The last paragraph > of the docstring of 'electric-layout-rules' seems to describe > a powerful enough mechanism to do achieve what the custom > psif does. > > João The custom psif is not for the newline, but for pairing up the triple quotes `"""` with another set of tripe quotes when electric-pair-mode is enabled. The newline is for keeping the syntax valid in elixir, otherwise the font and navigation breaks because `""""""` is not valid Elixir syntax and the tree-sitter grammar really does not like it. Unless electric-indent-chars can identify the triple quotes, we need a custom psif regardless of other functionality, but I don't see this option. Maybe I am wrong and you can point me to this feature? If it does not make sense to add the newline as this might not be expected behaviour when electric-layout-mode is not enabled, we can add a check before inserting the newline? I don't know if many people using elixir-ts-mode will know about the electric layout mode and might get annoyed with electric-pair-mode for heredocs ( tripple quote pairs ). ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-04 17:54 ` Wilhelm Kirschbaum @ 2023-04-04 18:39 ` João Távora 2023-04-04 19:03 ` Wilhelm Kirschbaum 0 siblings, 1 reply; 30+ messages in thread From: João Távora @ 2023-04-04 18:39 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: Eli Zaretskii, 62536, mou.tong On Tue, Apr 4, 2023 at 7:12 PM Wilhelm Kirschbaum <wkirschbaum@gmail.com> wrote: > The custom psif is not for the newline, but for pairing up the > triple > quotes `"""` with another set of tripe quotes when > electric-pair-mode is > enabled. The newline is for keeping the syntax valid in elixir, > otherwise the font and navigation breaks because `""""""` is not > valid > Elixir syntax and the tree-sitter grammar really does not like it. I see. My apologies for not having noticed this in the preceding emails. If you want some kind of electricity so that typing the third `"` results in two `"` and a newline getting added, then that is completely new and probably does require a custom psif. If that's _not_ what is meant, read on. Else skip to last paragraph. > Unless electric-indent-chars can identify the triple quotes, we > need a > custom psif regardless of other functionality, but I don't see > this > option. Maybe I am wrong and you can point me to this feature? Here, you'd not want electric-indent-chars, right? If you're dealing with the necessity of electrically adding a newline to maintain syntax correctness, you're looking for electric-layout-mode and electric-layout-rules, not electric-indent. And in electric-layout-rules you _can_, I think, identify triple quotes using a function as described in the last paragraph of its docstring. > If it does not make sense to add the newline as this might not be > expected behaviour when electric-layout-mode is not enabled, we > can add > a check before inserting the newline? Sure, but I'd say just use electric-layout-rules in that case. > I don't know if many people > using > elixir-ts-mode will know about the electric layout mode and might > get annoyed with > electric-pair-mode for heredocs ( tripple quote pairs ). But your suggestion to check for electric-layout-mode in the custom psif leads to same situation, right? You can choose to turn on electric-layout-mode by default locally in elixir-ts-mode if you want to give your users this sane behaviour by default. Like other electric-* modes, it's a mode that helps maintain the buffer valid (for some meaning of "valid"). Just like without electric-pair-mode you type a '{' and the buffer is syntactically invalid until you fix it. The "annoyance" can be solved by turning it on, unless it's on already. Anyway, I just wanted to call attention to these existing facilities in Emacs that were designed to help out major-mode authors so that ad-hoc solutions aren't replicated again and again, making maintenance difficult. But, then again, I understand that the facilities might not be as powerful as I and Stefan (mostly Stefan, really) intended. Here, my naive understanding of the matter tells me they are, but if I'm wrong and using the custom psif gets the job done, then don't take my suggestion as a blocker, especially if all your patches are ready and good to go. We can always come back to this later. João ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 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 0 siblings, 2 replies; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-04 19:03 UTC (permalink / raw) To: João Távora; +Cc: Eli Zaretskii, 62536, mou.tong > On Tue, Apr 4, 2023 at 7:12 PM Wilhelm Kirschbaum > <wkirschbaum@gmail.com> wrote: > >> The custom psif is not for the newline, but for pairing up the >> triple >> quotes `"""` with another set of tripe quotes when >> electric-pair-mode is >> enabled. The newline is for keeping the syntax valid in >> elixir, >> otherwise the font and navigation breaks because `""""""` is >> not >> valid >> Elixir syntax and the tree-sitter grammar really does not like >> it. > > I see. My apologies for not having noticed this in the > preceding > emails. If you want some kind of electricity so that typing the > third `"` results in two `"` and a newline getting added, then > that is completely new and probably does require a custom psif. > > If that's _not_ what is meant, read on. Else skip to last > paragraph. > >> Unless electric-indent-chars can identify the triple quotes, we >> need a >> custom psif regardless of other functionality, but I don't see >> this >> option. Maybe I am wrong and you can point me to this feature? > > Here, you'd not want electric-indent-chars, right? If you're > dealing with the necessity of electrically adding a newline > to maintain syntax correctness, you're looking for > electric-layout-mode and electric-layout-rules, not > electric-indent. > Right, makes sense. > And in electric-layout-rules you _can_, I think, identify > triple quotes using a function as described in the last > paragraph of its docstring. > Yes, I tested this earlier and it does work. >> If it does not make sense to add the newline as this might not >> be >> expected behaviour when electric-layout-mode is not enabled, we >> can add >> a check before inserting the newline? > > Sure, but I'd say just use electric-layout-rules in that case. > You convinced me :) > You can choose to turn on electric-layout-mode by default > locally in elixir-ts-mode if you want to give your users this > sane > behaviour by default. > Is that a good idea? I have not used electric-pair-mode or electric-layout-mode enough to know how this impacts the workflow. > Like other electric-* modes, it's a mode that helps maintain > the buffer valid (for some meaning of "valid"). Just like > without > electric-pair-mode you type a '{' and the buffer is > syntactically > invalid until you fix it. The "annoyance" can be solved by > turning it on, unless it's on already. > > Anyway, I just wanted to call attention to these existing > facilities in Emacs that were designed to help out major-mode > authors so that ad-hoc solutions aren't replicated again > and again, making maintenance difficult. But, then again, > I understand that the facilities might not be as powerful > as I and Stefan (mostly Stefan, really) intended. Here, my > naive understanding of the matter tells me they are, but > if I'm wrong and using the custom psif gets the job done, > then don't take my suggestion as a blocker, especially if all > your patches are ready and good to go. We can always come > back to this later. > > João Thanks for doing so, I am relatively new to hacking on Emacs, so appreciate the feedback. Is there perhaps a way we can allow a similar function for electric-pair as with electric-layout to identify more than just the last typed char? This will mean that we can entirely get rid of custom psifs and think it will be useful in at least 1 more mode. ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-04 19:03 ` Wilhelm Kirschbaum @ 2023-04-04 19:46 ` Wilhelm Kirschbaum 2023-04-04 20:29 ` Wilhelm Kirschbaum 1 sibling, 0 replies; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-04 19:46 UTC (permalink / raw) To: João Távora; +Cc: Eli Zaretskii, 62536, mou.tong >> On Tue, Apr 4, 2023 at 7:12 PM Wilhelm Kirschbaum >> <wkirschbaum@gmail.com> wrote: >> >>> The custom psif is not for the newline, but for pairing up the >>> triple >>> quotes `"""` with another set of tripe quotes when >>> electric-pair-mode is >>> enabled. The newline is for keeping the syntax valid in >>> elixir, >>> otherwise the font and navigation breaks because `""""""` is >>> not >>> valid >>> Elixir syntax and the tree-sitter grammar really does not like >>> it. >> >> I see. My apologies for not having noticed this in the >> preceding >> emails. If you want some kind of electricity so that typing >> the >> third `"` results in two `"` and a newline getting added, then >> that is completely new and probably does require a custom psif. >> Just to clarify on the desired behaviour: With `|` is (point): When typing the first quote `"|` it pairs to be `"|"` as single quote strings are valid in Elixir. Then when typing the second `"` it skips the second quote `""|`, now when typing the third quote `""|"` it pairs the heredoc: `"""|"""` and with electric-layout it will now insert the newlines: `"""\n|\n"""`. This means, as the original report, the user can type `@doc """|` and will correctly jump into the heredoc and tree-sitter grammar will stay valid. @doc """ | """ There is no scenario in Elixir I can thin of where this is not the desired behaviour. ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 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 1 sibling, 2 replies; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-04 20:29 UTC (permalink / raw) To: João Távora; +Cc: Eli Zaretskii, 62536, mou.tong >> And in electric-layout-rules you _can_, I think, identify >> triple quotes using a function as described in the last >> paragraph of its docstring. >> > Yes, I tested this earlier and it does work. > Even though it works, it does not play nice with the custom psif, because the layout rules seems to trigger before the hook. ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 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 1 sibling, 0 replies; 30+ messages in thread From: João Távora @ 2023-04-05 13:09 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: Eli Zaretskii, 62536, mou.tong On Tue, Apr 4, 2023 at 9:34 PM Wilhelm Kirschbaum <wkirschbaum@gmail.com> wrote: > > > >> And in electric-layout-rules you _can_, I think, identify > >> triple quotes using a function as described in the last > >> paragraph of its docstring. > >> > > Yes, I tested this earlier and it does work. > > > > Even though it works, it does not play nice with the custom psif, > because the layout rules seems to trigger before the hook. You should be able to adjust the order of the functions in post-self-insert-hook. The core psifs of electric-pair-mode, electric-indent-mode and electric-layout-mode coordinate their action in this way, as it's very common to reach situations as you describe. Use a numeric third argument to add-hook to coordinate. Assume that the numbers you see in elec-pair.el and electric.el for the core psifs will be relatively unchanged. If you have the latest code you're testing, I can help you test, as I've just installed the tree-sitter grammar for elixir-ts-mode and it seems to be working. João ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 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 1 sibling, 1 reply; 30+ messages in thread From: João Távora @ 2023-04-05 14:33 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: Eli Zaretskii, 62536, mou.tong Wilhelm Kirschbaum <wkirschbaum@gmail.com> writes: >>> And in electric-layout-rules you _can_, I think, identify >>> triple quotes using a function as described in the last >>> paragraph of its docstring. >>> >> Yes, I tested this earlier and it does work. >> > > Even though it works, it does not play nice with the custom psif, > because the layout rules seems to trigger before the hook. There could yet be a snag here, which I didn't anticipate, which is that rules in electric-layout-rules do not trigger if inside strings or comments, and that is hardcoded. Let's assume we got rid of the snag or had some mechanism to override it: diff --git a/lisp/electric.el b/lisp/electric.el index bac3f5a2b3c..cef5326852c 100644 --- a/lisp/electric.el +++ b/lisp/electric.el @@ -409,9 +409,7 @@ electric-layout-post-self-insert-function-1 (goto-char pos) (funcall probe last-command-event)))) (when res (throw 'done res)))))))))) - (when (and rule - ;; Not in a string or comment. - (not (nth 8 (save-excursion (syntax-ppss pos))))) + (when rule (goto-char pos) (when (functionp rule) (setq rule (funcall rule))) (dolist (sym (if (symbolp rule) (list rule) rule)) Then, the following simple patch seems to have good results in my tests. (defun joaot/looking-back-at-exactly-three-quotes-p () (looking-back "\\(\\`\\|[^\"]\\)\"\"\"" (- (point) 4))) (defun joaot/triple-quotes () (when (and (eq ?\" last-command-event) (joaot/looking-back-at-exactly-three-quotes-p)) (save-excursion (insert "\"\"\"" )))) (defun joaot/layout-after-triple-quotes (inserted) (when (and (eq inserted ?\") (joaot/looking-back-at-exactly-three-quotes-p)) '(after-stay after))) (defun joaot/setup-elixir-electricity () (electric-layout-local-mode 1) (electric-pair-local-mode 1) (electric-indent-local-mode 1) (add-hook 'post-self-insert-hook 'joaot/super-quotes-and-newlines -05 t) (setq-local electric-layout-rules (list #'joaot/layout-after-triple-quotes))) (add-hook 'elixir-ts-mode-hook 'joaot/setup-elixir-electricity) João ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 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 0 siblings, 1 reply; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-05 17:28 UTC (permalink / raw) To: João Távora; +Cc: Eli Zaretskii, 62536, mou.tong > Wilhelm Kirschbaum <wkirschbaum@gmail.com> writes: > >>>> And in electric-layout-rules you _can_, I think, identify >>>> triple quotes using a function as described in the last >>>> paragraph of its docstring. >>>> >>> Yes, I tested this earlier and it does work. >>> >> >> Even though it works, it does not play nice with the custom >> psif, >> because the layout rules seems to trigger before the hook. > > There could yet be a snag here, which I didn't anticipate, which > is that > rules in electric-layout-rules do not trigger if inside strings > or > comments, and that is hardcoded. > > Let's assume we got rid of the snag or had some mechanism to > override > it: > > diff --git a/lisp/electric.el b/lisp/electric.el > index bac3f5a2b3c..cef5326852c 100644 > --- a/lisp/electric.el > +++ b/lisp/electric.el > @@ -409,9 +409,7 @@ > electric-layout-post-self-insert-function-1 > (goto-char pos) > (funcall probe > last-command-event)))) > (when res (throw 'done > res)))))))))) > - (when (and rule > - ;; Not in a string or comment. > - (not (nth 8 (save-excursion (syntax-ppss > pos))))) > + (when rule > (goto-char pos) > (when (functionp rule) (setq rule (funcall rule))) > (dolist (sym (if (symbolp rule) (list rule) rule)) > > > Then, the following simple patch seems to have good results in > my tests. > > (defun joaot/looking-back-at-exactly-three-quotes-p () > (looking-back "\\(\\`\\|[^\"]\\)\"\"\"" (- (point) 4))) > > (defun joaot/triple-quotes () > (when (and (eq ?\" last-command-event) > (joaot/looking-back-at-exactly-three-quotes-p)) > (save-excursion (insert "\"\"\"" )))) > > (defun joaot/layout-after-triple-quotes (inserted) > (when (and (eq inserted ?\") > (joaot/looking-back-at-exactly-three-quotes-p)) > '(after-stay after))) > > (defun joaot/setup-elixir-electricity () > (electric-layout-local-mode 1) > (electric-pair-local-mode 1) > (electric-indent-local-mode 1) > (add-hook 'post-self-insert-hook > 'joaot/super-quotes-and-newlines -05 t) > (setq-local electric-layout-rules > (list #'joaot/layout-after-triple-quotes))) > > (add-hook 'elixir-ts-mode-hook > 'joaot/setup-elixir-electricity) > > João Thanks, yes. I think this brings us closer. For some more fun, and the other part of the issue I have not mentioned is this scenario: ```elixir def foo() do ~H""" <foo class= """ end ``` then completing `class=""` jumps into the end of the template , which is pretty annoying, more so that the current issue at hand. The other patch in this thread will porpertize the `"""` as `$` for a lack of a better syntax class, which then breaks it again and produces: ``` """ |" """ ``` the highlight the propertize function change: @ -550,6 +552,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." @@ -630,7 +648,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))) ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-05 17:28 ` Wilhelm Kirschbaum @ 2023-04-06 0:17 ` João Távora 2023-04-06 5:39 ` Wilhelm Kirschbaum 0 siblings, 1 reply; 30+ messages in thread From: João Távora @ 2023-04-06 0:17 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: Eli Zaretskii, 62536, mou.tong Wilhelm Kirschbaum <wkirschbaum@gmail.com> writes: > Thanks, yes. I think this brings us closer. Yes, though keep in mind that needs that patch to lisp/electric.el, or some other mechanism that allows electric-layout-mode to happen in strings or coments. If this mechanism is needed, I don't think it is a lot of work to implement it. But who knows if the text inside the '""" """' should _not_ be considered a string or comment class at all? > For some more fun, and the other part of the issue I have not > mentioned is this scenario: > > ```elixir > def foo() do > ~H""" > <foo class= > """ > end > ``` > > then completing `class=""` jumps into the end of the template , which > is > pretty annoying, more so that the current issue at hand. Ah, I think you want to look at elec-pair's electric-pair-skip-whitespace (which is a user variable, but you major-mode could set it to nil) and the function electric-pair-skip-whitespace-function controlling how whitespace is skipped. > > The other patch in this thread will porpertize the `"""` as > `$` for a lack of a better syntax class, which then breaks it again > and > produces: > > ``` > """ > |" > """ > ``` Maybe that means that the text inside the """ """ no longer is "string or comment". Can you evaluate `(syntax-ppss)` inside that block > (define-derived-mode elixir-ts-mode prog-mode "Elixir" > "Major mode for editing Elixir, powered by tree-sitter." > @@ -630,7 +648,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))) I think at this point it would be a good idea for you to push whatever is the "best" version of your code up to this point to a branch in a Git repository somewhere, so that I can grab it, test it and try to suggest simplification based on that. Alternatively, attach patches (but at least I sometimes get confused when there are multiple patchsets in a thread). João ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-06 0:17 ` João Távora @ 2023-04-06 5:39 ` Wilhelm Kirschbaum 0 siblings, 0 replies; 30+ messages in thread From: Wilhelm Kirschbaum @ 2023-04-06 5:39 UTC (permalink / raw) To: João Távora; +Cc: Eli Zaretskii, 62536, mou.tong > Wilhelm Kirschbaum <wkirschbaum@gmail.com> writes: > >> Thanks, yes. I think this brings us closer. > > Yes, though keep in mind that needs that patch to > lisp/electric.el, or > some other mechanism that allows electric-layout-mode to happen > in > strings or coments. If this mechanism is needed, I don't think > it is a > lot of work to implement it. > > But who knows if the text inside the '""" """' should _not_ be > considered a string or comment class at all? > I will spend some time in the next couple of days to familiar myself a bit more with he electric modes and can hopefully weigh in as well. If we can patch electric.el and make this change simpler it is still preferable imo. >> For some more fun, and the other part of the issue I have not >> mentioned is this scenario: >> >> ```elixir >> def foo() do >> ~H""" >> <foo class= >> """ >> end >> ``` >> >> then completing `class=""` jumps into the end of the template , >> which >> is >> pretty annoying, more so that the current issue at hand. > > Ah, I think you want to look at elec-pair's > electric-pair-skip-whitespace (which is a user variable, but you > major-mode could set it to nil) and the function > electric-pair-skip-whitespace-function controlling how > whitespace is > skipped. > Will try that, thanks. >> >> The other patch in this thread will porpertize the `"""` as >> `$` for a lack of a better syntax class, which then breaks it >> again >> and >> produces: >> >> ``` >> """ >> |" >> """ >> ``` > > Maybe that means that the text inside the """ """ no longer is > "string > or comment". Can you evaluate `(syntax-ppss)` inside that block > (syntax-ppss) here is: (0 nil 1 nil nil nil 0 nil nil nil nil) """ | """ So does not see it as inside a string. The triple quotes can either be a heredoc or a embedded template if prefixed with ~H. > > I think at this point it would be a good idea for you to push > whatever > is the "best" version of your code up to this point to a branch > in a Git > repository somewhere, so that I can grab it, test it and try to > suggest > simplification based on that. Alternatively, attach patches > (but at > least I sometimes get confused when there are multiple patchsets > in a > thread). > > João My current "working" version is here: https://git.sr.ht/~whk/emacs. I rebase on there quite often though. ^ permalink raw reply [flat|nested] 30+ messages in thread
* bug#62536: 30.0.50; Can we add """ ... """ electric pair in elixir, just like python 2023-04-04 5:39 ` Wilhelm Kirschbaum 2023-04-04 9:08 ` João Távora @ 2023-04-06 10:07 ` Eli Zaretskii 1 sibling, 0 replies; 30+ messages in thread From: Eli Zaretskii @ 2023-04-06 10:07 UTC (permalink / raw) To: Wilhelm Kirschbaum; +Cc: 62536-done, mou.tong > From: Wilhelm Kirschbaum <wkirschbaum@gmail.com> > Cc: mou.tong@outlook.com, 62536@debbugs.gnu.org > Date: Tue, 04 Apr 2023 07:39:24 +0200 > > > OK, but I understand you are still considering a better way to > > solve this? So I should not yet install the last patches? > > I had a look and don't see how without changing elec-pair.el that > there is a more elegant way to do this. I am happy with the current > patches to be installed and then perhaps if there is a better way we > can apply it to both python-mode and elixir-ts-mode later. OK, installed on master, and closing the bug. Thanks. ^ permalink raw reply [flat|nested] 30+ messages in thread
end of thread, other threads:[~2023-04-06 10:07 UTC | newest] Thread overview: 30+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 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
Code repositories for project(s) associated with this public inbox https://git.savannah.gnu.org/cgit/emacs.git This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).