Tyriar / vscode-sort-lines

Visual Studio Code extension to sort lines of text
https://marketplace.visualstudio.com/items/Tyriar.sort-lines
MIT License
146 stars 43 forks source link

[feature request] Sort lines by selection #129

Open amok opened 9 months ago

amok commented 9 months ago

Use case: sort lines by keys

Example

1) input:

type Data = {
  buyers?: string[];
  recordingDate: SomeRecordingDate;
  saleDate: SaleDateType;
  documentNumber: string;
  documentType: string;
  resale?: boolean;
};

2) Select keys

image

3) Execute "sort lines" extension command

4) output:

type Data = {
  buyers?: string[];
  resale?: boolean;
  saleDate: SaleDateType;
  documentType: string;
  recordingDate: SomeRecordingDate;
  documentNumber: string;
};

This would be similar to "JSON: sort by keys", but this functionality is also useful for TS', Python' and other kind of objects, dicts, and also imports etc.

Tyriar commented 9 months ago

You want to sort lines by the length of the key?

amok commented 9 months ago

You want to sort lines by the length of the key?

Not really. I'd like to sort by the length of what I selected Here is another example

input: image

output: image


PS The problems with "sort by keys" is with this "what is the key?" built in logic F.e, I have Sort JSON extension installed but almost never use it :) since it works only on valid JSON (quoted keys and no extra commas and etc) - using that extension I cannot easily "sort by keys" named Python (see below) dict or TypeScript (example in the description) type definition, or.. what about PHP associative array?

thisdict = { # <----- oops, this is "invalid JSON" line
  "brand": "Ford",
  "year": 1964
}
array( // And this does not look like JSON at all
  "Peter"=>"35",
  "Ben"=>37
);

Whether it would be really convenient to use cmd+d (maybe in combination with cmd+k cmd+d) or cmd+shift+L to "select all occurrences" and then option+shift+left to "select to the left" and the "sort lines by selection"

https://github.com/Tyriar/vscode-sort-lines/assets/1369559/f8984d05-2edc-4da9-a6ce-95857aec1494

ranolfi commented 1 month ago

@amok, does the following extension do exactly what you want? Sort Lines by Selection by earshinov.

amok commented 1 month ago

@amok, does the following extension do exactly what you want? Sort Lines by Selection by earshinov.

Unfortunately, nope. It applies natural sort to the selection. The issue primarily is about sorting by length, something like:

const selected = ['aaa', 'aa', 'xx', 'fffff']
const sorted = selected.toSorted((a, b) => a.length - b.length)
// --> ['aa', 'xx', 'aaa', 'fffff']