Merge pull request #1004 from Guldoman/PR_commandview_options

Add options table to `CommandView:enter`
This commit is contained in:
Jefferson González 2022-05-31 16:38:36 -04:00 committed by GitHub
commit c09715d0e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 343 additions and 274 deletions

View File

@ -48,26 +48,31 @@ command.add(nil, {
end, end,
["core:reload-module"] = function() ["core:reload-module"] = function()
core.command_view:enter("Reload Module", function(text, item) core.command_view:enter("Reload Module", {
submit = function(text, item)
local text = item and item.text or text local text = item and item.text or text
core.reload_module(text) core.reload_module(text)
core.log("Reloaded module %q", text) core.log("Reloaded module %q", text)
end, function(text) end,
suggest = function(text)
local items = {} local items = {}
for name in pairs(package.loaded) do for name in pairs(package.loaded) do
table.insert(items, name) table.insert(items, name)
end end
return common.fuzzy_match(items, text) return common.fuzzy_match(items, text)
end) end
})
end, end,
["core:find-command"] = function() ["core:find-command"] = function()
local commands = command.get_all_valid() local commands = command.get_all_valid()
core.command_view:enter("Do Command", function(text, item) core.command_view:enter("Do Command", {
submit = function(text, item)
if item then if item then
command.perform(item.command) command.perform(item.command)
end end
end, function(text) end,
suggest = function(text)
local res = common.fuzzy_match(commands, text) local res = common.fuzzy_match(commands, text)
for i, name in ipairs(res) do for i, name in ipairs(res) do
res[i] = { res[i] = {
@ -77,7 +82,8 @@ command.add(nil, {
} }
end end
return res return res
end) end
})
end, end,
["core:find-file"] = function() ["core:find-file"] = function()
@ -91,12 +97,15 @@ command.add(nil, {
table.insert(files, common.home_encode(path .. item.filename)) table.insert(files, common.home_encode(path .. item.filename))
end end
end end
core.command_view:enter("Open File From Project", function(text, item) core.command_view:enter("Open File From Project", {
submit = function(text, item)
text = item and item.text or text text = item and item.text or text
core.root_view:open_doc(core.open_doc(common.home_expand(text))) core.root_view:open_doc(core.open_doc(common.home_expand(text)))
end, function(text) end,
suggest = function(text)
return common.fuzzy_match_with_recents(files, core.visited_files, text) return common.fuzzy_match_with_recents(files, core.visited_files, text)
end) end
})
end, end,
["core:new-doc"] = function() ["core:new-doc"] = function()
@ -104,12 +113,11 @@ command.add(nil, {
end, end,
["core:new-named-doc"] = function() ["core:new-named-doc"] = function()
core.command_view:enter( core.command_view:enter("File name", {
"File name", submit = function(text)
function(text)
core.root_view:open_doc(core.open_doc(text)) core.root_view:open_doc(core.open_doc(text))
end end
) })
end, end,
["core:open-file"] = function() ["core:open-file"] = function()
@ -122,12 +130,15 @@ command.add(nil, {
core.command_view:set_text(text) core.command_view:set_text(text)
end end
end end
core.command_view:enter("Open File", function(text) core.command_view:enter("Open File", {
submit = function(text)
local filename = system.absolute_path(common.home_expand(text)) local filename = system.absolute_path(common.home_expand(text))
core.root_view:open_doc(core.open_doc(filename)) core.root_view:open_doc(core.open_doc(filename))
end, function (text) end,
suggest = function (text)
return common.home_encode_list(common.path_suggest(common.home_expand(text))) return common.home_encode_list(common.path_suggest(common.home_expand(text)))
end, nil, function(text) end,
validate = function(text)
local filename = common.home_expand(text) local filename = common.home_expand(text)
local path_stat, err = system.get_file_info(filename) local path_stat, err = system.get_file_info(filename)
if err then if err then
@ -145,7 +156,8 @@ command.add(nil, {
else else
return true return true
end end
end, true) end,
})
end, end,
["core:open-log"] = function() ["core:open-log"] = function()
@ -173,7 +185,8 @@ command.add(nil, {
if dirname then if dirname then
core.command_view:set_text(common.home_encode(dirname) .. PATHSEP) core.command_view:set_text(common.home_encode(dirname) .. PATHSEP)
end end
core.command_view:enter("Change Project Folder", function(text) core.command_view:enter("Change Project Folder", {
submit = function(text)
local path = common.home_expand(text) local path = common.home_expand(text)
local abs_path = check_directory_path(path) local abs_path = check_directory_path(path)
if not abs_path then if not abs_path then
@ -184,7 +197,9 @@ command.add(nil, {
core.confirm_close_docs(core.docs, function(dirpath) core.confirm_close_docs(core.docs, function(dirpath)
core.open_folder_project(dirpath) core.open_folder_project(dirpath)
end, abs_path) end, abs_path)
end, suggest_directory) end,
suggest = suggest_directory
})
end, end,
["core:open-project-folder"] = function() ["core:open-project-folder"] = function()
@ -192,7 +207,8 @@ command.add(nil, {
if dirname then if dirname then
core.command_view:set_text(common.home_encode(dirname) .. PATHSEP) core.command_view:set_text(common.home_encode(dirname) .. PATHSEP)
end end
core.command_view:enter("Open Project", function(text) core.command_view:enter("Open Project", {
submit = function(text)
local path = common.home_expand(text) local path = common.home_expand(text)
local abs_path = check_directory_path(path) local abs_path = check_directory_path(path)
if not abs_path then if not abs_path then
@ -204,11 +220,14 @@ command.add(nil, {
return return
end end
system.exec(string.format("%q %q", EXEFILE, abs_path)) system.exec(string.format("%q %q", EXEFILE, abs_path))
end, suggest_directory) end,
suggest = suggest_directory
})
end, end,
["core:add-directory"] = function() ["core:add-directory"] = function()
core.command_view:enter("Add Directory", function(text) core.command_view:enter("Add Directory", {
submit = function(text)
text = common.home_expand(text) text = common.home_expand(text)
local path_stat, err = system.get_file_info(text) local path_stat, err = system.get_file_info(text)
if not path_stat then if not path_stat then
@ -219,7 +238,9 @@ command.add(nil, {
return return
end end
core.add_project_directory(system.absolute_path(text)) core.add_project_directory(system.absolute_path(text))
end, suggest_directory) end,
suggest = suggest_directory
})
end, end,
["core:remove-directory"] = function() ["core:remove-directory"] = function()
@ -228,14 +249,17 @@ command.add(nil, {
for i = n, 2, -1 do for i = n, 2, -1 do
dir_list[n - i + 1] = core.project_directories[i].name dir_list[n - i + 1] = core.project_directories[i].name
end end
core.command_view:enter("Remove Directory", function(text, item) core.command_view:enter("Remove Directory", {
submit = function(text, item)
text = common.home_expand(item and item.text or text) text = common.home_expand(item and item.text or text)
if not core.remove_project_directory(text) then if not core.remove_project_directory(text) then
core.error("No directory %q to be removed", text) core.error("No directory %q to be removed", text)
end end
end, function(text) end,
suggest = function(text)
text = common.home_expand(text) text = common.home_expand(text)
return common.home_encode_list(common.dir_list_suggest(text, dir_list)) return common.home_encode_list(common.dir_list_suggest(text, dir_list))
end) end
})
end, end,
}) })

View File

@ -423,7 +423,8 @@ local commands = {
end end
end end
core.command_view:enter("Go To Line", function(text, item) core.command_view:enter("Go To Line", {
submit = function(text, item)
local line = item and item.line or tonumber(text) local line = item and item.line or tonumber(text)
if not line then if not line then
core.error("Invalid line number or unmatched string") core.error("Invalid line number or unmatched string")
@ -431,13 +432,14 @@ local commands = {
end end
dv.doc:set_selection(line, 1 ) dv.doc:set_selection(line, 1 )
dv:scroll_to_line(line, true) dv:scroll_to_line(line, true)
end,
end, function(text) suggest = function(text)
if not text:find("^%d*$") then if not text:find("^%d*$") then
init_items() init_items()
return common.fuzzy_match(items, text) return common.fuzzy_match(items, text)
end end
end) end
})
end, end,
["doc:toggle-line-ending"] = function() ["doc:toggle-line-ending"] = function()
@ -452,11 +454,14 @@ local commands = {
local dirname, filename = core.last_active_view.doc.abs_filename:match("(.*)[/\\](.+)$") local dirname, filename = core.last_active_view.doc.abs_filename:match("(.*)[/\\](.+)$")
core.command_view:set_text(core.normalize_to_project_dir(dirname) .. PATHSEP) core.command_view:set_text(core.normalize_to_project_dir(dirname) .. PATHSEP)
end end
core.command_view:enter("Save As", function(filename) core.command_view:enter("Save As", {
submit = function(filename)
save(common.home_expand(filename)) save(common.home_expand(filename))
end, function (text) end,
suggest = function (text)
return common.home_encode_list(common.path_suggest(common.home_expand(text))) return common.home_encode_list(common.path_suggest(common.home_expand(text)))
end) end
})
end, end,
["doc:save"] = function() ["doc:save"] = function()
@ -478,15 +483,18 @@ local commands = {
return return
end end
core.command_view:set_text(old_filename) core.command_view:set_text(old_filename)
core.command_view:enter("Rename", function(filename) core.command_view:enter("Rename", {
submit = function(filename)
save(common.home_expand(filename)) save(common.home_expand(filename))
core.log("Renamed \"%s\" to \"%s\"", old_filename, filename) core.log("Renamed \"%s\" to \"%s\"", old_filename, filename)
if filename ~= old_filename then if filename ~= old_filename then
os.remove(old_filename) os.remove(old_filename)
end end
end, function (text) end,
suggest = function (text)
return common.home_encode_list(common.path_suggest(common.home_expand(text))) return common.home_encode_list(common.path_suggest(common.home_expand(text)))
end) end
})
end, end,

View File

@ -4,11 +4,13 @@ local common = require "core.common"
command.add(nil, { command.add(nil, {
["files:create-directory"] = function() ["files:create-directory"] = function()
core.command_view:enter("New directory name", function(text) core.command_view:enter("New directory name", {
submit = function(text)
local success, err, path = common.mkdirp(text) local success, err, path = common.mkdirp(text)
if not success then if not success then
core.error("cannot create directory %q: %s", path, err) core.error("cannot create directory %q: %s", path, err)
end end
end) end
})
end, end,
}) })

View File

@ -62,7 +62,8 @@ local function find(label, search_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(label, function(text, item) core.command_view:enter(label, {
submit = function(text, item)
insert_unique(core.previous_find, text) insert_unique(core.previous_find, text)
core.status_view:remove_tooltip() core.status_view:remove_tooltip()
if found_expression then if found_expression then
@ -72,17 +73,20 @@ local function find(label, search_fn)
last_view.doc:set_selection(table.unpack(last_sel)) last_view.doc:set_selection(table.unpack(last_sel))
last_view:scroll_to_make_visible(table.unpack(last_sel)) last_view:scroll_to_make_visible(table.unpack(last_sel))
end end
end, function(text) end,
suggest = function(text)
update_preview(last_sel, search_fn, text) update_preview(last_sel, search_fn, text)
last_fn, last_text = search_fn, text last_fn, last_text = search_fn, text
return core.previous_find return core.previous_find
end, function(explicit) end,
cancel = function(explicit)
core.status_view:remove_tooltip() core.status_view:remove_tooltip()
if explicit then if explicit then
last_view.doc:set_selection(table.unpack(last_sel)) last_view.doc:set_selection(table.unpack(last_sel))
last_view:scroll_to_make_visible(table.unpack(last_sel)) last_view:scroll_to_make_visible(table.unpack(last_sel))
end end
end) end
})
end end
@ -91,25 +95,31 @@ 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, {
submit = function(old)
insert_unique(core.previous_find, 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, {
submit = function(new)
core.status_view:remove_tooltip() core.status_view:remove_tooltip()
insert_unique(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)
core.log("Replaced %d instance(s) of %s %q with %q", n, kind, old, new) core.log("Replaced %d instance(s) of %s %q with %q", n, kind, old, new)
end, function() return core.previous_replace end, function() end,
suggest = function() return core.previous_replace end, function()
core.status_view:remove_tooltip() core.status_view:remove_tooltip()
end) end
end, function() return core.previous_find end, function() })
end,
suggest = function() return core.previous_find end, function()
core.status_view:remove_tooltip() core.status_view:remove_tooltip()
end) end
})
end end
local function has_selection() local function has_selection()

View File

@ -50,20 +50,20 @@ command.add(nil, {
core.status_view:display_messages(true) core.status_view:display_messages(true)
end, end,
["status-bar:hide-item"] = function() ["status-bar:hide-item"] = function()
core.command_view:enter("Status bar item to hide", core.command_view:enter("Status bar item to hide", {
function(text, item) submit = function(text, item)
core.status_view:hide_items(item.name) core.status_view:hide_items(item.name)
end, end,
status_view_get_items suggest = status_view_get_items
) })
end, end,
["status-bar:show-item"] = function() ["status-bar:show-item"] = function()
core.command_view:enter("Status bar item to show", core.command_view:enter("Status bar item to show", {
function(text, item) submit = function(text, item)
core.status_view:show_items(item.name) core.status_view:show_items(item.name)
end, end,
status_view_get_items suggest = status_view_get_items
) })
end, end,
["status-bar:reset-items"] = function() ["status-bar:reset-items"] = function()
core.status_view:show_items() core.status_view:show_items()

View File

@ -25,7 +25,9 @@ local default_state = {
submit = noop, submit = noop,
suggest = noop, suggest = noop,
cancel = noop, cancel = noop,
validate = function() return true end validate = function() return true end,
typeahead = true,
wrap = true,
} }
@ -44,7 +46,6 @@ function CommandView:new()
self.font = "font" self.font = "font"
self.size.y = 0 self.size.y = 0
self.label = "" self.label = ""
self.wrap_on_overflow = false
end end
@ -90,9 +91,10 @@ function CommandView:set_text(text, select)
end end
end end
function CommandView:move_suggestion_idx(dir) function CommandView:move_suggestion_idx(dir)
local function overflow_suggestion_idx(n, count) local function overflow_suggestion_idx(n, count)
if self.wrap_on_overflow then if self.state.wrap then
return (n - 1) % count + 1 return (n - 1) % count + 1
else else
return common.clamp(n, 1, count) return common.clamp(n, 1, count)
@ -143,17 +145,25 @@ function CommandView:submit()
end end
function CommandView:enter(text, submit, suggest, cancel, validate, wrap_on_overflow) function CommandView:enter(text, ...)
if self.state ~= default_state then if self.state ~= default_state then
return return
end end
self.wrap_on_overflow = wrap_on_overflow or false; local options = select(1, ...)
self.state = {
submit = submit or noop, if type(options) ~= "table" then
suggest = suggest or noop, core.info("Warning: deprecated CommandView:enter usage")
cancel = cancel or noop, local submit, suggest, cancel, validate = ...
validate = validate or function() return true end options = {
submit = submit,
suggest = suggest,
cancel = cancel,
validate = validate,
} }
end
self.state = common.merge(default_state, options)
core.set_active_view(self) core.set_active_view(self)
self:update_suggestions() self:update_suggestions()
self.gutter_text_brightness = 100 self.gutter_text_brightness = 100
@ -210,7 +220,7 @@ 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 if self.state.typeahead and self.suggestions[self.suggestion_idx] then
local current_text = self:get_text() local current_text = self:get_text()
local suggested_text = self.suggestions[self.suggestion_idx].text or "" local suggested_text = self.suggestions[self.suggestion_idx].text or ""
if #self.last_text < #current_text and if #self.last_text < #current_text and

View File

@ -65,19 +65,22 @@ end
function DocView:try_close(do_close) function DocView:try_close(do_close)
if self.doc:is_dirty() if self.doc:is_dirty()
and #core.get_views_referencing_doc(self.doc) == 1 then and #core.get_views_referencing_doc(self.doc) == 1 then
core.command_view:enter("Unsaved Changes; Confirm Close", function(_, item) core.command_view:enter("Unsaved Changes; Confirm Close", {
submit = function(_, item)
if item.text:match("^[cC]") then if item.text:match("^[cC]") then
do_close() do_close()
elseif item.text:match("^[sS]") then elseif item.text:match("^[sS]") then
self.doc:save() self.doc:save()
do_close() do_close()
end end
end, function(text) end,
suggest = function(text)
local items = {} local items = {}
if not text:find("^[^cC]") then table.insert(items, "Close Without Saving") end 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 if not text:find("^[^sS]") then table.insert(items, "Save And Close") end
return items return items
end) end
})
else else
do_close() do_close()
end end

View File

@ -300,22 +300,20 @@ local function set_indent_type(doc, type)
end end
local function set_indent_type_command() local function set_indent_type_command()
core.command_view:enter( core.command_view:enter("Specify indent style for this file", {
"Specify indent style for this file", submit = function(value)
function(value) -- submit
local doc = core.active_view.doc local doc = core.active_view.doc
value = value:lower() value = value:lower()
set_indent_type(doc, value == "tabs" and "hard" or "soft") set_indent_type(doc, value == "tabs" and "hard" or "soft")
end, end,
function(text) -- suggest suggest = function(text)
return common.fuzzy_match({"tabs", "spaces"}, text) return common.fuzzy_match({"tabs", "spaces"}, text)
end, end,
nil, -- cancel validate = function(text)
function(text) -- validate
local t = text:lower() local t = text:lower()
return t == "tabs" or t == "spaces" return t == "tabs" or t == "spaces"
end end
) })
end end
@ -330,20 +328,17 @@ local function set_indent_size(doc, size)
end end
local function set_indent_size_command() local function set_indent_size_command()
core.command_view:enter( core.command_view:enter("Specify indent size for current file", {
"Specify indent size for current file", submit = function(value)
function(value) -- submit
value = math.floor(tonumber(value)) value = math.floor(tonumber(value))
local doc = core.active_view.doc local doc = core.active_view.doc
set_indent_size(doc, value) set_indent_size(doc, value)
end, end,
nil, -- suggest validate = function(value)
nil, -- cancel
function(value) -- validate
value = tonumber(value) value = tonumber(value)
return value ~= nil and value >= 1 return value ~= nil and value >= 1
end end
) })
end end

View File

@ -244,30 +244,36 @@ end
command.add(nil, { command.add(nil, {
["project-search:find"] = function() ["project-search:find"] = function()
set_command_view_text() set_command_view_text()
core.command_view:enter("Find Text In Project", function(text) core.command_view:enter("Find Text In Project", {
submit = function(text)
text = text:lower() text = text:lower()
begin_search(text, function(line_text) begin_search(text, function(line_text)
return line_text:lower():find(text, nil, true) return line_text:lower():find(text, nil, true)
end) end)
end) end
})
end, end,
["project-search:find-regex"] = function() ["project-search:find-regex"] = function()
core.command_view:enter("Find Regex In Project", function(text) core.command_view:enter("Find Regex In Project", {
submit = function(text)
local re = regex.compile(text, "i") local re = regex.compile(text, "i")
begin_search(text, function(line_text) begin_search(text, function(line_text)
return regex.cmatch(re, line_text) return regex.cmatch(re, line_text)
end) end)
end) end
})
end, end,
["project-search:fuzzy-find"] = function() ["project-search:fuzzy-find"] = function()
set_command_view_text() set_command_view_text()
core.command_view:enter("Fuzzy Find Text In Project", function(text) core.command_view:enter("Fuzzy Find Text In Project", {
submit = function(text)
begin_search(text, function(line_text) begin_search(text, function(line_text)
return common.fuzzy_match(line_text, text) and 1 return common.fuzzy_match(line_text, text) and 1
end) end)
end) end
})
end, end,
}) })

View File

@ -42,7 +42,8 @@ end
command.add("core.docview", { command.add("core.docview", {
["tabularize:tabularize"] = function() ["tabularize:tabularize"] = function()
core.command_view:enter("Tabularize On Delimiter", function(delim) core.command_view:enter("Tabularize On Delimiter", {
submit = function(delim)
if delim == "" then delim = " " end if delim == "" then delim = " " end
local doc = core.active_view.doc local doc = core.active_view.doc
@ -56,6 +57,7 @@ command.add("core.docview", {
tabularize_lines(lines, delim) tabularize_lines(lines, delim)
return table.concat(lines) return table.concat(lines)
end) end)
end) end
})
end, end,
}) })

View File

@ -690,7 +690,8 @@ command.add(function() return treeitem() ~= nil end, {
local old_filename = treeitem().filename local old_filename = treeitem().filename
local old_abs_filename = treeitem().abs_filename local old_abs_filename = treeitem().abs_filename
core.command_view:set_text(old_filename) core.command_view:set_text(old_filename)
core.command_view:enter("Rename", function(filename) core.command_view:enter("Rename", {
submit = function(filename)
filename = core.normalize_to_project_dir(filename) filename = core.normalize_to_project_dir(filename)
local abs_filename = core.project_absolute_path(filename) local abs_filename = core.project_absolute_path(filename)
local res, err = os.rename(old_abs_filename, abs_filename) local res, err = os.rename(old_abs_filename, abs_filename)
@ -706,32 +707,40 @@ command.add(function() return treeitem() ~= nil end, {
else else
core.error("Error while renaming \"%s\" to \"%s\": %s", old_abs_filename, abs_filename, err) core.error("Error while renaming \"%s\" to \"%s\": %s", old_abs_filename, abs_filename, err)
end end
end, common.path_suggest) end,
suggest = common.path_suggest
})
end, end,
["treeview:new-file"] = function() ["treeview:new-file"] = function()
if not is_project_folder(treeitem().abs_filename) then if not is_project_folder(treeitem().abs_filename) then
core.command_view:set_text(treeitem().filename .. "/") core.command_view:set_text(treeitem().filename .. "/")
end end
core.command_view:enter("Filename", function(filename) core.command_view:enter("Filename", {
submit = function(filename)
local doc_filename = core.project_dir .. PATHSEP .. filename local doc_filename = core.project_dir .. PATHSEP .. filename
local file = io.open(doc_filename, "a+") local file = io.open(doc_filename, "a+")
file:write("") file:write("")
file:close() file:close()
core.root_view:open_doc(core.open_doc(doc_filename)) core.root_view:open_doc(core.open_doc(doc_filename))
core.log("Created %s", doc_filename) core.log("Created %s", doc_filename)
end, common.path_suggest) end,
suggest = common.path_suggest
})
end, end,
["treeview:new-folder"] = function() ["treeview:new-folder"] = function()
if not is_project_folder(treeitem().abs_filename) then if not is_project_folder(treeitem().abs_filename) then
core.command_view:set_text(treeitem().filename .. "/") core.command_view:set_text(treeitem().filename .. "/")
end end
core.command_view:enter("Folder Name", function(filename) core.command_view:enter("Folder Name", {
submit = function(filename)
local dir_path = core.project_dir .. PATHSEP .. filename local dir_path = core.project_dir .. PATHSEP .. filename
common.mkdirp(dir_path) common.mkdirp(dir_path)
core.log("Created %s", dir_path) core.log("Created %s", dir_path)
end, common.path_suggest) end,
suggest = common.path_suggest
})
end, end,
["treeview:open-in-system"] = function() ["treeview:open-in-system"] = function()