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

New default sortables #2

Open
mtrajano opened this issue Aug 6, 2024 · 9 comments
Open

New default sortables #2

mtrajano opened this issue Aug 6, 2024 · 9 comments

Comments

@mtrajano
Copy link
Owner

mtrajano commented Aug 6, 2024

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

@ic-768
Copy link

ic-768 commented Aug 7, 2024

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

mtrajano commented Aug 8, 2024

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

fbearoff commented Aug 8, 2024

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

sortables = {
  r = { 
    arguments= {
      node = "argument",
    },
  },
},

@uwla
Copy link

uwla commented Aug 12, 2024

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

ic-768 commented Aug 12, 2024

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

uwla commented Aug 12, 2024

Yes, it is attribute

@mtrajano
Copy link
Owner Author

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

@uwla This commit should have fixed it: d4e95b4, let me know if that works for you, thanks!

@uwla
Copy link

uwla commented Aug 13, 2024

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

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