-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_sequence.py
66 lines (47 loc) · 1.98 KB
/
insert_sequence.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
import itertools
import string
import sublime
import sublime_plugin
def next_number(first_selection):
"""
Starting from the first selection, yield a sequence of numbers converted to strings.
"""
for number in itertools.count(first_selection):
yield str(number)
def next_letter(first_selection, letters):
"""
Starting from the first selection, yield a sequence of alphabetic characters.
If the first selection is 'a', this will yield ('a', 'b', 'c', ..., 'z', 'aa', 'ab', 'ac', ...)
and so on.
If the first selection is 'f', this will yield ('f', 'g', 'h', ..., 'z', 'aa', 'ab', 'ac', ...)
and so on.
"""
for size in itertools.count(1):
next_letters = letters[letters.find(first_selection):]
first_selection = letters[0]
for s in itertools.product(next_letters, repeat=size):
yield ''.join(s)
class InsertSequenceCommand(sublime_plugin.TextCommand):
"""
Command to insert a sequence of numbers or letters into the viewports active selection.
"""
def run(self, edit):
generator = self._create_sequence()
for (selection, sequence) in zip(self.view.sel(), generator):
self.view.insert(edit, selection.begin(), sequence)
for selection in self.view.sel():
self.view.erase(edit, selection)
def _create_sequence(self):
"""
Parse the first selection in the viewport and determine what sequence will be inserted.
"""
selections = self.view.sel()
assert len(selections) > 0
first_selection = self.view.substr(selections[0]).strip()
if first_selection.isdigit():
return next_number(int(first_selection))
if len(first_selection) == 1 and first_selection.isalpha():
if first_selection.islower():
return next_letter(first_selection, string.ascii_lowercase)
return next_letter(first_selection, string.ascii_uppercase)
return next_number(1)