-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathpipe_join.py
More file actions
72 lines (54 loc) · 1.77 KB
/
Copy pathpipe_join.py
File metadata and controls
72 lines (54 loc) · 1.77 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sublime_plugin
class ReversePipeJoinMultilineCommand(sublime_plugin.TextCommand):
def run(self, edit):
"""
BEFORE
blue
red
green
purple
orange
yellow
AFTER
yellow|
red|
purple|
orange|
green|
blue
"""
if len(self.view.sel()) < 1:
# no text is selected
return
# get the first region
reg = self.view.sel()[0]
# convert region to string
selection = self.view.substr(reg).strip()
values = selection.split("|")
values = strip_whitespace(values)
# remove duplicates and reverse sort
values = sorted(set(values), reverse=True)
self.view.replace(edit, reg, "|\n".join(values))
class ReversePipeJoinCommand(sublime_plugin.TextCommand):
def run(self, edit):
"""
>>> ReversePipeJoinCommand().run('blue|red|green|purple|orange|yellow')
'yellow|red|purple|orange|green|blue'
"""
if len(self.view.sel()) < 1:
# no text is selected
return
# get the first region
reg = self.view.sel()[0]
# convert region to string
selection = self.view.substr(reg).strip()
values = selection.split("|")
values = strip_whitespace(values)
# remove duplicates and reverse sort
values = sorted(set(values), reverse=True)
self.view.replace(edit, reg, "|".join(values))
def strip_whitespace(values):
"""Strip leading and trailing whitespace from each string in a list of
strings. If the string only contains whitespace, filter it out.
"""
return [v.strip() for v in values if v.strip()]