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.
This commit is contained in:
Francesco Abbate 2021-09-07 18:33:58 +02:00 committed by Francesco
parent fa8b3b33b1
commit b440a22581
2 changed files with 33 additions and 8 deletions

View File

@ -41,6 +41,16 @@ local function update_preview(sel, search_fn, text)
end end
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) local function find(label, search_fn)
last_view, last_sel, last_finds = core.active_view, last_view, last_sel, last_finds = core.active_view,
{ core.active_view.doc:get_selection() }, {} { 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:set_hidden_suggestions()
core.command_view:enter(label, function(text, item) 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() core.status_view:remove_tooltip()
if found then if found then
last_fn, last_text = search_fn, text 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.status_view:show_tooltip(get_find_tooltip())
core.command_view:set_hidden_suggestions() core.command_view:set_hidden_suggestions()
core.command_view:enter("Find To Replace " .. kind, function(old) core.command_view:enter("Find To Replace " .. kind, function(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:set_hidden_suggestions()
core.command_view:enter(s, function(new) 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) local n = doc():replace(function(text)
return fn(text, old, new) return fn(text, old, new)
end) end)

View File

@ -89,14 +89,27 @@ end
function CommandView:move_suggestion_idx(dir) function CommandView:move_suggestion_idx(dir)
local current_suggestion = #self.suggestions > 0 and self.suggestions[self.suggestion_idx].text if self.show_suggestions then
if self.show_suggestions or self:get_text() == current_suggestion 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 = common.clamp(n, 1, #self.suggestions)
end
self:complete() self:complete()
self.last_change_id = self.doc:get_change_id() self.last_change_id = self.doc:get_change_id()
if not self.show_suggestions then 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()) self.state.suggest(self:get_text())
end end
end end
@ -147,6 +160,7 @@ function CommandView:exit(submitted, inexplicit)
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.show_suggestions = true
self.save_suggestion = nil
end end