PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cmder_mini/vendor/SublimeText/sublime_plugin.py

https://gitlab.com/MarcuSacramento/ferramentas
Python | 371 lines | 287 code | 77 blank | 7 comment | 83 complexity | 063978da3fb756e8fddfba866a662218 MD5 | raw file
  1. import os
  2. import sys
  3. import time
  4. import sublime
  5. import imp
  6. application_command_classes = []
  7. window_command_classes = []
  8. text_command_classes = []
  9. all_command_classes = [application_command_classes, window_command_classes, text_command_classes]
  10. all_callbacks = {'on_new': [], 'on_clone': [], 'on_load': [], 'on_close': [],
  11. 'on_pre_save': [], 'on_post_save': [], 'on_modified': [],
  12. 'on_selection_modified': [],'on_activated': [], 'on_deactivated': [],
  13. 'on_project_load': [], 'on_project_close': [], 'on_query_context': [],
  14. 'on_query_completions': []}
  15. def unload_module(module):
  16. if "unload_handler" in module.__dict__:
  17. module.unload_handler()
  18. # Unload the old plugins
  19. if "plugins" in module.__dict__:
  20. for p in module.plugins:
  21. for cmd_cls_list in all_command_classes:
  22. try:
  23. cmd_cls_list.remove(p)
  24. except ValueError:
  25. pass
  26. for c in all_callbacks.values():
  27. try:
  28. c.remove(p)
  29. except ValueError:
  30. pass
  31. def unload_plugin(fname):
  32. print "Unloading plugin", fname
  33. modulename, ext = os.path.splitext(os.path.basename(fname))
  34. was_loaded = modulename in sys.modules
  35. if was_loaded:
  36. m = __import__(modulename)
  37. unload_module(m)
  38. def reload_plugin(fname):
  39. print "Reloading plugin", fname
  40. path = os.path.dirname(fname)
  41. # Change the current directory to that of the module. It's not safe to just
  42. # add the modules directory to sys.path, as that won't accept unicode paths
  43. # on Windows
  44. oldpath = os.getcwdu()
  45. os.chdir(path)
  46. modulename, ext = os.path.splitext(os.path.basename(fname))
  47. if modulename in sys.modules:
  48. unload_module(sys.modules[modulename])
  49. m_info = imp.find_module(modulename, ["."])
  50. m = imp.load_module(modulename, *m_info)
  51. # Restore the current directory
  52. os.chdir(oldpath)
  53. module_plugins = []
  54. for type_name in dir(m):
  55. try:
  56. t = m.__dict__[type_name]
  57. if t.__bases__:
  58. is_plugin = False
  59. if issubclass(t, ApplicationCommand):
  60. application_command_classes.append(t)
  61. is_plugin = True
  62. if issubclass(t, WindowCommand):
  63. window_command_classes.append(t)
  64. is_plugin = True
  65. if issubclass(t, TextCommand):
  66. text_command_classes.append(t)
  67. is_plugin = True
  68. if is_plugin:
  69. module_plugins.append(t)
  70. if issubclass(t, EventListener):
  71. obj = t()
  72. for p in all_callbacks.iteritems():
  73. if p[0] in dir(obj):
  74. p[1].append(obj)
  75. module_plugins.append(obj)
  76. except AttributeError:
  77. pass
  78. if len(module_plugins) > 0:
  79. m.plugins = module_plugins
  80. def create_application_commands():
  81. cmds = []
  82. for class_ in application_command_classes:
  83. cmds.append(class_())
  84. return cmds
  85. def create_window_commands(window):
  86. cmds = []
  87. for class_ in window_command_classes:
  88. cmds.append(class_(window))
  89. return cmds
  90. def create_text_commands(view):
  91. cmds = []
  92. for class_ in text_command_classes:
  93. cmds.append(class_(view))
  94. return cmds
  95. EVENT_TIMEOUT = 0.2
  96. FAST_EVENT_TIMEOUT = 1 / 60.0
  97. first_time_msgs = set()
  98. msgs = set()
  99. def show_timeout(plugin_name, elapsed, callback):
  100. global first_time_msgs
  101. global msgs
  102. key = plugin_name + callback
  103. msg = ("A plugin (%s) may be making Sublime Text unresponsive by taking too " +
  104. "long (%fs) in its %s callback.\n\nThis message can be disabled via the " +
  105. "detect_slow_plugins setting") % (plugin_name, elapsed, callback)
  106. # Give plugins one chance to respond slowly, to handle any initialisation issues etc.
  107. # This allowance may be removed in the future due to startup time concerns
  108. if not key in first_time_msgs:
  109. first_time_msgs.add(key)
  110. return
  111. if not key in msgs:
  112. msgs.add(key)
  113. if sublime.load_settings('Preferences.sublime-settings').get('detect_slow_plugins', True):
  114. sublime.error_message(msg)
  115. blocking_api_call_count = 0
  116. def on_blocking_api_call():
  117. global blocking_api_call_count
  118. blocking_api_call_count += 1
  119. def run_timed_function(f, name, event_name, timeout):
  120. global blocking_api_call_count
  121. t0 = time.time()
  122. blocking_count = blocking_api_call_count
  123. ret = f()
  124. elapsed = time.time() - t0
  125. if elapsed > timeout and blocking_api_call_count == blocking_count:
  126. show_timeout(name, elapsed, event_name)
  127. return ret
  128. def on_new(v):
  129. for callback in all_callbacks['on_new']:
  130. run_timed_function(lambda: callback.on_new(v),
  131. callback.__module__, "on_new", EVENT_TIMEOUT)
  132. def on_clone(v):
  133. for callback in all_callbacks['on_clone']:
  134. run_timed_function(lambda: callback.on_clone(v),
  135. callback.__module__, "on_clone", EVENT_TIMEOUT)
  136. def on_load(v):
  137. for callback in all_callbacks['on_load']:
  138. run_timed_function(lambda: callback.on_load(v),
  139. callback.__module__, "on_load", EVENT_TIMEOUT)
  140. def on_close(v):
  141. for callback in all_callbacks['on_close']:
  142. run_timed_function(lambda: callback.on_close(v),
  143. callback.__module__, "on_close", EVENT_TIMEOUT)
  144. def on_pre_save(v):
  145. for callback in all_callbacks['on_pre_save']:
  146. run_timed_function(lambda: callback.on_pre_save(v),
  147. callback.__module__, "on_pre_save", EVENT_TIMEOUT)
  148. def on_post_save(v):
  149. for callback in all_callbacks['on_post_save']:
  150. run_timed_function(lambda: callback.on_post_save(v),
  151. callback.__module__, "on_post_save", EVENT_TIMEOUT)
  152. def on_modified(v):
  153. for callback in all_callbacks['on_modified']:
  154. run_timed_function(lambda: callback.on_modified(v),
  155. callback.__module__, "on_modified", FAST_EVENT_TIMEOUT)
  156. def on_selection_modified(v):
  157. for callback in all_callbacks['on_selection_modified']:
  158. run_timed_function(lambda: callback.on_selection_modified(v),
  159. callback.__module__, "on_selection_modified", FAST_EVENT_TIMEOUT)
  160. def on_activated(v):
  161. for callback in all_callbacks['on_activated']:
  162. run_timed_function(lambda: callback.on_activated(v),
  163. callback.__module__, "on_activated", EVENT_TIMEOUT)
  164. def on_deactivated(v):
  165. for callback in all_callbacks['on_deactivated']:
  166. run_timed_function(lambda: callback.on_deactivated(v),
  167. callback.__module__, "on_deactivated", EVENT_TIMEOUT)
  168. def on_project_load(v):
  169. for callback in all_callbacks['on_project_load']:
  170. run_timed_function(lambda: callback.on_project_load(v),
  171. callback.__module__, "on_project_load", EVENT_TIMEOUT)
  172. def on_project_close(v):
  173. for callback in all_callbacks['on_project_close']:
  174. run_timed_function(lambda: callback.on_project_close(v),
  175. callback.__module__, "on_project_close", EVENT_TIMEOUT)
  176. def on_query_context(v, key, operator, operand, match_all):
  177. for callback in all_callbacks['on_query_context']:
  178. val = run_timed_function(lambda: callback.on_query_context(v, key, operator, operand, match_all),
  179. callback.__module__, "on_query_context", FAST_EVENT_TIMEOUT)
  180. if val:
  181. return True
  182. return False
  183. def on_query_completions(v, prefix, locations):
  184. completions = []
  185. flags = 0
  186. for callback in all_callbacks['on_query_completions']:
  187. res = callback.on_query_completions(v, prefix, locations)
  188. if isinstance(res, tuple):
  189. completions += res[0]
  190. flags |= res[1]
  191. elif isinstance(res, list):
  192. completions += res
  193. return (completions,flags)
  194. class Command(object):
  195. def name(self):
  196. clsname = self.__class__.__name__
  197. name = clsname[0].lower()
  198. last_upper = False
  199. for c in clsname[1:]:
  200. if c.isupper() and not last_upper:
  201. name += '_'
  202. name += c.lower()
  203. else:
  204. name += c
  205. last_upper = c.isupper()
  206. if name.endswith("_command"):
  207. name = name[0:-8]
  208. return name
  209. def is_enabled_(self, args):
  210. try:
  211. if args:
  212. if 'event' in args:
  213. del args['event']
  214. return self.is_enabled(**args)
  215. else:
  216. return self.is_enabled()
  217. except TypeError:
  218. return self.is_enabled()
  219. def is_enabled(self):
  220. return True
  221. def is_visible_(self, args):
  222. try:
  223. if args:
  224. return self.is_visible(**args)
  225. else:
  226. return self.is_visible()
  227. except TypeError:
  228. return self.is_visible()
  229. def is_visible(self):
  230. return True
  231. def is_checked_(self, args):
  232. try:
  233. if args:
  234. return self.is_checked(**args)
  235. else:
  236. return self.is_checked()
  237. except TypeError:
  238. return self.is_checked()
  239. def is_checked(self):
  240. return False
  241. def description_(self, args):
  242. try:
  243. if args:
  244. return self.description(**args)
  245. else:
  246. return self.description()
  247. except TypeError as e:
  248. return None
  249. def description(self):
  250. return None
  251. class ApplicationCommand(Command):
  252. def run_(self, args):
  253. if args:
  254. if 'event' in args:
  255. del args['event']
  256. return self.run(**args)
  257. else:
  258. return self.run()
  259. def run(self):
  260. pass
  261. class WindowCommand(Command):
  262. def __init__(self, window):
  263. self.window = window
  264. def run_(self, args):
  265. if args:
  266. if 'event' in args:
  267. del args['event']
  268. return self.run(**args)
  269. else:
  270. return self.run()
  271. def run(self):
  272. pass
  273. class TextCommand(Command):
  274. def __init__(self, view):
  275. self.view = view
  276. def run_(self, args):
  277. if args:
  278. if 'event' in args:
  279. del args['event']
  280. edit = self.view.begin_edit(self.name(), args)
  281. try:
  282. return self.run(edit, **args)
  283. finally:
  284. self.view.end_edit(edit)
  285. else:
  286. edit = self.view.begin_edit(self.name())
  287. try:
  288. return self.run(edit)
  289. finally:
  290. self.view.end_edit(edit)
  291. def run(self, edit):
  292. pass
  293. class EventListener(object):
  294. pass