-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_selections.py
executable file
·53 lines (42 loc) · 1.71 KB
/
filter_selections.py
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
#coding: utf8
#################################### IMPORTS ###################################
# Sublime Libs
import sublime_plugin
################################### COMMANDS ###################################
class SelectionFilterBase(sublime_plugin.TextCommand):
def is_enabled(self):
return len(self.view.sel()) > 1
def run(self, edit, **args):
self.setup(self.view)
filter_sels(self.view, self.filter_func)
self.view.show(self.view.sel())
def setup(self, view):
pass
class RemoveDuplicateSelections(sublime_plugin.TextCommand):
def run(self, edit):
replace_sels(self.view, list(
dict((self.view.substr(s), s) for s in reversed(self.view.sel()))
.values()))
class RestrictSelectionsToVisibleRegion(SelectionFilterBase):
def is_enabled(self):
vis = self.view.visible_region()
enabled = len([r for r in self.view.sel() if vis.intersects(r)]) > 0
return enabled
def setup(self, view):
self.visible_region = view.visible_region()
def filter_func(self, s):
return self.visible_region.contains(s)
class RemoveEmptySelections(SelectionFilterBase):
def filter_func(self, s):
return not s.empty()
#################################### HELPERS ###################################
def filter_sels(view, filter_func):
selection_set = view.sel()
for i in range(len(selection_set)-1, -1, -1):
if not filter_func(selection_set[i]): del selection_set[i]
return selection_set
def replace_sels(view, new_sels):
view.sel().clear()
view.sel().add_all(new_sels)
view.show(view.sel())
################################################################################