Skip to content

Commit

Permalink
Make autocomplete item scrolling more natural
Browse files Browse the repository at this point in the history
  • Loading branch information
Guldoman committed Mar 1, 2024
1 parent d2f4456 commit 309ee0d
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions data/plugins/autocomplete.lua
Expand Up @@ -282,12 +282,14 @@ end)


local partial = ""
local suggestions_offset = 1
local suggestions_idx = 1
local suggestions = {}
local last_line, last_col


local function reset_suggestions()
suggestions_offset = 1
suggestions_idx = 1
suggestions = {}

Expand Down Expand Up @@ -369,6 +371,7 @@ local function update_suggestions()
end
end
suggestions_idx = 1
suggestions_offset = 1
end

local function get_partial_symbol()
Expand Down Expand Up @@ -565,8 +568,8 @@ local function draw_suggestions_box(av)
local font = av:get_font()
local lh = font:get_height() + style.padding.y
local y = ry + style.padding.y / 2
local show_count = #suggestions <= ah and #suggestions or ah
local start_index = suggestions_idx > ah and (suggestions_idx-(ah-1)) or 1
local show_count = math.min(#suggestions, ah)
local start_index = suggestions_offset
local hide_info = config.plugins.autocomplete.hide_info

for i=start_index, start_index+show_count-1, 1 do
Expand Down Expand Up @@ -819,7 +822,7 @@ command.add(predicate, {
local current_partial = get_partial_symbol()
local sz = #current_partial

for idx, line1, col1, line2, col2 in doc:get_selections(true) do
for _, line1, col1, line2, _ in doc:get_selections(true) do
local n = col1 - 1
local line = doc.lines[line1]
for i = 1, sz + 1 do
Expand All @@ -840,10 +843,24 @@ command.add(predicate, {

["autocomplete:previous"] = function()
suggestions_idx = (suggestions_idx - 2) % #suggestions + 1

local ah = math.min(config.plugins.autocomplete.max_height, #suggestions)
if suggestions_offset > suggestions_idx then
suggestions_offset = suggestions_idx
elseif suggestions_offset + ah < suggestions_idx + 1 then
suggestions_offset = suggestions_idx - ah + 1
end
end,

["autocomplete:next"] = function()
suggestions_idx = (suggestions_idx % #suggestions) + 1

local ah = math.min(config.plugins.autocomplete.max_height, #suggestions)
if suggestions_offset + ah < suggestions_idx + 1 then
suggestions_offset = suggestions_idx - ah + 1
elseif suggestions_offset > suggestions_idx then
suggestions_offset = suggestions_idx
end
end,

["autocomplete:cycle"] = function()
Expand Down

0 comments on commit 309ee0d

Please sign in to comment.