From 11e27c6fda10b1223b572c187ce724a4e03dce5a Mon Sep 17 00:00:00 2001 From: Guldoman Date: Mon, 30 May 2022 22:06:47 +0200 Subject: [PATCH] Use new `CommandView:enter` options table --- data/core/commands/core.lua | 226 ++++++++++++++++------------- data/core/commands/doc.lua | 62 ++++---- data/core/commands/files.lua | 12 +- data/core/commands/findreplace.lua | 82 ++++++----- data/core/commands/statusbar.lua | 16 +- data/core/docview.lua | 27 ++-- data/plugins/detectindent.lua | 23 ++- data/plugins/projectsearch.lua | 40 ++--- data/plugins/tabularize.lua | 30 ++-- data/plugins/treeview.lua | 65 +++++---- 10 files changed, 321 insertions(+), 262 deletions(-) diff --git a/data/core/commands/core.lua b/data/core/commands/core.lua index 9ba809c4..40be12a0 100644 --- a/data/core/commands/core.lua +++ b/data/core/commands/core.lua @@ -48,36 +48,42 @@ command.add(nil, { end, ["core:reload-module"] = function() - core.command_view:enter("Reload Module", function(text, item) - local text = item and item.text or text - core.reload_module(text) - core.log("Reloaded module %q", text) - end, function(text) - local items = {} - for name in pairs(package.loaded) do - table.insert(items, name) + core.command_view:enter("Reload Module", { + submit = function(text, item) + local text = item and item.text or text + core.reload_module(text) + core.log("Reloaded module %q", text) + end, + suggest = function(text) + local items = {} + for name in pairs(package.loaded) do + table.insert(items, name) + end + return common.fuzzy_match(items, text) end - return common.fuzzy_match(items, text) - end) + }) end, ["core:find-command"] = function() local commands = command.get_all_valid() - core.command_view:enter("Do Command", function(text, item) - if item then - command.perform(item.command) + core.command_view:enter("Do Command", { + submit = function(text, item) + if item then + command.perform(item.command) + end + end, + suggest = function(text) + local res = common.fuzzy_match(commands, text) + for i, name in ipairs(res) do + res[i] = { + text = command.prettify_name(name), + info = keymap.get_binding(name), + command = name, + } + end + return res end - end, function(text) - local res = common.fuzzy_match(commands, text) - for i, name in ipairs(res) do - res[i] = { - text = command.prettify_name(name), - info = keymap.get_binding(name), - command = name, - } - end - return res - end) + }) end, ["core:find-file"] = function() @@ -91,12 +97,15 @@ command.add(nil, { table.insert(files, common.home_encode(path .. item.filename)) end end - core.command_view:enter("Open File From Project", function(text, item) - text = item and item.text or text - core.root_view:open_doc(core.open_doc(common.home_expand(text))) - end, function(text) - return common.fuzzy_match_with_recents(files, core.visited_files, text) - end) + core.command_view:enter("Open File From Project", { + submit = function(text, item) + text = item and item.text or text + core.root_view:open_doc(core.open_doc(common.home_expand(text))) + end, + suggest = function(text) + return common.fuzzy_match_with_recents(files, core.visited_files, text) + end + }) end, ["core:new-doc"] = function() @@ -104,12 +113,11 @@ command.add(nil, { end, ["core:new-named-doc"] = function() - core.command_view:enter( - "File name", - function(text) + core.command_view:enter("File name", { + submit = function(text) core.root_view:open_doc(core.open_doc(text)) end - ) + }) end, ["core:open-file"] = function() @@ -122,30 +130,34 @@ command.add(nil, { core.command_view:set_text(text) end end - core.command_view:enter("Open File", function(text) - local filename = system.absolute_path(common.home_expand(text)) - core.root_view:open_doc(core.open_doc(filename)) - end, function (text) - return common.home_encode_list(common.path_suggest(common.home_expand(text))) - end, nil, function(text) - local filename = common.home_expand(text) - local path_stat, err = system.get_file_info(filename) - if err then - if err:find("No such file", 1, true) then - -- check if the containing directory exists - local dirname = common.dirname(filename) - local dir_stat = dirname and system.get_file_info(dirname) - if not dirname or (dir_stat and dir_stat.type == 'dir') then + core.command_view:enter("Open File", { + submit = function(text) + local filename = system.absolute_path(common.home_expand(text)) + core.root_view:open_doc(core.open_doc(filename)) + end, + suggest = function (text) + return common.home_encode_list(common.path_suggest(common.home_expand(text))) + end, + validate = function(text) + local filename = common.home_expand(text) + local path_stat, err = system.get_file_info(filename) + if err then + if err:find("No such file", 1, true) then + -- check if the containing directory exists + local dirname = common.dirname(filename) + local dir_stat = dirname and system.get_file_info(dirname) + if not dirname or (dir_stat and dir_stat.type == 'dir') then + return true + end + end + core.error("Cannot open file %s: %s", text, err) + elseif path_stat.type == 'dir' then + core.error("Cannot open %s, is a folder", text) + else return true end - end - core.error("Cannot open file %s: %s", text, err) - elseif path_stat.type == 'dir' then - core.error("Cannot open %s, is a folder", text) - else - return true - end - end, true) + end, + }) end, ["core:open-log"] = function() @@ -173,18 +185,21 @@ command.add(nil, { if dirname then core.command_view:set_text(common.home_encode(dirname) .. PATHSEP) end - core.command_view:enter("Change Project Folder", function(text) - local path = common.home_expand(text) - local abs_path = check_directory_path(path) - if not abs_path then - core.error("Cannot open directory %q", path) - return - end - if abs_path == core.project_dir then return end - core.confirm_close_docs(core.docs, function(dirpath) - core.open_folder_project(dirpath) - end, abs_path) - end, suggest_directory) + core.command_view:enter("Change Project Folder", { + submit = function(text) + local path = common.home_expand(text) + local abs_path = check_directory_path(path) + if not abs_path then + core.error("Cannot open directory %q", path) + return + end + if abs_path == core.project_dir then return end + core.confirm_close_docs(core.docs, function(dirpath) + core.open_folder_project(dirpath) + end, abs_path) + end, + suggest = suggest_directory + }) end, ["core:open-project-folder"] = function() @@ -192,34 +207,40 @@ command.add(nil, { if dirname then core.command_view:set_text(common.home_encode(dirname) .. PATHSEP) end - core.command_view:enter("Open Project", function(text) - local path = common.home_expand(text) - local abs_path = check_directory_path(path) - if not abs_path then - core.error("Cannot open directory %q", path) - return - end - if abs_path == core.project_dir then - core.error("Directory %q is currently opened", abs_path) - return - end - system.exec(string.format("%q %q", EXEFILE, abs_path)) - end, suggest_directory) + core.command_view:enter("Open Project", { + submit = function(text) + local path = common.home_expand(text) + local abs_path = check_directory_path(path) + if not abs_path then + core.error("Cannot open directory %q", path) + return + end + if abs_path == core.project_dir then + core.error("Directory %q is currently opened", abs_path) + return + end + system.exec(string.format("%q %q", EXEFILE, abs_path)) + end, + suggest = suggest_directory + }) end, ["core:add-directory"] = function() - core.command_view:enter("Add Directory", function(text) - text = common.home_expand(text) - local path_stat, err = system.get_file_info(text) - if not path_stat then - core.error("cannot open %q: %s", text, err) - return - elseif path_stat.type ~= 'dir' then - core.error("%q is not a directory", text) - return - end - core.add_project_directory(system.absolute_path(text)) - end, suggest_directory) + core.command_view:enter("Add Directory", { + submit = function(text) + text = common.home_expand(text) + local path_stat, err = system.get_file_info(text) + if not path_stat then + core.error("cannot open %q: %s", text, err) + return + elseif path_stat.type ~= 'dir' then + core.error("%q is not a directory", text) + return + end + core.add_project_directory(system.absolute_path(text)) + end, + suggest = suggest_directory + }) end, ["core:remove-directory"] = function() @@ -228,14 +249,17 @@ command.add(nil, { for i = n, 2, -1 do dir_list[n - i + 1] = core.project_directories[i].name end - core.command_view:enter("Remove Directory", function(text, item) - text = common.home_expand(item and item.text or text) - if not core.remove_project_directory(text) then - core.error("No directory %q to be removed", text) + core.command_view:enter("Remove Directory", { + submit = function(text, item) + text = common.home_expand(item and item.text or text) + if not core.remove_project_directory(text) then + core.error("No directory %q to be removed", text) + end + end, + suggest = function(text) + text = common.home_expand(text) + return common.home_encode_list(common.dir_list_suggest(text, dir_list)) end - end, function(text) - text = common.home_expand(text) - return common.home_encode_list(common.dir_list_suggest(text, dir_list)) - end) + }) end, }) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 5d953805..f572ec26 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -423,21 +423,23 @@ local commands = { end end - core.command_view:enter("Go To Line", function(text, item) - local line = item and item.line or tonumber(text) - if not line then - core.error("Invalid line number or unmatched string") - return + core.command_view:enter("Go To Line", { + submit = function(text, item) + local line = item and item.line or tonumber(text) + if not line then + core.error("Invalid line number or unmatched string") + return + end + dv.doc:set_selection(line, 1 ) + dv:scroll_to_line(line, true) + end, + suggest = function(text) + if not text:find("^%d*$") then + init_items() + return common.fuzzy_match(items, text) + end end - dv.doc:set_selection(line, 1 ) - dv:scroll_to_line(line, true) - - end, function(text) - if not text:find("^%d*$") then - init_items() - return common.fuzzy_match(items, text) - end - end) + }) end, ["doc:toggle-line-ending"] = function() @@ -452,11 +454,14 @@ local commands = { local dirname, filename = core.last_active_view.doc.abs_filename:match("(.*)[/\\](.+)$") core.command_view:set_text(core.normalize_to_project_dir(dirname) .. PATHSEP) end - core.command_view:enter("Save As", function(filename) - save(common.home_expand(filename)) - end, function (text) - return common.home_encode_list(common.path_suggest(common.home_expand(text))) - end) + core.command_view:enter("Save As", { + submit = function(filename) + save(common.home_expand(filename)) + end, + suggest = function (text) + return common.home_encode_list(common.path_suggest(common.home_expand(text))) + end + }) end, ["doc:save"] = function() @@ -478,15 +483,18 @@ local commands = { return end core.command_view:set_text(old_filename) - core.command_view:enter("Rename", function(filename) - save(common.home_expand(filename)) - core.log("Renamed \"%s\" to \"%s\"", old_filename, filename) - if filename ~= old_filename then - os.remove(old_filename) + core.command_view:enter("Rename", { + submit = function(filename) + save(common.home_expand(filename)) + core.log("Renamed \"%s\" to \"%s\"", old_filename, filename) + if filename ~= old_filename then + os.remove(old_filename) + end + end, + suggest = function (text) + return common.home_encode_list(common.path_suggest(common.home_expand(text))) end - end, function (text) - return common.home_encode_list(common.path_suggest(common.home_expand(text))) - end) + }) end, diff --git a/data/core/commands/files.lua b/data/core/commands/files.lua index b2fdb336..a13db5df 100644 --- a/data/core/commands/files.lua +++ b/data/core/commands/files.lua @@ -4,11 +4,13 @@ local common = require "core.common" command.add(nil, { ["files:create-directory"] = function() - core.command_view:enter("New directory name", function(text) - local success, err, path = common.mkdirp(text) - if not success then - core.error("cannot create directory %q: %s", path, err) + core.command_view:enter("New directory name", { + submit = function(text) + local success, err, path = common.mkdirp(text) + if not success then + core.error("cannot create directory %q: %s", path, err) + end end - end) + }) end, }) diff --git a/data/core/commands/findreplace.lua b/data/core/commands/findreplace.lua index db6a2dd6..ccfbf118 100644 --- a/data/core/commands/findreplace.lua +++ b/data/core/commands/findreplace.lua @@ -62,27 +62,31 @@ local function find(label, search_fn) core.status_view:show_tooltip(get_find_tooltip()) core.command_view:set_hidden_suggestions() - core.command_view:enter(label, function(text, item) - insert_unique(core.previous_find, text) - core.status_view:remove_tooltip() - if found_expression then + core.command_view:enter(label, { + submit = function(text, item) + insert_unique(core.previous_find, text) + core.status_view:remove_tooltip() + if found_expression then + last_fn, last_text = search_fn, text + else + core.error("Couldn't find %q", text) + last_view.doc:set_selection(table.unpack(last_sel)) + last_view:scroll_to_make_visible(table.unpack(last_sel)) + end + end, + suggest = function(text) + update_preview(last_sel, search_fn, text) last_fn, last_text = search_fn, text - else - core.error("Couldn't find %q", text) - last_view.doc:set_selection(table.unpack(last_sel)) - last_view:scroll_to_make_visible(table.unpack(last_sel)) + return core.previous_find + end, + cancel = function(explicit) + core.status_view:remove_tooltip() + if explicit then + last_view.doc:set_selection(table.unpack(last_sel)) + last_view:scroll_to_make_visible(table.unpack(last_sel)) + end end - end, function(text) - 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 - last_view.doc:set_selection(table.unpack(last_sel)) - last_view:scroll_to_make_visible(table.unpack(last_sel)) - end - end) + }) end @@ -91,25 +95,31 @@ 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) + core.command_view:enter("Find To Replace " .. kind, { + 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, function(new) + local s = string.format("Replace %s %q With", kind, old) + core.command_view:set_hidden_suggestions() + core.command_view:enter(s, { + submit = function(new) + core.status_view:remove_tooltip() + insert_unique(core.previous_replace, new) + local n = doc():replace(function(text) + return fn(text, old, new) + end) + core.log("Replaced %d instance(s) of %s %q with %q", n, kind, old, new) + end, + suggest = function() return core.previous_replace end, function() + core.status_view:remove_tooltip() + end + }) + end, + suggest = function() return core.previous_find end, function() core.status_view:remove_tooltip() - insert_unique(core.previous_replace, new) - local n = doc():replace(function(text) - return fn(text, old, new) - end) - core.log("Replaced %d instance(s) of %s %q with %q", n, kind, old, new) - end, function() return core.previous_replace end, function() - core.status_view:remove_tooltip() - end) - end, function() return core.previous_find end, function() - core.status_view:remove_tooltip() - end) + end + }) end local function has_selection() diff --git a/data/core/commands/statusbar.lua b/data/core/commands/statusbar.lua index ac8837ed..5676ef52 100644 --- a/data/core/commands/statusbar.lua +++ b/data/core/commands/statusbar.lua @@ -50,20 +50,20 @@ command.add(nil, { core.status_view:display_messages(true) end, ["status-bar:hide-item"] = function() - core.command_view:enter("Status bar item to hide", - function(text, item) + core.command_view:enter("Status bar item to hide", { + submit = function(text, item) core.status_view:hide_items(item.name) end, - status_view_get_items - ) + suggest = status_view_get_items + }) end, ["status-bar:show-item"] = function() - core.command_view:enter("Status bar item to show", - function(text, item) + core.command_view:enter("Status bar item to show", { + submit = function(text, item) core.status_view:show_items(item.name) end, - status_view_get_items - ) + suggest = status_view_get_items + }) end, ["status-bar:reset-items"] = function() core.status_view:show_items() diff --git a/data/core/docview.lua b/data/core/docview.lua index f5440262..bf76db38 100644 --- a/data/core/docview.lua +++ b/data/core/docview.lua @@ -65,19 +65,22 @@ end function DocView:try_close(do_close) if self.doc:is_dirty() and #core.get_views_referencing_doc(self.doc) == 1 then - core.command_view:enter("Unsaved Changes; Confirm Close", function(_, item) - if item.text:match("^[cC]") then - do_close() - elseif item.text:match("^[sS]") then - self.doc:save() - do_close() + core.command_view:enter("Unsaved Changes; Confirm Close", { + submit = function(_, item) + if item.text:match("^[cC]") then + do_close() + elseif item.text:match("^[sS]") then + self.doc:save() + do_close() + end + end, + suggest = function(text) + local items = {} + if not text:find("^[^cC]") then table.insert(items, "Close Without Saving") end + if not text:find("^[^sS]") then table.insert(items, "Save And Close") end + return items end - end, function(text) - local items = {} - if not text:find("^[^cC]") then table.insert(items, "Close Without Saving") end - if not text:find("^[^sS]") then table.insert(items, "Save And Close") end - return items - end) + }) else do_close() end diff --git a/data/plugins/detectindent.lua b/data/plugins/detectindent.lua index 3501f472..29204760 100644 --- a/data/plugins/detectindent.lua +++ b/data/plugins/detectindent.lua @@ -300,22 +300,20 @@ local function set_indent_type(doc, type) end local function set_indent_type_command() - core.command_view:enter( - "Specify indent style for this file", - function(value) -- submit + core.command_view:enter("Specify indent style for this file", { + submit = function(value) local doc = core.active_view.doc value = value:lower() set_indent_type(doc, value == "tabs" and "hard" or "soft") end, - function(text) -- suggest + suggest = function(text) return common.fuzzy_match({"tabs", "spaces"}, text) end, - nil, -- cancel - function(text) -- validate + validate = function(text) local t = text:lower() return t == "tabs" or t == "spaces" end - ) + }) end @@ -330,20 +328,17 @@ local function set_indent_size(doc, size) end local function set_indent_size_command() - core.command_view:enter( - "Specify indent size for current file", - function(value) -- submit + core.command_view:enter("Specify indent size for current file", { + submit = function(value) value = math.floor(tonumber(value)) local doc = core.active_view.doc set_indent_size(doc, value) end, - nil, -- suggest - nil, -- cancel - function(value) -- validate + validate = function(value) value = tonumber(value) return value ~= nil and value >= 1 end - ) + }) end diff --git a/data/plugins/projectsearch.lua b/data/plugins/projectsearch.lua index d7a79120..6ac51272 100644 --- a/data/plugins/projectsearch.lua +++ b/data/plugins/projectsearch.lua @@ -244,30 +244,36 @@ end command.add(nil, { ["project-search:find"] = function() set_command_view_text() - core.command_view:enter("Find Text In Project", function(text) - text = text:lower() - begin_search(text, function(line_text) - return line_text:lower():find(text, nil, true) - end) - end) + core.command_view:enter("Find Text In Project", { + submit = function(text) + text = text:lower() + begin_search(text, function(line_text) + return line_text:lower():find(text, nil, true) + end) + end + }) end, ["project-search:find-regex"] = function() - core.command_view:enter("Find Regex In Project", function(text) - local re = regex.compile(text, "i") - begin_search(text, function(line_text) - return regex.cmatch(re, line_text) - end) - end) + core.command_view:enter("Find Regex In Project", { + submit = function(text) + local re = regex.compile(text, "i") + begin_search(text, function(line_text) + return regex.cmatch(re, line_text) + end) + end + }) end, ["project-search:fuzzy-find"] = function() set_command_view_text() - core.command_view:enter("Fuzzy Find Text In Project", function(text) - begin_search(text, function(line_text) - return common.fuzzy_match(line_text, text) and 1 - end) - end) + core.command_view:enter("Fuzzy Find Text In Project", { + submit = function(text) + begin_search(text, function(line_text) + return common.fuzzy_match(line_text, text) and 1 + end) + end + }) end, }) diff --git a/data/plugins/tabularize.lua b/data/plugins/tabularize.lua index 7519a387..7d69a4a4 100644 --- a/data/plugins/tabularize.lua +++ b/data/plugins/tabularize.lua @@ -42,20 +42,22 @@ end command.add("core.docview", { ["tabularize:tabularize"] = function() - core.command_view:enter("Tabularize On Delimiter", function(delim) - if delim == "" then delim = " " end + core.command_view:enter("Tabularize On Delimiter", { + submit = function(delim) + if delim == "" then delim = " " end - local doc = core.active_view.doc - local line1, col1, line2, col2, swap = doc:get_selection(true) - line1, col1 = doc:position_offset(line1, col1, translate.start_of_line) - line2, col2 = doc:position_offset(line2, col2, translate.end_of_line) - doc:set_selection(line1, col1, line2, col2, swap) - - doc:replace(function(text) - local lines = gmatch_to_array(text, "[^\n]*\n?") - tabularize_lines(lines, delim) - return table.concat(lines) - end) - end) + local doc = core.active_view.doc + local line1, col1, line2, col2, swap = doc:get_selection(true) + line1, col1 = doc:position_offset(line1, col1, translate.start_of_line) + line2, col2 = doc:position_offset(line2, col2, translate.end_of_line) + doc:set_selection(line1, col1, line2, col2, swap) + + doc:replace(function(text) + local lines = gmatch_to_array(text, "[^\n]*\n?") + tabularize_lines(lines, delim) + return table.concat(lines) + end) + end + }) end, }) diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 16f369b7..419878ee 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -690,48 +690,57 @@ command.add(function() return treeitem() ~= nil end, { local old_filename = treeitem().filename local old_abs_filename = treeitem().abs_filename core.command_view:set_text(old_filename) - core.command_view:enter("Rename", function(filename) - filename = core.normalize_to_project_dir(filename) - local abs_filename = core.project_absolute_path(filename) - local res, err = os.rename(old_abs_filename, abs_filename) - if res then -- successfully renamed - for _, doc in ipairs(core.docs) do - if doc.abs_filename and old_abs_filename == doc.abs_filename then - doc:set_filename(filename, abs_filename) -- make doc point to the new filename - doc:reset_syntax() - break -- only first needed + core.command_view:enter("Rename", { + submit = function(filename) + filename = core.normalize_to_project_dir(filename) + local abs_filename = core.project_absolute_path(filename) + local res, err = os.rename(old_abs_filename, abs_filename) + if res then -- successfully renamed + for _, doc in ipairs(core.docs) do + if doc.abs_filename and old_abs_filename == doc.abs_filename then + doc:set_filename(filename, abs_filename) -- make doc point to the new filename + doc:reset_syntax() + break -- only first needed + end end + core.log("Renamed \"%s\" to \"%s\"", old_filename, filename) + else + core.error("Error while renaming \"%s\" to \"%s\": %s", old_abs_filename, abs_filename, err) end - core.log("Renamed \"%s\" to \"%s\"", old_filename, filename) - else - core.error("Error while renaming \"%s\" to \"%s\": %s", old_abs_filename, abs_filename, err) - end - end, common.path_suggest) + end, + suggest = common.path_suggest + }) end, ["treeview:new-file"] = function() if not is_project_folder(treeitem().abs_filename) then core.command_view:set_text(treeitem().filename .. "/") end - core.command_view:enter("Filename", function(filename) - local doc_filename = core.project_dir .. PATHSEP .. filename - local file = io.open(doc_filename, "a+") - file:write("") - file:close() - core.root_view:open_doc(core.open_doc(doc_filename)) - core.log("Created %s", doc_filename) - end, common.path_suggest) + core.command_view:enter("Filename", { + submit = function(filename) + local doc_filename = core.project_dir .. PATHSEP .. filename + local file = io.open(doc_filename, "a+") + file:write("") + file:close() + core.root_view:open_doc(core.open_doc(doc_filename)) + core.log("Created %s", doc_filename) + end, + suggest = common.path_suggest + }) end, ["treeview:new-folder"] = function() if not is_project_folder(treeitem().abs_filename) then core.command_view:set_text(treeitem().filename .. "/") end - core.command_view:enter("Folder Name", function(filename) - local dir_path = core.project_dir .. PATHSEP .. filename - common.mkdirp(dir_path) - core.log("Created %s", dir_path) - end, common.path_suggest) + core.command_view:enter("Folder Name", { + submit = function(filename) + local dir_path = core.project_dir .. PATHSEP .. filename + common.mkdirp(dir_path) + core.log("Created %s", dir_path) + end, + suggest = common.path_suggest + }) end, ["treeview:open-in-system"] = function()