From b440a2258118c916dfec657c676befca53e108ac Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Tue, 7 Sep 2021 18:33:58 +0200 Subject: [PATCH] Remeber initial user text for hidden suggestions When using hidden suggestions remember the text user was typing when navigating suggestions. Ensure also that in the previously searched expressiosn we have no duplicate entries. --- data/core/commands/findreplace.lua | 15 +++++++++++++-- data/core/commandview.lua | 26 ++++++++++++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/data/core/commands/findreplace.lua b/data/core/commands/findreplace.lua index 7027f293..56761690 100644 --- a/data/core/commands/findreplace.lua +++ b/data/core/commands/findreplace.lua @@ -41,6 +41,16 @@ local function update_preview(sel, search_fn, text) end end + +local function insert_unique(t, v) + local n = #t + for i = 1, n do + if t[i] == v then return end + end + t[n + 1] = v +end + + local function find(label, search_fn) last_view, last_sel, last_finds = core.active_view, { core.active_view.doc:get_selection() }, {} @@ -51,7 +61,7 @@ local function find(label, search_fn) core.command_view:set_hidden_suggestions() core.command_view:enter(label, function(text, item) - table.insert(core.previous_find, text) + insert_unique(core.previous_find, text) core.status_view:remove_tooltip() if found then last_fn, last_text = search_fn, text @@ -80,12 +90,13 @@ local function replace(kind, default, fn) core.status_view:show_tooltip(get_find_tooltip()) core.command_view:set_hidden_suggestions() core.command_view:enter("Find To Replace " .. kind, function(old) + insert_unique(core.previous_find, old) core.command_view:set_text(old, true) local s = string.format("Replace %s %q With", kind, old) core.command_view:set_hidden_suggestions() core.command_view:enter(s, function(new) - table.insert(core.previous_replace, new) + insert_unique(core.previous_replace, new) local n = doc():replace(function(text) return fn(text, old, new) end) diff --git a/data/core/commandview.lua b/data/core/commandview.lua index 5ccb8d3a..b91f1394 100644 --- a/data/core/commandview.lua +++ b/data/core/commandview.lua @@ -89,14 +89,27 @@ end function CommandView:move_suggestion_idx(dir) - local current_suggestion = #self.suggestions > 0 and self.suggestions[self.suggestion_idx].text - if self.show_suggestions or self:get_text() == current_suggestion then + if self.show_suggestions 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:complete() + self.last_change_id = self.doc:get_change_id() + else + local current_suggestion = #self.suggestions > 0 and self.suggestions[self.suggestion_idx].text + local text = self:get_text() + if text == current_suggestion then + local n = self.suggestion_idx + dir + if n == 0 and self.save_suggestion then + self:set_text(self.save_suggestion) + else + self.suggestion_idx = common.clamp(n, 1, #self.suggestions) + self:complete() + end + else + self.save_suggestion = text + self:complete() + end + self.last_change_id = self.doc:get_change_id() self.state.suggest(self:get_text()) end end @@ -147,6 +160,7 @@ function CommandView:exit(submitted, inexplicit) self.suggestions = {} if not submitted then cancel(not inexplicit) end self.show_suggestions = true + self.save_suggestion = nil end