Sublime Forum

Plugin to sort functions or methods alphabetically

#1

Anyone knows a plugin to sort functions or methods alphabetically? Specifically I’m interested to do that
with python code.

Related links I’ve found after googling for a while:

I’ve attached the above links to clarify a bit more what I’d like to achieve… if anyone knows a package that already does that or has a very clear idea how to code it please let me know… I’ve got in mind few ideas how I’d do that but I’d like to know if there is already safe well tested code to use in the first place.

Thx.

0 Likes

#2

I seem to remember reviewing a package for this, if that helps. Have you looked on PC yet?
Not sure if it was for python, but it could be.

Regarding coding, you would need to find classes and functions in a file (can use scope selectors), then get their indent and determine their extents from that. Afterwards you can begin with sorting them in blocks, Keyed by function name.

Possible problems: any line that is not part of a function, i. E. Class level assignments or code on the module root.

0 Likes

#3

That’s why asking if there was already a package doing that… because of the possible problems… you ask if I’ve looked at PC and after a little bit of seaching with keywords such as [“python”, “sort”, …] I’ve found this one, tried it out and it’s not exactly what I’m looking for.

Here’s the thing, I’m not looking for a super-smart package… I’d like to achieve something quite “simple”, for instance, imagine this input:

input

As you can see, there are several selection, so I’d expect to get this outcome:

output

So basically this is not a parsing problem but a text processing problem… I guess the first I need to do is figuring out the “minimal level of indentation” per selection… In the above example you can see I’m just using spaces but it’d be also cool if the code worked with tabs, so that’s the first thing I need to think of.

One of my ideas was using ast/lark/ply or other parsing libraries and sort out the parse-tree but I definitely won’t go that road… as I said, I don’t want a super-smart package.

0 Likes

#4

I think the Multi-Select-Alphabetizer Package could be part of a solution.

For ruby code I search for def and multi-select (CMD+d) the methods I want to alphabetize. I then use the jump down shortcut from the Jump-Along-Indent-Package to expand the selections until the end of the function and use “alphabetize selections” from the Multi-Select-Alphabetizer Package. This only works in languages with an explicit end for each function definition, though.

You could temporarily add a newline before each def and use sublimes built in “expand selection to paragraph” instead of jump along indent to get the right selections.

Hope this helps.

0 Likes