Add typeahead to `CommandView` (#963)

This commit is contained in:
Guldoman 2022-04-30 22:09:40 +02:00 committed by GitHub
parent 9de75988ba
commit 2e0d0995d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -36,6 +36,7 @@ function CommandView:new()
self.suggestions_height = 0 self.suggestions_height = 0
self.show_suggestions = true self.show_suggestions = true
self.last_change_id = 0 self.last_change_id = 0
self.last_text = ""
self.gutter_width = 0 self.gutter_width = 0
self.gutter_text_brightness = 0 self.gutter_text_brightness = 0
self.selection_offset = 0 self.selection_offset = 0
@ -80,6 +81,7 @@ end
function CommandView:set_text(text, select) function CommandView:set_text(text, select)
self.last_text = text
self.doc:remove(1, 1, math.huge, math.huge) self.doc:remove(1, 1, math.huge, math.huge)
self.doc:text_input(text) self.doc:text_input(text)
if select then if select then
@ -161,6 +163,7 @@ function CommandView:exit(submitted, inexplicit)
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 self.save_suggestion = nil
self.last_text = ""
end end
@ -198,6 +201,16 @@ 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
local current_text = self:get_text()
local suggested_text = self.suggestions[self.suggestion_idx].text or ""
if #self.last_text < #current_text and
string.find(suggested_text, current_text, 1, true) == 1 then
self:set_text(suggested_text)
self.doc:set_selection(1, #current_text + 1, 1, math.huge)
end
self.last_text = current_text
end
self.last_change_id = self.doc:get_change_id() self.last_change_id = self.doc:get_change_id()
end end