For things like:
def a(): stmt1 stmt2 def b(): stmt3
Or C:
void a() { stmt1; stmt2; } void b() { stmt3; }
You will need enough semantic knowledge to determine that the empty space between stmt1 and stmt2 is still part of a .
For python, this means that you are reading ahead to find the first line that is not empty or indented. You will also need to consider nested indentation (when functions are part of a class or module, and def already indented).
For C, you need to read ahead while matching the parenthesis β this means that you will need to consider nested braces.
There is a similar topic regarding C ++ that has not been resolved: Automatically sort functions alphabetically in C ++ code
I believe this is not trivial in the general case, and you would be better off using yacc or some other semantic parser. You can also manually add markers for start and end and do something similar to the kev clause.
MaRkNeXt def a(): stmt1 stmt2 MaRkNeXt def b(): stmt3 MaRkNeXt
Then something like:
:%s/$/$/ :g/^MaRkNeXt/,/MaRkNeXt/-1join! :%sort :%s/\$/\r/g :g/MaRkNeXt/d
source share