forked from SublimeText/RSpec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSpecCreateModule.py
69 lines (55 loc) · 2.2 KB
/
RSpecCreateModule.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 sublime, sublime_plugin, time
import re
from RSpec import shared
_patterns = dict((k, re.compile('_*' + v)) for (k, v)
in dict(allcamel=r'(?:[A-Z]+[a-z0-9]*)+$',
trailingcamel=r'[a-z]+(?:[A-Z0-9]*[a-z0-9]*)+$',
underscores=r'(?:[a-z]+_*)+[a-z0-9]+$').items())
_caseTransition = re.compile('([A-Z][a-z]+)')
def translate(name, _from, to):
leading_underscores = str()
while name[0] == '_':
leading_underscores += '_'
name = name[1:]
if _from in ('allcamel', 'trailingcamel'):
words = _caseTransition.split(name)
else:
words = name.split('_')
words = list(w for w in words if w is not None and 0 < len(w))
camelize = lambda words: ''.join(w[0].upper() + w[1:] for w in words)
v = dict(smushed=lambda: ''.join(words).lower(),
allcamel=lambda: camelize(words),
trailingcamel=lambda: words[0].lower() + camelize(words[1:]),
underscores=lambda: '_'.join(words).lower())[to]()
return leading_underscores + v
class RspecCreateModuleCommand(sublime_plugin.WindowCommand):
def run(self):
# self.view.insert(edit, 0, "Hello, World!")
self.window.show_input_panel("Enter module name:", "", self.on_done, None, None)
def on_done(self, text):
# create the module
module = self.window.new_file()
module.set_syntax_file('Packages/Ruby/Ruby.tmLanguage')
module.set_name(translate(text, 'allcamel', 'underscores') + '.rb')
module_template = "\n\
class " + text + "\n\
end"
edit = module.begin_edit()
module.insert(edit, 0, module_template)
module.end_edit(edit)
# create the spec
spec = self.window.new_file()
self.window.run_command('move_to_group', {'group': shared.other_group_in_pair(self.window)})
spec.set_syntax_file('Packages/Ruby/Ruby.tmLanguage')
spec.set_name(translate(text, 'allcamel', 'underscores') + '_spec.rb')
spec_template = "require 'spec_helper'\n\
require '" + translate(text, 'allcamel', 'underscores') + "'\n\n\
describe " + text + " do\n\
\tit \"should do something\"\n\
end"
edit = spec.begin_edit()
spec.insert(edit, 0, spec_template)
spec.end_edit(edit)
# try:
# except ValueError:
# pass