mawww / kakoune

mawww's experiment for a better code editor
http://kakoune.org
The Unlicense
9.9k stars 713 forks source link

Support paragraph breaks in python block comments. #5148

Closed sjjf closed 3 months ago

sjjf commented 5 months ago

The current python filetype module treats a single empty comment line (typically created by hitting enter twice while in a block comment) as the end of the block comment, deleting the empty comment line and ending comment prefix copying. This runs contrary to PEP8, which explicitly allows for paragraphs in block comments, with an empty comment as the paragraph separator (see https://peps.python.org/pep-0008/#block-comments for the actual text).

This change implements support for using a single empty comment as a paragraph separator, with two consecutive empty comments being treated as the end of the block comment; both empty comment lines are deleted and comment prefix copying is ended.

This would close issue #5147

This change basically means that you can hit enter twice while you're in a block comment, and you'll get an empty comment and the cursor at the start of a new commented line - if you enter text the block comment will continue as normal, if you hit enter again at this point the block comment is terminated, the empty comments are deleted, and you go back to regular code. This follows the basic logic of the current implementation, while allowing for paragraph breaks in block comments.

This behaviour arguably isn't ideal, particularly with the two empty comments disappearing suddenly - that rather goes against the principle of least surprise. But given the way block comments work it's hard to see a better approach - something like Ctrl-enter, perhaps? I don't know how that would work with a modal editor like Kakoune, though.

Possible issues with the whole idea aside, I'm not confident this is the best possible implementation - it was a lot fiddlier than I anticipated to make it work the way I intended, and it doesn't feel very elegant. There may be cases that aren't handled properly, though I've tried to make it reasonably robust. Any suggestions for improving the code would be appreciated.

I've added tests in the regression/5147-python-block-comment-paragraphs directory, if there's a more appropriate place for them please let me know and I'll relocate them. Additional test cases or a better way to implement them are welcome.

I've already submitted a copyright waiver for previous contributions, I assume that's still considered valid.

sjjf commented 5 months ago

Fixed regression on issue #860 - I forgot to run the full test suite before pushing the changes, only the tests I'd added . . .

sjjf commented 5 months ago

And some further tweaking, hopefully improving the implementation a bit.