Use new CommandView:enter options table
This commit is contained in:
+125
-101
@@ -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,
|
||||
})
|
||||
|
||||
+35
-27
@@ -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,
|
||||
|
||||
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user