mtrajano / tssorter.nvim

Sort almost anything in neovim using treesitter
MIT License
47 stars 0 forks source link

New default sortables #2

Open mtrajano opened 1 month ago

mtrajano commented 1 month ago

Submit requests for any new default sortables in this issue. Please include the filetype and the behavior you think should be default.

ic-768 commented 1 month ago

Hey, great work on this plugin, it's really cool!

The first thing I went for was trying to sort the values of an array in typescript. This code works for string and number values :

        sortables = {
          typescript = { -- filetype
            array = { -- sortable name
              node = { "number", "string" }, -- treesitter node to capture

              order_by = function(node1, node2)

                local line1 = require("tssorter.tshelper").get_text(node1)
                local line2 = require("tssorter.tshelper").get_text(node2)

                return line1 < line2
              end,
            },
          },
        },

Small caveat, there are many filetypes that the sortables should apply to. javascript,typescript, javascriptreact,typescriptreact, and other framework-specific filetypes.

So maybe it would make sense to extract some defaults and let users apply them to the filetypes that they're interested in, e.g. vue/astro/svelte

mtrajano commented 1 month ago

Hey, glad you like the plugin!

This looks good. Just a heads up the order_by is optional as that's the default behavior (sort alphabeticlaly) so this can be simplified to:

sortables = {
  typescript = { 
    array = {
      node = { "number", "string" },
    },
  },
},

Yes unfortunately javascript has a lot of filetypes 😅. With the current implementation you would have to define the same sortable for each filetype (assuming they all have the same capture nodes). I think the idea of having defaults that can be reused across multiple filetypes is interesting, I can definitely look into that. Another idea worth looking at would be to have filetype groups.

I'm currently doing some clean up of the code and adding testing to make it easier to contribute to so I'll have to get back to this at a later date. I appreciate the issue though!

fbearoff commented 1 month ago

Sorting a list of arguments in R works with the following config:

sortables = {
  r = { 
    arguments= {
      node = "argument",
    },
  },
},
uwla commented 1 month ago

What is the config to sort HTML attributes in a PHP ? When I use php = { attributes = { node = 'attribute' } } it gives the warning no sortable node under cursor, even though there is. I'd like to sort HTML attributes in PHP and in Vue files.

ic-768 commented 1 month ago

What is the config to sort HTML attributes in a PHP ? When I use php = { attributes = { node = 'attribute' } } it gives the warning no sortable node under cursor, even though there is. I'd like to sort HTML attributes in PHP and in Vue files.

Have you tried running :InspectTree to see the treesitter node tree and make sure that the node is really an attribute?

uwla commented 1 month ago

Yes, it is attribute

mtrajano commented 1 month ago

Hey @uwla thank you for the ticket. I was able to repro this as well. It looks like I need to take injected languages into account when getting the current node under the cursor. It was completely ignoring those attribute nodes and jumping straight to the top level text node. I'll have a fix out in a few minutes.

mtrajano commented 1 month ago

@uwla This commit should have fixed it: https://github.com/mtrajano/tssorter.nvim/commit/d4e95b46e33c65c620bb0a6b842937ea86acc178, let me know if that works for you, thanks!

uwla commented 1 month ago

@mtrajano It does work now, at least in PHP. THanks!