zed icon indicating copy to clipboard operation
zed copied to clipboard

Can not format unsaved file/buffer with custom formatter

Open failable opened this issue 1 year ago • 6 comments

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

  1. Set an arbitrary formatter for some language in "language_overrides".
  2. Call the new file command to create a new file without saving it.
  3. Input some unformatted content into the buffer.
  4. 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

failable avatar Aug 18 '23 11:08 failable

Hey @liebkne, I'm curious about this workflow - do you use another editor where you are able to format before saving?

JosephTLyons avatar Aug 18 '23 13:08 JosephTLyons

@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.)

failable avatar Aug 18 '23 13:08 failable

Cool, apprecaie you letting me know @liebkne.

JosephTLyons avatar Aug 18 '23 20:08 JosephTLyons

Same problem here, without even have to set some settings in language_overrides, just using the default one :(

NicoAdrian avatar Jan 30 '24 08:01 NicoAdrian

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".

edtorba avatar Feb 18 '24 21:02 edtorba

It seems this is not possible with built-in formatters either. To reproduce:

  1. Open a new (unsaved) buffer
  2. Paste some unformatted JSON
  3. Set syntax to JSON
  4. Try to format the buffer (nothing happens)
  5. Save it as temp.json
  6. Try to format the buffer (now it works)

eproxus avatar Mar 04 '24 09:03 eproxus

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.

jessevdp avatar Mar 26 '24 08:03 jessevdp

Any update on this matter ?

NicoAdrian avatar Apr 09 '24 10:04 NicoAdrian

This is really essential to an in-development code editor, this will make me ditch VSCode as my second IDE

eh-lucas-maffazioli avatar Apr 11 '24 13:04 eh-lucas-maffazioli

@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.

failable avatar May 23 '24 03:05 failable

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.

mrnugget avatar Jun 03 '24 04:06 mrnugget

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! 😁

jessevdp avatar Jun 03 '24 06:06 jessevdp

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.

mrnugget avatar Jun 03 '24 07:06 mrnugget

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:

image

bennetbo avatar Jun 03 '24 09:06 bennetbo

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": ...

failable avatar Jun 03 '24 09:06 failable

Formatting unsaved buffers using external formatters will be supported when #12597 is merged. I was just talking about the LSP formatting situation.

bennetbo avatar Jun 03 '24 09:06 bennetbo

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 avatar Jun 03 '24 09:06 mrnugget

@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 "]
        }
      }

failable avatar Jun 03 '24 10:06 failable

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).

mrnugget avatar Jun 03 '24 10:06 mrnugget

We do have prettier built-in

Didn't know that, thanks for the information.

failable avatar Jun 03 '24 10:06 failable

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..

  1. 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}"]
        }
      }
    }
  }
}
  1. also install prettier globally

pnpm add -g prettier

Then it recognises my .prettierrc file, and formats as I would like it to on save.

magician11 avatar Jun 16 '24 07:06 magician11