Skip to content

fix: preserve leading empty lines in code cells (if any) during formatting#953

Open
mcanouil wants to merge 2 commits intoquarto-dev:mainfrom
mcanouil:fix/preserve-leading-empty-lines-formatting
Open

fix: preserve leading empty lines in code cells (if any) during formatting#953
mcanouil wants to merge 2 commits intoquarto-dev:mainfrom
mcanouil:fix/preserve-leading-empty-lines-formatting

Conversation

@mcanouil
Copy link
Copy Markdown
Contributor

@mcanouil mcanouil commented Apr 22, 2026

  • Formatting an R code cell with Air stripped all leading empty lines between option directives and code. Python cells were unaffected because their virtual documents include inject lines that push user code off position 0.
  • formatBlock now strips leading empty lines from the virtual document and adjusts the line offset accordingly, so formatter edits land at the correct position.
  • A normalizeEdit collapses two or more leading empty lines to exactly one; this fires even when no language formatter is active.
  • New tests cover: single empty line preserved, multiple collapsed to one, no empty lines unaffected, a hostile formatter proving empty lines are hidden, and normalisation without a formatter — for both R and Python.

Tested interactively on a VSIX installed build with (my formatters: Ruff and Air):

---
title: Leading Empty Lines
format: html
---

```{r}
#| label: one-empty-line

x<-1
```

```{r}
#| label: two-empty-lines


x<-2
```

```{r}
#| label: no-empty-lines
x<-3
```

```{python}
#| label: no-empty-line
x=1;y=2
```

```{python}
#| label: one-empty-line

x=1;y=2
```

```{python}
#| label: two-empty-lines


x=3;y=4
```

Which lead to:

---
title: Leading Empty Lines
format: html
---

```{r}
#| label: one-empty-line

x <- 1
```

```{r}
#| label: two-empty-lines

x <- 2
```

```{r}
#| label: no-empty-lines
x <- 3
```

```{python}
#| label: no-empty-line
x = 1
y = 2
```

```{python}
#| label: one-empty-line

x = 1
y = 2
```

```{python}
#| label: two-empty-lines

x = 3
y = 4
```

Fixes #950

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.
@mcanouil mcanouil changed the title fix: preserve leading empty lines in code cells during formatting fix: preserve leading empty lines in code cells (if any) during formatting Apr 22, 2026
@cscheid
Copy link
Copy Markdown
Member

cscheid commented Apr 22, 2026

Do I understand it correctly that the code you added here undoes some of the work that Air does?

@mcanouil
Copy link
Copy Markdown
Contributor Author

mcanouil commented Apr 22, 2026

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.🤷‍♂️

@mcanouil
Copy link
Copy Markdown
Contributor Author

To clarify, this PR creates normalisation of the leading empty lines regardless of languages and formatters, this makes things consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add empty line after quarto comments

2 participants