vscode-yaml-sort icon indicating copy to clipboard operation
vscode-yaml-sort copied to clipboard

Add mecanics that sorts `YAML` blocks of defined level

Open Gregory108 opened this issue 3 years ago • 5 comments

Hello! Not sure it is easily possible. As far as I understand, currently the plugin sorts keys inside the last "leaves" of the yaml "tree" alphabetically or along with custom order.

I do not want to change the insides of the keys. But I do want to sort alphabetically by the values of the "top" keys of a certain level.

I want to sort blocks of level 3 by values of name:s, without changing/formatting/sorting the content of the block. including other keys used in the block. And, apparently, only withing the scope of the containing block 2 - not across all blocks of the 2nd level. E.g.:

models: # level 1
  - name: database_name # level 2
    tables:
      - name: B_table_name # level 3, member 1
        description: >
          Something 1
      - name: A_table_name # level 3, member 2
        meta:
          owner: me # level 4 (maybe? by indentation)
        description: >
          Something 2
          
          * markdown item 1

          * markdown item 2

Into this:

models:
  - name: database_name
    tables:
      - name: A_table_name
        meta:
          owner: me
        description: >
          Something 2
          
          * markdown item 1

          * markdown item 2
      
      - name: B_table_name
        description: >
          Something 1

Without sorting name <-> description inside the folding block of level 4 and without squashing lines. Currently YAML-Sort returns:

---
models:
  - name: database_name
    tables:
      - description: |
          Something 1
        name: B_table_name
      - description: |
          Something 2
          * markdown item 1
          * markdown item 2
        meta:
          owner: me
        name: A_table_name

-> even with some extra --ses and changes > to | which is okay, but it would be nicer to keep the original formatting. And (!) not sorting blocks between each other

Gregory108 avatar Nov 11 '22 17:11 Gregory108

I second this request 🙏

If we were able to sort only at 2nd level, I could sort my .gitlab-ci.yml keywords. That would be so useful.

Thanks.

bcouetil avatar Dec 29 '22 16:12 bcouetil

I'm discovering the useCustomSortRecursively. This sorts too much, and removes blank lines, which is not desirable...

So only level 2 sorting would be great !

bcouetil avatar Dec 29 '22 18:12 bcouetil

For anyone coming here searching for a way to sort gitlab jobs attributes : waiting for the feature, I asked ChatGPT to make me a script, which works perfectly : https://dev.to/zenika/chatgpt-if-you-please-make-me-a-gitlab-jobs-attributes-sorter-3co3

bcouetil avatar Mar 30 '23 09:03 bcouetil

+1 this would be an incredibly useful feature

Lif3line avatar Oct 13 '23 08:10 Lif3line

+1 this was the original feature I've needed

Andracles avatar Oct 25 '24 22:10 Andracles