-
Notifications
You must be signed in to change notification settings - Fork 8
/
hist.py
48 lines (35 loc) · 1.25 KB
/
hist.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
import sublime
import sublime_plugin
class History:
hist = []
index = None
def insert(self, user_input):
if not self.hist or (user_input != self.last()):
self.hist.append(user_input)
self.index = None
def roll(self, backwards=False):
if self.index is None:
self.index = -1 if backwards else 0
else:
self.index += -1 if backwards else 1
if self.index == len(self.hist) or self.index < -len(self.hist):
self.index = -1 if backwards else 0
def last(self):
return self.hist[-1] if self.hist else None
def get(self, index=None):
if not index:
index = self.index
return self.hist[index] if self.hist else None
def reset_index(self):
self.index = None
if 'history1' not in globals():
history1 = History()
class InputArgsHistory(sublime_plugin.TextCommand):
def run(self, edit, backwards=False):
history1.roll(backwards)
self.view.erase(edit, sublime.Region(0, self.view.size()))
self.view.insert(edit, 0, history1.get())
class InputArgsHistoryListener(sublime_plugin.EventListener):
# restore History index
def on_deactivated(self, view):
history1.reset_index()