Sublime Forum

How to sort regions?

#1

I was wondering if there is already an existing way or package which allows to sort folded regions, one screenshot will clarify much better what i mean:

https://dl.dropboxusercontent.com/s/o601wv6o3fuke63/sublime_text_2017-11-07_21-59-22.png

The idea would be, sorting a bunch of regions where each region is treated as a “single entity”, I mean, I wouldn’t want to mess up the body content of the different regions. Personally I’d find it a really awesome way to refactor modules so the public interface becomes really tidy. Usually you sorted the API of a package in the docs but i still find it quite useful way to make coding a faster experience.

If this is not already implemented anywhere, what would it take me to write a plugin to do? Would it be very hard to achieve?

Thanks in advance.!

0 Likes

#2

the closest existing plugin I found is https://packagecontrol.io/packages/Indent%20Respectful%20Sort but it’s not really suitable for code it seems as it does “mess up the body content of the different regions”

to write your own plugin, you’ll want to make use of the undocumented view.folded_regions() method, iterate through those, get the first line of each region and sort that, then reorder the regions in the view. Obviously keeping track of region offsets as they move around will be the trickiest, it may be easier to populate a new view (maybe in an output panel) with the sorted content and then update the entire original buffer. I guess your sorting method will also need to take into account your comment lines that split the regions you want to sort.

1 Like