Make the handling of inserted newlines more consistent #144
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This removes a bunch of special-case addition of newlines and attempts to preserve exactly the number provided by the user when inserting or setting. It also fixes #48, where inserting a line after a 'def' could produce additional newlines after the 'def' block.
The erroneous newline triggered by insertion (issue #48) is due to this:
The sensible solution seemed to be to make
IfelseblockNode
inherit fromCodeBlockNode
(it's a code block after all), which removed the stray newline, but that made the following test fail:The new result lost the final newline. This led me into
CodeBlockNode.parse_code_block
, where I discovered a lot of questionable (to an outsider) removal and addition of newlines.It starts with:
Note that the call to strip removes whitespace and newlines.
Then there was:
The
%s\n
adds a newline.Then there was a bunch of code trying to guess how many newlines to add back, without taking into consideration how many there were to begin with. I ditched all of that. This means that this PR changes the expected results of the setter tests, but it does so in a way that is truer to the user's request.
e.g.
Now inserts a single newline as requested, instead of 3.
There is a very good chance that I've misunderstood something, since I've only just started using redbaron. Please let me know if that is the case.