-
Notifications
You must be signed in to change notification settings - Fork 0
/
prclt.py
62 lines (47 loc) · 1.57 KB
/
prclt.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
59
60
61
62
import sublime
import sublime_plugin
import re
import itertools
def extract_src(f):
match = re.search(r'rosetta\/(src\/.*?)$', f)
if not match:
return None
return match.group(1)
def extract_non_src(f):
match = re.search(r'rosetta\/(?!src\/)(.*?)$', f)
if not match:
return None
return match.group(1)
PATH_EXTRACTERS = [
extract_src,
extract_non_src,
]
class PrcltFinderCommand(sublime_plugin.TextCommand):
def run(self, edit):
arrs_of_exprs = [
compute_file_name_regexes(self.view.file_name(), extracter)
for extracter in PATH_EXTRACTERS]
flat_exprs = itertools.chain.from_iterable(arrs_of_exprs)
exprs = list(filter(None, flat_exprs))
if len(exprs):
find = '|'.join(exprs)
sublime.active_window()\
.run_command('show_panel',
{'panel': 'find_in_files'})
cb = sublime.get_clipboard()
sublime.set_clipboard(find)
sublime.active_window().run_command('paste')
sublime.set_clipboard(cb)
def compute_file_name_regexes(file_name, extracter):
file_name = extracter(file_name)
if not file_name:
return []
# optional /src/
file_name = re.sub(r'src\/', '(src\/)?', file_name)
# optional index.js
file_name = re.sub(r'\/index\.js$', '(\/index(.js)?)?', file_name)
# escape other extensions
file_name = re.sub(r'\.(.*?)$',
lambda m: '(\.%s)?' % m.group(1),
file_name)
return [file_name]