unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dmitry@gutov.dev>
To: kobarity <kobarity@gmail.com>
Cc: Yuan Fu <casouri@gmail.com>, 68445@debbugs.gnu.org
Subject: bug#68445: [PATCH] Problem with python--treesit-syntax-propertize
Date: Mon, 22 Jan 2024 20:52:58 +0200	[thread overview]
Message-ID: <707dc3b4-c7dd-45b5-9764-a9738a9142b6@gutov.dev> (raw)
In-Reply-To: <eke7bk9d74q7.wl-kobarity@gmail.com>

On 22/01/2024 17:44, kobarity wrote:
> Hi,
> 
> Dmitry Gutov wrote:
>> On 21/01/2024 16:47, kobarity wrote:
>>> I am resending my mail, as I made a mistake in X-Debbugs-CC.
>> Was it supposed to appear in the bug's thread? I don't see it anywhere.
> 
> My first mail was registered as Bug#68445, and my patch is there.
> 
> https://debbugs.gnu.org/cgi/bugreport.cgi?bug=68445
> 
> It says:
> 
> Report forwarded to casouri <at> gmail.com, dmitry@.gutov.dev, bug-gnu-emacs <at> gnu.org:
> 
> The extra period is my mistake and it may have caused the problem.
> I'm sorry for the confusion.

Yeah, but even so that's odd: I'm subscribed to the bug tracker, so the 
email should have at least arrived in my inbox, but it did not.

>> I think there is also another approach--handle two different types of
>> nodes separately, instead of just string_content, so we don't have to
>> start from the beginning of the literal. Like this:
>>
>> diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
>> index e2f614f52c2..4f8b0cb9473 100644
>> --- a/lisp/progmodes/python.el
>> +++ b/lisp/progmodes/python.el
>> @@ -1361,13 +1361,15 @@ python--treesit-syntax-propertize
>>       (while (re-search-forward (rx (or "\"\"\"" "'''")) end t)
>>         (let ((node (treesit-node-at (point))))
>>           ;; The triple quotes surround a non-empty string.
>> -        (when (equal (treesit-node-type node) "string_content")
>> -          (let ((start (treesit-node-start node))
>> -                (end (treesit-node-end node)))
>> -            (put-text-property (1- start) start
>> -                               'syntax-table (string-to-syntax "|"))
>> -            (put-text-property end (min (1+ end) (point-max))
>> -                               'syntax-table (string-to-syntax "|"))))))))
>> +        (cond
>> +         ((equal (treesit-node-type node) "string_content")
>> +          (put-text-property (1- (treesit-node-start node))
>> +                             (treesit-node-start node)
>> +                             'syntax-table (string-to-syntax "|")))
>> +         ((and (equal (treesit-node-type node) "string_end")
>> +               (= (treesit-node-start node) (- (point) 3)))
>> +          (put-text-property (- (point) 3) (- (point) 2)
>> +                             'syntax-table (string-to-syntax "|"))))))))
>>
>>   \f
>>   ;;; Indentation
>>
> 
> This approach seems better than my patch, but it does not seem to
> address the following special case.
> 
> #+begin_src python
> """a""""""b"""
> #+end_src

All right, try the patch below, please. It also covers the case of the 
empty literal.

I've tried to find a case where it would behave poorly (e.g. by 
misdetecting three quotes from a combination of some other string 
literals), but couldn't. E.g.,

   s = '''asdasd'

is not a concatenation. It's always an error, at least according to the 
TS grammar.

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index e2f614f52c2..41f612c8b1c 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1359,15 +1359,15 @@ python--treesit-syntax-propertize
    (save-excursion
      (goto-char start)
      (while (re-search-forward (rx (or "\"\"\"" "'''")) end t)
-      (let ((node (treesit-node-at (point))))
-        ;; The triple quotes surround a non-empty string.
-        (when (equal (treesit-node-type node) "string_content")
-          (let ((start (treesit-node-start node))
-                (end (treesit-node-end node)))
-            (put-text-property (1- start) start
-                               'syntax-table (string-to-syntax "|"))
-            (put-text-property end (min (1+ end) (point-max))
-                               'syntax-table (string-to-syntax "|"))))))))
+      (let ((node (treesit-node-at (- (point) 3))))
+        ;; Handle triple-quoted strings.
+        (pcase (treesit-node-type node)
+          ("string_start"
+           (put-text-property (1- (point)) (point)
+                              'syntax-table (string-to-syntax "|")))
+          ("string_end"
+           (put-text-property (- (point) 3) (- (point) 2)
+                              'syntax-table (string-to-syntax "|"))))))))

  \f
  ;;; Indentation







  reply	other threads:[~2024-01-22 18:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <eke7il3w6ztw.wl-kobarity@gmail.com>
2024-01-21 14:47 ` bug#68445: [PATCH] Problem with python--treesit-syntax-propertize kobarity
2024-01-21 18:09   ` Dmitry Gutov
2024-01-22 15:44     ` kobarity
2024-01-22 18:52       ` Dmitry Gutov [this message]
2024-01-23 14:14         ` kobarity
2024-01-26  1:15           ` Dmitry Gutov

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=707dc3b4-c7dd-45b5-9764-a9738a9142b6@gutov.dev \
    --to=dmitry@gutov.dev \
    --cc=68445@debbugs.gnu.org \
    --cc=casouri@gmail.com \
    --cc=kobarity@gmail.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).