Skip to content

Commit e77674f

Browse files
committed
Simplify RspecCreateModuleCommand
1 parent 7ca582a commit e77674f

2 files changed

Lines changed: 30 additions & 35 deletions

File tree

RSpecCreateModule.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,53 +24,34 @@ class {name}
2424
"""
2525

2626

27-
def snake_case(name):
27+
def indent(text: str, space: int = 2):
28+
return "\n".join(" " * space + line for line in text.split("\n"))
29+
30+
31+
def snake_case(name: str):
2832
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
2933
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
3034

3135

32-
class GotoLineCommand(sublime_plugin.TextCommand):
33-
def run(self, edit, line, column=0):
36+
class RspecGotoLineAndIndentCommand(sublime_plugin.TextCommand):
37+
def run(self, edit: sublime.Edit, line: int = 0, column: int = 0):
3438
pt = self.view.text_point(line - 1, column)
3539

3640
self.view.sel().clear()
3741
self.view.sel().add(sublime.Region(pt))
3842

3943
self.view.show(pt)
44+
self.view.insert(edit, pt, " ")
4045

41-
42-
class RspecNewModuleCommand(sublime_plugin.TextCommand):
43-
def run(self, edit, name, namespace):
44-
class_template = CLASS_TEMPLATE.format(name=name)
45-
46-
template, level = class_template, len(namespace)
47-
48-
while namespace:
49-
module = namespace.pop()
50-
template = MODULE_TEMPLATE.format(
51-
module=module, definition=self.indent(template)
52-
)
53-
54-
self.view.insert(edit, 0, template)
55-
self.view.run_command("goto_line", {"line": 2 + level, "column": level * 2})
56-
57-
def indent(self, text, space=2):
58-
return "\n".join(" " * space + line for line in text.split("\n"))
59-
60-
61-
class RspecNewSpecCommand(sublime_plugin.TextCommand):
62-
def run(self, edit, name):
63-
template = SPEC_TEMPLATE.format(name=name)
64-
65-
self.view.insert(edit, 0, template)
66-
self.view.run_command("goto_line", {"line": 4})
46+
def is_visible(self):
47+
return False
6748

6849

6950
class RspecCreateModuleCommand(sublime_plugin.WindowCommand):
7051
def run(self):
7152
self.window.show_input_panel("Enter module name:", "", self.on_done, None, None)
7253

73-
def on_done(self, text):
54+
def on_done(self, text: str):
7455
if not text:
7556
return
7657

@@ -79,14 +60,29 @@ def on_done(self, text):
7960
# create the module
8061
module = self.window.new_file(syntax="scope:source.ruby")
8162
module.set_name(snake_case(name) + ".rb")
82-
83-
module.run_command("rspec_new_module", {"name": name, "namespace": namespace})
63+
module_template = CLASS_TEMPLATE.format(name=name)
64+
for mod in reversed(namespace):
65+
module_template = MODULE_TEMPLATE.format(
66+
module=mod, definition=indent(module_template)
67+
)
68+
module.run_command("rspec_insert_content", {"text": module_template})
69+
module.run_command(
70+
"rspec_goto_line_and_indent",
71+
{"line": 2 + len(namespace), "column": len(namespace) * 2},
72+
)
8473

8574
# create the spec
8675
spec = self.window.new_file(syntax="scope:source.ruby.rspec")
8776
self.window.run_command(
8877
"move_to_group", {"group": other_group_in_pair(self.window)}
8978
)
9079
spec.set_name(snake_case(name) + "_spec.rb")
91-
92-
spec.run_command("rspec_new_spec", {"name": "::".join(namespace + [name])})
80+
self.window.run_command(
81+
"move_to_group", {"group": other_group_in_pair(self.window)}
82+
)
83+
spec.set_name(snake_case(name) + "_spec.rb")
84+
spec.run_command(
85+
"rspec_insert_content",
86+
{"text": SPEC_TEMPLATE.format(name="::".join(namespace + [name]))},
87+
)
88+
spec.run_command("rspec_goto_line_and_indent", {"line": 4})

shared.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def other_group_in_pair(window: sublime.Window):
1111
return min(target_group, window.num_groups() - 1)
1212

1313

14-
# TODO: Call from RspecNewModuleCommand and RspecNewSpecCommand
1514
class RspecInsertContentCommand(sublime_plugin.TextCommand):
1615
def run(self, edit: sublime.Edit, text: str = ""):
1716
self.view.insert(edit, 0, text)

0 commit comments

Comments
 (0)