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