/
RepeatInput.py
144 lines (94 loc) · 2.9 KB
/
RepeatInput.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import sublime, sublime_plugin
class RepeatInputCommand(sublime_plugin.WindowCommand):
def eraseErrorMessage( self ):
print "here"
view = sublime.active_window().active_view()
view.erase_status( "repeat_input_message" )
return
def errorMessage( self, str ):
view = sublime.active_window().active_view()
view.set_status( "repeat_input_message", str )
sublime.set_timeout( self.eraseErrorMessage, 2500 )
def showInputPanel_onDone( self, str ):
if "/" not in str:
self.errorMessage( "invalid string \"" + str + "\"" )
self.showInputPanel( str )
return
inputs = str.split( "/" )
base = inputs[0]
ranges = inputs[1].split( "," )
for i in range( 0,len(ranges) ):
ranges[i] = int(ranges[i])
inputRangeParamCount = len(ranges)
while len(ranges) < 3:
ranges.append(0)
begin, end, step = tuple( ranges )
if inputRangeParamCount > 1:
rangeSign = ( end - begin ) > 0
stepSign = step >= 0
if step == 0:
stepSign = rangeSign
step = 1
if rangeSign != stepSign:
step = -step
end += step
else:
# count
end = begin + 1
begin = 1
step = 1
print begin, end, step
output = ""
for i in range( begin, end, step ):
num = u"%d" % i
tmp = base.replace( "##", num )
output += tmp
view = sublime.active_window().active_view()
edit = view.begin_edit()
view.insert( edit, view.sel()[0].a, output )
# add history.
histories = []
if view.settings().has( "repeat_input" ):
histories = view.settings().get( "repeat_input" )
if str in histories:
histories.remove(str)
histories.append( str )
if len( histories ) > 10:
del histories[0]
view.settings().set( "repeat_input", histories )
return
def showInputPanel( self, defaultString ):
window = sublime.active_window()
view = window.active_view()
if view is None:
return
window.show_input_panel( "repeat input. ex) \"input string ( ## is replaced numeric ) / ( count | begin [ , end [ , step ] ] )\"", defaultString, self.showInputPanel_onDone, None, None )
def showQuickPanel_onDone( self, index ):
window = sublime.active_window()
view = window.active_view()
if view is None:
return
if index < 0:
self.showInputPanel( "" )
else:
histories = view.settings().get( "repeat_input" )
histories.reverse()
self.showInputPanel( histories[index] )
def showQuickPanel( self ):
window = sublime.active_window()
view = window.active_view()
if view is None:
return
histories = view.settings().get( "repeat_input" )
histories.reverse()
window.show_quick_panel( histories, self.showQuickPanel_onDone )
def run(self):
window = sublime.active_window()
view = window.active_view()
if view is None:
return
if view.settings().has( "repeat_input" ):
self.showQuickPanel()
else:
self.showInputPanel( "" )
return