-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfilters.py
58 lines (43 loc) · 1.8 KB
/
filters.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
54
55
56
57
58
"""Filtering for auto-completion."""
import sublime
class _BaseFilter(object):
"""Subclass this."""
def filter(self, view, prefix, locations, completions):
pass
class DotFilter(_BaseFilter):
"""Provide matching completions when typing a `.` after a keyword."""
def dotfilter(self, view):
dot = view.substr(sublime.Region(view.sel()[0].a - 1,
view.sel()[0].b))
is_dot = ord(dot[0]) == 46
return is_dot
def filter(self, view, prefix, locations, completions):
if self.dotfilter(view):
completions_list_filter = []
wordstart = view.word(
sublime.Region(view.sel()[0].a - 1, view.sel()[0].b)).a
prefix = view.substr(sublime.Region(wordstart, view.sel()[0].b))
for c in completions:
try:
if len(prefix) > 0:
if prefix.lower() in c[0].lower()[0:len(prefix)]:
completions_list_filter.append((c[0], c[1]))
except UnicodeDecodeError:
continue
return completions_list_filter
class FilterManager(object):
"""Store multiple filter for usage from outside."""
filters = []
def add_filter(self, cfilter, index=None):
if index is None:
self.filters.append(cfilter)
else:
self.filters.insert(index, cfilter)
def apply_filters(self, view, prefix, locations, completions):
for f in self.filters:
filtered = f.filter(view, prefix, locations, completions)
if filtered is not None:
completions = filtered
return completions
manager = FilterManager()
manager.add_filter(DotFilter())