@@ -114,6 +114,19 @@ local function move(opts, query_strings, query_group)
114114 end
115115 end
116116
117+ -- precompute enclosing scope once so we can discard out-of-scope results
118+ -- during wrap-around without re-walking the tree each iteration.
119+ local scope --- @type Range6 ?
120+ if opts .wrap then
121+ for _ , query_string in ipairs (query_strings ) do
122+ local current = shared .textobject_at_point (query_string , query_group , bufnr )
123+ if current then
124+ scope = shared .get_scope_range (bufnr , current )
125+ if scope then break end
126+ end
127+ end
128+ end
129+
117130 for _ = 1 , vim .v .count1 do
118131 local best_range --- @type Range6 ?
119132 local best_score --- @type integer
@@ -132,6 +145,12 @@ local function move(opts, query_strings, query_group)
132145 end
133146 )
134147
148+ if current_range then
149+ if scope and not ts_range .contains (scope , current_range ) then
150+ current_range = nil
151+ end
152+ end
153+
135154 if current_range then
136155 local score = scoring_function (start_ , current_range )
137156 if not best_range then
@@ -147,6 +166,19 @@ local function move(opts, query_strings, query_group)
147166 end
148167 end
149168 end
169+
170+ -- no direct successor/precursor was found => pick first/last of siblings
171+ if not best_range and scope then
172+ for _ , query_string in ipairs (query_strings ) do
173+ local siblings = shared .ranges_in_scope (bufnr , query_string , query_group , scope )
174+ if # siblings > 0 then
175+ best_range = forward and siblings [1 ] or siblings [# siblings ]
176+ best_start = starts [1 ]
177+ break
178+ end
179+ end
180+ end
181+
150182 goto_node (best_range and best_range , not best_start , not config .set_jumps )
151183 end
152184end
@@ -156,51 +188,61 @@ local move_repeatable = repeatable_move.make_repeatable_move(move)
156188
157189--- @param query_strings string | string[]
158190--- @param query_group ? string
159- M .goto_next_start = function (query_strings , query_group )
191+ --- @param opts ? { wrap ?: boolean }
192+ M .goto_next_start = function (query_strings , query_group , opts )
160193 move_repeatable ({
161194 forward = true ,
162195 start = true ,
196+ wrap = opts and opts .wrap ,
163197 }, query_strings , query_group )
164198end
165199--- @param query_strings string | string[]
166200--- @param query_group ? string
167- M .goto_next_end = function (query_strings , query_group )
201+ --- @param opts ? { wrap ?: boolean }
202+ M .goto_next_end = function (query_strings , query_group , opts )
168203 move_repeatable ({
169204 forward = true ,
170205 start = false ,
206+ wrap = opts and opts .wrap ,
171207 }, query_strings , query_group )
172208end
173209--- @param query_strings string | string[]
174210--- @param query_group ? string
175- M .goto_previous_start = function (query_strings , query_group )
211+ --- @param opts ? { wrap ?: boolean }
212+ M .goto_previous_start = function (query_strings , query_group , opts )
176213 move_repeatable ({
177214 forward = false ,
178215 start = true ,
216+ wrap = opts and opts .wrap ,
179217 }, query_strings , query_group )
180218end
181219--- @param query_strings string | string[]
182220--- @param query_group ? string
183- M .goto_previous_end = function (query_strings , query_group )
221+ --- @param opts ? { wrap ?: boolean }
222+ M .goto_previous_end = function (query_strings , query_group , opts )
184223 move_repeatable ({
185224 forward = false ,
186225 start = false ,
226+ wrap = opts and opts .wrap ,
187227 }, query_strings , query_group )
188228end
189229
190230--- @param query_strings string | string[]
191231--- @param query_group ? string
192- M .goto_next = function (query_strings , query_group )
232+ --- @param opts ? { wrap ?: boolean }
233+ M .goto_next = function (query_strings , query_group , opts )
193234 move_repeatable ({
194235 forward = true ,
236+ wrap = opts and opts .wrap ,
195237 }, query_strings , query_group )
196238end
197239--- @param query_strings string | string[]
198240--- @param query_group ? string
199- M .goto_previous = function (query_strings , query_group )
241+ --- @param opts ? { wrap ?: boolean }
242+ M .goto_previous = function (query_strings , query_group , opts )
200243 move_repeatable ({
201244 forward = false ,
202- query_strings = query_strings ,
203- query_group = query_group ,
245+ wrap = opts and opts .wrap ,
204246 }, query_strings , query_group )
205247end
206248
0 commit comments