zed
zed copied to clipboard
Can not format unsaved file/buffer with custom formatter
Check for existing issues
- [X] Completed
Describe the bug / provide steps to reproduce it
- Set an arbitrary formatter for some language in
"language_overrides"
. - Call the new file command to create a new file without saving it.
- Input some unformatted content into the buffer.
- Call the format command.
Observation: The buffer is not formatted.
Environment
Zed: v0.100.0 (preview) OS: macOS 13.5.0 Memory: 32 GiB Architecture: x86_64
If applicable, add mockups / screenshots to help explain present your vision of the feature
No response
If applicable, attach your ~/Library/Logs/Zed/Zed.log
file to this issue.
If you only need the most recent lines, you can run the zed: open log
command palette action to see the last 1000.
No response
Hey @liebkne, I'm curious about this workflow - do you use another editor where you are able to format before saving?
@JosephTLyons
Hi, I can do this in Emacs.
Sometimes I copy some text from terminal or browser, create a scratch buffer and paste, then format the content for later usage like pasting the formatted content into a named file (usually different file type from the content, like Python
to markdown
) or other application outside the editor.
This let me recall the Snippet function. I personally think some of the functions (like snippets, external formatters, etc.) may not should be bound to language server. (As the formatter setting is placed in langauge_overrides
, I assumed it's a "language server" thing in Zed.)
Cool, apprecaie you letting me know @liebkne.
Same problem here, without even have to set some settings in language_overrides
, just using the default one :(
Ran into the same issue. Just to add on, it's pretty normal for devs to do stuff like copying a messy api response (e.g. json) and trying to make sense of it. In VS Code, it's a as simple as - just set the type, right-click, and hit "Format".
It seems this is not possible with built-in formatters either. To reproduce:
- Open a new (unsaved) buffer
- Paste some unformatted JSON
- Set syntax to
JSON
- Try to format the buffer (nothing happens)
- Save it as
temp.json
- Try to format the buffer (now it works)
Same experience here. Not using any language overrides. For a JSON file.
I recorded a video in #9773.
For what it's worth, I'm able to run a formatter like this in VSCode.
Any update on this matter ?
This is really essential to an in-development code editor, this will make me ditch VSCode as my second IDE
@mrnugget
The release note says Added the ability to format unsaved buffers with Prettier.
Is that mean only languages supported by Prettier will work? So I can not format Python or Rust code in a unsaved buffer?
Besides, why not just use the same formatter of files for unsaved buffers? For example one use jq
for JSON file , but have to use Prettier
for unsaved buffers. One would have to deal with different config options to make them consistent.
For me, the
"formatter": ... (Why would one have two formatter configs for every language?)
"prettier": ...
configs are also confusing.
Reopening this since, yes, the issue isn't fixed for custom formatters.
I went with prettier first, because that's consistent across many file types and languages and by default allows formatting arbitrary text input.
For external commands (custom formatters) we'll have to make sure the syntax is consistent so that it works with saved/unsaved buffers alike.
For language servers (rust-analyzer, ...) the problem is that Zed doesn't even know which language server should be associated with a file if it's not saved.
Does that mean that language servers are picked based on file extensions?
I don't know what else would prevent Zed from knowing what language server to associate with an unsaved a file.
Since syntax highlighting is controlled by the language selector right? I would assume that also controls the language server, but perhaps not?
Mainly just curious! 😁
I don't know what else would prevent Zed from knowing what language server to associate with an unsaved a file.
I'll need to dig into this, I think maybe it works the way you described it.
I don't know what else would prevent Zed from knowing what language server to associate with an unsaved a file.
It looks like the LSP protocol does not support formatting files that are not saved to disk (formatting spec).
All requests that do some kind of formatting expect a TextDocumentIdentifier
, which points to the corresponding file path.
Do you know any editors that support this? Looks like VSCode does not support it, when trying to format an unsaved rust file I see:
Formatting unsaved buffers works in Emacs.
"formatter": {
"external": {
"command": "pyformat", // Or whatever
"arguments": ["-"]
}
}
I think at least the external formatter setting should be able to work in this case. Just like the last release of using Prettier
. But instead of using Prettier
, we should merge that two configs into one.
"formatter": ... (Why would one have two formatter configs for every language?)
"prettier": ...
Formatting unsaved buffers using external formatters will be supported when #12597 is merged. I was just talking about the LSP formatting situation.
But instead of using
Prettier
, we should merge that two configs into one.
This is orthogonal to this ticket, but I don't understand why we should do this.
formatter
specifies which formatter to use globally/per-language, prettier
specifies which options to pass to prettier if/when it's used.
@mrnugget Maybe I misunderstood what Prettier
is, I have thought the config should be like
"formatter": {
"external": {
"command": "prettier",
"arguments": ["options to pass to prettier "]
}
}
We do have prettier built-in, so it's not really an external command like the others. That's why we have 3 options (language server, prettier, external command).
We do have prettier built-in
Didn't know that, thanks for the information.
We do have prettier built-in, so it's not really an external command like the others. That's why we have 3 options (language server, prettier, external command).
I wanted to customise prettier to use single quotes.
{ "singleQuote": true }
But just creating a .prettierrc
in my home directory with that JSON didn't work. I don't fully understand what it means that prettier is built-in, but to get the editor to recognise my customisations, I needed to..
- update my settings.json file
{
"features": {
"copilot": false
},
"theme": "One Light",
"ui_font_size": 16,
"buffer_font_size": 16,
"languages": {
"JavaScript": {
"format_on_save": {
"external": {
"command": "prettier",
"arguments": ["--stdin-filepath", "{buffer_path}"]
}
}
}
}
}
- also install prettier globally
pnpm add -g prettier
Then it recognises my .prettierrc
file, and formats as I would like it to on save.