fix: preserve leading empty lines in code cells (if any) during formatting#953
Open
mcanouil wants to merge 2 commits intoquarto-dev:mainfrom
Open
fix: preserve leading empty lines in code cells (if any) during formatting#953mcanouil wants to merge 2 commits intoquarto-dev:mainfrom
mcanouil wants to merge 2 commits intoquarto-dev:mainfrom
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.
Member
|
Do I understand it correctly that the code you added here undoes some of the work that Air does? |
Contributor
Author
|
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.🤷♂️ |
Contributor
Author
|
To clarify, this PR creates normalisation of the leading empty lines regardless of languages and formatters, this makes things consistent. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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