forked from eladyarkoni/MySignaturePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysign.py
129 lines (116 loc) · 3.86 KB
/
mysign.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
#-----------------------------------------------------------------------------------
# MySignature Sublime Text Plugin
# Author: Elad Yarkoni
# Version: 1.0
# Description: Sublime text autocomplete improvements:
# - showing javascript methods with parameters
#-----------------------------------------------------------------------------------
import sublime, sublime_plugin, os, re, threading
from os.path import basename
#
# Method Class
#
class Method:
_name = ""
_signature = ""
_filename = ""
def __init__(self, name, signature, filename):
self._name = name
self._filename = filename;
self._signature = signature
def name(self):
return self._name
def signature(self):
return self._signature
def filename(self):
return self._filename
#
# MySign Class
#
class MySign:
_functions = []
MAX_WORD_SIZE = 100
MAX_FUNC_SIZE = 50
def clear(self):
self._functions = []
def addFunc(self, name, signature, filename):
self._functions.append(Method(name, signature, filename))
def get_autocomplete_list(self, word):
autocomplete_list = []
for method_obj in self._functions:
if word in method_obj.name():
method_str_to_append = method_obj.name() + '(' + method_obj.signature()+ ')'
method_file_location = method_obj.filename();
autocomplete_list.append((method_str_to_append + '\t' + method_file_location,method_str_to_append))
return autocomplete_list
def is_javascript_file(filename):
return '.js' in filename
#
# MySign Collector Thread
#
class MySignCollectorThread(threading.Thread):
def __init__(self, collector, open_folder_arr, timeout_seconds):
self.collector = collector
self.timeout = timeout_seconds
self.open_folder_arr = open_folder_arr
threading.Thread.__init__(self)
#
# Get all method signatures
#
def save_method_signature(self, file_name):
file_lines = open(file_name, 'rU')
for line in file_lines:
if "function" in line:
matches = re.search('(\w+)\s*[: | =]\s*function\s*\((.*)\)', line)
matches2 = re.search('function\s*(\w+)\s*\((.*)\)', line)
if matches != None and (len(matches.group(1)) < self.collector.MAX_FUNC_SIZE and len(matches.group(2)) < self.collector.MAX_FUNC_SIZE):
self.collector.addFunc(matches.group(1), matches.group(2), basename(file_name))
elif matches2 != None and (len(matches2.group(1)) < self.collector.MAX_FUNC_SIZE and len(matches2.group(2)) < self.collector.MAX_FUNC_SIZE):
self.collector.addFunc(matches2.group(1), matches2.group(2), basename(file_name))
#
# Get Javascript files paths
#
def get_javascript_files(self, dir_name, *args):
fileList = []
for file in os.listdir(dir_name):
dirfile = os.path.join(dir_name, file)
if os.path.isfile(dirfile):
fileName, fileExtension = os.path.splitext(dirfile)
if fileExtension == ".js" and ".min." not in fileName:
fileList.append(dirfile)
elif os.path.isdir(dirfile):
fileList += self.get_javascript_files(dirfile, *args)
return fileList
def run(self):
for folder in self.open_folder_arr:
jsfiles = self.get_javascript_files(folder)
for file_name in jsfiles:
self.save_method_signature(file_name)
def stop(self):
if self.isAlive():
self._Thread__stop()
#
# MySign Collector Class
#
class MySignCollector(MySign, sublime_plugin.EventListener):
_collector_thread = None
#
# Invoked when user save a file
#
def on_post_save(self, view):
self.clear()
open_folder_arr = view.window().folders()
if self._collector_thread != None:
self._collector_thread.stop()
self._collector_thread = MySignCollectorThread(self, open_folder_arr, 30)
self._collector_thread.start()
#
# Change autocomplete suggestions
#
def on_query_completions(self, view, prefix, locations):
current_file = view.file_name()
completions = []
if is_javascript_file(current_file):
return self.get_autocomplete_list(prefix)
completions.sort()
return (completions,sublime.INHIBIT_EXPLICIT_COMPLETIONS)