Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better object selection #79

Open
marcospgp opened this issue Apr 21, 2023 · 5 comments
Open

Better object selection #79

marcospgp opened this issue Apr 21, 2023 · 5 comments

Comments

@marcospgp
Copy link

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
Copy link
Owner

richie5um commented Apr 21, 2023

[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
Copy link
Author

marcospgp commented Apr 22, 2023

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

  • Selecting the lines containing the object's surrounding brackets, which may include additional text (such as "myObject": { ... })
  • Selecting the lines inside the object's surrounding brackets.

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
Copy link

@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
Copy link

redactedscribe commented Jun 9, 2023

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
Copy link
Author

marcospgp commented Jun 9, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants