This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SequenceGenerator.py
57 lines (46 loc) · 1.97 KB
/
SequenceGenerator.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
import sublime, sublime_plugin
class SequenceGeneratorCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.status_message("You could set the sequence format to: {start:step} or {start}.")
self.view.window().show_input_panel("Enter Sequence Format:", "1", self.on_done, self.on_change, None)
def on_done(self, text):
parameter = self.VerifyFormat(text)
if not parameter:
sublime.error_message("Your input is invalid.")
else:
self.view.run_command("generate_sequence", {"parameter": parameter})
def on_change(self, text):
if not self.VerifyFormat(text):
sublime.status_message("Your input is invalid.")
else:
sublime.status_message("You could set the sequence format to: {start:step} or {start}.")
def VerifyFormat(self, text):
splits = text.replace(":", " ").split()
length = len(splits)
if not 1<=length<=2:
return False
for number in splits:
try:
number = int(number)
except:
return False
parameter = {'start':1, 'step':1}
if length==1:
parameter['start'] = int(splits[0])
else:
parameter['start'] = int(splits[0])
parameter['step'] = int(splits[1])
return parameter
class GenerateSequenceCommand(sublime_plugin.TextCommand):
def run(self, edit, parameter):
new_sel = []
for region in self.view.sel():
self.view.erase(edit, region)
sequence = str(parameter['start'])
self.view.insert(edit, region.begin(), sequence)
new_sel.append(sublime.Region(region.begin(), region.begin() + len(sequence)))
parameter['start'] += parameter['step']
if len(new_sel) > 0:
self.view.sel().clear()
for r in new_sel:
self.view.sel().add(r)