From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Wilhelm Kirschbaum Newsgroups: gmane.emacs.bugs 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 Message-ID: <87lejay8n3.fsf@gmail.com> References: <83sfdm6bet.fsf@gnu.org> <87edp6oim6.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14985"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.9.3; emacs 30.0.50 Cc: 62536@debbugs.gnu.org, =?UTF-8?Q?=E7=89=9F_?= =?UTF-8?Q?=E6=A1=90?= To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Apr 02 10:47:26 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pitN0-0003ji-1r for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 02 Apr 2023 10:47:26 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pitMq-0002yK-PZ; Sun, 02 Apr 2023 04:47:16 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pitMp-0002yA-2g for bug-gnu-emacs@gnu.org; Sun, 02 Apr 2023 04:47:15 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pitMc-0000NE-8r for bug-gnu-emacs@gnu.org; Sun, 02 Apr 2023 04:47:14 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pitMb-0006dT-NL for bug-gnu-emacs@gnu.org; Sun, 02 Apr 2023 04:47:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Wilhelm Kirschbaum Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 02 Apr 2023 08:47:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 62536 X-GNU-PR-Package: emacs Original-Received: via spool by 62536-submit@debbugs.gnu.org id=B62536.168042516625422 (code B ref 62536); Sun, 02 Apr 2023 08:47:01 +0000 Original-Received: (at 62536) by debbugs.gnu.org; 2 Apr 2023 08:46:06 +0000 Original-Received: from localhost ([127.0.0.1]:39195 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pitLh-0006bx-FG for submit@debbugs.gnu.org; Sun, 02 Apr 2023 04:46:06 -0400 Original-Received: from mail-wr1-f52.google.com ([209.85.221.52]:33732) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pitLe-0006bJ-Mw for 62536@debbugs.gnu.org; Sun, 02 Apr 2023 04:46:03 -0400 Original-Received: by mail-wr1-f52.google.com with SMTP id j24so26414475wrd.0 for <62536@debbugs.gnu.org>; Sun, 02 Apr 2023 01:46:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680425156; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:from:to:cc:subject:date:message-id:reply-to; bh=KjVg7RMr64SAggH/ZKBYlWDwrtSMF1TxRjcrB8XHzl4=; b=LSLdCqCXnOETs+TK/2GiTUFYHoRQ8I9yN3rcreH0q69oyLQUA7qzHf3aUeoUt2wbDk Jzss/m6Lp32XQ42b7VkQKSxfcXyam663m0ZdhgepfIWbfvbgvxoKScQGndO45TI8MS14 4t8bzsG4Qh/wGmf8swnztLS6uA8ASfcdDRJ8VYJjXefnLvgP4lhJia9VW/PmfHLiSvPP 3BX+uJDHwd8fKWyg43HvoRN5Qgv+EvckcmNwXCn5O8rQOmRyCgdrZzoZX9iVi0KGInY7 2hvJzA4pws6Id63wEwwv/r3UpfLhP5HO/m+lAXTtz9fdsFHY/SnDpj/TIflm84k5ANvC bZDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680425156; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=KjVg7RMr64SAggH/ZKBYlWDwrtSMF1TxRjcrB8XHzl4=; b=C0oXnoF5cCoB4Lb3FFGQVN9Z2dkYjFjdG3j5xJC+DgO8c0qATzqTv4uFKJA3HC5HG9 lnSwbmK4oAQ0qD8QTIrCsgP1MwweDdeKRBusA8USoHug2x0HrTTruzIaFPKtERhDA57e ZMvX11W7gEe8HttX6e98BPPhykgCJshnHFK8T7l1TTUd9U36nvXAjID2NoMpcSGVhohx 9fFb0O+aZZurdKb2FaxqERHMGylqsH/Rh4DpU4mkNHQC80YIEHwQ+dADEPSvxH5JsHgm piB1JlddmCRHEgb9sZIEvX7tsdCVJm78LLJRucuZ1kdyx5UtPoA0V/Yd3+PJdC0t3GKn n6bQ== X-Gm-Message-State: AAQBX9eNxSTEIl/sdzoNwg8gwu48uCxT17En8CcA4sgL71Tfpjsm5vc5 hq/7yiHhUdWMy3hsxB3251TuSRqoVpQ= X-Google-Smtp-Source: AKy350aYPHgiitFU4MdOGXeJWPwC3C4GJFCWRiiHSAGqb0XKZ5Meb4ne2EZlqJPjX7jRYQ7x9jjMUA== X-Received: by 2002:adf:f40e:0:b0:2cf:3399:998d with SMTP id g14-20020adff40e000000b002cf3399998dmr24901672wro.57.1680425156151; Sun, 02 Apr 2023 01:45:56 -0700 (PDT) Original-Received: from melissa.local ([2c0f:ef18:1431:0:b09:9616:db04:c248]) by smtp.gmail.com with ESMTPSA id g7-20020a5d5407000000b002e6423cb207sm5738221wrv.112.2023.04.02.01.45.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Apr 2023 01:45:55 -0700 (PDT) In-reply-to: <87edp6oim6.fsf@gmail.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:259102 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Wilhelm Kirschbaum writes: >>> From: =E7=89=9F =E6=A1=90 >>> 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=20 >>> insert >>> the >>> left quotes when electric-pair-mode is on. >>> python-mode did it here: >>> https://github.com/emacs-mirror/emacs/blob/bfa3500c3c6e4df58978e8475371= 8cd5358c06fb/lisp/progmodes/python.el#L6599-L6607 >>> https://github.com/emacs-mirror/emacs/blob/bfa3500c3c6e4df58978e8475371= 8cd5358c06fb/lisp/progmodes/python.el#L6637-L6639 >>> This behavior is also very common in elixir, so can we add=20 >>> 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=20 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 """|"""=20=20=20=20=20=20 I am adding two patches, one for the electric pair and another to=20 fix the issue where the point jumps to the end of a multi-line comment=20 when type `"` within the multi-line comment. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Propertize-heredocs-in-elixir-ts-mode.patch Content-Description: Propertize heredocs >From 5005439e007aa3f766b323f9a07cb9e5039820e8 Mon Sep 17 00:00:00 2001 From: Wilhelm H Kirschbaum 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-Handle-electric-heredocs-pairs-in-elixir-ts-mode.patch Content-Description: Handle electric heredocs >From 19704012ac83530e1a06531905411d0972e88a44 Mon Sep 17 00:00:00 2001 From: Wilhelm H Kirschbaum 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 --=-=-=--