-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublime_magic.py
69 lines (59 loc) · 2.12 KB
/
sublime_magic.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
import re
import sublime
import sublime_plugin
from .messenger import messenger
from .spells import *
from .context import context
class SublimeMagic(sublime_plugin.TextCommand):
def run(self, edit):
self.edit = edit
self.known_spells = [
'replace_text',
'perform_line_regex',
'toggle_values',
'sublime_command'
]
self.get_user_spells()
self.find_first_matching_user_spell()
if self.spell is None:
self.message('No spell found.')
else:
try:
self.cast_spell()
except NotImplementedError as e:
self.message(str(e))
def get_user_spells(self):
self.magic_settings = sublime.load_settings(
'SublimeMagic.sublime-settings')
self.user_spells = self.magic_settings.get('spells')
def find_first_matching_user_spell(self):
self.spell = None
for spell in self.user_spells:
if context.check(self.view, spell):
self.spell = spell
break
def message(self, message):
if not hasattr(self, 'messenger'):
self.messenger = messenger.Messenger()
self.messenger.message(self.view, '★ ' + message, True)
def spell_to_class(self, string):
return ''.join(x.capitalize() or '_' for x in string.split('_'))
def cast_spell(self):
spell_name = self.spell.get('spell')
if not spell_name in self.known_spells:
raise NotImplementedError('Unknown spell: ' + spell_name)
try:
spell_class = eval(
spell_name +
'.' +
self.spell_to_class(spell_name) +
'Spell')
spell_class = spell_class(self.edit, self.view, self.spell)
spell_class.cast()
name = self.spell.get('name', None)
if not name:
name = spell_name
self.message('Casted ' + name)
except AttributeError as e:
return self.message('Invalid args for "' +
spell_name + '". ' + str(e))