QScintilla/Editor.py

changeset 2590
abe9b3e04381
parent 2589
a51b0c113ed7
child 2632
94121e2f55b9
--- a/QScintilla/Editor.py	Wed Apr 17 19:58:24 2013 +0200
+++ b/QScintilla/Editor.py	Thu Apr 18 19:08:09 2013 +0200
@@ -6741,14 +6741,20 @@
         """
         Public slot to sort the lines spanned by a rectangular selection.
         """
-        if not self.hasSelectedText() or not self.selectionIsRectangle():
+        if not self.selectionIsRectangle():
             return
         
         from .SortOptionsDialog import SortOptionsDialog
         dlg = SortOptionsDialog()
         if dlg.exec_() == QDialog.Accepted:
             ascending, alnum, caseSensitive = dlg.getData()
-            startLine, startIndex, endLine, endIndex = self.getRectangularSelection()
+            origStartLine, origStartIndex, origEndLine, origEndIndex = \
+                self.getRectangularSelection()
+            # convert to upper-left to lower-right
+            startLine = min(origStartLine, origEndLine)
+            startIndex = min(origStartIndex, origEndIndex)
+            endLine = max(origStartLine, origEndLine)
+            endIndex = max(origStartIndex, origEndIndex)
             
             # step 1: extract the text of the rectangular selection and the lines
             selText = {}
@@ -6779,10 +6785,18 @@
                 keyFun = None
             
             # step 3: sort the lines
+            eol = self.getLineSeparator()
+            lastWithEol = True
             newLines = []
             for txt in sorted(selText.keys(), key=keyFun, reverse=reverse):
                 for line in selText[txt]:
-                    newLines.append(txtLines[line])
+                    txt = txtLines[line]
+                    if not txt.endswith(eol):
+                        lastWithEol = False
+                        txt += eol
+                    newLines.append(txt)
+            if not lastWithEol:
+                newLines[-1] = newLines[-1][:-len(eol)]
             
             # step 4: replace the lines by the sorted ones
             self.setSelection(startLine, 0, endLine + 1, 0)
@@ -6791,4 +6805,6 @@
             self.endUndoAction()
             
             # step 5: reset the rectangular selection
-            self.setRectangularSelection(startLine, startIndex, endLine, endIndex)
+            self.setRectangularSelection(origStartLine, origStartIndex,
+                                         origEndLine, origEndIndex)
+            self.selectionChanged.emit()

eric ide

mercurial