prettier/plugin-python

Use two spaces before inline comments

patrick91 opened this issue · 3 comments

I'm using this bug to get familiar with the internals of prettier and this plugin. My naive approach to getting this done is by adding a new prettier printer option to specify a trailing comment prefix here. Something like options.printer.trailingCommentPrefix, with a default of a single space, but plugin-python would provide a two-space string.

This change yields the following output for yarn prettier tests/python_comments/comments.py:

$ prettier --plugin=. --parser=python tests/python_comments/comments.py
- if a: # a
+ if a:  # a
    # b
    c

    # c

    d

    # e

    call()
    # a

However, this seems like a pretty granular configuration option to me, compared to the existing printer options. And I doubt you all want to go down the path where plugin-python is doing all the comment printer handling with the willPrintOwnComments option. Interested in hearing alternative solutions.

j-f1 commented

What about putting a leading line when printing the comment?

@j-f1 Not sure if I'm understanding your suggestion, but here's what happens when I change plugin-python's printComment to return group(concat([line, comment.value])):

$ prettier --plugin=. --parser=python tests/python_comments/comments.py
if a:  # a
     # b
    c

     # c

    d

     # e

    call()
     # a

As you can see, it adds an extra space even to comments that aren't inline.

Note: I also had to modify the final case in prettier's printTrailingComment from " " + contents to concat([" ", contents]), otherwise it would print:

$ prettier --plugin=. --parser=python tests/python_comments/comments.py
if a: [object Object]
     # b
    c

     # c

    d

     # e

    call()
     # a