richie5um / vscode-sort-json

VSCode Extension to Sort JSON objects
https://marketplace.visualstudio.com/items?itemName=richie5um2.vscode-sort-json
MIT License
110 stars 19 forks source link

Better object selection #79

Open marcospgp opened 1 year ago

marcospgp commented 1 year ago

I was using this extension for the first time and had some trouble figuring out how I could sort an object inside a larger JSON file.

Turns out I had to select exactly from the opening bracket { to the closing one }.

Intuitively, I expected selecting the lines (either the ones inside the brackets or also including the ones containing the brackets) to be enough, but when I attempted that, nothing happened. I initially thought the extension was simply broken.

Is there any chance sorting an object by selecting its interior lines or all lines including those with the brackets?

richie5um commented 1 year ago

[Hi. Thanks for the update kind words in your review]

The code currently works by trying to convert the specifically selected text to JSON, so if what is selected isn’t JSON, then it can’t proceed. It could be possible to attempt to add missing brackets (and remove them before the final replacement), but this feels error prone.

If you have specific ideas of how this might work, let me know.

marcospgp commented 1 year ago

@richie5um I think most people would initially attempt what I did, aside from those wanting to sort an entire JSON file.

That means supporting these two approaches for sorting an object/array inside a larger JSON file should be enough:

Currently, it seems only selecting exactly the object (starting and ending with the brackets) is supported.

This could be solved by following these steps when parsing the selected code in order to find a valid JSON object:

  1. Check if the exact selected code is a JSON object;
  2. Check if selected code is surrounded by { } or [ ] (ignore whitespace and newlines);
  3. Find first { or [ and last } or ] inside selected code, and check if that is valid JSON;
  4. No object found, show error message to user (currently fails silently).
digeomel commented 1 year ago

@marcospgp I was about to uninstall the extension when I thought about checking the reviews first. Indeed, it's neither intuitive, nor well explained. Also, the original indentation is not kept, but I can live with that because I have an automatic formatter on save.

redactedscribe commented 1 year ago

It could be possible to attempt to add missing brackets (and remove them before the final replacement), but this feels error prone.

  1. Check if the exact selected code is a JSON object;

  2. Check if selected code is surrounded by { } or [ ] (ignore whitespace and newlines);

  3. Find first { or [ and last } or ] inside selected code, and check if that is valid JSON;

  4. No object found, show error message to user (currently fails silently).

3.5. If the extension looked pre and post the user's selection for the first matching pair of brackets (array), if not found, then braces (object), then worked from that range as the selection, but only processed the line numbers which the user's selection originally spanned across, maybe that would result in the desired effect?

Problems: Obviously, the first matching pair of brackets / braces may be very far apart, or be on the same line as everything else if the JSON isn't formatted at all. If a pair doesn't exist, then fail like usual.

marcospgp commented 1 year ago

@redactedscribe You mean detecting the object in question but only sorting the lines the user selected... Hm I don't think I see a use case for partial sorting of JSON objects/arrays.

Most likely the user would be trying to simply point out which inner object they wanted to sort, and the partial sorting would be unintended (and could be hard to notice if there's for example only one non-selected line).