Merge pull request #1003 from zesterer/master

Added suggestion wrapping for CommandView
This commit is contained in:
Jefferson González 2022-05-30 12:53:06 -04:00 committed by GitHub
commit f1f61f034b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 6 deletions

View File

@ -145,7 +145,7 @@ command.add(nil, {
else else
return true return true
end end
end) end, true)
end, end,
["core:open-log"] = function() ["core:open-log"] = function()

View File

@ -44,6 +44,7 @@ function CommandView:new()
self.font = "font" self.font = "font"
self.size.y = 0 self.size.y = 0
self.label = "" self.label = ""
self.wrap_on_overflow = false
end end
@ -89,11 +90,18 @@ function CommandView:set_text(text, select)
end end
end end
function CommandView:move_suggestion_idx(dir) function CommandView:move_suggestion_idx(dir)
local function overflow_suggestion_idx(n, count)
if self.wrap_on_overflow then
return (n - 1) % count + 1
else
return common.clamp(n, 1, count)
end
end
if self.show_suggestions then if self.show_suggestions then
local n = self.suggestion_idx + dir local n = self.suggestion_idx + dir
self.suggestion_idx = common.clamp(n, 1, #self.suggestions) self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions)
self:complete() self:complete()
self.last_change_id = self.doc:get_change_id() self.last_change_id = self.doc:get_change_id()
else else
@ -104,7 +112,7 @@ function CommandView:move_suggestion_idx(dir)
if n == 0 and self.save_suggestion then if n == 0 and self.save_suggestion then
self:set_text(self.save_suggestion) self:set_text(self.save_suggestion)
else else
self.suggestion_idx = common.clamp(n, 1, #self.suggestions) self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions)
self:complete() self:complete()
end end
else else
@ -135,10 +143,11 @@ function CommandView:submit()
end end
function CommandView:enter(text, submit, suggest, cancel, validate) function CommandView:enter(text, submit, suggest, cancel, validate, wrap_on_overflow)
if self.state ~= default_state then if self.state ~= default_state then
return return
end end
self.wrap_on_overflow = wrap_on_overflow or false;
self.state = { self.state = {
submit = submit or noop, submit = submit or noop,
suggest = suggest or noop, suggest = suggest or noop,

View File

@ -13,7 +13,7 @@ function DocView:draw_overlay(...)
local y = self.position.y local y = self.position.y
local w = math.ceil(SCALE * 1) local w = math.ceil(SCALE * 1)
local h = self.size.y local h = self.size.y
local color = style.guide or style.selection local color = style.guide or style.selection
renderer.draw_rect(x, y, w, h, color) renderer.draw_rect(x, y, w, h, color)
end end