yapf icon indicating copy to clipboard operation
yapf copied to clipboard

Fix unexpected whitespace removal outside line range

Open limwz01 opened this issue 2 years ago • 4 comments
trafficstars

  • save the whitespace prefix in the pytree node even for those occurring with comments
  • disable the comment splicer's removal of trailing whitespace
  • add back the whitespace before emitting unformatted line
  • remove trailing whitespace in comments for enabled lines later when printing

TODO:

  • extensive testing is necessary as this method is like a hack
  • this still will remove trailing whitespace in line just before line range and will ignore trailing whitespace in last line of line range

limwz01 avatar Jun 06 '23 02:06 limwz01

Could you give an example of what this is fixing?

bwendling avatar Oct 25 '23 17:10 bwendling

The example is in the linked issue.

limwz01 avatar Oct 25 '23 17:10 limwz01

Resolve conflicts

Spitfire1900 avatar Jan 20 '24 20:01 Spitfire1900

After a long while of using this with only the problems mentioned in my first post, I've found a further problem that occurs with the following:

def main():
    if 1:
        if 1:
            pass

        # extra
        elif 1:
            pass
    pass

Applying the modified yapf on anywhere except including the extra comment line, we will get extra trailing space on the line before the extra comment line. I've not figured out the bug yet.

limwz01 avatar Jan 22 '24 07:01 limwz01