glyph / python-docstring-mode

Emacs minor-mode for editing Python docstrings.
MIT License
70 stars 22 forks source link

Broken formatting with new python.el (0.28) #30

Closed timlod closed 2 years ago

timlod commented 2 years ago

Since the new update to the built-in python mode, python-docstring-filldoes not work fully anymore.

The following code will not change when formatted with python-docstring-fill, python.el version 0.27.1:

def fun(a):
    """
    Do something.

    :param a: do something with a
    """

When using python.el, version 0.28, python-docstring-fillwill now format this code as such:

def fun(a):
    """
 Do something.

      :param a: do something with a
        """

I'm now going to step through execution of the function to see if I can find the issue, but I may not be adept enough at elisp to debug it myself.

shabble commented 2 years ago

I think the problem might be related to the change described here (this actual change) and so the offsets that python-docstring-fill uses are now slightly off.

Very hacky patch because I'm too lazy to fix my github auth problems and push to a fork:

diff --git a/python-docstring.el b/python-docstring.el
index de9781e..ff5fa30 100644
--- a/python-docstring.el
+++ b/python-docstring.el
@@ -61,7 +61,7 @@ single space is used."
                                       (setf fill-it-anyway t)
                                       (throw 'not-a-string nil))))
                        (string-start (+ (goto-char (nth 8 syx))
-                                        3))
+                                        1))
                        (rawchar (if (eql (char-before (point)) ?r)
                                     1
                                   0))
@@ -75,7 +75,7 @@ single space is used."
                                (progn (forward-sexp)
                                       (point))
                              (error (point-max)))
-                           3))
+                           1))
                        (orig-offset (- orig-point string-start)))
                   (let*
                       ((offset-within

Seems to work for me. A proper fix probably needs to check the version of python.el and set offsets appropriately rather than just blindly overwriting them, or possibly could check that (- string-start 1) is actually the quote as expected, or something?

timlod commented 2 years ago

Appears to work for me, too - thanks! I will patch this for myself for now and see if it would run into any issues.

vale981 commented 2 years ago

created a pr

avatar-lavventura commented 2 years ago

@vale981 Is the PR approved?

vale981 commented 2 years ago

not yet :{

glyph commented 2 years ago

Sorry, I get a lot of github spam and things often fall through the cracks.

glyph commented 2 years ago

After installing from Elpa I can see the error quite clearly, thanks for the example.