From 48aa350a3e1032039254c3173f7dea41eb07613c Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 23 Mar 2024 20:24:08 +0800 Subject: [PATCH] Improve `CommandView` and `autocomplete` scroll behavior (#1732) * Make command palette item scrolling more natural Also add a config option for the maximum number of visible entries in the command palette. * Make `autocomplete` item scrolling more natural Co-authored-by: Guldoman --- data/core/commandview.lua | 33 +++++++++++++++++++++++++++------ data/plugins/autocomplete.lua | 23 ++++++++++++++++++++--- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/data/core/commandview.lua b/data/core/commandview.lua index 1f38867..305936b 100644 --- a/data/core/commandview.lua +++ b/data/core/commandview.lua @@ -50,6 +50,7 @@ local default_state = { function CommandView:new() CommandView.super.new(self, SingleLineDoc()) self.suggestion_idx = 1 + self.suggestions_offset = 1 self.suggestions = {} self.suggestions_height = 0 self.last_change_id = 0 @@ -123,6 +124,24 @@ function CommandView:move_suggestion_idx(dir) end end + local function get_suggestions_offset() + local max_visible = math.min(max_suggestions, #self.suggestions) + if dir > 0 then + if self.suggestions_offset + max_visible < self.suggestion_idx + 1 then + return self.suggestion_idx - max_visible + 1 + elseif self.suggestions_offset > self.suggestion_idx then + return self.suggestion_idx + end + else + if self.suggestions_offset > self.suggestion_idx then + return self.suggestion_idx + elseif self.suggestions_offset + max_visible < self.suggestion_idx + 1 then + return self.suggestion_idx - max_visible + 1 + end + end + return self.suggestions_offset + end + if self.state.show_suggestions then local n = self.suggestion_idx + dir self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions) @@ -146,6 +165,8 @@ function CommandView:move_suggestion_idx(dir) self.last_change_id = self.doc:get_change_id() self.state.suggest(self:get_text()) end + + self.suggestions_offset = get_suggestions_offset() end @@ -256,6 +277,7 @@ function CommandView:update_suggestions() end self.suggestions = res self.suggestion_idx = 1 + self.suggestions_offset = 1 end @@ -299,7 +321,7 @@ function CommandView:update() self:move_towards("suggestions_height", dest, nil, "commandview") -- update suggestion cursor offset - local dest = math.min(self.suggestion_idx, max_suggestions) * self:get_suggestion_line_height() + local dest = (self.suggestion_idx - self.suggestions_offset + 1) * self:get_suggestion_line_height() self:move_towards("selection_offset", dest, nil, "commandview") -- update size based on whether this is the active_view @@ -335,6 +357,7 @@ local function draw_suggestions_box(self) local h = math.ceil(self.suggestions_height) local rx, ry, rw, rh = self.position.x, self.position.y - h - dh, self.size.x, h + core.push_clip_rect(rx, ry, rw, rh) -- draw suggestions background if #self.suggestions > 0 then renderer.draw_rect(rx, ry, rw, rh, style.background3) @@ -344,14 +367,12 @@ local function draw_suggestions_box(self) end -- draw suggestion text - local offset = math.max(self.suggestion_idx - max_suggestions, 0) - local last = math.min(offset + max_suggestions, #self.suggestions) - core.push_clip_rect(rx, ry, rw, rh) - local first = 1 + offset + local first = math.max(self.suggestions_offset, 1) + local last = math.min(self.suggestions_offset + max_suggestions, #self.suggestions) for i=first, last do local item = self.suggestions[i] local color = (i == self.suggestion_idx) and style.accent or style.text - local y = self.position.y - (i - offset) * lh - dh + local y = self.position.y - (i - first + 1) * lh - dh common.draw_text(self:get_font(), color, item.text, nil, x, y, 0, lh) if item.info then diff --git a/data/plugins/autocomplete.lua b/data/plugins/autocomplete.lua index cf8aee6..f2e02e2 100644 --- a/data/plugins/autocomplete.lua +++ b/data/plugins/autocomplete.lua @@ -212,12 +212,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 = {} @@ -261,6 +263,7 @@ local function update_suggestions() end end suggestions_idx = 1 + suggestions_offset = 1 end local function get_partial_symbol() @@ -457,8 +460,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 for i=start_index, start_index+show_count-1, 1 do if not suggestions[i] then @@ -665,7 +668,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 @@ -686,10 +689,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()