Skip to content

Commit e37cb9a

Browse files
committed
support for jumping between selection on re-marking again
1 parent 6a68e08 commit e37cb9a

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

StickySearch.py

+61-21
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,57 @@ class StickysearchCommand(sublime_plugin.TextCommand):
1919
def run(self, edit, op):
2020
# keep sticky per window (each window has its own set)
2121
if 'add' in op:
22-
self.op_add(self.view)
22+
self.op_add()
2323
if 'clear' in op:
24-
self.op_clear(self.view)
24+
self.op_clear()
2525
if 'set' in op:
26-
self.op_clear(self.view)
27-
self.op_add(self.view)
26+
self.op_clear()
27+
self.op_add()
2828

29-
def op_add(self, view):
30-
selection = self.find_all_under_cursor(view)
31-
regions = view.find_all(selection)
32-
key = self._keybase + str(len(self._keys))
33-
self.mark(key, view, regions)
34-
self._keys.append(key)
29+
def op_add(self):
30+
selected_region = self.view.sel()[0]
31+
selected_word = self.selection_under_cursor(selected_region)
32+
key = self.marker_key(selected_word)
33+
regions = self.view.find_all(selected_word)
34+
# Mark new selections, or jump to next resion on exsiting marks
35+
if key not in self._keys:
36+
self.mark(key, regions)
37+
self._keys.append(key)
38+
else:
39+
self.jump_to_next_match(selected_region, regions)
3540

36-
def op_clear(self, view):
41+
def op_clear(self):
42+
selected_region = self.view.sel()[0]
43+
selected_word = self.selection_under_cursor(selected_region)
44+
marker_key = self.marker_key(selected_word)
45+
46+
preserve_keys = []
3747
for key in self._keys:
38-
view.erase_regions(key)
39-
self._keys = []
48+
if key != marker_key:
49+
self.view.erase_regions(key)
50+
else:
51+
preserve_keys.append(marker_key)
52+
53+
self._keys = preserve_keys
54+
55+
def marker_key(self, uid):
56+
return f"{self._keybase}_{uid}"
4057

41-
def mark(self, key, view, regions):
58+
def jump_to_next_match(self, sel, regions):
59+
the_word_region = self.region_under_cursor(sel)
60+
for pos in regions:
61+
if pos.a > the_word_region.a:
62+
break
63+
else:
64+
# cycle back to first match
65+
pos = regions[0]
66+
67+
pos.b = pos.a
68+
self.view.sel().clear()
69+
self.view.sel().add(pos)
70+
self.view.show(pos)
71+
72+
def mark(self, key, regions):
4273
settings = sublime.load_settings("StickySearch.sublime-settings")
4374
flags = sublime.PERSISTENT
4475
if not settings.get("fill", False):
@@ -59,22 +90,31 @@ def mark(self, key, view, regions):
5990
scope = self._scopes[next_marker_index % num_of_available_scopes]
6091
else:
6192
scope = "region.yellowish"
62-
view.add_regions(key, regions, scope, icon_name, flags)
93+
94+
self.view.add_regions(key, regions, scope, icon_name, flags)
6395

64-
def find_all_under_cursor(self, view):
65-
# view.window().run_command('find_all_under')
66-
sel = view.sel()[0]
96+
def selection_under_cursor(self, sel):
6797
# if there is visual selection, just use it to find all without word boundaries
6898
# if no selection then expand to word under cursor and find all using word boundaries
6999
has_selection = sel.a != sel.b
70100
if has_selection:
71101
the_word_region = sel
72-
selected_txt = view.substr(the_word_region)
102+
selected_txt = self.view.substr(the_word_region)
73103
# support special charecters
74104
selected_word = re.escape(selected_txt)
75105
else:
76-
the_word_region = view.word(sel)
77-
selected_word = "\\b" + view.substr(the_word_region) + "\\b"
106+
the_word_region = self.view.word(sel)
107+
selected_word = "\\b" + self.view.substr(the_word_region) + "\\b"
78108

79109
return selected_word
80110

111+
def region_under_cursor(self, sel):
112+
# if there is visual selection, just use it to find all without word boundaries
113+
# if no selection then expand to word under cursor and find all using word boundaries
114+
has_selection = sel.a != sel.b
115+
if has_selection:
116+
the_word_region = sel
117+
else:
118+
the_word_region = self.view.word(sel)
119+
120+
return the_word_region

0 commit comments

Comments
 (0)