@@ -138,20 +138,41 @@ function autocomplete.select_next()
138
138
139
139
local current_line = vim .api .nvim_win_get_cursor (autocomplete .win :get_win ())[1 ]
140
140
local line_count = vim .api .nvim_buf_line_count (autocomplete .win :get_buf ())
141
- if current_line == line_count then return end
142
-
143
- vim .api .nvim_win_set_cursor (autocomplete .win :get_win (), { current_line + 1 , 0 })
144
- autocomplete .event_targets .on_select (autocomplete .get_selected_item (), autocomplete .context )
141
+ local cycle_from_bottom = config .windows .autocomplete .cycle .from_bottom
142
+ local is_last_completion = current_line == line_count
143
+
144
+ -- at the end of completion list and the config is not enabled: do nothing
145
+ if is_last_completion and not cycle_from_bottom then return end
146
+ if is_last_completion then
147
+ -- at the end of completion list and the config is enabled: cycle back to first completion
148
+ vim .api .nvim_win_set_cursor (autocomplete .win :get_win (), { 1 , 0 })
149
+ autocomplete .event_targets .on_select (autocomplete .get_selected_item (), autocomplete .context )
150
+ else
151
+ -- select next completion
152
+ vim .api .nvim_win_set_cursor (autocomplete .win :get_win (), { current_line + 1 , 0 })
153
+ autocomplete .event_targets .on_select (autocomplete .get_selected_item (), autocomplete .context )
154
+ end
145
155
end
146
156
147
157
function autocomplete .select_prev ()
148
158
if not autocomplete .win :is_open () then return end
149
159
150
160
local current_line = vim .api .nvim_win_get_cursor (autocomplete .win :get_win ())[1 ]
151
- if current_line == 1 then return end
152
-
153
- vim .api .nvim_win_set_cursor (autocomplete .win :get_win (), { math.max (current_line - 1 , 1 ), 0 })
154
- autocomplete .event_targets .on_select (autocomplete .get_selected_item (), autocomplete .context )
161
+ local line_count = vim .api .nvim_buf_line_count (autocomplete .win :get_buf ())
162
+ local cycle_from_top = config .windows .autocomplete .cycle .from_top
163
+ local is_first_completion = current_line == 1
164
+
165
+ -- at the beginning of completion list and the config is not enabled: do nothing
166
+ if is_first_completion and not cycle_from_top then return end
167
+ if is_first_completion then
168
+ -- at the beginning of completion list and the config is enabled: cycle back to last completion
169
+ vim .api .nvim_win_set_cursor (autocomplete .win :get_win (), { line_count , 0 })
170
+ autocomplete .event_targets .on_select (autocomplete .get_selected_item (), autocomplete .context )
171
+ else
172
+ -- select previous completion
173
+ vim .api .nvim_win_set_cursor (autocomplete .win :get_win (), { math.max (current_line - 1 , 1 ), 0 })
174
+ autocomplete .event_targets .on_select (autocomplete .get_selected_item (), autocomplete .context )
175
+ end
155
176
end
156
177
157
178
function autocomplete .listen_on_select (callback ) autocomplete .event_targets .on_select = callback end
0 commit comments