zed icon indicating copy to clipboard operation
zed copied to clipboard

Pretty JSON

Open krstp opened this issue 1 year ago • 13 comments

Check for existing issues

  • [X] Completed

Describe the feature

Allow to prettify hairy JSON payloads.

If applicable, add mockups / screenshots to help present your vision of the feature

No response

krstp avatar Mar 30 '23 04:03 krstp

@krstp would you mind expanding on how this is different from formatting a JSON buffer?

hovsater avatar Apr 30 '23 08:04 hovsater

Well, JSON can be minified or simply not formatted correctly. What I mean I would like to operate on sectional portions of the code, for example I am writing a Markdown file and bit of it carries a JSON code within the block, such as:

 ```json
 {}
 ``` .

and I would like to be able to select desired portion of the code and invoke JSON prettifier just to tidy up selected portion of the code. That's it. I hope it explains.

krstp avatar May 02 '23 03:05 krstp

just a little nudge 😃

Pipping would somewhat help. There is a handy jq tool to prettify json, i.e. cat ~/.config/zed/conversations/Converting\ Pandas\ DataFrame\ to\ Dictionary\ in\ Python\ -\ 1.zed.json | jq

Nonetheless, currently no pipping to zed is allowed, even via | xargs zed... The only way now is to dump the above cat into file > sample.json and follow up with another command zed sample.json.

The other way around it to have alias defined along the following lines: cat ~/.config/marvin/glorious_config.json | jq > ~/Desktop/_.json | zed ~/Desktop/_.json

Eitherway, to have prettify json within ZED is much desirable 😄 🙏

krstp avatar Nov 17 '23 16:11 krstp

@hovsater what do you mean by "formatting a JSON buffer"?

I just installed Zed in order to format some copy pasted JSON, and it does not format it at all. Unlike vscode, where I can paste in json, I don't even need to choose a document type (vscode infers it automatically), and I just hit cmd+shift+P > format document, and it formats it

himat avatar Mar 01 '24 21:03 himat

If your file isn't saved on the system, you'll have to choose the language yourself: Zed doesn't currently do automatic language detection for "untitled" / unsaved files.

Moshyfawn avatar Mar 02 '24 01:03 Moshyfawn

Well... I must pitch in here. I don't think anybody expects auto-detection (cha-ching!; somebody tries to really step up the game 😉). However, what one would expect is CMD+SHIFT+P --> Prettify JSON selection would allow ZED to prettify JSON without having to save the file/content first. So cheers to the team if we could have this functionality there. Merci 🙏

krstp avatar Mar 02 '24 05:03 krstp

The comment above mine talks about auto-detection, but I see what you're saying now, thanks for the explanation.

I think it's a fair ask, although I don't think Zed is great at mixing languages and language specific commands yet. In this case, where there is no language defined, and even more so when it's a markdown file with a json code block, I don't think Zed can make use of multiple unrelated language servers.

This is not to say, it's not going to be worked on, but rather I'm curious if it's even possible at this stage.

Moshyfawn avatar Mar 02 '24 05:03 Moshyfawn

On the other hand... to pull the thread of auto-detection... I think it is somewhat relatively easy way to implement. Have a small-and-simple ML trained-model baked into ZED and make it predict what kind of syntax it is(!), I don't think there is any rocket science in here, no? 🤔 Well... at least here, without recognizable accent, I can expose my CTO marvels 😄

krstp avatar Mar 02 '24 05:03 krstp

I think it's a fair ask, although I don't think Zed is great at mixing languages and language specific commands yet. In this case, where there is no language defined, and even more so when it's a markdown file with a json code block, I don't think Zed can make use of multiple unrelated language servers.

Well, I don't think anybody expects Markdown or languages mixing. What would be really helpful is the ability to just paste a text buffer without the need of saving the file and be able to call the formatting/prettifying from within the ZED panel (CMD+SHIFT+P). Thanks for the quick follow up.

krstp avatar Mar 02 '24 05:03 krstp

Agree. Just opened a bunch of JSON files and having to drag the mouse pointer down to the language selector and choose "JSON" for each file was quite tedious.

hooch avatar Mar 18 '24 23:03 hooch

+1 on this. I have JSON i copy paste from a curl response and it would be nice if Zed could format it. I tried CMD+SHIFT+P -> editor: format but nothing happens

drale2k avatar Mar 28 '24 21:03 drale2k

I think just having the ability to format an unsaved buffer would be great!

I often just paste some random data, like JSON or a GraphQL query, in an usaved buffer - then choose the language and then format. This does not currently work.

Bjorn-Eric-Abr avatar Apr 08 '24 11:04 Bjorn-Eric-Abr

I would also like to have native support, but in the meanwhile, I'm using this workaround. As you can see, I'm using biome, but prettier is also viable (or any other formatter for that matter, you would just need to adjust the arguments).

For Biome, put the following in your Zed settings.json:

{
 // Other settings, such as theme, etc.
 "language_overrides": {
    "JSON": {
      "format_on_save": "off",
      "formatter": {
        "external": {
          "command": "biome",
          "arguments": ["format", "--stdin-file-path={buffer_path}"]
        }
      }
    }
  }
}

For Prettier you need to swap the command/arguments to this:

"external": {
  "command": "prettier",
  "arguments": ["--stdin-filepath={buffer_path}"]
}

After doing so, you can use editor: format in the command palette to format your file, without it being formatted every time you save your file. Of course, you can also enable format on saving just by removing the line above that sets it to off.

Hope this helps :)

PS.: Make sure Biome/Prettier is available in your path. Because I'm on macOS and installed Biome using brew, it's available globally, but if you installed Biome via npm, you might need to change the command to npx and add @biomejs/biome as the first parameter.

d0x7 avatar Apr 11 '24 20:04 d0x7

I think just having the ability to format an unsaved buffer would be great!

@Bjorn-Eric-Abr FYI This is possible now. And entirely using the keyboard if that's your jam: cmd+k, m, <language>, cmd+shift+i. (more info)

notpeter avatar Jun 25 '24 20:06 notpeter

With latest update the following change to deprecated settings json arguments did fix it: https://github.com/zed-industries/zed/issues/13562#issuecomment-2193877544

krstp avatar Jun 27 '24 15:06 krstp