From f85fe102d99aa6af042ac1f6f7855bfaf7c07f92 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Tue, 7 Sep 2021 17:06:19 +0200 Subject: [PATCH] Implement hidden suggestions for find dialog --- data/core/commands/findreplace.lua | 5 ++++- data/core/commandview.lua | 23 +++++++++++++++++++---- data/core/init.lua | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/data/core/commands/findreplace.lua b/data/core/commands/findreplace.lua index 03aa7737..73a5ae07 100644 --- a/data/core/commands/findreplace.lua +++ b/data/core/commands/findreplace.lua @@ -49,7 +49,9 @@ local function find(label, search_fn) core.command_view:set_text(text, true) core.status_view:show_tooltip(get_find_tooltip()) - core.command_view:enter(label, function(text) + core.command_view:set_hidden_suggestions() + core.command_view:enter(label, function(text, item) + table.insert(core.previous_find, text) core.status_view:remove_tooltip() if found then last_fn, last_text = search_fn, text @@ -61,6 +63,7 @@ local function find(label, search_fn) end, function(text) found = update_preview(last_sel, search_fn, text) last_fn, last_text = search_fn, text + return core.previous_find end, function(explicit) core.status_view:remove_tooltip() if explicit then diff --git a/data/core/commandview.lua b/data/core/commandview.lua index eb7febc7..d41db0d5 100644 --- a/data/core/commandview.lua +++ b/data/core/commandview.lua @@ -34,6 +34,7 @@ function CommandView:new() self.suggestion_idx = 1 self.suggestions = {} self.suggestions_height = 0 + self.show_suggestions = true self.last_change_id = 0 self.gutter_width = 0 self.gutter_text_brightness = 0 @@ -45,6 +46,11 @@ function CommandView:new() end +function CommandView:set_hidden_suggestions() + self.show_suggestions = false +end + + function CommandView:get_name() return View.get_name(self) end @@ -83,10 +89,16 @@ end function CommandView:move_suggestion_idx(dir) - local n = self.suggestion_idx + dir - self.suggestion_idx = common.clamp(n, 1, #self.suggestions) + local current_suggestion = self.suggestions[self.suggestion_idx].text + if self.show_suggestions or self:get_text() == current_suggestion then + local n = self.suggestion_idx + dir + self.suggestion_idx = common.clamp(n, 1, #self.suggestions) + end self:complete() self.last_change_id = self.doc:get_change_id() + if not self.show_suggestions then + self.state.suggest(self:get_text()) + end end @@ -134,6 +146,7 @@ function CommandView:exit(submitted, inexplicit) self.doc:reset() self.suggestions = {} if not submitted then cancel(not inexplicit) end + self.show_suggestions = true end @@ -187,7 +200,7 @@ function CommandView:update() -- update suggestions box height local lh = self:get_suggestion_line_height() - local dest = math.min(#self.suggestions, max_suggestions) * lh + local dest = self.show_suggestions and math.min(#self.suggestions, max_suggestions) * lh or 0 self:move_towards("suggestions_height", dest) -- update suggestion cursor offset @@ -256,7 +269,9 @@ end function CommandView:draw() CommandView.super.draw(self) - core.root_view:defer_draw(draw_suggestions_box, self) + if self.show_suggestions then + core.root_view:defer_draw(draw_suggestions_box, self) + end end diff --git a/data/core/init.lua b/data/core/init.lua index e84792ac..b1072b38 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -496,6 +496,7 @@ function core.init() core.redraw = true core.visited_files = {} + core.previous_find = {} core.restart_request = false core.quit_request = false core.replacements = whitespace_replacements()