unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Wilhelm Kirschbaum <wkirschbaum@gmail.com>
To: "João Távora" <joaotavora@gmail.com>
Cc: Eli Zaretskii <eliz@gnu.org>,
	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: Wed, 05 Apr 2023 19:28:06 +0200	[thread overview]
Message-ID: <87y1n6b56c.fsf@gmail.com> (raw)
In-Reply-To: <875yaa2yce.fsf@gmail.com>



> 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)))






  reply	other threads:[~2023-04-05 17:28 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
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 [this message]
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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87y1n6b56c.fsf@gmail.com \
    --to=wkirschbaum@gmail.com \
    --cc=62536@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=joaotavora@gmail.com \
    --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 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).