fix: preserve leading empty lines in code cells (if any) during formatting#953
fix: preserve leading empty lines in code cells (if any) during formatting#953mcanouil wants to merge 5 commits into
Conversation
Before this change, formatting an R code cell with Air stripped all leading empty lines between option directives and code. Python cells were unaffected because Python's virtual documents include inject lines (# type: ignore, # flake8: noqa) that pushed user code off position 0, inadvertently buffering it from the formatter. The fix is formatter-agnostic: leading empty lines are stripped from the virtual document before it is passed to any language formatter, and the line offset is adjusted so formatter edits land in the correct position. A normaliseEdit collapses two or more leading empty lines to exactly one; this fires as a Quarto-level operation even when no language formatter is active, and also for cells that consist only of option directives followed by blank lines.
|
Do I understand it correctly that the code you added here undoes some of the work that Air does? |
|
Yes😅 (but before it does it, here it does not even know there were empty lines at the top of the code cells) Ruff/Black would have done the same thing if the extension was not injected a comment at the top of the virtual doc.🤷♂️ |
|
To clarify, this PR creates normalisation of the leading empty lines regardless of languages and formatters, this makes things consistent. |
|
@DavisVaughan does this solution seem reasonable to you? |
|
@DavisVaughan thanks for the review. I'll get back on this PR probably this weekend or next week. Edit: comments were addressed. |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
|
Please merge this. It's an OCD nightmare. |
formatBlocknow strips leading empty lines from the virtual document and adjusts the line offset accordingly, so formatter edits land at the correct position.normalizeEditcollapses two or more leading empty lines to exactly one; this fires even when no language formatter is active.Tested interactively on a VSIX installed build with (my formatters: Ruff and Air):
Which lead to:
Fixes #950