From 6c89a3e5752d71e52bdeed2619a8990628f50784 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Wed, 1 Jun 2022 18:34:11 +0200 Subject: [PATCH 1/2] Add `show_suggestions` to `CommandView` options --- data/core/commands/findreplace.lua | 6 +++--- data/core/commandview.lua | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/data/core/commands/findreplace.lua b/data/core/commands/findreplace.lua index c999103f..cd3d4b44 100644 --- a/data/core/commands/findreplace.lua +++ b/data/core/commands/findreplace.lua @@ -61,8 +61,8 @@ local function find(label, search_fn) core.command_view:set_text(text, true) core.status_view:show_tooltip(get_find_tooltip()) - core.command_view:set_hidden_suggestions() core.command_view:enter(label, { + show_suggestions = false, submit = function(text, item) insert_unique(core.previous_find, text) core.status_view:remove_tooltip() @@ -94,15 +94,15 @@ local function replace(kind, default, fn) core.command_view:set_text(default, true) core.status_view:show_tooltip(get_find_tooltip()) - core.command_view:set_hidden_suggestions() core.command_view:enter("Find To Replace " .. kind, { + show_suggestions = false, submit = 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, { + show_suggestions = false, submit = function(new) core.status_view:remove_tooltip() insert_unique(core.previous_replace, new) diff --git a/data/core/commandview.lua b/data/core/commandview.lua index 401164d2..abba458e 100644 --- a/data/core/commandview.lua +++ b/data/core/commandview.lua @@ -26,6 +26,7 @@ local default_state = { suggest = noop, cancel = noop, validate = function() return true end, + show_suggestions = true, typeahead = true, wrap = true, } @@ -36,7 +37,6 @@ function CommandView:new() self.suggestion_idx = 1 self.suggestions = {} self.suggestions_height = 0 - self.show_suggestions = true self.last_change_id = 0 self.last_text = "" self.gutter_width = 0 @@ -50,7 +50,8 @@ end function CommandView:set_hidden_suggestions() - self.show_suggestions = false + core.warn("Using deprecated function CommandView:set_hidden_suggestions") + self.state.show_suggestions = false end @@ -101,7 +102,7 @@ function CommandView:move_suggestion_idx(dir) end end - if self.show_suggestions then + if self.state.show_suggestions then local n = self.suggestion_idx + dir self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions) self:complete() @@ -162,6 +163,12 @@ function CommandView:enter(text, ...) } 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) core.set_active_view(self) @@ -180,7 +187,6 @@ function CommandView:exit(submitted, inexplicit) self.doc:reset() self.suggestions = {} if not submitted then cancel(not inexplicit) end - self.show_suggestions = true self.save_suggestion = nil self.last_text = "" end @@ -246,7 +252,7 @@ function CommandView:update() -- update suggestions box 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") -- update suggestion cursor offset @@ -316,7 +322,7 @@ end function CommandView:draw() CommandView.super.draw(self) - if self.show_suggestions then + if self.state.show_suggestions then core.root_view:defer_draw(draw_suggestions_box, self) end end From ec58b1f0bdaafd8791743c7f04e99b0cf0733976 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Wed, 1 Jun 2022 19:34:46 +0200 Subject: [PATCH 2/2] Add `text` and `select_text` to `CommandView` options --- data/core/commands/core.lua | 13 +++++++++---- data/core/commands/doc.lua | 8 +++++--- data/core/commands/findreplace.lua | 10 ++++++---- data/core/commandview.lua | 23 ++++++++++++++++++++--- data/plugins/projectsearch.lua | 13 ++++++------- data/plugins/treeview.lua | 10 +++++++--- 6 files changed, 53 insertions(+), 24 deletions(-) diff --git a/data/core/commands/core.lua b/data/core/commands/core.lua index 40be12a0..cdf8d421 100644 --- a/data/core/commands/core.lua +++ b/data/core/commands/core.lua @@ -122,15 +122,16 @@ command.add(nil, { ["core:open-file"] = function() local view = core.active_view + local text if view.doc and view.doc.abs_filename then local dirname, filename = view.doc.abs_filename:match("(.*)[/\\](.+)$") if dirname then dirname = core.normalize_to_project_dir(dirname) - local text = dirname == core.project_dir and "" or common.home_encode(dirname) .. PATHSEP - core.command_view:set_text(text) + text = dirname == core.project_dir and "" or common.home_encode(dirname) .. PATHSEP end end core.command_view:enter("Open File", { + text = text, submit = function(text) local filename = system.absolute_path(common.home_expand(text)) core.root_view:open_doc(core.open_doc(filename)) @@ -182,10 +183,12 @@ command.add(nil, { ["core:change-project-folder"] = function() local dirname = common.dirname(core.project_dir) + local text if dirname then - core.command_view:set_text(common.home_encode(dirname) .. PATHSEP) + text = common.home_encode(dirname) .. PATHSEP end core.command_view:enter("Change Project Folder", { + text = text, submit = function(text) local path = common.home_expand(text) local abs_path = check_directory_path(path) @@ -204,10 +207,12 @@ command.add(nil, { ["core:open-project-folder"] = function() local dirname = common.dirname(core.project_dir) + local text if dirname then - core.command_view:set_text(common.home_encode(dirname) .. PATHSEP) + text = common.home_encode(dirname) .. PATHSEP end core.command_view:enter("Open Project", { + text = text, submit = function(text) local path = common.home_expand(text) local abs_path = check_directory_path(path) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index f572ec26..1b39a9d9 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -448,13 +448,15 @@ local commands = { ["doc:save-as"] = function() local last_doc = core.last_active_view and core.last_active_view.doc + local text if doc().filename then - core.command_view:set_text(doc().filename) + text = doc().filename elseif last_doc and last_doc.filename then local dirname, filename = core.last_active_view.doc.abs_filename:match("(.*)[/\\](.+)$") - core.command_view:set_text(core.normalize_to_project_dir(dirname) .. PATHSEP) + text = core.normalize_to_project_dir(dirname) .. PATHSEP end core.command_view:enter("Save As", { + text = text, submit = function(filename) save(common.home_expand(filename)) end, @@ -482,8 +484,8 @@ local commands = { core.error("Cannot rename unsaved doc") return end - core.command_view:set_text(old_filename) core.command_view:enter("Rename", { + text = old_filename, submit = function(filename) save(common.home_expand(filename)) core.log("Renamed \"%s\" to \"%s\"", old_filename, filename) diff --git a/data/core/commands/findreplace.lua b/data/core/commands/findreplace.lua index cd3d4b44..584d4f67 100644 --- a/data/core/commands/findreplace.lua +++ b/data/core/commands/findreplace.lua @@ -58,10 +58,11 @@ local function find(label, search_fn) local text = last_view.doc:get_text(table.unpack(last_sel)) found_expression = false - core.command_view:set_text(text, true) core.status_view:show_tooltip(get_find_tooltip()) core.command_view:enter(label, { + text = text, + select_text = true, show_suggestions = false, submit = function(text, item) insert_unique(core.previous_find, text) @@ -91,17 +92,18 @@ end local function replace(kind, default, fn) - core.command_view:set_text(default, true) - core.status_view:show_tooltip(get_find_tooltip()) core.command_view:enter("Find To Replace " .. kind, { + text = default, + select_text = true, show_suggestions = false, submit = 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:enter(s, { + text = old, + select_text = true, show_suggestions = false, submit = function(new) core.status_view:remove_tooltip() diff --git a/data/core/commandview.lua b/data/core/commandview.lua index abba458e..8017fcfc 100644 --- a/data/core/commandview.lua +++ b/data/core/commandview.lua @@ -26,6 +26,8 @@ local default_state = { suggest = noop, cancel = noop, validate = function() return true end, + text = "", + select_text = false, show_suggestions = true, typeahead = true, wrap = true, @@ -146,14 +148,14 @@ function CommandView:submit() end -function CommandView:enter(text, ...) +function CommandView:enter(label, ...) if self.state ~= default_state then return end local options = select(1, ...) if type(options) ~= "table" then - core.log("Warning: deprecated CommandView:enter usage") + core.warn("Using CommandView:enter in a deprecated way") local submit, suggest, cancel, validate = ... options = { submit = submit, @@ -171,10 +173,25 @@ function CommandView:enter(text, ...) self.state = common.merge(default_state, options) + -- We need to keep the text entered with CommandView:set_text to + -- maintain compatibility with deprecated usage, but still allow + -- overwriting with options.text + local old_text = self:get_text() + if old_text ~= "" then + core.warn("Using deprecated function CommandView:set_text") + end + if options.text or options.select_text then + local text = options.text or old_text + self:set_text(text, self.state.select_text) + end + -- Replace with a simple + -- self:set_text(self.state.text, self.state.select_text) + -- once old usage is removed + core.set_active_view(self) self:update_suggestions() self.gutter_text_brightness = 100 - self.label = text .. ": " + self.label = label .. ": " end diff --git a/data/plugins/projectsearch.lua b/data/plugins/projectsearch.lua index c43e3147..2145199a 100644 --- a/data/plugins/projectsearch.lua +++ b/data/plugins/projectsearch.lua @@ -229,22 +229,20 @@ local function begin_search(text, fn) end -local function set_command_view_text() +local function get_selected_text() local view = core.active_view local doc = (view and view.doc) and view.doc or nil if doc then - core.command_view:set_text( - doc:get_text(table.unpack({ doc:get_selection() })), - true - ) + return doc:get_text(table.unpack({ doc:get_selection() })) end end command.add(nil, { ["project-search:find"] = function() - set_command_view_text() core.command_view:enter("Find Text In Project", { + text = get_selected_text(), + select_text = true, submit = function(text) text = text:lower() begin_search(text, function(line_text) @@ -266,8 +264,9 @@ command.add(nil, { end, ["project-search:fuzzy-find"] = function() - set_command_view_text() core.command_view:enter("Fuzzy Find Text In Project", { + text = get_selected_text(), + select_text = true, submit = function(text) begin_search(text, function(line_text) return common.fuzzy_match(line_text, text) and 1 diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index ecb3a76d..7d53e790 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -689,8 +689,8 @@ command.add(function() return treeitem() ~= nil end, { ["treeview:rename"] = function() local old_filename = treeitem().filename local old_abs_filename = treeitem().abs_filename - core.command_view:set_text(old_filename) core.command_view:enter("Rename", { + text = old_filename, submit = function(filename) filename = core.normalize_to_project_dir(filename) local abs_filename = core.project_absolute_path(filename) @@ -713,10 +713,12 @@ command.add(function() return treeitem() ~= nil end, { end, ["treeview:new-file"] = function() + local text if not is_project_folder(treeitem().abs_filename) then - core.command_view:set_text(treeitem().filename .. "/") + text = treeitem().filename .. "/" end core.command_view:enter("Filename", { + text = text, submit = function(filename) local doc_filename = core.project_dir .. PATHSEP .. filename local file = io.open(doc_filename, "a+") @@ -730,10 +732,12 @@ command.add(function() return treeitem() ~= nil end, { end, ["treeview:new-folder"] = function() + local text if not is_project_folder(treeitem().abs_filename) then - core.command_view:set_text(treeitem().filename .. "/") + text = treeitem().filename .. "/" end core.command_view:enter("Folder Name", { + text = text, submit = function(filename) local dir_path = core.project_dir .. PATHSEP .. filename common.mkdirp(dir_path)