-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: Detect cursor position in array to sort it #2
feat: Detect cursor position in array to sort it #2
Conversation
If this works good enough we might add AST parsing for the selection the same way. That would ensure that even partially selected arrays get properly detected. Or we can ditch the selection based detection all together. Then, since we have a proper AST of the array now, we could (instead of parsing it using JSON.parse) sort the AST elements and maybe then transform the AST back to source code to not change the code formatting like it is happening now.
Thanks for contributing. I really like the idea and the usability and prospect of not fucking up the formatting, but during the code review I stumbled upon a few things on the line that invokes the acon Error handlingThis is lacking error handling. Ecmascript version and other options
Other programming languagesThis also doesn't work for typescript files. Typescript probably requires usage of the I am not sure if these are valid use-cases, but technically it is possible and I think it would be mistake to prevent users from doing this. Maybe it would be a good idea to do both. Try to parse the file and try to determine the enclosing array and if that doesn't work use the current selection. |
Right, I totally forgot about the error handling. Silly me. I'll add it. I can also try to use the typescript compiler, which comes with each VS installation to be up to date. The TSC can compile ECMA script too and I think JSX/TSX too. Of course, as I proposed, the 2 methods of detecting the sortable array should exist next to each other. |
I've checked the arcon docs again and you are right, they are not up to date with current proposals. I guess I'll select the typescript compiler coming with JS for all json/[java|type]script documents and for all other we rely on the selection algo as a fallback. |
I thought a little bit more about this issue. I think the command, in its current form, is not as widely applicable as I think, because it uses Adding support to files that are not parsable by It works something like this:
it will return the following result:
Not sure if commands that are prefix with I think we can wrap up this ticket with the extended error handling and usage of |
I created a new ticket #7 to enable sorting on JS arrays that are not valid JSON arrays. I was trying to find out whether the const formattingTextEdits: vscode.TextEdit[] = await vscode.commands.executeCommand('vscode.executeFormatRangeProvider', document.uri, new vscode.Range(selection.start, selection.end), {
tabSize: editor.options.tabSize,
insertSpaces: editor.options.insertSpaces
} as vscode.FormattingOptions) as vscode.TextEdit[]
const workspaceEdit = new vscode.WorkspaceEdit()
workspaceEdit.set(document.uri, formattingTextEdits)
await workspace.applyEdit(workspaceEdit); Maybe this is useful later. I did find the So I think this may be the best solution for parsing an array (if it actually works, which only testing can show). |
Ok, lets try that. I tried to find out how the bracket highlights work and it seems they use some kind of TextMate matcher (regex?). But this functionality is not exposed but maybe its worth a look how they detect array bounds. I also briefly thought I had figured out a reg-ex to find the outer array, but I discarded it in favour of the acorn solution which looked more solid at the time. |
That sounds interesting. Do you have a starting point in the vscode codebase for the bracket highlight functionality? For your regex solution, did you consider the following scenario: [
{
"foo": "[][][\"",
"bar": "a[bx]d",
"array": [1, 2, 3],
"baz": '][]]][][]]]]'
}
] Assuming the cursor is somewhere in the |
Sorry for the delay. Kind of busy the last 2 weeks. How do we want us to proceed here? |
Sorry for the delay. Just so I understand properly: Highlight is when you put the cursor into an array and it shows the array boundaries? And this is implemented using a regex? What is a TextMate regex? If this works with a regex, I guess that would leave us with two options, the When you post the regex here, I would be happy to test it. |
TextMate is a famous text editor on the Mac platform. I am not sure, if that really helps us though. But maybe you find the right array regex in there somewhere in |
I was in the mood to work on this a little. I used the |
This is now blocked by microsoft/vscode#91852. The selectionRangeProvider has a bug in 1.42. We must wait until the march release, 1.43. |
Ah, do you know when the release will be? |
The issue is scheduled for the March 2020 release. They have an iteration plan here microsoft/vscode#92242, which says that the release should be ready in early Apri. |
This will not be part of 1.43, but 1.44. I just tested the 1.44-insiders and the |
This is now part of |
@pke, I forgot to ask, do you want to be listed as contributor? |
Sure why not.
…-----Original Message-----
From: "fvclaus" <[email protected]>
Sent: 03/05/2020 07:50
To: "fvclaus/vsc-sort-json-array" <[email protected]>
Cc: "Philipp Kursawe" <[email protected]>; "Mention" <[email protected]>
Subject: Re: [fvclaus/vsc-sort-json-array] feat: Detect cursor position inarray to sort it (#2)
@pke, I forgot to ask, do you want to be listed as contributor?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
With this there is no need to select the array before sorting it.
Just place the cursor anywhere in the array and sort it.
If this works good enough we might add AST parsing for the selection the
same way. That would ensure that even partially selected arrays get properly
detected.
Or we can ditch the selection based detection all together.
Then, since we have a proper AST of the array now, we could (instead of
parsing it using JSON.parse) sort the AST elements and maybe then
transform the AST back to source code to not change the code
formatting like it is happening now.