From: Yuan Fu <casouri@gmail.com>
To: nasyxx@gmail.com
Cc: 59534@debbugs.gnu.org
Subject: bug#59534: 29.0.50; Improvement for treesit in python.
Date: Thu, 24 Nov 2022 10:29:47 -0800 [thread overview]
Message-ID: <E2544B34-456A-4A7F-9B9E-A255D45718F5@gmail.com> (raw)
In-Reply-To: <51760046-6AB8-4161-A95E-F090AC680116@gmail.com>
Nasy <nasyxx@gmail.com> writes:
> Hi,
>
> 1. Operators
>
> Python added "@" and "@=" as operators in version 3.5 and ":=" in
> version 3.8. So I guess these three should be in the
> =python--treesit-operators=.
Thanks! I added them.
> 2. Doc strings
>
> Treesit does not consider the content inside the triple quotes at the
> beginning of the Python file to be the documents.
>
> For example, lines 2-4 of this file
> https://github.com/python/cpython/blob/main/Lib/csv.py#L2-L4
>
> """
> csv.py - read/write/investigate CSV files
> """
>
> It be fontify as string instead of doc.
>
> I changed this function to recognize the outermost single string as the
> doc string.
>
> (defun python--treesit-fontify-string (node override start end &rest _)
> "Fontify string.
> NODE is the string node. Do not fontify the initial f for
> f-strings. OVERRIDE is the override flag described in
> `treesit-font-lock-rules'. START and END mark the region to be
> fontified."
> (let* ((string-beg (treesit-node-start node))
> (string-end (treesit-node-end node))
> (maybe-expression (treesit-node-parent node))
> (maybe-defun (treesit-node-parent
> (treesit-node-parent
> maybe-expression)))
> (face (if (and (member (treesit-node-type maybe-defun)
> '("function_definition"
> - "class_definition"))
> + "class_definition"
> + nil))
> ;; This check filters out this case:
> ;; def function():
> ;; return "some string"
> (equal (treesit-node-type maybe-expression)
> "expression_statement"))
> 'font-lock-doc-face
> 'font-lock-string-face)))
> (when (eq (char-after string-beg) ?f)
> (cl-incf string-beg))
> (treesit-fontify-with-override
> (max start string-beg) (min end string-end) face override)))
>
> I'm not familiar with treesit, this solution could not be the best, but
> I don't know if there is any other simple way to improve it. Maybe we
> could use something like =python-info-docstring-p= to check if it is doc
> or net.
I added something similar to the function. Now it will highlight the
string in doc-face if there is nothing or only comments before it.
Yuan
prev parent reply other threads:[~2022-11-24 18:29 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-23 19:53 bug#59534: 29.0.50; Improvement for treesit in python Nasy
2022-11-24 18:29 ` Yuan Fu [this message]
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=E2544B34-456A-4A7F-9B9E-A255D45718F5@gmail.com \
--to=casouri@gmail.com \
--cc=59534@debbugs.gnu.org \
--cc=nasyxx@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).