Add options to `CommandView:enter`

Now `CommandView:enter` can accept a table that specifies its behavior.

The old behavior is kept for compatibility.
This commit is contained in:
Guldoman 2022-05-30 21:12:16 +02:00
parent c92c545fb9
commit e4a806a9d0
No known key found for this signature in database
GPG Key ID: EA928C8BDA1A8825
1 changed files with 22 additions and 12 deletions

View File

@ -25,7 +25,9 @@ local default_state = {
submit = noop, submit = noop,
suggest = noop, suggest = noop,
cancel = noop, cancel = noop,
validate = function() return true end validate = function() return true end,
typeahead = true,
wrap = true,
} }
@ -44,7 +46,6 @@ 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
@ -90,9 +91,10 @@ 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) local function overflow_suggestion_idx(n, count)
if self.wrap_on_overflow then if self.state.wrap then
return (n - 1) % count + 1 return (n - 1) % count + 1
else else
return common.clamp(n, 1, count) return common.clamp(n, 1, count)
@ -143,17 +145,25 @@ function CommandView:submit()
end end
function CommandView:enter(text, submit, suggest, cancel, validate, wrap_on_overflow) function CommandView:enter(text, ...)
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; local options = select(1, ...)
self.state = {
submit = submit or noop, if type(options) ~= "table" then
suggest = suggest or noop, core.info("Warning: deprecated CommandView:enter usage")
cancel = cancel or noop, local submit, suggest, cancel, validate = ...
validate = validate or function() return true end options = {
submit = submit,
suggest = suggest,
cancel = cancel,
validate = validate,
} }
end
self.state = common.merge(default_state, options)
core.set_active_view(self) core.set_active_view(self)
self:update_suggestions() self:update_suggestions()
self.gutter_text_brightness = 100 self.gutter_text_brightness = 100
@ -210,7 +220,7 @@ function CommandView:update()
-- update suggestions if text has changed -- update suggestions if text has changed
if self.last_change_id ~= self.doc:get_change_id() then if self.last_change_id ~= self.doc:get_change_id() then
self:update_suggestions() self:update_suggestions()
if self.suggestions[self.suggestion_idx] then if self.state.typeahead and self.suggestions[self.suggestion_idx] then
local current_text = self:get_text() local current_text = self:get_text()
local suggested_text = self.suggestions[self.suggestion_idx].text or "" local suggested_text = self.suggestions[self.suggestion_idx].text or ""
if #self.last_text < #current_text and if #self.last_text < #current_text and