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 Feb 29, 2024
1 parent d2f4456 commit e9d599e
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 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 @@ -398,8 +401,13 @@ local function get_suggestions_rect(av)
local hide_info = config.plugins.autocomplete.hide_info
local hide_icons = config.plugins.autocomplete.hide_icons

local ah = config.plugins.autocomplete.max_height
local show_count = math.min(#suggestions, ah)
local start_index = suggestions_offset

local max_width = 0
for _, s in ipairs(suggestions) do
for i = start_index, start_index + show_count - 1 do
local s = suggestions[i]
local w = font:get_width(s.text)
if s.info and not hide_info then
w = w + style.font:get_width(s.info) + style.padding.x
Expand All @@ -414,7 +422,6 @@ local function get_suggestions_rect(av)
max_width = math.max(max_width, w)
end

local ah = config.plugins.autocomplete.max_height

local max_items = #suggestions
if max_items > ah then
Expand Down Expand Up @@ -565,8 +572,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 +826,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 +847,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 e9d599e

Please sign in to comment.