bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#59534: 29.0.50; Improvement for treesit in python.


From: Yuan Fu
Subject: bug#59534: 29.0.50; Improvement for treesit in python.
Date: Thu, 24 Nov 2022 10:29:47 -0800

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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]