forked from akrabat/SublimeFunctionNameDisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionNameStatus.py
141 lines (117 loc) · 4.81 KB
/
FunctionNameStatus.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import os
import sublime
import sublime_plugin
import thread
import re
from time import time, sleep
# Ideas taken from C0D312, nizur & tito in
# http://www.sublimetext.com/forum/viewtopic.php?f=2&t=4589
s = sublime.load_settings('Function Name Display.sublime-settings')
class Pref:
def load(self):
Pref.display_file = s.get('display_file', False)
Pref.display_class = s.get('display_class', False)
Pref.display_function = s.get('display_function', True)
Pref.display_arguments = s.get('display_arguments', False)
Pref.wait_time = 0.12
Pref.time = time()
Pref.modified = False
Pref = Pref()
Pref.load()
s.add_on_change('reload', lambda:Pref.load())
clean_name = re.compile('^\s*(public\s+|private\s+|protected\s+|static\s+|function\s+|def\s+)+', re.I)
class FunctionNameStatusEventHandler(sublime_plugin.EventListener):
def on_load(self, view):
Pref.time = time()
Pref.modified = True
view.settings().set('function_name_status_row', -1)
sublime.set_timeout(
lambda: self.display_current_class_and_function(view,
'on_load'), 0)
def get_filename(self, view):
s = ""
if Pref.display_file == "path":
s = view.file_name()
else:
s = os.path.basename(view.file_name())
if s:
return s
return s
def on_modified(self, view):
Pref.time = time()
Pref.modified = True
# Event handlers
def on_selection_modified(self, view):
now = time()
if now - Pref.time > Pref.wait_time:
Pref.time = now
Pref.modified = False
sublime.set_timeout(
lambda:self.display_current_class_and_function(view,
'on_selection_modified'), 0)
else:
Pref.modified = True
Pref.time = now
# display the current class and function name
def display_current_class_and_function(self, view, where):
view_settings = view.settings()
if view_settings.get('is_widget'):
return
for region in view.sel():
region_row, region_col = view.rowcol(region.begin())
if region_row != view_settings.get('function_name_status_row', -1):
view_settings.set('function_name_status_row', region_row)
else:
return
# print 'running from '+where
s = [self.get_filename(view)]
# Look for any classes
if Pref.display_class:
class_regions = view.find_by_selector('entity.name.type.class')
for r in reversed(class_regions):
row, col = view.rowcol(r.begin())
if row <= region_row:
s.append(view.substr(r))
break
# Look for any functions
if Pref.display_function:
function_regions = view.find_by_selector('meta.function')
if function_regions:
for r in reversed(function_regions):
row, col = view.rowcol(r.begin())
if row <= region_row:
lines = view.substr(r).splitlines()
name = clean_name.sub('', lines[0])
if Pref.display_arguments:
s += name.strip()
else:
if 'C++' in view.settings().get('syntax'):
if (Pref.display_class or
len(name.split('(')[0].split('::')) < 2):
s.append(name.split('(')[0].strip())
else:
s.append(name.split('(')[0].split('::')[1].strip())
else:
s.append(name.split('(')[0].split(':')[0].strip())
break
if s:
s = "|[ %s ]|" % " :: ".join(s)
view.set_status('function', s)
return
view.erase_status('function')
function_name_run_call = FunctionNameStatusEventHandler().display_current_class_and_function
def function_name_run():
Pref.modified = False
Pref.time = time()
window = sublime.active_window()
view = window.active_view() if window != None else None
if view:
function_name_run_call(view, 'thread')
def function_name_loop():
while True:
if Pref.modified == True and time() - Pref.time > Pref.wait_time:
sublime.set_timeout(lambda: function_name_run(), 0)
sleep(0.5)
if not 'running_function_name_loop' in globals():
running_function_name_loop = True
thread.start_new_thread(function_name_loop, ())