Skip to content

Use buffer language when formatting with Prettier#43368

Merged
SomeoneToIgnore merged 5 commits intozed-industries:mainfrom
JPGibb:main
Dec 4, 2025
Merged

Use buffer language when formatting with Prettier#43368
SomeoneToIgnore merged 5 commits intozed-industries:mainfrom
JPGibb:main

Conversation

@JPGibb
Copy link
Contributor

@JPGibb JPGibb commented Nov 23, 2025

Set prettier_parser explicitly if the file extension for the buffer does not match a known one for the current language

Release Notes:

  • N/A

@cla-bot
Copy link

cla-bot bot commented Nov 23, 2025

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: John.
This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email email@example.com
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails

@maxdeviant maxdeviant changed the title Use buffer langauge when formatting with prettier Use buffer language when formatting with Prettier Nov 23, 2025
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Nov 23, 2025
Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Can you please provide more context on what's broken here, how to test the fix, etc.?

@SomeoneToIgnore SomeoneToIgnore self-assigned this Nov 24, 2025
@JPGibb
Copy link
Contributor Author

JPGibb commented Nov 24, 2025

Hi,

In my case I have json that gets stored in files like "feature.settings".
When editing a buffer without an associated file formatting works correctly as the prettier parser assiociated with the language is used.
However, after saving this buffer to a file like the on above, formatting would fail with the error:

UndefinedParserError: No parser could be inferred for file {path_to_file}

So for a file like 'feature.settings':

{
  "foo": "foo",
"bar": "misformatted"
}

Formatting would fail, even with the buffer language in Zed set to json.

With this change, the prettier parser would be set explicitly using the buffer's language rather than inferred from the file extension.
So the file would be correctly formatted as json.

Cheers

Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Thank you for the context: sounds like this is quite a logic-related one, and we'd better have a test so it does not degrade later.

We have async fn test_document_format_with_prettier and other prettier tests, let's write another one for this scenario.

@JPGibb
Copy link
Contributor Author

JPGibb commented Nov 30, 2025

Hi, Sorry for the delay getting back to you. Long week :).

I've added a new test test_document_format_with_prettier_explicit_language which passes.
However, I noticed after doing this that the code I wrote is in a Self::Real block whereas the test uses Self::Test.

I'm not entirely sure how to handle this, is there some way I can set my test up to run the code that I changed?

Thanks

@JPGibb JPGibb marked this pull request as ready for review November 30, 2025 14:45
Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Thank you for the test, and no worries, I have other things to do anyway.

What if we adjust
let formatted_text = buffer.text() + FORMAT_SUFFIX; to write the parser name too?

That will require amending a few existing tests and restructuring the code of async fn format so that the common code for parser determination is shared between both test and real branches.

But after that, the code before the change should output a totally different value, which we can assert.

@JPGibb JPGibb marked this pull request as draft December 1, 2025 22:13
@katie-z-geer katie-z-geer moved this from Community PRs to Done in Quality Week – December 2025 Dec 2, 2025
@JPGibb JPGibb force-pushed the main branch 2 times, most recently from 800bd35 to cc22257 Compare December 2, 2025 22:40
@JPGibb JPGibb marked this pull request as ready for review December 2, 2025 22:53
@JPGibb
Copy link
Contributor Author

JPGibb commented Dec 2, 2025

I've modified the test code to include the parser name and put the parser determination code in a closure used by both the real and test prettier code.

Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Looking at the CI and the changes, this turned out to be the first test that uses Prettier parsers...

Thank you a lot for adding one.
I've left a few minor style nits and will be happy to merge this after today's release, just in case.

@SomeoneToIgnore SomeoneToIgnore enabled auto-merge (squash) December 4, 2025 11:58
@SomeoneToIgnore SomeoneToIgnore merged commit bad6bde into zed-industries:main Dec 4, 2025
23 checks passed
baldwindavid added a commit to baldwindavid/zed that referenced this pull request Dec 4, 2025
* main: (155 commits)
  Add support for git remotes (zed-industries#42819)
  python: Improve sorting order of toolchains to give higher precedence to project-local virtual environments that are within current subproject (zed-industries#44141)
  Use buffer language when formatting with Prettier (zed-industries#43368)
  search: Fix sort order not being maintained in presence of open buffers (zed-industries#44135)
  bedrock: Support global endpoints and new regional endpoints (zed-industries#44103)
  linux: Spawn at least two background threads (zed-industries#44110)
  macos: Add missing file access entitlements (zed-industries#43609)
  Re-colorize the brackets when the theme changes (zed-industries#44130)
  Reduce priority of Windows thread pool work items (zed-industries#44121)
  Update fancy-regex (zed-industries#44120)
  Prefer to disable options over hiding (git panel entry context menu) (zed-industries#44102)
  tab_switcher: Subscribe to workspace events instead of pane events (zed-industries#44101)
  editor: Add active match highlight for buffer and project search (zed-industries#44098)
  Add more preview tab settings and fix janky behavior (zed-industries#43921)
  ai: Add an eval for the inline assistant (zed-industries#43291)
  Fix circular reference issue around PopoverMenu again (zed-industries#44084)
  Run `git2::Repository::find_remote` in the background (zed-industries#44092)
  Improve support for multiple registrations of  `textDocument/diagnostic` (zed-industries#43703)
  Revert "http_client: Add integrity checks for GitHub binaries using digest checks (zed-industries#43737)" (zed-industries#44086)
  editor: Fix blame hover not working when inline git blame is disabled (zed-industries#42992)
  ...
AlpSha pushed a commit to AlpSha/zed that referenced this pull request Dec 5, 2025
Set `prettier_parser` explicitly if the file extension for the buffer
does not match a known one for the current language

Release Notes:

- N/A

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
CherryWorm pushed a commit to CherryWorm/zed that referenced this pull request Dec 16, 2025
Set `prettier_parser` explicitly if the file extension for the buffer
does not match a known one for the current language

Release Notes:

- N/A

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
someone13574 pushed a commit to someone13574/zed that referenced this pull request Dec 16, 2025
Set `prettier_parser` explicitly if the file extension for the buffer
does not match a known one for the current language

Release Notes:

- N/A

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

Development

Successfully merging this pull request may close these issues.

2 participants