Skip to content

Refactor formatting tooling and don't fire message when code cell is already formatted#933

Merged
DavisVaughan merged 5 commits intoquarto-dev:mainfrom
DavisVaughan:feature/no-format-cell-note
Mar 13, 2026
Merged

Refactor formatting tooling and don't fire message when code cell is already formatted#933
DavisVaughan merged 5 commits intoquarto-dev:mainfrom
DavisVaughan:feature/no-format-cell-note

Conversation

@DavisVaughan
Copy link
Collaborator

@DavisVaughan DavisVaughan commented Mar 11, 2026

Closes #932

The intent of the window.showInformationMessage("Editor selection is not within a code cell."); message seems to have mainly been about catching when Quarto: Format Cell was being called when you weren't actually in a code cell. I've done some refactoring to now fire this message when we fail to find a block at the user's cursor position, rather than later on when we fail to get edits for that block. It's perfectly legal for a formatter to return undefined as edits when there is nothing to do, and we shouldn't take that as a signal that we aren't in a code cell.

I've added a test for this specific case by mocking vscode.window.showInformationMessage() for the duration of the test so we can check that it gets called. Pretty neat way to test this.


While I was working on this I felt the need to pay down some tech debt on how complicated these formatting functions have become. They are made up of extremely nested if statements and don't need to be (I've felt this pain every time I touch these functions). I think these are way easier to manage if you write them as a series of early exits.

Most of this PR is this refactor, but I don't expect it to change any actual behavior. I realize this is a bit messy though.

I think you should mostly approach this PR as a refactor, with a very minor change that I'll call out below.


I test this with this qmd



```{python}
def fn():
  1+ 1
```

```{r}
1 + 1
```

```{r}
1 + 1
```


```{python}
def fn():
    1 + 1
```

We can't actually tell the difference between a cell that doesn't have a formatter and a cell with a formatter that said "there is nothing to do, it's already formatted"
@DavisVaughan DavisVaughan changed the title Remove the message about not having a formatter available Refactor formatting tooling and don't fire message when code cell is already formatted Mar 13, 2026
@DavisVaughan DavisVaughan requested a review from juliasilge March 13, 2026 16:09
Copy link
Contributor

@cscheid cscheid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, and the flatter structure with early returns is so much cleaner to read, thank you.

@DavisVaughan DavisVaughan merged commit 0be30fc into quarto-dev:main Mar 13, 2026
4 checks passed
@DavisVaughan DavisVaughan deleted the feature/no-format-cell-note branch March 13, 2026 18:35
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.

Quarto: Format Cell should not "error" when the code is already formatted

3 participants