@@ -19,26 +19,57 @@ class StickysearchCommand(sublime_plugin.TextCommand):
19
19
def run (self , edit , op ):
20
20
# keep sticky per window (each window has its own set)
21
21
if 'add' in op :
22
- self .op_add (self . view )
22
+ self .op_add ()
23
23
if 'clear' in op :
24
- self .op_clear (self . view )
24
+ self .op_clear ()
25
25
if 'set' in op :
26
- self .op_clear (self . view )
27
- self .op_add (self . view )
26
+ self .op_clear ()
27
+ self .op_add ()
28
28
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 )
35
40
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 = []
37
47
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 } "
40
57
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 ):
42
73
settings = sublime .load_settings ("StickySearch.sublime-settings" )
43
74
flags = sublime .PERSISTENT
44
75
if not settings .get ("fill" , False ):
@@ -59,22 +90,31 @@ def mark(self, key, view, regions):
59
90
scope = self ._scopes [next_marker_index % num_of_available_scopes ]
60
91
else :
61
92
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 )
63
95
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 ):
67
97
# if there is visual selection, just use it to find all without word boundaries
68
98
# if no selection then expand to word under cursor and find all using word boundaries
69
99
has_selection = sel .a != sel .b
70
100
if has_selection :
71
101
the_word_region = sel
72
- selected_txt = view .substr (the_word_region )
102
+ selected_txt = self . view .substr (the_word_region )
73
103
# support special charecters
74
104
selected_word = re .escape (selected_txt )
75
105
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"
78
108
79
109
return selected_word
80
110
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