Implement scrolling in command view

This commit is contained in:
Francesco Abbate 2021-02-28 19:52:38 +01:00
parent a35ef764a0
commit e46800347f
1 changed files with 18 additions and 11 deletions

View File

@ -29,6 +29,7 @@ local default_state = {
function CommandView:new()
CommandView.super.new(self, SingleLineDoc())
self.suggestion_idx = 1
self.suggestion_offset = 0
self.suggestions = {}
self.suggestions_height = 0
self.last_change_id = 0
@ -82,6 +83,11 @@ end
function CommandView:move_suggestion_idx(dir)
local n = self.suggestion_idx + dir
self.suggestion_idx = common.clamp(n, 1, #self.suggestions)
if self.suggestion_idx > max_suggestions then
self.suggestion_offset = self.suggestion_idx - max_suggestions
else
self.suggestion_offset = 0
end
self:complete()
self.last_change_id = self.doc:get_change_id()
end
@ -145,9 +151,6 @@ function CommandView:update_suggestions()
local t = self.state.suggest(self:get_text()) or {}
local res = {}
for i, item in ipairs(t) do
if i == max_suggestions then
break
end
if type(item) == "string" then
item = { text = item }
end
@ -155,6 +158,7 @@ function CommandView:update_suggestions()
end
self.suggestions = res
self.suggestion_idx = 1
self.suggestion_offset = 0
end
@ -184,11 +188,11 @@ function CommandView:update()
-- update suggestions box height
local lh = self:get_suggestion_line_height()
local dest = #self.suggestions * lh
local dest = math.min(#self.suggestions, max_suggestions) * lh
self:move_towards("suggestions_height", dest)
-- update suggestion cursor offset
local dest = self.suggestion_idx * self:get_suggestion_line_height()
local dest = (self.suggestion_idx - self.suggestion_offset) * self:get_suggestion_line_height()
self:move_towards("selection_offset", dest)
-- update size based on whether this is the active_view
@ -234,13 +238,16 @@ local function draw_suggestions_box(self)
-- draw suggestion text
core.push_clip_rect(rx, ry, rw, rh)
for i, item in ipairs(self.suggestions) do
local color = (i == self.suggestion_idx) and style.accent or style.text
local y = self.position.y - i * lh - dh
common.draw_text(self:get_font(), color, item.text, nil, x, y, 0, lh)
local irel = (i - self.suggestion_offset)
if irel >= 0 then
local color = (i == self.suggestion_idx) and style.accent or style.text
local y = self.position.y - irel * lh - dh
common.draw_text(self:get_font(), color, item.text, nil, x, y, 0, lh)
if item.info then
local w = self.size.x - x - style.padding.x
common.draw_text(self:get_font(), style.dim, item.info, "right", x, y, w, lh)
if item.info then
local w = self.size.x - x - style.padding.x
common.draw_text(self:get_font(), style.dim, item.info, "right", x, y, w, lh)
end
end
end
core.pop_clip_rect()