Repository navigation
Expand file tree
/
Copy pathSortNumericallyCommand.py
More file actions
47 lines (33 loc) · 1.54 KB
/
Copy pathSortNumericallyCommand.py
File metadata and controls
47 lines (33 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# encoding: utf-8
import os
import sys
import sublime
import sublime_plugin
sys.path.append(os.path.dirname(sys.executable))
try:
# This import method works in Sublime Text 2.
from sort_numerically.sort_numerically import sort_lines
except ImportError:
# While this works in Sublime Text 3.
from .sort_numerically.sort_numerically import sort_lines
# The buffer always uses \n, whatever the line endings setting of the view says.
# That setting only applies when the file is saved.
LINE_ENDING_CHARACTER = '\n'
class SortNumericallyCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Work on a copy of the selection. Replacing whole lines can merge
# selections that share a line. The copy stays valid, because sorting
# does not change the length of the text.
regions = list(self.view.sel())
if len(regions) == 1 and regions[0].empty():
# Selection is empty, use the entire buffer.
regions = [sublime.Region(0, self.view.size())]
for region in regions:
lines = self.view.lines(region)
input_lines = [self.view.substr(r) for r in lines]
sorted_lines = sort_lines(input_lines)
output = LINE_ENDING_CHARACTER.join(sorted_lines)
# Replace whole lines, also when the selection starts or ends inside
# a line. The line ending after the last line stays in place.
whole_lines = sublime.Region(lines[0].begin(), lines[-1].end())
self.view.replace(edit, whole_lines, output)