Add `show_suggestions` to `CommandView` options

This commit is contained in:
Guldoman 2022-06-01 18:34:11 +02:00
parent 8ce1ecb897
commit 6c89a3e575
No known key found for this signature in database
GPG Key ID: EA928C8BDA1A8825
2 changed files with 15 additions and 9 deletions

View File

@ -61,8 +61,8 @@ local function find(label, search_fn)
core.command_view:set_text(text, true) core.command_view:set_text(text, true)
core.status_view:show_tooltip(get_find_tooltip()) core.status_view:show_tooltip(get_find_tooltip())
core.command_view:set_hidden_suggestions()
core.command_view:enter(label, { core.command_view:enter(label, {
show_suggestions = false,
submit = function(text, item) submit = function(text, item)
insert_unique(core.previous_find, text) insert_unique(core.previous_find, text)
core.status_view:remove_tooltip() core.status_view:remove_tooltip()
@ -94,15 +94,15 @@ local function replace(kind, default, fn)
core.command_view:set_text(default, true) core.command_view:set_text(default, true)
core.status_view:show_tooltip(get_find_tooltip()) core.status_view:show_tooltip(get_find_tooltip())
core.command_view:set_hidden_suggestions()
core.command_view:enter("Find To Replace " .. kind, { core.command_view:enter("Find To Replace " .. kind, {
show_suggestions = false,
submit = function(old) submit = function(old)
insert_unique(core.previous_find, old) insert_unique(core.previous_find, old)
core.command_view:set_text(old, true) core.command_view:set_text(old, true)
local s = string.format("Replace %s %q With", kind, old) local s = string.format("Replace %s %q With", kind, old)
core.command_view:set_hidden_suggestions()
core.command_view:enter(s, { core.command_view:enter(s, {
show_suggestions = false,
submit = function(new) submit = function(new)
core.status_view:remove_tooltip() core.status_view:remove_tooltip()
insert_unique(core.previous_replace, new) insert_unique(core.previous_replace, new)

View File

@ -26,6 +26,7 @@ local default_state = {
suggest = noop, suggest = noop,
cancel = noop, cancel = noop,
validate = function() return true end, validate = function() return true end,
show_suggestions = true,
typeahead = true, typeahead = true,
wrap = true, wrap = true,
} }
@ -36,7 +37,6 @@ function CommandView:new()
self.suggestion_idx = 1 self.suggestion_idx = 1
self.suggestions = {} self.suggestions = {}
self.suggestions_height = 0 self.suggestions_height = 0
self.show_suggestions = true
self.last_change_id = 0 self.last_change_id = 0
self.last_text = "" self.last_text = ""
self.gutter_width = 0 self.gutter_width = 0
@ -50,7 +50,8 @@ end
function CommandView:set_hidden_suggestions() function CommandView:set_hidden_suggestions()
self.show_suggestions = false core.warn("Using deprecated function CommandView:set_hidden_suggestions")
self.state.show_suggestions = false
end end
@ -101,7 +102,7 @@ function CommandView:move_suggestion_idx(dir)
end end
end end
if self.show_suggestions then if self.state.show_suggestions then
local n = self.suggestion_idx + dir local n = self.suggestion_idx + dir
self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions) self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions)
self:complete() self:complete()
@ -162,6 +163,12 @@ function CommandView:enter(text, ...)
} }
end end
-- Support deprecated CommandView:set_hidden_suggestions
-- Remove this when set_hidden_suggestions is not supported anymore
if options.show_suggestions == nil then
options.show_suggestions = self.state.show_suggestions
end
self.state = common.merge(default_state, options) self.state = common.merge(default_state, options)
core.set_active_view(self) core.set_active_view(self)
@ -180,7 +187,6 @@ function CommandView:exit(submitted, inexplicit)
self.doc:reset() self.doc:reset()
self.suggestions = {} self.suggestions = {}
if not submitted then cancel(not inexplicit) end if not submitted then cancel(not inexplicit) end
self.show_suggestions = true
self.save_suggestion = nil self.save_suggestion = nil
self.last_text = "" self.last_text = ""
end end
@ -246,7 +252,7 @@ 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.show_suggestions and math.min(#self.suggestions, max_suggestions) * lh or 0 local dest = self.state.show_suggestions and math.min(#self.suggestions, max_suggestions) * lh or 0
self:move_towards("suggestions_height", dest, nil, "commandview") self:move_towards("suggestions_height", dest, nil, "commandview")
-- update suggestion cursor offset -- update suggestion cursor offset
@ -316,7 +322,7 @@ end
function CommandView:draw() function CommandView:draw()
CommandView.super.draw(self) CommandView.super.draw(self)
if self.show_suggestions then if self.state.show_suggestions then
core.root_view:defer_draw(draw_suggestions_box, self) core.root_view:defer_draw(draw_suggestions_box, self)
end end
end end