Sublime Forum

Next line is included when sorting a multi-line selection

#1

Hi,

If I triple-click to select several lines, the selection outline highlights the lines as expected. If I then go Edit -> Sort Lines, the selected lines are sorted as expected, but the selection appears to have been extended to the line after my selection.

This is not what I expected, and is not helpful. What I expect is that only the selected lines should be sorted, and that the selection not be extended to the following line.

This behaviour has been in Sublime for ages.

Video showing this behaviour with Sublime Text build 3142 on Mac OS X 10.12.6.

David B.

0 Likes

#2

This looks like it’s because the code in Default\sort.py expands a non-empty selection so that it fills all lines that span the selection, and with the cursor at the start of the next line it’s technically a part of the selection and so the selection fills the entire line.

Trivially, a patch such as the following stops this from happening by checking if the column that the cursor is on in a non-empty selection is 0, and if so backing it up one so that it appears at the end of the prior line:

--- Shipped Packages/Default/sort.py	2016-06-22 14:24:42
+++ Packages/Default/sort.py	2017-08-15 16:25:26
@@ -100,6 +100,8 @@
 
     for sel in v.sel():
         if not sel.empty():
+            if v.rowcol(sel.end())[1] == 0:
+                sel = sublime.Region(sel.a, sel.b - 1)
             regions.append(v.line(sw(v, v.line(sel))))
             v.sel().subtract(sel)
 

With this in place, you get the behaviour that you would expect, as seen here. Note however that I have by no means done any sort of exhaustive testing to ensure that this would work in all selection cases.

3 Likes