-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsublime_plugin.py
More file actions
153 lines (118 loc) · 3.8 KB
/
sublime_plugin.py
File metadata and controls
153 lines (118 loc) · 3.8 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
import os
import os.path
import inspect
import traceback
import imp
import sublime
import sys
import importlib
class Command(object):
def is_enabled(self, args=None):
return True
def is_visible(self, args=None):
return True
class ApplicationCommand(Command):
pass
class WindowCommand(Command):
def __init__(self, wnd):
self.window = wnd
def run_(self, kwargs):
if kwargs and 'event' in kwargs:
del kwargs['event']
if kwargs:
self.run(**kwargs)
else:
self.run()
class TextCommand(Command):
def __init__(self, view):
self.view = view
def run__(self, edit_token, kwargs):
if kwargs and 'event' in kwargs:
del kwargs['event']
if kwargs:
self.run(edit_token, **kwargs)
else:
self.run(edit_token)
class EventListener(object):
pass
def fn(fullname):
paths = fullname.split(".")
paths = "/".join(paths)
for p in sys.path:
f = os.path.abspath(os.path.join(p, paths))
if os.path.exists(f):
return f
f += ".py"
if os.path.exists(f):
return f
return None
class __myfinder:
class myloader(object):
def load_module(self, fullname):
if fullname in sys.modules:
return sys.modules[fullname]
f = fn(fullname)
if not f.endswith(".py"):
print("new module: %s" % f)
m = imp.new_module(fullname)
m.__path__ = f
sys.modules[fullname] = m
return m
return imp.load_source(fullname, f)
def find_module(self, fullname, path=None):
f = fn(fullname)
if f and "/lime/" in f: # TODO
return self.myloader()
sys.meta_path.append(__myfinder())
def reload_plugin(module):
def cmdname(name):
if name.endswith("Command"):
name = name[:-7]
ret = ""
for c in name:
l = c.lower()
if c != l and len(ret) > 0:
ret += "_"
ret += l
return ret
print("Loading plugin %s" % module)
try:
module = importlib.import_module(module)
for item in inspect.getmembers(module):
if not isinstance(item[1], type(EventListener)):
continue
try:
cmd = cmdname(item[0])
if issubclass(item[1], EventListener):
inst = item[1]()
toadd = getattr(inst, "on_query_context", None)
if toadd:
sublime.OnQueryContextGlue(toadd)
for name in ["on_load"]: # TODO
toadd = getattr(inst, name, None)
if toadd:
sublime.ViewEventGlue(toadd, name)
elif issubclass(item[1], TextCommand):
sublime.register(cmd, sublime.TextCommandGlue(item[1]))
elif issubclass(item[1], WindowCommand):
sublime.register(cmd, sublime.WindowCommandGlue(item[1]))
elif issubclass(item[1], ApplicationCommand):
sublime.register(cmd, sublime.ApplicationCommandGlue(item[1]))
except:
print("Skipping registering %s: %s" % (cmd, sys.exc_info()[1]))
if "plugin_loaded" in dir(module):
module.plugin_loaded()
except:
traceback.print_exc()
class MyLogger:
def __init__(self):
self.data = ""
def flush(self):
sublime.console(self.data)
self.data = ""
def write(self, data):
self.data += str(data)
if data.endswith("\n"):
self.data = self.data[:-1]
self.flush()
sys.stdout = sys.stderr = MyLogger()