forked from elm-community/SublimeElmLanguageSupport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elm_plugin.py
68 lines (58 loc) · 2.21 KB
/
elm_plugin.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
import sublime
import sublime_plugin
import os.path as fs
def is_ST2():
return sublime.version().startswith('2')
def get_string(key, *args):
strings = sublime.load_settings('Elm User Strings.sublime-settings')
return strings.get('logging.prefix') + strings.get(key).format(*args)
def log_string(key, *args):
def log_string_with_retry(retry):
try:
# ST2: RuntimeError: Must call on main thread
settings = sublime.load_settings('Elm Language Support.sublime-settings')
except RuntimeError:
if retry:
sublime.set_timeout(lambda: log_string_with_retry(False), 0)
else:
import traceback
traceback.print_exc()
else:
if settings.get('debug'):
print(get_string(key, *args))
log_string_with_retry(True)
def import_module(path):
names = path.split('.')
index = 1 if is_ST2() else 0
base = __import__(names[index])
for name in names[index + 1:]:
base = getattr(base, name)
return base
# defer import as long as possible in case plugin not loaded
def replace_base_class(path):
def splice_bases(old_base, *extra_bases):
try:
new_base = import_module(path)
except ImportError:
module_name = path[:path.index('.')]
log_string('logging.missing_plugin', module_name)
return None
else:
return (new_base,) + extra_bases
def monkey_patch(target_cls):
if not hasattr(target_cls, 'is_patched'):
new_bases = splice_bases(target_cls.__bases__)
target_cls.is_patched = bool(new_bases)
if new_bases:
target_cls.__bases__ = new_bases
def decorator(target_cls):
def new(cls, *args, **kwargs):
monkey_patch(target_cls)
super_ = super(target_cls, cls).__new__
# TypeError: object() takes no parameters
return super_(cls) if super_ is object.__new__ else super_(cls, *args, **kwargs)
assert '__new__' not in target_cls.__dict__
# ST2: TypeError: unbound method new()
target_cls.__new__ = classmethod(new)
return target_cls
return decorator