-
Notifications
You must be signed in to change notification settings - Fork 5
/
FindCallers.py
102 lines (80 loc) · 3.02 KB
/
FindCallers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sublime
import sublime_plugin
import threading
import os
import time
import sys
if sys.version_info < (3,):
from BaseCommand import BaseCommand
from BaseThread import BaseThread
from lib.show_error import *
from lib.trim_paths_of_root import trim_paths_of_root
from lib.printer import p
from node_dependents_editor_backend import backend
else:
from .BaseCommand import BaseCommand
from .BaseThread import BaseThread
from .lib.show_error import *
from .lib.trim_paths_of_root import trim_paths_of_root
from .lib.printer import p
from .node_dependents_editor_backend import backend
class FindCallersCommand(BaseCommand, sublime_plugin.WindowCommand):
def run(self, modifier=''):
if super(FindCallersCommand, self).run(modifier):
self.init_thread(FindCallersThread, 'Finding callers')
class FindCallersThread(BaseThread):
def __init__(self, command):
self.window = command.window
self.view = command.view
threading.Thread.__init__(self)
def run(self):
"""
Finds the files that call the current file
"""
function_name = self.get_function_name()
self.callers = trim_paths_of_root(self.get_callers(function_name), self.window.config['directory'])
if self.view.modifier and self.view.modifier == 'OPEN_ALL':
for driver in self.callers:
self.open_file(driver)
return
if len(self.callers) == 1:
self.open_file(self.callers[0])
else:
sublime.set_timeout(self.show_quick_panel, 10)
def get_function_name(self):
selections = self.view.sel()
if selections:
region = selections[0]
if region.a == region.b:
selected_word = self.view.substr(self.view.word(region))
p('selected word: ', selected_word)
return selected_word
return None
def get_callers(self, function_name):
"""
Asks the node tool for the drivers of the current module
"""
args = {
'filename': self.view.filename,
'path': function_name,
'command': 'find-callers'
}
fetch_time = time.time()
callers = [c for c in backend(args) if c]
p('Fetch time:', time.time() - fetch_time)
p(len(callers), 'callers found:\n' + '\n'.join(callers))
return callers
def show_quick_panel(self):
if not self.callers:
show_error('Can\'t find any callers of that function')
return
self.window.show_quick_panel(self.callers, self.on_done)
def on_done(self, picked):
if picked == -1:
return
caller = self.callers[picked]
self.open_file(caller)
def open_file(self, caller):
# We removed the root originally when populating the dependents list
filename = os.path.normpath(os.path.join(self.window.config['directory'], caller))
super(FindCallersThread, self).open_file(filename)